-
Notifications
You must be signed in to change notification settings - Fork 24
/
provider.go
266 lines (231 loc) · 7.86 KB
/
provider.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package exoscale
import (
"context"
"fmt"
"net/http"
"os"
"strings"
"time"
retryablehttp "github.com/hashicorp/go-retryablehttp"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/exoscale/terraform-provider-exoscale/pkg/config"
"github.com/exoscale/terraform-provider-exoscale/pkg/resources/anti_affinity_group"
"github.com/exoscale/terraform-provider-exoscale/pkg/resources/instance"
"github.com/exoscale/terraform-provider-exoscale/pkg/resources/instance_pool"
exov2 "github.com/exoscale/egoscale/v2"
exov3 "github.com/exoscale/egoscale/v3"
"github.com/exoscale/egoscale/v3/credentials"
providerConfig "github.com/exoscale/terraform-provider-exoscale/pkg/provider/config"
)
const (
// FIXME: defaultZone is used for global resources management, as at the
// time of this implementation the Exoscale public API V2 doesn't
// expose a global endpoint – only zone-local endpoints.
// This should be removed once the Exoscale public API V2 exposes a
// global endpoint.
defaultZone = "ch-gva-2"
)
func init() {
schema.DescriptionKind = schema.StringMarkdown
schema.SchemaDescriptionBuilder = func(s *schema.Schema) string {
if s.ForceNew {
// we add an indication that modifying this attribute, will force the creation of a new resource.
return fmt.Sprintf("❗ %s", s.Description)
}
return s.Description
}
}
// Provider returns an Exoscale Provider.
func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Optional: true,
Description: "Exoscale API key",
},
"secret": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
Description: "Exoscale API secret",
},
"environment": {
Type: schema.TypeString,
Optional: true,
},
"timeout": {
Type: schema.TypeFloat,
Optional: true,
Description: fmt.Sprintf(
"Timeout in seconds for waiting on compute resources to become available (by default: %.0f)",
config.DefaultTimeout.Seconds()),
},
"delay": {
Type: schema.TypeInt,
Optional: true,
Deprecated: "Does nothing",
},
},
DataSourcesMap: map[string]*schema.Resource{
"exoscale_anti_affinity_group": anti_affinity_group.DataSource(),
"exoscale_compute_instance": instance.DataSource(),
"exoscale_compute_instance_list": instance.DataSourceList(),
"exoscale_domain": dataSourceDomain(),
"exoscale_domain_record": dataSourceDomainRecord(),
"exoscale_elastic_ip": dataSourceElasticIP(),
"exoscale_instance_pool": instance_pool.DataSource(),
"exoscale_instance_pool_list": instance_pool.DataSourceList(),
"exoscale_nlb": dataSourceNLB(),
"exoscale_private_network": dataSourcePrivateNetwork(),
"exoscale_security_group": dataSourceSecurityGroup(),
"exoscale_template": dataSourceTemplate(),
dsSKSClusterIdentifier: dataSourceSKSCluster(),
dsSKSClustersListIdentifier: dataSourceSKSClusterList(),
dsSKSNodepoolsListIdentifier: dataSourceSKSNodepoolList(),
dsSKSNodepoolIdentifier: dataSourceSKSNodepool(),
},
ResourcesMap: map[string]*schema.Resource{
"exoscale_anti_affinity_group": anti_affinity_group.Resource(),
"exoscale_compute_instance": instance.Resource(),
"exoscale_domain": resourceDomain(),
"exoscale_domain_record": resourceDomainRecord(),
"exoscale_elastic_ip": resourceElasticIP(),
"exoscale_iam_access_key": resourceIAMAccessKey(),
"exoscale_instance_pool": instance_pool.Resource(),
"exoscale_nlb": resourceNLB(),
"exoscale_nlb_service": resourceNLBService(),
"exoscale_private_network": resourcePrivateNetwork(),
"exoscale_security_group": resourceSecurityGroup(),
"exoscale_security_group_rule": resourceSecurityGroupRule(),
"exoscale_sks_cluster": resourceSKSCluster(),
"exoscale_sks_kubeconfig": resourceSKSKubeconfig(),
"exoscale_sks_nodepool": resourceSKSNodepool(),
"exoscale_ssh_key": resourceSSHKey(),
},
ConfigureContextFunc: ProviderConfigure,
}
}
func ConvertTimeout(timeout float64) time.Duration {
return time.Duration(int64(timeout) * int64(time.Second))
}
func CreateClient(baseConfig *providerConfig.BaseConfig) (*exov2.Client, error) {
return exov2.NewClient(
baseConfig.Key,
baseConfig.Secret,
exov2.ClientOptWithTimeout(baseConfig.Timeout),
exov2.ClientOptWithHTTPClient(func() *http.Client {
rc := retryablehttp.NewClient()
rc.Logger = LeveledTFLogger{Verbose: logging.IsDebugOrHigher()}
hc := rc.StandardClient()
if logging.IsDebugOrHigher() {
hc.Transport = logging.NewSubsystemLoggingHTTPTransport("exoscale", hc.Transport)
}
return hc
}()))
}
func ProviderConfigure(_ context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
var diags diag.Diagnostics
// we only need to set UserAgent once, so lets do it right away.
exov2.UserAgent = UserAgent
key, keyOK := d.GetOk("key")
if !keyOK {
key = providerConfig.GetMultiEnvDefault([]string{
"EXOSCALE_KEY",
"EXOSCALE_API_KEY",
"CLOUDSTACK_KEY",
"CLOUDSTACK_API_KEY",
}, "")
if key != "" {
keyOK = true
}
}
secret, secretOK := d.GetOk("secret")
if !secretOK {
secret = providerConfig.GetMultiEnvDefault([]string{
"EXOSCALE_SECRET",
"EXOSCALE_SECRET_KEY",
"EXOSCALE_API_SECRET",
"CLOUDSTACK_SECRET",
"CLOUDSTACK_SECRET_KEY",
}, "")
if secret != "" {
secretOK = true
}
}
environment, environmentOK := d.GetOk("environment")
if !environmentOK {
environment = providerConfig.GetEnvDefault(
"EXOSCALE_API_ENVIRONMENT",
DefaultEnvironment)
}
if keyOK || secretOK {
if !keyOK || !secretOK {
return nil, diag.Errorf(
"key (%#v) and secret (%#v) must be set",
key.(string),
secret.(string),
)
}
}
var timeout float64
timeoutRaw, timeoutOk := d.GetOk("timeout")
if timeoutOk {
timeout = timeoutRaw.(float64)
} else {
var err error
timeout, err = providerConfig.GetTimeout()
if err != nil {
return nil, diag.FromErr(err)
}
}
baseConfig := providerConfig.BaseConfig{
Key: key.(string),
Secret: secret.(string),
Timeout: ConvertTimeout(timeout),
Environment: environment.(string),
}
clv2, err := CreateClient(&baseConfig)
if err != nil {
return nil, diag.FromErr(err)
}
// Exoscale v3 client
creds := credentials.NewStaticCredentials(
key.(string),
secret.(string),
)
opts := []exov3.ClientOpt{}
if ep := os.Getenv("EXOSCALE_API_ENDPOINT"); ep != "" {
opts = append(opts, exov3.ClientOptWithEndpoint(exov3.Endpoint(ep)))
}
clv3, err := exov3.NewClient(creds, opts...)
if err != nil {
return nil, diag.FromErr(err)
}
exov3.UserAgent = UserAgent
return map[string]interface{}{
"config": baseConfig,
"client": clv2,
"clientV3": clv3,
"environment": environment,
},
diags
}
// zonedStateContextFunc is an alternative resource importer function to be
// used for importing zone-local resources, where the resource ID is expected
// to be suffixed with "@ZONE" (e.g. "c01af84d-6ac6-4784-98bb-127c98be8258@ch-gva-2").
// Upon successful execution, the returned resource state contains the ID of the
// resource and the "zone" attribute set to the value parsed from the import ID.
func zonedStateContextFunc(_ context.Context, d *schema.ResourceData, _ interface{}) ([]*schema.ResourceData, error) {
parts := strings.SplitN(d.Id(), "@", 2)
if len(parts) != 2 {
return nil, fmt.Errorf(`invalid ID %q, expected format "<ID>@<ZONE>"`, d.Id())
}
d.SetId(parts[0])
if err := d.Set("zone", parts[1]); err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}