-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
resource_clouddeploy_target.go
619 lines (547 loc) · 21.7 KB
/
resource_clouddeploy_target.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** Type: DCL ***
//
// ----------------------------------------------------------------------------
//
// This file is managed by Magic Modules (https://github.com/GoogleCloudPlatform/magic-modules)
// and is based on the DCL (https://github.com/GoogleCloudPlatform/declarative-resource-client-library).
// Changes will need to be made to the DCL or Magic Modules instead of here.
//
// We are not currently able to accept contributions to this file. If changes
// are required, please file an issue at https://github.com/hashicorp/terraform-provider-google/issues/new/choose
//
// ----------------------------------------------------------------------------
package google
import (
"context"
"fmt"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
clouddeploy "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/clouddeploy"
)
func resourceClouddeployTarget() *schema.Resource {
return &schema.Resource{
Create: resourceClouddeployTargetCreate,
Read: resourceClouddeployTargetRead,
Update: resourceClouddeployTargetUpdate,
Delete: resourceClouddeployTargetDelete,
Importer: &schema.ResourceImporter{
State: resourceClouddeployTargetImport,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(20 * time.Minute),
Update: schema.DefaultTimeout(20 * time.Minute),
Delete: schema.DefaultTimeout(20 * time.Minute),
},
Schema: map[string]*schema.Schema{
"location": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The location for the resource",
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Name of the `Target`. Format is [a-z][a-z0-9\\-]{0,62}.",
},
"annotations": {
Type: schema.TypeMap,
Optional: true,
Description: "Optional. User annotations. These attributes can only be set and used by the user, and not by Google Cloud Deploy. See https://google.aip.dev/128#annotations for more details such as format and size limitations.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"anthos_cluster": {
Type: schema.TypeList,
Optional: true,
Description: "Information specifying an Anthos Cluster.",
MaxItems: 1,
Elem: ClouddeployTargetAnthosClusterSchema(),
ConflictsWith: []string{"gke"},
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "Optional. Description of the `Target`. Max length is 255 characters.",
},
"execution_configs": {
Type: schema.TypeList,
Computed: true,
Optional: true,
Description: "Configurations for all execution that relates to this `Target`. Each `ExecutionEnvironmentUsage` value may only be used in a single configuration; using the same value multiple times is an error. When one or more configurations are specified, they must include the `RENDER` and `DEPLOY` `ExecutionEnvironmentUsage` values. When no configurations are specified, execution will use the default specified in `DefaultPool`.",
Elem: ClouddeployTargetExecutionConfigsSchema(),
},
"gke": {
Type: schema.TypeList,
Optional: true,
Description: "Information specifying a GKE Cluster.",
MaxItems: 1,
Elem: ClouddeployTargetGkeSchema(),
ConflictsWith: []string{"anthos_cluster"},
},
"labels": {
Type: schema.TypeMap,
Optional: true,
Description: "Optional. Labels are attributes that can be set and used by both the user and by Google Cloud Deploy. Labels must meet the following constraints: * Keys and values can contain only lowercase letters, numeric characters, underscores, and dashes. * All characters must use UTF-8 encoding, and international characters are allowed. * Keys must start with a lowercase letter or international character. * Each resource is limited to a maximum of 64 labels. Both keys and values are additionally constrained to be <= 128 bytes.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"project": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
Description: "The project for the resource",
},
"require_approval": {
Type: schema.TypeBool,
Optional: true,
Description: "Optional. Whether or not the `Target` requires approval.",
},
"create_time": {
Type: schema.TypeString,
Computed: true,
Description: "Output only. Time at which the `Target` was created.",
},
"etag": {
Type: schema.TypeString,
Computed: true,
Description: "Optional. This checksum is computed by the server based on the value of other fields, and may be sent on update and delete requests to ensure the client has an up-to-date value before proceeding.",
},
"target_id": {
Type: schema.TypeString,
Computed: true,
Description: "Output only. Resource id of the `Target`.",
},
"uid": {
Type: schema.TypeString,
Computed: true,
Description: "Output only. Unique identifier of the `Target`.",
},
"update_time": {
Type: schema.TypeString,
Computed: true,
Description: "Output only. Most recent time at which the `Target` was updated.",
},
},
}
}
func ClouddeployTargetAnthosClusterSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"membership": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
Description: "Membership of the GKE Hub-registered cluster to which to apply the Skaffold configuration. Format is `projects/{project}/locations/{location}/memberships/{membership_name}`.",
},
},
}
}
func ClouddeployTargetExecutionConfigsSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"usages": {
Type: schema.TypeList,
Required: true,
Description: "Required. Usages when this configuration should be applied.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"artifact_storage": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "Optional. Cloud Storage location in which to store execution outputs. This can either be a bucket (\"gs://my-bucket\") or a path within a bucket (\"gs://my-bucket/my-dir\"). If unspecified, a default bucket located in the same region will be used.",
},
"service_account": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "Optional. Google service account to use for execution. If unspecified, the project execution service account (-compute@developer.gserviceaccount.com) is used.",
},
"worker_pool": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
Description: "Optional. The resource name of the `WorkerPool`, with the format `projects/{project}/locations/{location}/workerPools/{worker_pool}`. If this optional field is unspecified, the default Cloud Build pool will be used.",
},
},
}
}
func ClouddeployTargetGkeSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"cluster": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
Description: "Information specifying a GKE Cluster. Format is `projects/{project_id}/locations/{location_id}/clusters/{cluster_id}.",
},
"internal_ip": {
Type: schema.TypeBool,
Optional: true,
Description: "Optional. If true, `cluster` is accessed using the private IP address of the control plane endpoint. Otherwise, the default IP address of the control plane endpoint is used. The default IP address is the private IP address for clusters with private control-plane endpoints and the public IP address otherwise. Only specify this option when `cluster` is a [private GKE cluster](https://cloud.google.com/kubernetes-engine/docs/concepts/private-cluster-concept).",
},
},
}
}
func resourceClouddeployTargetCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
obj := &clouddeploy.Target{
Location: dcl.String(d.Get("location").(string)),
Name: dcl.String(d.Get("name").(string)),
Annotations: checkStringMap(d.Get("annotations")),
AnthosCluster: expandClouddeployTargetAnthosCluster(d.Get("anthos_cluster")),
Description: dcl.String(d.Get("description").(string)),
ExecutionConfigs: expandClouddeployTargetExecutionConfigsArray(d.Get("execution_configs")),
Gke: expandClouddeployTargetGke(d.Get("gke")),
Labels: checkStringMap(d.Get("labels")),
Project: dcl.String(project),
RequireApproval: dcl.Bool(d.Get("require_approval").(bool)),
}
id, err := obj.ID()
if err != nil {
return fmt.Errorf("error constructing id: %s", err)
}
d.SetId(id)
directive := CreateDirective
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
billingProject := project
// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}
client := NewDCLClouddeployClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutCreate))
if bp, err := replaceVars(d, config, client.Config.BasePath); err != nil {
d.SetId("")
return fmt.Errorf("Could not format %q: %w", client.Config.BasePath, err)
} else {
client.Config.BasePath = bp
}
res, err := client.ApplyTarget(context.Background(), obj, directive...)
if _, ok := err.(dcl.DiffAfterApplyError); ok {
log.Printf("[DEBUG] Diff after apply returned from the DCL: %s", err)
} else if err != nil {
// The resource didn't actually create
d.SetId("")
return fmt.Errorf("Error creating Target: %s", err)
}
log.Printf("[DEBUG] Finished creating Target %q: %#v", d.Id(), res)
return resourceClouddeployTargetRead(d, meta)
}
func resourceClouddeployTargetRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
obj := &clouddeploy.Target{
Location: dcl.String(d.Get("location").(string)),
Name: dcl.String(d.Get("name").(string)),
Annotations: checkStringMap(d.Get("annotations")),
AnthosCluster: expandClouddeployTargetAnthosCluster(d.Get("anthos_cluster")),
Description: dcl.String(d.Get("description").(string)),
ExecutionConfigs: expandClouddeployTargetExecutionConfigsArray(d.Get("execution_configs")),
Gke: expandClouddeployTargetGke(d.Get("gke")),
Labels: checkStringMap(d.Get("labels")),
Project: dcl.String(project),
RequireApproval: dcl.Bool(d.Get("require_approval").(bool)),
}
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
billingProject := project
// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}
client := NewDCLClouddeployClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutRead))
if bp, err := replaceVars(d, config, client.Config.BasePath); err != nil {
d.SetId("")
return fmt.Errorf("Could not format %q: %w", client.Config.BasePath, err)
} else {
client.Config.BasePath = bp
}
res, err := client.GetTarget(context.Background(), obj)
if err != nil {
resourceName := fmt.Sprintf("ClouddeployTarget %q", d.Id())
return handleNotFoundDCLError(err, d, resourceName)
}
if err = d.Set("location", res.Location); err != nil {
return fmt.Errorf("error setting location in state: %s", err)
}
if err = d.Set("name", res.Name); err != nil {
return fmt.Errorf("error setting name in state: %s", err)
}
if err = d.Set("annotations", res.Annotations); err != nil {
return fmt.Errorf("error setting annotations in state: %s", err)
}
if err = d.Set("anthos_cluster", flattenClouddeployTargetAnthosCluster(res.AnthosCluster)); err != nil {
return fmt.Errorf("error setting anthos_cluster in state: %s", err)
}
if err = d.Set("description", res.Description); err != nil {
return fmt.Errorf("error setting description in state: %s", err)
}
if err = d.Set("execution_configs", flattenClouddeployTargetExecutionConfigsArray(res.ExecutionConfigs)); err != nil {
return fmt.Errorf("error setting execution_configs in state: %s", err)
}
if err = d.Set("gke", flattenClouddeployTargetGke(res.Gke)); err != nil {
return fmt.Errorf("error setting gke in state: %s", err)
}
if err = d.Set("labels", res.Labels); err != nil {
return fmt.Errorf("error setting labels in state: %s", err)
}
if err = d.Set("project", res.Project); err != nil {
return fmt.Errorf("error setting project in state: %s", err)
}
if err = d.Set("require_approval", res.RequireApproval); err != nil {
return fmt.Errorf("error setting require_approval in state: %s", err)
}
if err = d.Set("create_time", res.CreateTime); err != nil {
return fmt.Errorf("error setting create_time in state: %s", err)
}
if err = d.Set("etag", res.Etag); err != nil {
return fmt.Errorf("error setting etag in state: %s", err)
}
if err = d.Set("target_id", res.TargetId); err != nil {
return fmt.Errorf("error setting target_id in state: %s", err)
}
if err = d.Set("uid", res.Uid); err != nil {
return fmt.Errorf("error setting uid in state: %s", err)
}
if err = d.Set("update_time", res.UpdateTime); err != nil {
return fmt.Errorf("error setting update_time in state: %s", err)
}
return nil
}
func resourceClouddeployTargetUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
obj := &clouddeploy.Target{
Location: dcl.String(d.Get("location").(string)),
Name: dcl.String(d.Get("name").(string)),
Annotations: checkStringMap(d.Get("annotations")),
AnthosCluster: expandClouddeployTargetAnthosCluster(d.Get("anthos_cluster")),
Description: dcl.String(d.Get("description").(string)),
ExecutionConfigs: expandClouddeployTargetExecutionConfigsArray(d.Get("execution_configs")),
Gke: expandClouddeployTargetGke(d.Get("gke")),
Labels: checkStringMap(d.Get("labels")),
Project: dcl.String(project),
RequireApproval: dcl.Bool(d.Get("require_approval").(bool)),
}
directive := UpdateDirective
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
billingProject := ""
// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}
client := NewDCLClouddeployClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutUpdate))
if bp, err := replaceVars(d, config, client.Config.BasePath); err != nil {
d.SetId("")
return fmt.Errorf("Could not format %q: %w", client.Config.BasePath, err)
} else {
client.Config.BasePath = bp
}
res, err := client.ApplyTarget(context.Background(), obj, directive...)
if _, ok := err.(dcl.DiffAfterApplyError); ok {
log.Printf("[DEBUG] Diff after apply returned from the DCL: %s", err)
} else if err != nil {
// The resource didn't actually create
d.SetId("")
return fmt.Errorf("Error updating Target: %s", err)
}
log.Printf("[DEBUG] Finished creating Target %q: %#v", d.Id(), res)
return resourceClouddeployTargetRead(d, meta)
}
func resourceClouddeployTargetDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
obj := &clouddeploy.Target{
Location: dcl.String(d.Get("location").(string)),
Name: dcl.String(d.Get("name").(string)),
Annotations: checkStringMap(d.Get("annotations")),
AnthosCluster: expandClouddeployTargetAnthosCluster(d.Get("anthos_cluster")),
Description: dcl.String(d.Get("description").(string)),
ExecutionConfigs: expandClouddeployTargetExecutionConfigsArray(d.Get("execution_configs")),
Gke: expandClouddeployTargetGke(d.Get("gke")),
Labels: checkStringMap(d.Get("labels")),
Project: dcl.String(project),
RequireApproval: dcl.Bool(d.Get("require_approval").(bool)),
}
log.Printf("[DEBUG] Deleting Target %q", d.Id())
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
billingProject := project
// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}
client := NewDCLClouddeployClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutDelete))
if bp, err := replaceVars(d, config, client.Config.BasePath); err != nil {
d.SetId("")
return fmt.Errorf("Could not format %q: %w", client.Config.BasePath, err)
} else {
client.Config.BasePath = bp
}
if err := client.DeleteTarget(context.Background(), obj); err != nil {
return fmt.Errorf("Error deleting Target: %s", err)
}
log.Printf("[DEBUG] Finished deleting Target %q", d.Id())
return nil
}
func resourceClouddeployTargetImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)
if err := parseImportId([]string{
"projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/targets/(?P<name>[^/]+)",
"(?P<project>[^/]+)/(?P<location>[^/]+)/(?P<name>[^/]+)",
"(?P<location>[^/]+)/(?P<name>[^/]+)",
}, d, config); err != nil {
return nil, err
}
// Replace import id for the resource id
id, err := replaceVarsForId(d, config, "projects/{{project}}/locations/{{location}}/targets/{{name}}")
if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)
return []*schema.ResourceData{d}, nil
}
func expandClouddeployTargetAnthosCluster(o interface{}) *clouddeploy.TargetAnthosCluster {
if o == nil {
return clouddeploy.EmptyTargetAnthosCluster
}
objArr := o.([]interface{})
if len(objArr) == 0 || objArr[0] == nil {
return clouddeploy.EmptyTargetAnthosCluster
}
obj := objArr[0].(map[string]interface{})
return &clouddeploy.TargetAnthosCluster{
Membership: dcl.String(obj["membership"].(string)),
}
}
func flattenClouddeployTargetAnthosCluster(obj *clouddeploy.TargetAnthosCluster) interface{} {
if obj == nil || obj.Empty() {
return nil
}
transformed := map[string]interface{}{
"membership": obj.Membership,
}
return []interface{}{transformed}
}
func expandClouddeployTargetExecutionConfigsArray(o interface{}) []clouddeploy.TargetExecutionConfigs {
if o == nil {
return nil
}
objs := o.([]interface{})
if len(objs) == 0 || objs[0] == nil {
return nil
}
items := make([]clouddeploy.TargetExecutionConfigs, 0, len(objs))
for _, item := range objs {
i := expandClouddeployTargetExecutionConfigs(item)
items = append(items, *i)
}
return items
}
func expandClouddeployTargetExecutionConfigs(o interface{}) *clouddeploy.TargetExecutionConfigs {
if o == nil {
return nil
}
obj := o.(map[string]interface{})
return &clouddeploy.TargetExecutionConfigs{
Usages: expandClouddeployTargetExecutionConfigsUsagesArray(obj["usages"]),
ArtifactStorage: dcl.StringOrNil(obj["artifact_storage"].(string)),
ServiceAccount: dcl.StringOrNil(obj["service_account"].(string)),
WorkerPool: dcl.String(obj["worker_pool"].(string)),
}
}
func flattenClouddeployTargetExecutionConfigsArray(objs []clouddeploy.TargetExecutionConfigs) []interface{} {
if objs == nil {
return nil
}
items := []interface{}{}
for _, item := range objs {
i := flattenClouddeployTargetExecutionConfigs(&item)
items = append(items, i)
}
return items
}
func flattenClouddeployTargetExecutionConfigs(obj *clouddeploy.TargetExecutionConfigs) interface{} {
if obj == nil || obj.Empty() {
return nil
}
transformed := map[string]interface{}{
"usages": flattenClouddeployTargetExecutionConfigsUsagesArray(obj.Usages),
"artifact_storage": obj.ArtifactStorage,
"service_account": obj.ServiceAccount,
"worker_pool": obj.WorkerPool,
}
return transformed
}
func expandClouddeployTargetGke(o interface{}) *clouddeploy.TargetGke {
if o == nil {
return clouddeploy.EmptyTargetGke
}
objArr := o.([]interface{})
if len(objArr) == 0 || objArr[0] == nil {
return clouddeploy.EmptyTargetGke
}
obj := objArr[0].(map[string]interface{})
return &clouddeploy.TargetGke{
Cluster: dcl.String(obj["cluster"].(string)),
InternalIP: dcl.Bool(obj["internal_ip"].(bool)),
}
}
func flattenClouddeployTargetGke(obj *clouddeploy.TargetGke) interface{} {
if obj == nil || obj.Empty() {
return nil
}
transformed := map[string]interface{}{
"cluster": obj.Cluster,
"internal_ip": obj.InternalIP,
}
return []interface{}{transformed}
}
func flattenClouddeployTargetExecutionConfigsUsagesArray(obj []clouddeploy.TargetExecutionConfigsUsagesEnum) interface{} {
if obj == nil {
return nil
}
items := []string{}
for _, item := range obj {
items = append(items, string(item))
}
return items
}
func expandClouddeployTargetExecutionConfigsUsagesArray(o interface{}) []clouddeploy.TargetExecutionConfigsUsagesEnum {
objs := o.([]interface{})
items := make([]clouddeploy.TargetExecutionConfigsUsagesEnum, 0, len(objs))
for _, item := range objs {
i := clouddeploy.TargetExecutionConfigsUsagesEnumRef(item.(string))
items = append(items, *i)
}
return items
}