-
Notifications
You must be signed in to change notification settings - Fork 131
/
resource_cloud_project_containerregistry.go
395 lines (336 loc) · 10.9 KB
/
resource_cloud_project_containerregistry.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
package ovh
import (
"fmt"
"log"
"net/url"
"regexp"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
"github.com/ovh/go-ovh/ovh"
)
func resourceCloudProjectContainerRegistry() *schema.Resource {
return &schema.Resource{
Create: resourceCloudProjectContainerRegistryCreate,
Read: resourceCloudProjectContainerRegistryRead,
Delete: resourceCloudProjectContainerRegistryDelete,
Update: resourceCloudProjectContainerRegistryUpdate,
Importer: &schema.ResourceImporter{
State: resourceCloudProjectContainerRegistryImportState,
},
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
},
"name": {
Type: schema.TypeString,
Description: "Registry name",
Required: true,
},
"region": {
Type: schema.TypeString,
Description: "Region of the registry.",
Required: true,
},
"plan_id": {
Type: schema.TypeString,
Description: "Plan ID of the registry.",
Optional: true,
Computed: true,
},
// Computed
"created_at": {
Type: schema.TypeString,
Description: "Registry creation date",
Computed: true,
},
"project_id": {
Type: schema.TypeString,
Description: "Project ID of your registry",
Computed: true,
},
"size": {
Type: schema.TypeInt,
Description: "Current size of the registry (bytes)",
Computed: true,
},
"status": {
Type: schema.TypeString,
Description: "Registry status",
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Description: "Registry last update date",
Computed: true,
},
"url": {
Type: schema.TypeString,
Description: "Access url of the registry",
Computed: true,
},
"version": {
Type: schema.TypeString,
Description: "Version of your registry",
Computed: true,
},
"plan": {
Type: schema.TypeList,
Computed: true,
Description: "Plan of the registry",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"code": {
Type: schema.TypeString,
Description: "Plan code from catalog",
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Description: "Plan creation date",
Computed: true,
},
"features": {
Type: schema.TypeList,
Computed: true,
Description: "Features of the plan",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"vulnerability": {
Type: schema.TypeBool,
Description: "Vulnerability scanning",
Computed: true,
},
},
},
},
"id": {
Type: schema.TypeString,
Description: "Plan ID",
Computed: true,
},
"name": {
Type: schema.TypeString,
Description: "Plan name",
Computed: true,
},
"registry_limits": {
Type: schema.TypeList,
Computed: true,
Description: "Container registry limits",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"image_storage": {
Type: schema.TypeInt,
Description: "Docker image storage limits in bytes",
Computed: true,
},
"parallel_request": {
Type: schema.TypeInt,
Description: "Parallel requests on Docker image API (/v2 Docker registry API)",
Computed: true,
},
},
},
},
"updated_at": {
Type: schema.TypeString,
Description: "Plan last update date",
Computed: true,
},
},
},
},
},
}
}
func resourceCloudProjectContainerRegistryImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
givenId := d.Id()
splitId := strings.SplitN(givenId, "/", 2)
if len(splitId) != 2 {
return nil, fmt.Errorf("Import Id is not service_name/id id formatted")
}
serviceName := splitId[0]
id := splitId[1]
d.SetId(id)
d.Set("service_name", serviceName)
results := make([]*schema.ResourceData, 1)
results[0] = d
return results, nil
}
func resourceCloudProjectContainerRegistryCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
log.Printf("[DEBUG] Will create cloud project registry for project: %s", serviceName)
opts := (&CloudProjectContainerRegistryCreateOpts{}).FromResource(d)
reg := &CloudProjectContainerRegistry{}
endpoint := fmt.Sprintf(
"/cloud/project/%s/containerRegistry",
url.PathEscape(serviceName),
)
if err := config.OVHClient.Post(endpoint, opts, reg); err != nil {
return fmt.Errorf("Error calling post %s:\n\t %q", endpoint, err)
}
d.SetId(reg.Id)
stateConf := &resource.StateChangeConf{
Pending: []string{"INSTALLING"},
Target: []string{"READY"},
Refresh: waitForCloudProjectContainerRegistry(config.OVHClient, serviceName, d.Id()),
Timeout: 60 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf("waiting for registry (%s): %s", d.Id(), err)
}
return resourceCloudProjectContainerRegistryRead(d, meta)
}
func resourceCloudProjectContainerRegistryUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
log.Printf("[DEBUG] Will update cloud project registry for project: %s", serviceName)
opts := (&CloudProjectContainerRegistryUpdateOpts{}).FromResource(d)
endpoint := fmt.Sprintf(
"/cloud/project/%s/containerRegistry/%s",
url.PathEscape(serviceName),
url.PathEscape(d.Id()),
)
if err := config.OVHClient.Put(endpoint, opts, nil); err != nil {
return fmt.Errorf("Error calling put %s:\n\t %q", endpoint, err)
}
if err := cloudProjectContainerRegistryPlanUpdate(d, meta); err != nil {
return err
}
return resourceCloudProjectContainerRegistryRead(d, meta)
}
func resourceCloudProjectContainerRegistryRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
id := d.Id()
log.Printf("[DEBUG] Will read cloud project registry %s for project: %s", id, serviceName)
reg := &CloudProjectContainerRegistry{}
endpoint := fmt.Sprintf(
"/cloud/project/%s/containerRegistry/%s",
url.PathEscape(serviceName),
url.PathEscape(id),
)
if err := config.OVHClient.Get(endpoint, reg); err != nil {
return helpers.CheckDeleted(d, err, endpoint)
}
for k, v := range reg.ToMap() {
if k != "id" {
d.Set(k, v)
}
}
// OVH API Bug: the api doesn't set the region attribute value.
// As a temp workaround, if the API sets an empty string for the region attr
// we override it by extracting the region from the URL
if d.Get("region").(string) == "" {
urlRegionRx := regexp.MustCompile(`^https://[[:alnum:]]+\.([[:alpha:]]+)[0-9]+\.container-registry\.ovh\.net`)
matches := urlRegionRx.FindStringSubmatch(reg.Url)
if len(matches) > 1 {
d.Set("region", strings.ToUpper(matches[1]))
}
}
return cloudProjectContainerRegistryPlanRead(d, meta)
}
func cloudProjectContainerRegistryPlanRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
id := d.Id()
log.Printf("[DEBUG] Will read cloud project registry plan %s for project: %s", id, serviceName)
plan := &CloudProjectCapabilitiesContainerRegistryPlan{}
endpoint := fmt.Sprintf(
"/cloud/project/%s/containerRegistry/%s/plan",
url.PathEscape(serviceName),
url.PathEscape(id),
)
if err := config.OVHClient.Get(endpoint, plan); err != nil {
return fmt.Errorf("Error calling get %s:\n\t %q", endpoint, err)
}
d.Set("plan", []interface{}{plan.ToMap()})
d.Set("plan_id", plan.Id)
return nil
}
func cloudProjectContainerRegistryPlanUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
if !d.HasChange("plan_id") {
log.Printf("[DEBUG] cloud project registry plan for project : %s hasnt changed.", serviceName)
return nil
}
log.Printf("[DEBUG] Will update cloud project registry plan for project: %s", serviceName)
opts := (&CloudProjectContainerRegistryPlanUpdateOpts{}).FromResource(d)
endpoint := fmt.Sprintf(
"/cloud/project/%s/containerRegistry/%s/plan",
url.PathEscape(serviceName),
url.PathEscape(d.Id()),
)
if err := config.OVHClient.Put(endpoint, opts, nil); err != nil {
return fmt.Errorf("Error calling put %s:\n\t %q", endpoint, err)
}
stateConf := &resource.StateChangeConf{
Pending: []string{"SCALING_UP"},
Target: []string{"READY"},
Refresh: waitForCloudProjectContainerRegistry(config.OVHClient, serviceName, d.Id()),
Timeout: 30 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf("waiting for registry (%s): %s", d.Id(), err)
}
return nil
}
func resourceCloudProjectContainerRegistryDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
id := d.Id()
log.Printf("[DEBUG] Will delete cloud project registry %s for project: %s", id, serviceName)
endpoint := fmt.Sprintf(
"/cloud/project/%s/containerRegistry/%s",
url.PathEscape(serviceName),
url.PathEscape(id),
)
if err := config.OVHClient.Delete(endpoint, nil); err != nil {
return helpers.CheckDeleted(d, err, endpoint)
}
stateConf := &resource.StateChangeConf{
Pending: []string{"DELETING"},
Target: []string{"DELETED", "deleted"},
Refresh: waitForCloudProjectContainerRegistry(config.OVHClient, serviceName, d.Id()),
Timeout: 30 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf("Deleting container registry %s from project %s: %s", id, serviceName, err)
}
d.SetId("")
return nil
}
func waitForCloudProjectContainerRegistry(c *ovh.Client, serviceName, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
r := &CloudProjectContainerRegistry{}
endpoint := fmt.Sprintf(
"/cloud/project/%s/containerRegistry/%s",
url.PathEscape(serviceName),
url.PathEscape(id),
)
err := c.Get(endpoint, r)
if err != nil {
if err.(*ovh.APIError).Code == 404 {
log.Printf("[DEBUG] container registry id %s on project %s deleted", id, serviceName)
return r, "deleted", nil
} else {
return r, "", err
}
}
log.Printf("[DEBUG] Pending container registry: %s", r)
return r, r.Status, nil
}
}