-
Notifications
You must be signed in to change notification settings - Fork 120
/
meta.go
185 lines (163 loc) · 5.43 KB
/
meta.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package meta
import (
"context"
"fmt"
"net/http"
"os"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/transport"
"github.com/scaleway/terraform-provider-scaleway/v2/version"
)
const appendUserAgentEnvVar = "TF_APPEND_USER_AGENT"
// Meta contains config and SDK clients used by resources.
//
// This meta value is passed into all resources.
type Meta struct {
// scwClient is the Scaleway SDK client.
scwClient *scw.Client
// httpClient can be either a regular http.Client used to make real HTTP requests
// or it can be a http.Client used to record and replay cassettes which is useful
// to replay recorded interactions with APIs locally
httpClient *http.Client
}
func (m Meta) ScwClient() *scw.Client {
return m.scwClient
}
func (m Meta) HTTPClient() *http.Client {
return m.httpClient
}
type Config struct {
ProviderSchema *schema.ResourceData
TerraformVersion string
ForceZone scw.Zone
ForceProjectID string
ForceOrganizationID string
ForceAccessKey string
ForceSecretKey string
HTTPClient *http.Client
}
// providerConfigure creates the Meta object containing the SDK client.
func NewMeta(ctx context.Context, config *Config) (*Meta, error) {
////
// Load Profile
////
profile, err := loadProfile(ctx, config.ProviderSchema)
if err != nil {
return nil, err
}
if config.ForceZone != "" {
region, err := config.ForceZone.Region()
if err != nil {
return nil, err
}
profile.DefaultRegion = scw.StringPtr(region.String())
profile.DefaultZone = scw.StringPtr(config.ForceZone.String())
}
if config.ForceProjectID != "" {
profile.DefaultProjectID = scw.StringPtr(config.ForceProjectID)
}
if config.ForceOrganizationID != "" {
profile.DefaultOrganizationID = scw.StringPtr(config.ForceOrganizationID)
}
if config.ForceAccessKey != "" {
profile.AccessKey = scw.StringPtr(config.ForceAccessKey)
}
if config.ForceSecretKey != "" {
profile.SecretKey = scw.StringPtr(config.ForceSecretKey)
}
// TODO validated profile
////
// Create scaleway SDK client
////
opts := []scw.ClientOption{
scw.WithUserAgent(customizeUserAgent(version.Version, config.TerraformVersion)),
scw.WithProfile(profile),
}
httpClient := &http.Client{Transport: transport.NewRetryableTransport(http.DefaultTransport)}
if config.HTTPClient != nil {
httpClient = config.HTTPClient
}
opts = append(opts, scw.WithHTTPClient(httpClient))
scwClient, err := scw.NewClient(opts...)
if err != nil {
return nil, err
}
return &Meta{
scwClient: scwClient,
httpClient: httpClient,
}, nil
}
func customizeUserAgent(providerVersion string, terraformVersion string) string {
userAgent := fmt.Sprintf("terraform-provider/%s terraform/%s", providerVersion, terraformVersion)
if appendUserAgent := os.Getenv(appendUserAgentEnvVar); appendUserAgent != "" {
userAgent += " " + appendUserAgent
}
return userAgent
}
//gocyclo:ignore
func loadProfile(ctx context.Context, d *schema.ResourceData) (*scw.Profile, error) {
config, err := scw.LoadConfig()
// If the config file do not exist, don't return an error as we may find config in ENV or flags.
if _, isNotFoundError := err.(*scw.ConfigFileNotFoundError); isNotFoundError {
config = &scw.Config{}
} else if err != nil {
return nil, err
}
// By default we set default zone and region to fr-par
defaultZoneProfile := &scw.Profile{
DefaultRegion: scw.StringPtr(scw.RegionFrPar.String()),
DefaultZone: scw.StringPtr(scw.ZoneFrPar1.String()),
}
activeProfile, err := config.GetActiveProfile()
if err != nil {
return nil, err
}
envProfile := scw.LoadEnvProfile()
providerProfile := &scw.Profile{}
if d != nil {
if profileName, exist := d.GetOk("profile"); exist {
profileFromConfig, err := config.GetProfile(profileName.(string))
if err == nil {
providerProfile = profileFromConfig
}
}
if accessKey, exist := d.GetOk("access_key"); exist {
providerProfile.AccessKey = scw.StringPtr(accessKey.(string))
}
if secretKey, exist := d.GetOk("secret_key"); exist {
providerProfile.SecretKey = scw.StringPtr(secretKey.(string))
}
if projectID, exist := d.GetOk("project_id"); exist {
providerProfile.DefaultProjectID = scw.StringPtr(projectID.(string))
}
if orgID, exist := d.GetOk("organization_id"); exist {
providerProfile.DefaultOrganizationID = scw.StringPtr(orgID.(string))
}
if region, exist := d.GetOk("region"); exist {
providerProfile.DefaultRegion = scw.StringPtr(region.(string))
}
if zone, exist := d.GetOk("zone"); exist {
providerProfile.DefaultZone = scw.StringPtr(zone.(string))
}
if apiURL, exist := d.GetOk("api_url"); exist {
providerProfile.APIURL = scw.StringPtr(apiURL.(string))
}
}
profile := scw.MergeProfiles(defaultZoneProfile, activeProfile, providerProfile, envProfile)
// If profile have a defaultZone but no defaultRegion we set the defaultRegion
// to the one of the defaultZone
if profile.DefaultZone != nil && *profile.DefaultZone != "" &&
(profile.DefaultRegion == nil || *profile.DefaultRegion == "") {
zone := scw.Zone(*profile.DefaultZone)
tflog.Debug(ctx, fmt.Sprintf("guess region from %s zone", zone))
region, err := zone.Region()
if err == nil {
profile.DefaultRegion = scw.StringPtr(region.String())
} else {
tflog.Debug(ctx, "cannot guess region: "+err.Error())
}
}
return profile, nil
}