-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
resource_container_aws_cluster.go
1200 lines (1055 loc) · 41.2 KB
/
resource_container_aws_cluster.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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ----------------------------------------------------------------------------
//
// *** 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"
containeraws "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/containeraws"
)
func resourceContainerAwsCluster() *schema.Resource {
return &schema.Resource{
Create: resourceContainerAwsClusterCreate,
Read: resourceContainerAwsClusterRead,
Update: resourceContainerAwsClusterUpdate,
Delete: resourceContainerAwsClusterDelete,
Importer: &schema.ResourceImporter{
State: resourceContainerAwsClusterImport,
},
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{
"authorization": {
Type: schema.TypeList,
Required: true,
Description: "Configuration related to the cluster RBAC settings.",
MaxItems: 1,
Elem: ContainerAwsClusterAuthorizationSchema(),
},
"aws_region": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The AWS region where the cluster runs. Each Google Cloud region supports a subset of nearby AWS regions. You can call to list all supported AWS regions within a given Google Cloud region.",
},
"control_plane": {
Type: schema.TypeList,
Required: true,
Description: "Configuration related to the cluster control plane.",
MaxItems: 1,
Elem: ContainerAwsClusterControlPlaneSchema(),
},
"fleet": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Description: "Fleet configuration.",
MaxItems: 1,
Elem: ContainerAwsClusterFleetSchema(),
},
"location": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The location for the resource",
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The name of this resource.",
},
"networking": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Description: "Cluster-wide networking configuration.",
MaxItems: 1,
Elem: ContainerAwsClusterNetworkingSchema(),
},
"annotations": {
Type: schema.TypeMap,
Optional: true,
Description: "Optional. Annotations on the cluster. This field has the same restrictions as Kubernetes annotations. The total size of all keys and values combined is limited to 256k. Key can have 2 segments: prefix (optional) and name (required), separated by a slash (/). Prefix must be a DNS subdomain. Name must be 63 characters or less, begin and end with alphanumerics, with dashes (-), underscores (_), dots (.), and alphanumerics between.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "Optional. A human readable description of this cluster. Cannot be longer than 255 UTF-8 encoded bytes.",
},
"project": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
Description: "The project for the resource",
},
"create_time": {
Type: schema.TypeString,
Computed: true,
Description: "Output only. The time at which this cluster was created.",
},
"endpoint": {
Type: schema.TypeString,
Computed: true,
Description: "Output only. The endpoint of the cluster's API server.",
},
"etag": {
Type: schema.TypeString,
Computed: true,
Description: "Allows clients to perform consistent read-modify-writes through optimistic concurrency control. May be sent on update and delete requests to ensure the client has an up-to-date value before proceeding.",
},
"reconciling": {
Type: schema.TypeBool,
Computed: true,
Description: "Output only. If set, there are currently changes in flight to the cluster.",
},
"state": {
Type: schema.TypeString,
Computed: true,
Description: "Output only. The current state of the cluster. Possible values: STATE_UNSPECIFIED, PROVISIONING, RUNNING, RECONCILING, STOPPING, ERROR, DEGRADED",
},
"uid": {
Type: schema.TypeString,
Computed: true,
Description: "Output only. A globally unique identifier for the cluster.",
},
"update_time": {
Type: schema.TypeString,
Computed: true,
Description: "Output only. The time at which this cluster was last updated.",
},
"workload_identity_config": {
Type: schema.TypeList,
Computed: true,
Description: "Output only. Workload Identity settings.",
Elem: ContainerAwsClusterWorkloadIdentityConfigSchema(),
},
},
}
}
func ContainerAwsClusterAuthorizationSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"admin_users": {
Type: schema.TypeList,
Required: true,
Description: "Users to perform operations as a cluster admin. A managed ClusterRoleBinding will be created to grant the `cluster-admin` ClusterRole to the users. Up to ten admin users can be provided. For more info on RBAC, see https://kubernetes.io/docs/reference/access-authn-authz/rbac/#user-facing-roles",
Elem: ContainerAwsClusterAuthorizationAdminUsersSchema(),
},
},
}
}
func ContainerAwsClusterAuthorizationAdminUsersSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"username": {
Type: schema.TypeString,
Required: true,
Description: "The name of the user, e.g. `my-gcp-id@gmail.com`.",
},
},
}
}
func ContainerAwsClusterControlPlaneSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"aws_services_authentication": {
Type: schema.TypeList,
Required: true,
Description: "Authentication configuration for management of AWS resources.",
MaxItems: 1,
Elem: ContainerAwsClusterControlPlaneAwsServicesAuthenticationSchema(),
},
"config_encryption": {
Type: schema.TypeList,
Required: true,
Description: "The ARN of the AWS KMS key used to encrypt cluster configuration.",
MaxItems: 1,
Elem: ContainerAwsClusterControlPlaneConfigEncryptionSchema(),
},
"database_encryption": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Description: "The ARN of the AWS KMS key used to encrypt cluster secrets.",
MaxItems: 1,
Elem: ContainerAwsClusterControlPlaneDatabaseEncryptionSchema(),
},
"iam_instance_profile": {
Type: schema.TypeString,
Required: true,
Description: "The name of the AWS IAM instance pofile to assign to each control plane replica.",
},
"subnet_ids": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Description: "The list of subnets where control plane replicas will run. A replica will be provisioned on each subnet and up to three values can be provided. Each subnet must be in a different AWS Availability Zone (AZ).",
Elem: &schema.Schema{Type: schema.TypeString},
},
"version": {
Type: schema.TypeString,
Required: true,
Description: "The Kubernetes version to run on control plane replicas (e.g. `1.19.10-gke.1000`). You can list all supported versions on a given Google Cloud region by calling .",
},
"instance_type": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "Optional. The AWS instance type. When unspecified, it defaults to `m5.large`.",
},
"main_volume": {
Type: schema.TypeList,
Computed: true,
Optional: true,
ForceNew: true,
Description: "Optional. Configuration related to the main volume provisioned for each control plane replica. The main volume is in charge of storing all of the cluster's etcd state. Volumes will be provisioned in the availability zone associated with the corresponding subnet. When unspecified, it defaults to 8 GiB with the GP2 volume type.",
MaxItems: 1,
Elem: ContainerAwsClusterControlPlaneMainVolumeSchema(),
},
"proxy_config": {
Type: schema.TypeList,
Optional: true,
Description: "Proxy configuration for outbound HTTP(S) traffic.",
MaxItems: 1,
Elem: ContainerAwsClusterControlPlaneProxyConfigSchema(),
},
"root_volume": {
Type: schema.TypeList,
Computed: true,
Optional: true,
Description: "Optional. Configuration related to the root volume provisioned for each control plane replica. Volumes will be provisioned in the availability zone associated with the corresponding subnet. When unspecified, it defaults to 32 GiB with the GP2 volume type.",
MaxItems: 1,
Elem: ContainerAwsClusterControlPlaneRootVolumeSchema(),
},
"security_group_ids": {
Type: schema.TypeList,
Optional: true,
Description: "Optional. The IDs of additional security groups to add to control plane replicas. The Anthos Multi-Cloud API will automatically create and manage security groups with the minimum rules needed for a functioning cluster.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"ssh_config": {
Type: schema.TypeList,
Optional: true,
Description: "Optional. SSH configuration for how to access the underlying control plane machines.",
MaxItems: 1,
Elem: ContainerAwsClusterControlPlaneSshConfigSchema(),
},
"tags": {
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Description: "Optional. A set of AWS resource tags to propagate to all underlying managed AWS resources. Specify at most 50 pairs containing alphanumerics, spaces, and symbols (.+-=_:@/). Keys can be up to 127 Unicode characters. Values can be up to 255 Unicode characters.",
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func ContainerAwsClusterControlPlaneAwsServicesAuthenticationSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"role_arn": {
Type: schema.TypeString,
Required: true,
Description: "The Amazon Resource Name (ARN) of the role that the Anthos Multi-Cloud API will assume when managing AWS resources on your account.",
},
"role_session_name": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "Optional. An identifier for the assumed role session. When unspecified, it defaults to `multicloud-service-agent`.",
},
},
}
}
func ContainerAwsClusterControlPlaneConfigEncryptionSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"kms_key_arn": {
Type: schema.TypeString,
Required: true,
Description: "The ARN of the AWS KMS key used to encrypt cluster configuration.",
},
},
}
}
func ContainerAwsClusterControlPlaneDatabaseEncryptionSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"kms_key_arn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The ARN of the AWS KMS key used to encrypt cluster secrets.",
},
},
}
}
func ContainerAwsClusterControlPlaneMainVolumeSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"iops": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
ForceNew: true,
Description: "Optional. The number of I/O operations per second (IOPS) to provision for GP3 volume.",
},
"kms_key_arn": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "Optional. The Amazon Resource Name (ARN) of the Customer Managed Key (CMK) used to encrypt AWS EBS volumes. If not specified, the default Amazon managed key associated to the AWS region where this cluster runs will be used.",
},
"size_gib": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
ForceNew: true,
Description: "Optional. The size of the volume, in GiBs. When unspecified, a default value is provided. See the specific reference in the parent resource.",
},
"volume_type": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
Description: "Optional. Type of the EBS volume. When unspecified, it defaults to GP2 volume. Possible values: VOLUME_TYPE_UNSPECIFIED, GP2, GP3",
},
},
}
}
func ContainerAwsClusterControlPlaneProxyConfigSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"secret_arn": {
Type: schema.TypeString,
Required: true,
Description: "The ARN of the AWS Secret Manager secret that contains the HTTP(S) proxy configuration.",
},
"secret_version": {
Type: schema.TypeString,
Required: true,
Description: "The version string of the AWS Secret Manager secret that contains the HTTP(S) proxy configuration.",
},
},
}
}
func ContainerAwsClusterControlPlaneRootVolumeSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"iops": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: "Optional. The number of I/O operations per second (IOPS) to provision for GP3 volume.",
},
"kms_key_arn": {
Type: schema.TypeString,
Optional: true,
Description: "Optional. The Amazon Resource Name (ARN) of the Customer Managed Key (CMK) used to encrypt AWS EBS volumes. If not specified, the default Amazon managed key associated to the AWS region where this cluster runs will be used.",
},
"size_gib": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: "Optional. The size of the volume, in GiBs. When unspecified, a default value is provided. See the specific reference in the parent resource.",
},
"volume_type": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "Optional. Type of the EBS volume. When unspecified, it defaults to GP2 volume. Possible values: VOLUME_TYPE_UNSPECIFIED, GP2, GP3",
},
},
}
}
func ContainerAwsClusterControlPlaneSshConfigSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"ec2_key_pair": {
Type: schema.TypeString,
Required: true,
Description: "The name of the EC2 key pair used to login into cluster machines.",
},
},
}
}
func ContainerAwsClusterFleetSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"project": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
Description: "The number of the Fleet host project where this cluster will be registered.",
},
"membership": {
Type: schema.TypeString,
Computed: true,
Description: "The name of the managed Hub Membership resource associated to this cluster. Membership names are formatted as projects/<project-number>/locations/global/membership/<cluster-id>.",
},
},
}
}
func ContainerAwsClusterNetworkingSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"pod_address_cidr_blocks": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Description: "All pods in the cluster are assigned an RFC1918 IPv4 address from these ranges. Only a single range is supported. This field cannot be changed after creation.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"service_address_cidr_blocks": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Description: "All services in the cluster are assigned an RFC1918 IPv4 address from these ranges. Only a single range is supported. This field cannot be changed after creation.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"vpc_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The VPC associated with the cluster. All component clusters (i.e. control plane and node pools) run on a single VPC. This field cannot be changed after creation.",
},
},
}
}
func ContainerAwsClusterWorkloadIdentityConfigSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"identity_provider": {
Type: schema.TypeString,
Computed: true,
Description: "The ID of the OIDC Identity Provider (IdP) associated to the Workload Identity Pool.",
},
"issuer_uri": {
Type: schema.TypeString,
Computed: true,
Description: "The OIDC issuer URL for this cluster.",
},
"workload_pool": {
Type: schema.TypeString,
Computed: true,
Description: "The Workload Identity Pool associated to the cluster.",
},
},
}
}
func resourceContainerAwsClusterCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
obj := &containeraws.Cluster{
Authorization: expandContainerAwsClusterAuthorization(d.Get("authorization")),
AwsRegion: dcl.String(d.Get("aws_region").(string)),
ControlPlane: expandContainerAwsClusterControlPlane(d.Get("control_plane")),
Fleet: expandContainerAwsClusterFleet(d.Get("fleet")),
Location: dcl.String(d.Get("location").(string)),
Name: dcl.String(d.Get("name").(string)),
Networking: expandContainerAwsClusterNetworking(d.Get("networking")),
Annotations: checkStringMap(d.Get("annotations")),
Description: dcl.String(d.Get("description").(string)),
Project: dcl.String(project),
}
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 := NewDCLContainerAwsClient(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.ApplyCluster(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 Cluster: %s", err)
}
log.Printf("[DEBUG] Finished creating Cluster %q: %#v", d.Id(), res)
return resourceContainerAwsClusterRead(d, meta)
}
func resourceContainerAwsClusterRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
obj := &containeraws.Cluster{
Authorization: expandContainerAwsClusterAuthorization(d.Get("authorization")),
AwsRegion: dcl.String(d.Get("aws_region").(string)),
ControlPlane: expandContainerAwsClusterControlPlane(d.Get("control_plane")),
Fleet: expandContainerAwsClusterFleet(d.Get("fleet")),
Location: dcl.String(d.Get("location").(string)),
Name: dcl.String(d.Get("name").(string)),
Networking: expandContainerAwsClusterNetworking(d.Get("networking")),
Annotations: checkStringMap(d.Get("annotations")),
Description: dcl.String(d.Get("description").(string)),
Project: dcl.String(project),
}
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 := NewDCLContainerAwsClient(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.GetCluster(context.Background(), obj)
if err != nil {
resourceName := fmt.Sprintf("ContainerAwsCluster %q", d.Id())
return handleNotFoundDCLError(err, d, resourceName)
}
if err = d.Set("authorization", flattenContainerAwsClusterAuthorization(res.Authorization)); err != nil {
return fmt.Errorf("error setting authorization in state: %s", err)
}
if err = d.Set("aws_region", res.AwsRegion); err != nil {
return fmt.Errorf("error setting aws_region in state: %s", err)
}
if err = d.Set("control_plane", flattenContainerAwsClusterControlPlane(res.ControlPlane)); err != nil {
return fmt.Errorf("error setting control_plane in state: %s", err)
}
if err = d.Set("fleet", flattenContainerAwsClusterFleet(res.Fleet)); err != nil {
return fmt.Errorf("error setting fleet in state: %s", err)
}
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("networking", flattenContainerAwsClusterNetworking(res.Networking)); err != nil {
return fmt.Errorf("error setting networking 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("description", res.Description); err != nil {
return fmt.Errorf("error setting description 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("create_time", res.CreateTime); err != nil {
return fmt.Errorf("error setting create_time in state: %s", err)
}
if err = d.Set("endpoint", res.Endpoint); err != nil {
return fmt.Errorf("error setting endpoint 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("reconciling", res.Reconciling); err != nil {
return fmt.Errorf("error setting reconciling in state: %s", err)
}
if err = d.Set("state", res.State); err != nil {
return fmt.Errorf("error setting state 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)
}
if err = d.Set("workload_identity_config", flattenContainerAwsClusterWorkloadIdentityConfig(res.WorkloadIdentityConfig)); err != nil {
return fmt.Errorf("error setting workload_identity_config in state: %s", err)
}
return nil
}
func resourceContainerAwsClusterUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
obj := &containeraws.Cluster{
Authorization: expandContainerAwsClusterAuthorization(d.Get("authorization")),
AwsRegion: dcl.String(d.Get("aws_region").(string)),
ControlPlane: expandContainerAwsClusterControlPlane(d.Get("control_plane")),
Fleet: expandContainerAwsClusterFleet(d.Get("fleet")),
Location: dcl.String(d.Get("location").(string)),
Name: dcl.String(d.Get("name").(string)),
Networking: expandContainerAwsClusterNetworking(d.Get("networking")),
Annotations: checkStringMap(d.Get("annotations")),
Description: dcl.String(d.Get("description").(string)),
Project: dcl.String(project),
}
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 := NewDCLContainerAwsClient(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.ApplyCluster(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 Cluster: %s", err)
}
log.Printf("[DEBUG] Finished creating Cluster %q: %#v", d.Id(), res)
return resourceContainerAwsClusterRead(d, meta)
}
func resourceContainerAwsClusterDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
obj := &containeraws.Cluster{
Authorization: expandContainerAwsClusterAuthorization(d.Get("authorization")),
AwsRegion: dcl.String(d.Get("aws_region").(string)),
ControlPlane: expandContainerAwsClusterControlPlane(d.Get("control_plane")),
Fleet: expandContainerAwsClusterFleet(d.Get("fleet")),
Location: dcl.String(d.Get("location").(string)),
Name: dcl.String(d.Get("name").(string)),
Networking: expandContainerAwsClusterNetworking(d.Get("networking")),
Annotations: checkStringMap(d.Get("annotations")),
Description: dcl.String(d.Get("description").(string)),
Project: dcl.String(project),
}
log.Printf("[DEBUG] Deleting Cluster %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 := NewDCLContainerAwsClient(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.DeleteCluster(context.Background(), obj); err != nil {
return fmt.Errorf("Error deleting Cluster: %s", err)
}
log.Printf("[DEBUG] Finished deleting Cluster %q", d.Id())
return nil
}
func resourceContainerAwsClusterImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)
if err := parseImportId([]string{
"projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/awsClusters/(?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}}/awsClusters/{{name}}")
if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)
return []*schema.ResourceData{d}, nil
}
func expandContainerAwsClusterAuthorization(o interface{}) *containeraws.ClusterAuthorization {
if o == nil {
return containeraws.EmptyClusterAuthorization
}
objArr := o.([]interface{})
if len(objArr) == 0 || objArr[0] == nil {
return containeraws.EmptyClusterAuthorization
}
obj := objArr[0].(map[string]interface{})
return &containeraws.ClusterAuthorization{
AdminUsers: expandContainerAwsClusterAuthorizationAdminUsersArray(obj["admin_users"]),
}
}
func flattenContainerAwsClusterAuthorization(obj *containeraws.ClusterAuthorization) interface{} {
if obj == nil || obj.Empty() {
return nil
}
transformed := map[string]interface{}{
"admin_users": flattenContainerAwsClusterAuthorizationAdminUsersArray(obj.AdminUsers),
}
return []interface{}{transformed}
}
func expandContainerAwsClusterAuthorizationAdminUsersArray(o interface{}) []containeraws.ClusterAuthorizationAdminUsers {
if o == nil {
return make([]containeraws.ClusterAuthorizationAdminUsers, 0)
}
objs := o.([]interface{})
if len(objs) == 0 || objs[0] == nil {
return make([]containeraws.ClusterAuthorizationAdminUsers, 0)
}
items := make([]containeraws.ClusterAuthorizationAdminUsers, 0, len(objs))
for _, item := range objs {
i := expandContainerAwsClusterAuthorizationAdminUsers(item)
items = append(items, *i)
}
return items
}
func expandContainerAwsClusterAuthorizationAdminUsers(o interface{}) *containeraws.ClusterAuthorizationAdminUsers {
if o == nil {
return containeraws.EmptyClusterAuthorizationAdminUsers
}
obj := o.(map[string]interface{})
return &containeraws.ClusterAuthorizationAdminUsers{
Username: dcl.String(obj["username"].(string)),
}
}
func flattenContainerAwsClusterAuthorizationAdminUsersArray(objs []containeraws.ClusterAuthorizationAdminUsers) []interface{} {
if objs == nil {
return nil
}
items := []interface{}{}
for _, item := range objs {
i := flattenContainerAwsClusterAuthorizationAdminUsers(&item)
items = append(items, i)
}
return items
}
func flattenContainerAwsClusterAuthorizationAdminUsers(obj *containeraws.ClusterAuthorizationAdminUsers) interface{} {
if obj == nil || obj.Empty() {
return nil
}
transformed := map[string]interface{}{
"username": obj.Username,
}
return transformed
}
func expandContainerAwsClusterControlPlane(o interface{}) *containeraws.ClusterControlPlane {
if o == nil {
return containeraws.EmptyClusterControlPlane
}
objArr := o.([]interface{})
if len(objArr) == 0 || objArr[0] == nil {
return containeraws.EmptyClusterControlPlane
}
obj := objArr[0].(map[string]interface{})
return &containeraws.ClusterControlPlane{
AwsServicesAuthentication: expandContainerAwsClusterControlPlaneAwsServicesAuthentication(obj["aws_services_authentication"]),
ConfigEncryption: expandContainerAwsClusterControlPlaneConfigEncryption(obj["config_encryption"]),
DatabaseEncryption: expandContainerAwsClusterControlPlaneDatabaseEncryption(obj["database_encryption"]),
IamInstanceProfile: dcl.String(obj["iam_instance_profile"].(string)),
SubnetIds: expandStringArray(obj["subnet_ids"]),
Version: dcl.String(obj["version"].(string)),
InstanceType: dcl.StringOrNil(obj["instance_type"].(string)),
MainVolume: expandContainerAwsClusterControlPlaneMainVolume(obj["main_volume"]),
ProxyConfig: expandContainerAwsClusterControlPlaneProxyConfig(obj["proxy_config"]),
RootVolume: expandContainerAwsClusterControlPlaneRootVolume(obj["root_volume"]),
SecurityGroupIds: expandStringArray(obj["security_group_ids"]),
SshConfig: expandContainerAwsClusterControlPlaneSshConfig(obj["ssh_config"]),
Tags: checkStringMap(obj["tags"]),
}
}
func flattenContainerAwsClusterControlPlane(obj *containeraws.ClusterControlPlane) interface{} {
if obj == nil || obj.Empty() {
return nil
}
transformed := map[string]interface{}{
"aws_services_authentication": flattenContainerAwsClusterControlPlaneAwsServicesAuthentication(obj.AwsServicesAuthentication),
"config_encryption": flattenContainerAwsClusterControlPlaneConfigEncryption(obj.ConfigEncryption),
"database_encryption": flattenContainerAwsClusterControlPlaneDatabaseEncryption(obj.DatabaseEncryption),
"iam_instance_profile": obj.IamInstanceProfile,
"subnet_ids": obj.SubnetIds,
"version": obj.Version,
"instance_type": obj.InstanceType,
"main_volume": flattenContainerAwsClusterControlPlaneMainVolume(obj.MainVolume),
"proxy_config": flattenContainerAwsClusterControlPlaneProxyConfig(obj.ProxyConfig),
"root_volume": flattenContainerAwsClusterControlPlaneRootVolume(obj.RootVolume),
"security_group_ids": obj.SecurityGroupIds,
"ssh_config": flattenContainerAwsClusterControlPlaneSshConfig(obj.SshConfig),
"tags": obj.Tags,
}
return []interface{}{transformed}
}
func expandContainerAwsClusterControlPlaneAwsServicesAuthentication(o interface{}) *containeraws.ClusterControlPlaneAwsServicesAuthentication {
if o == nil {
return containeraws.EmptyClusterControlPlaneAwsServicesAuthentication
}
objArr := o.([]interface{})
if len(objArr) == 0 || objArr[0] == nil {
return containeraws.EmptyClusterControlPlaneAwsServicesAuthentication
}
obj := objArr[0].(map[string]interface{})
return &containeraws.ClusterControlPlaneAwsServicesAuthentication{
RoleArn: dcl.String(obj["role_arn"].(string)),
RoleSessionName: dcl.StringOrNil(obj["role_session_name"].(string)),
}
}
func flattenContainerAwsClusterControlPlaneAwsServicesAuthentication(obj *containeraws.ClusterControlPlaneAwsServicesAuthentication) interface{} {
if obj == nil || obj.Empty() {
return nil
}
transformed := map[string]interface{}{
"role_arn": obj.RoleArn,
"role_session_name": obj.RoleSessionName,
}
return []interface{}{transformed}
}
func expandContainerAwsClusterControlPlaneConfigEncryption(o interface{}) *containeraws.ClusterControlPlaneConfigEncryption {
if o == nil {
return containeraws.EmptyClusterControlPlaneConfigEncryption
}
objArr := o.([]interface{})
if len(objArr) == 0 || objArr[0] == nil {
return containeraws.EmptyClusterControlPlaneConfigEncryption
}
obj := objArr[0].(map[string]interface{})
return &containeraws.ClusterControlPlaneConfigEncryption{
KmsKeyArn: dcl.String(obj["kms_key_arn"].(string)),
}
}
func flattenContainerAwsClusterControlPlaneConfigEncryption(obj *containeraws.ClusterControlPlaneConfigEncryption) interface{} {
if obj == nil || obj.Empty() {
return nil
}
transformed := map[string]interface{}{
"kms_key_arn": obj.KmsKeyArn,
}
return []interface{}{transformed}
}
func expandContainerAwsClusterControlPlaneDatabaseEncryption(o interface{}) *containeraws.ClusterControlPlaneDatabaseEncryption {
if o == nil {
return containeraws.EmptyClusterControlPlaneDatabaseEncryption
}
objArr := o.([]interface{})
if len(objArr) == 0 || objArr[0] == nil {
return containeraws.EmptyClusterControlPlaneDatabaseEncryption
}
obj := objArr[0].(map[string]interface{})
return &containeraws.ClusterControlPlaneDatabaseEncryption{
KmsKeyArn: dcl.String(obj["kms_key_arn"].(string)),
}
}