-
Notifications
You must be signed in to change notification settings - Fork 120
/
helpers_container.go
160 lines (129 loc) · 4.65 KB
/
helpers_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
package container
import (
"context"
"errors"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
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/locality"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/meta"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
)
const (
defaultContainerNamespaceTimeout = 5 * time.Minute
defaultContainerCronTimeout = 5 * time.Minute
defaultContainerTimeout = 12*time.Minute + 30*time.Second
defaultContainerDomainTimeout = 10 * time.Minute
DefaultContainerRetryInterval = 5 * time.Second
defaultTriggerRetryInterval = 5 * time.Second
)
// newAPIWithRegion returns a new container API and the region.
func newAPIWithRegion(d *schema.ResourceData, m interface{}) (*container.API, scw.Region, error) {
api := container.NewAPI(meta.ExtractScwClient(m))
region, err := meta.ExtractRegion(d, m)
if err != nil {
return nil, "", err
}
return api, region, nil
}
// NewAPIWithRegionAndID returns a new container API, region and ID.
func NewAPIWithRegionAndID(m interface{}, id string) (*container.API, scw.Region, string, error) {
api := container.NewAPI(meta.ExtractScwClient(m))
region, id, err := regional.ParseID(id)
if err != nil {
return nil, "", "", err
}
return api, region, id, nil
}
func setCreateContainerRequest(d *schema.ResourceData, region scw.Region) (*container.CreateContainerRequest, error) {
// required
nameRaw := d.Get("name")
namespaceID := d.Get("namespace_id")
name := types.ExpandOrGenerateString(nameRaw.(string), "co")
privacyType := d.Get("privacy")
protocol := d.Get("protocol")
httpOption := d.Get("http_option")
req := &container.CreateContainerRequest{
Region: region,
NamespaceID: locality.ExpandID(namespaceID),
Name: name,
Privacy: container.ContainerPrivacy(privacyType.(string)),
Protocol: container.ContainerProtocol(*types.ExpandStringPtr(protocol)),
HTTPOption: container.ContainerHTTPOption(httpOption.(string)),
}
// optional
if envVariablesRaw, ok := d.GetOk("environment_variables"); ok {
req.EnvironmentVariables = types.ExpandMapPtrStringString(envVariablesRaw)
}
if secretEnvVariablesRaw, ok := d.GetOk("secret_environment_variables"); ok {
req.SecretEnvironmentVariables = expandContainerSecrets(secretEnvVariablesRaw)
}
if minScale, ok := d.GetOk("min_scale"); ok {
req.MinScale = scw.Uint32Ptr(uint32(minScale.(int)))
}
if maxScale, ok := d.GetOk("max_scale"); ok {
req.MaxScale = scw.Uint32Ptr(uint32(maxScale.(int)))
}
if memoryLimit, ok := d.GetOk("memory_limit"); ok {
req.MemoryLimit = scw.Uint32Ptr(uint32(memoryLimit.(int)))
}
if cpuLimit, ok := d.GetOk("cpu_limit"); ok {
req.CPULimit = scw.Uint32Ptr(uint32(cpuLimit.(int)))
}
if timeout, ok := d.GetOk("timeout"); ok {
timeInt := timeout.(int)
req.Timeout = &scw.Duration{Seconds: int64(timeInt)}
}
if port, ok := d.GetOk("port"); ok {
req.Port = scw.Uint32Ptr(uint32(port.(int)))
}
if description, ok := d.GetOk("description"); ok {
req.Description = types.ExpandStringPtr(description)
}
if registryImage, ok := d.GetOk("registry_image"); ok {
req.RegistryImage = types.ExpandStringPtr(registryImage)
}
if maxConcurrency, ok := d.GetOk("max_concurrency"); ok {
req.MaxConcurrency = scw.Uint32Ptr(uint32(maxConcurrency.(int)))
}
return req, nil
}
func expandContainerSecrets(secretsRawMap interface{}) []*container.Secret {
secretsMap := secretsRawMap.(map[string]interface{})
secrets := make([]*container.Secret, 0, len(secretsMap))
for k, v := range secretsMap {
secrets = append(secrets, &container.Secret{
Key: k,
Value: types.ExpandStringPtr(v),
})
}
return secrets
}
func isContainerDNSResolveError(err error) bool {
responseError := &scw.ResponseError{}
if !errors.As(err, &responseError) {
return false
}
if strings.HasPrefix(responseError.Message, "could not validate domain") {
return true
}
return false
}
func retryCreateContainerDomain(ctx context.Context, containerAPI *container.API, req *container.CreateDomainRequest, timeout time.Duration) (*container.Domain, error) {
timeoutChannel := time.After(timeout)
for {
select {
case <-time.After(DefaultContainerRetryInterval):
domain, err := containerAPI.CreateDomain(req, scw.WithContext(ctx))
if err != nil && isContainerDNSResolveError(err) {
continue
}
return domain, err
case <-timeoutChannel:
return containerAPI.CreateDomain(req, scw.WithContext(ctx))
}
}
}