forked from scaleway/terraform-provider-scaleway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer.go
418 lines (373 loc) · 13 KB
/
container.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package container
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
container "github.com/scaleway/scaleway-sdk-go/api/container/v1beta1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)
const (
containerMaxConcurrencyLimit int = 80
)
func ResourceContainer() *schema.Resource {
return &schema.Resource{
CreateContext: ResourceContainerCreate,
ReadContext: ResourceContainerRead,
UpdateContext: ResourceContainerUpdate,
DeleteContext: ResourceContainerDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(defaultContainerTimeout),
Read: schema.DefaultTimeout(defaultContainerTimeout),
Update: schema.DefaultTimeout(defaultContainerTimeout),
Delete: schema.DefaultTimeout(defaultContainerTimeout),
Default: schema.DefaultTimeout(defaultContainerTimeout),
},
SchemaVersion: 0,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
Description: "The container name",
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "The container description",
},
"namespace_id": {
Type: schema.TypeString,
Required: true,
Description: "The container namespace associated",
},
"environment_variables": {
Type: schema.TypeMap,
Optional: true,
Computed: true,
Description: "The environment variables to be injected into your container at runtime.",
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringLenBetween(0, 1000),
},
ValidateDiagFunc: validation.MapKeyLenBetween(0, 100),
},
"secret_environment_variables": {
Type: schema.TypeMap,
Optional: true,
Sensitive: true,
Description: "The secret environment variables to be injected into your container at runtime.",
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringLenBetween(0, 1000),
},
ValidateDiagFunc: validation.MapKeyLenBetween(0, 100),
},
"min_scale": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: "The minimum of running container instances continuously. Defaults to 0.",
},
"max_scale": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: "The maximum of number of instances this container can scale to. Default to 20.",
},
"memory_limit": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: "The memory computing resources in MB to allocate to each container. Defaults to 128.",
},
"cpu_limit": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: "The amount of vCPU computing resources to allocate to each container. Defaults to 70.",
},
"timeout": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: "The maximum amount of time in seconds during which your container can process a request before we stop it. Defaults to 300s.",
},
"privacy": {
Type: schema.TypeString,
Optional: true,
Description: "The privacy type define the way to authenticate to your container",
Default: container.ContainerPrivacyPublic,
ValidateDiagFunc: verify.ValidateEnum[container.ContainerPrivacy](),
},
"registry_image": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The scaleway registry image address",
},
"registry_sha256": {
Type: schema.TypeString,
Optional: true,
RequiredWith: []string{"registry_image"},
Description: "The sha256 of your source registry image, changing it will re-apply the deployment. Can be any string",
},
"max_concurrency": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "The maximum the number of simultaneous requests your container can handle at the same time. Defaults to 50.",
ValidateFunc: validation.IntAtMost(containerMaxConcurrencyLimit),
},
"domain_name": {
Type: schema.TypeString,
Computed: true,
Description: "The native container domain name.",
},
"protocol": {
Type: schema.TypeString,
Optional: true,
Description: "The communication protocol http1 or h2c. Defaults to http1.",
Default: container.ContainerProtocolHTTP1.String(),
ValidateDiagFunc: verify.ValidateEnum[container.ContainerProtocol](),
},
"port": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: "The port to expose the container. Defaults to 8080",
},
"deploy": {
Type: schema.TypeBool,
Optional: true,
Description: "This allows you to control your production environment",
Default: false,
},
"http_option": {
Type: schema.TypeString,
Optional: true,
Description: "HTTP traffic configuration",
Default: container.ContainerHTTPOptionEnabled.String(),
ValidateDiagFunc: verify.ValidateEnum[container.ContainerHTTPOption](),
},
"sandbox": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Execution environment of the container.",
ValidateDiagFunc: verify.ValidateEnum[container.ContainerSandbox](),
},
// computed
"status": {
Type: schema.TypeString,
Optional: true,
Description: "The container status",
Computed: true,
},
"cron_status": {
Type: schema.TypeString,
Description: "The cron status",
Computed: true,
},
"error_message": {
Type: schema.TypeString,
Computed: true,
Description: "The error description",
},
"region": regional.Schema(),
},
}
}
func ResourceContainerCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api, region, err := newAPIWithRegion(d, m)
if err != nil {
return diag.FromErr(err)
}
namespaceID := locality.ExpandID(d.Get("namespace_id").(string))
// verify name space state
_, err = waitForNamespace(ctx, api, region, namespaceID, d.Timeout(schema.TimeoutCreate))
if err != nil {
return diag.Errorf("unexpected namespace error: %s", err)
}
req, err := setCreateContainerRequest(d, region)
if err != nil {
return diag.FromErr(err)
}
res, err := api.CreateContainer(req, scw.WithContext(ctx))
if err != nil {
return diag.Errorf("creation container error: %s", err)
}
// check if container should be deployed
shouldDeploy := d.Get("deploy")
if *types.ExpandBoolPtr(shouldDeploy) {
_, err = waitForContainer(ctx, api, res.ID, region, d.Timeout(schema.TimeoutCreate))
if err != nil {
return diag.Errorf("unexpected waiting container error: %s", err)
}
reqUpdate := &container.UpdateContainerRequest{
Region: res.Region,
ContainerID: res.ID,
Redeploy: types.ExpandBoolPtr(shouldDeploy),
}
_, err = api.UpdateContainer(reqUpdate, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}
_, err = waitForContainer(ctx, api, res.ID, region, d.Timeout(schema.TimeoutCreate))
if err != nil {
return diag.Errorf("unexpected waiting container error: %s", err)
}
}
d.SetId(regional.NewIDString(region, res.ID))
return ResourceContainerRead(ctx, d, m)
}
func ResourceContainerRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api, region, containerID, err := NewAPIWithRegionAndID(m, d.Id())
if err != nil {
return diag.FromErr(err)
}
co, err := waitForContainer(ctx, api, containerID, region, d.Timeout(schema.TimeoutCreate))
if err != nil {
if httperrors.Is404(err) {
d.SetId("")
return nil
}
return diag.Errorf("unexpected waiting container error: %s", err)
}
_ = d.Set("name", co.Name)
_ = d.Set("namespace_id", regional.NewID(region, co.NamespaceID).String())
_ = d.Set("status", co.Status.String())
_ = d.Set("error_message", co.ErrorMessage)
_ = d.Set("environment_variables", types.FlattenMap(co.EnvironmentVariables))
_ = d.Set("min_scale", int(co.MinScale))
_ = d.Set("max_scale", int(co.MaxScale))
_ = d.Set("memory_limit", int(co.MemoryLimit))
_ = d.Set("cpu_limit", int(co.CPULimit))
_ = d.Set("timeout", co.Timeout.Seconds)
_ = d.Set("privacy", co.Privacy.String())
_ = d.Set("description", scw.StringPtr(*co.Description))
_ = d.Set("registry_image", co.RegistryImage)
_ = d.Set("max_concurrency", int(co.MaxConcurrency))
_ = d.Set("domain_name", co.DomainName)
_ = d.Set("protocol", co.Protocol.String())
_ = d.Set("cron_status", co.Status.String())
_ = d.Set("port", int(co.Port))
_ = d.Set("deploy", scw.BoolPtr(*types.ExpandBoolPtr(d.Get("deploy"))))
_ = d.Set("http_option", co.HTTPOption)
_ = d.Set("sandbox", co.Sandbox)
_ = d.Set("region", co.Region.String())
return nil
}
func ResourceContainerUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api, region, containerID, err := NewAPIWithRegionAndID(m, d.Id())
if err != nil {
return diag.FromErr(err)
}
namespaceID := d.Get("namespace_id")
// verify name space state
_, err = waitForNamespace(ctx, api, region, locality.ExpandID(namespaceID), d.Timeout(schema.TimeoutUpdate))
if err != nil {
return diag.Errorf("unexpected namespace error: %s", err)
}
// check for container state
_, err = waitForContainer(ctx, api, containerID, region, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return diag.Errorf("unexpected waiting container error: %s", err)
}
// update container
req := &container.UpdateContainerRequest{
Region: region,
ContainerID: containerID,
}
if d.HasChanges("environment_variables") {
envVariablesRaw := d.Get("environment_variables")
req.EnvironmentVariables = types.ExpandMapPtrStringString(envVariablesRaw)
}
if d.HasChanges("secret_environment_variables") {
req.SecretEnvironmentVariables = expandContainerSecrets(d.Get("secret_environment_variables"))
}
if d.HasChanges("min_scale") {
req.MinScale = scw.Uint32Ptr(uint32(d.Get("min_scale").(int)))
}
if d.HasChanges("max_scale") {
req.MaxScale = scw.Uint32Ptr(uint32(d.Get("max_scale").(int)))
}
if d.HasChanges("memory_limit") {
req.MemoryLimit = scw.Uint32Ptr(uint32(d.Get("memory_limit").(int)))
}
if d.HasChanges("cpu_limit") {
req.CPULimit = scw.Uint32Ptr(uint32(d.Get("cpu_limit").(int)))
}
if d.HasChanges("timeout") {
req.Timeout = &scw.Duration{Seconds: int64(d.Get("timeout").(int))}
}
if d.HasChanges("privacy") {
req.Privacy = container.ContainerPrivacy(*types.ExpandStringPtr(d.Get("privacy")))
}
if d.HasChanges("description") {
req.Description = types.ExpandUpdatedStringPtr(d.Get("description"))
}
if d.HasChanges("registry_image") {
req.RegistryImage = types.ExpandStringPtr(d.Get("registry_image"))
}
if d.HasChanges("max_concurrency") {
req.MaxConcurrency = scw.Uint32Ptr(uint32(d.Get("max_concurrency").(int)))
}
if d.HasChanges("protocol") {
req.Protocol = container.ContainerProtocol(*types.ExpandStringPtr(d.Get("protocol")))
}
if d.HasChanges("port") {
req.Port = scw.Uint32Ptr(uint32(d.Get("port").(int)))
}
if d.HasChanges("http_option") {
req.HTTPOption = container.ContainerHTTPOption(d.Get("http_option").(string))
}
if d.HasChanges("deploy") {
req.Redeploy = types.ExpandBoolPtr(d.Get("deploy"))
}
if d.HasChanges("sandbox") {
req.Sandbox = container.ContainerSandbox(d.Get("sandbox").(string))
}
imageHasChanged := d.HasChanges("registry_sha256")
if imageHasChanged {
req.Redeploy = &imageHasChanged
}
con, err := api.UpdateContainer(req, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}
_, err = waitForContainer(ctx, api, con.ID, region, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return diag.FromErr(err)
}
return ResourceContainerRead(ctx, d, m)
}
func ResourceContainerDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api, region, containerID, err := NewAPIWithRegionAndID(m, d.Id())
if err != nil {
return diag.FromErr(err)
}
// check for container state
_, err = waitForContainer(ctx, api, containerID, region, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return diag.FromErr(err)
}
// delete container
_, err = api.DeleteContainer(&container.DeleteContainerRequest{
Region: region,
ContainerID: containerID,
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}
return nil
}