/
resource_privateca_certificate_template.go
1214 lines (1052 loc) · 45.4 KB
/
resource_privateca_certificate_template.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"
privateca "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/privateca"
)
func resourcePrivatecaCertificateTemplate() *schema.Resource {
return &schema.Resource{
Create: resourcePrivatecaCertificateTemplateCreate,
Read: resourcePrivatecaCertificateTemplateRead,
Update: resourcePrivatecaCertificateTemplateUpdate,
Delete: resourcePrivatecaCertificateTemplateDelete,
Importer: &schema.ResourceImporter{
State: resourcePrivatecaCertificateTemplateImport,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * 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: "The resource name for this CertificateTemplate in the format `projects/*/locations/*/certificateTemplates/*`.",
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "Optional. A human-readable description of scenarios this template is intended for.",
},
"identity_constraints": {
Type: schema.TypeList,
Optional: true,
Description: "Optional. Describes constraints on identities that may be appear in Certificates issued using this template. If this is omitted, then this template will not add restrictions on a certificate's identity.",
MaxItems: 1,
Elem: PrivatecaCertificateTemplateIdentityConstraintsSchema(),
},
"labels": {
Type: schema.TypeMap,
Optional: true,
Description: "Optional. Labels with user-defined metadata.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"passthrough_extensions": {
Type: schema.TypeList,
Optional: true,
Description: "Optional. Describes the set of X.509 extensions that may appear in a Certificate issued using this CertificateTemplate. If a certificate request sets extensions that don't appear in the passthrough_extensions, those extensions will be dropped. If the issuing CaPool's IssuancePolicy defines baseline_values that don't appear here, the certificate issuance request will fail. If this is omitted, then this template will not add restrictions on a certificate's X.509 extensions. These constraints do not apply to X.509 extensions set in this CertificateTemplate's predefined_values.",
MaxItems: 1,
Elem: PrivatecaCertificateTemplatePassthroughExtensionsSchema(),
},
"predefined_values": {
Type: schema.TypeList,
Optional: true,
Description: "Optional. A set of X.509 values that will be applied to all issued certificates that use this template. If the certificate request includes conflicting values for the same properties, they will be overwritten by the values defined here. If the issuing CaPool's IssuancePolicy defines conflicting baseline_values for the same properties, the certificate issuance request will fail.",
MaxItems: 1,
Elem: PrivatecaCertificateTemplatePredefinedValuesSchema(),
},
"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 CertificateTemplate was created.",
},
"update_time": {
Type: schema.TypeString,
Computed: true,
Description: "Output only. The time at which this CertificateTemplate was updated.",
},
},
}
}
func PrivatecaCertificateTemplateIdentityConstraintsSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"allow_subject_alt_names_passthrough": {
Type: schema.TypeBool,
Required: true,
Description: "Required. If this is true, the SubjectAltNames extension may be copied from a certificate request into the signed certificate. Otherwise, the requested SubjectAltNames will be discarded.",
},
"allow_subject_passthrough": {
Type: schema.TypeBool,
Required: true,
Description: "Required. If this is true, the Subject field may be copied from a certificate request into the signed certificate. Otherwise, the requested Subject will be discarded.",
},
"cel_expression": {
Type: schema.TypeList,
Optional: true,
Description: "Optional. A CEL expression that may be used to validate the resolved X.509 Subject and/or Subject Alternative Name before a certificate is signed. To see the full allowed syntax and some examples, see https://cloud.google.com/certificate-authority-service/docs/using-cel",
MaxItems: 1,
Elem: PrivatecaCertificateTemplateIdentityConstraintsCelExpressionSchema(),
},
},
}
}
func PrivatecaCertificateTemplateIdentityConstraintsCelExpressionSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Optional: true,
Description: "Optional. Description of the expression. This is a longer text which describes the expression, e.g. when hovered over it in a UI.",
},
"expression": {
Type: schema.TypeString,
Optional: true,
Description: "Textual representation of an expression in Common Expression Language syntax.",
},
"location": {
Type: schema.TypeString,
Optional: true,
Description: "Optional. String indicating the location of the expression for error reporting, e.g. a file name and a position in the file.",
},
"title": {
Type: schema.TypeString,
Optional: true,
Description: "Optional. Title for the expression, i.e. a short string describing its purpose. This can be used e.g. in UIs which allow to enter the expression.",
},
},
}
}
func PrivatecaCertificateTemplatePassthroughExtensionsSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"additional_extensions": {
Type: schema.TypeList,
Optional: true,
Description: "Optional. A set of ObjectIds identifying custom X.509 extensions. Will be combined with known_extensions to determine the full set of X.509 extensions.",
Elem: PrivatecaCertificateTemplatePassthroughExtensionsAdditionalExtensionsSchema(),
},
"known_extensions": {
Type: schema.TypeList,
Optional: true,
Description: "Optional. A set of named X.509 extensions. Will be combined with additional_extensions to determine the full set of X.509 extensions.",
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func PrivatecaCertificateTemplatePassthroughExtensionsAdditionalExtensionsSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"object_id_path": {
Type: schema.TypeList,
Required: true,
Description: "Required. The parts of an OID path. The most significant parts of the path come first.",
Elem: &schema.Schema{Type: schema.TypeInt},
},
},
}
}
func PrivatecaCertificateTemplatePredefinedValuesSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"additional_extensions": {
Type: schema.TypeList,
Optional: true,
Description: "Optional. Describes custom X.509 extensions.",
Elem: PrivatecaCertificateTemplatePredefinedValuesAdditionalExtensionsSchema(),
},
"aia_ocsp_servers": {
Type: schema.TypeList,
Optional: true,
Description: "Optional. Describes Online Certificate Status Protocol (OCSP) endpoint addresses that appear in the \"Authority Information Access\" extension in the certificate.",
Elem: &schema.Schema{Type: schema.TypeString},
},
"ca_options": {
Type: schema.TypeList,
Optional: true,
Description: "Optional. Describes options in this X509Parameters that are relevant in a CA certificate.",
MaxItems: 1,
Elem: PrivatecaCertificateTemplatePredefinedValuesCaOptionsSchema(),
},
"key_usage": {
Type: schema.TypeList,
Optional: true,
Description: "Optional. Indicates the intended use for keys that correspond to a certificate.",
MaxItems: 1,
Elem: PrivatecaCertificateTemplatePredefinedValuesKeyUsageSchema(),
},
"policy_ids": {
Type: schema.TypeList,
Optional: true,
Description: "Optional. Describes the X.509 certificate policy object identifiers, per https://tools.ietf.org/html/rfc5280#section-4.2.1.4.",
Elem: PrivatecaCertificateTemplatePredefinedValuesPolicyIdsSchema(),
},
},
}
}
func PrivatecaCertificateTemplatePredefinedValuesAdditionalExtensionsSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"object_id": {
Type: schema.TypeList,
Required: true,
Description: "Required. The OID for this X.509 extension.",
MaxItems: 1,
Elem: PrivatecaCertificateTemplatePredefinedValuesAdditionalExtensionsObjectIdSchema(),
},
"value": {
Type: schema.TypeString,
Required: true,
Description: "Required. The value of this X.509 extension.",
},
"critical": {
Type: schema.TypeBool,
Optional: true,
Description: "Optional. Indicates whether or not this extension is critical (i.e., if the client does not know how to handle this extension, the client should consider this to be an error).",
},
},
}
}
func PrivatecaCertificateTemplatePredefinedValuesAdditionalExtensionsObjectIdSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"object_id_path": {
Type: schema.TypeList,
Required: true,
Description: "Required. The parts of an OID path. The most significant parts of the path come first.",
Elem: &schema.Schema{Type: schema.TypeInt},
},
},
}
}
func PrivatecaCertificateTemplatePredefinedValuesCaOptionsSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"is_ca": {
Type: schema.TypeBool,
Optional: true,
Description: "Optional. Refers to the \"CA\" X.509 extension, which is a boolean value. When this value is missing, the extension will be omitted from the CA certificate.",
},
"max_issuer_path_length": {
Type: schema.TypeInt,
Optional: true,
Description: "Optional. Refers to the path length restriction X.509 extension. For a CA certificate, this value describes the depth of subordinate CA certificates that are allowed. If this value is less than 0, the request will fail. If this value is missing, the max path length will be omitted from the CA certificate.",
},
},
}
}
func PrivatecaCertificateTemplatePredefinedValuesKeyUsageSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"base_key_usage": {
Type: schema.TypeList,
Optional: true,
Description: "Describes high-level ways in which a key may be used.",
MaxItems: 1,
Elem: PrivatecaCertificateTemplatePredefinedValuesKeyUsageBaseKeyUsageSchema(),
},
"extended_key_usage": {
Type: schema.TypeList,
Optional: true,
Description: "Detailed scenarios in which a key may be used.",
MaxItems: 1,
Elem: PrivatecaCertificateTemplatePredefinedValuesKeyUsageExtendedKeyUsageSchema(),
},
"unknown_extended_key_usages": {
Type: schema.TypeList,
Optional: true,
Description: "Used to describe extended key usages that are not listed in the KeyUsage.ExtendedKeyUsageOptions message.",
Elem: PrivatecaCertificateTemplatePredefinedValuesKeyUsageUnknownExtendedKeyUsagesSchema(),
},
},
}
}
func PrivatecaCertificateTemplatePredefinedValuesKeyUsageBaseKeyUsageSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"cert_sign": {
Type: schema.TypeBool,
Optional: true,
Description: "The key may be used to sign certificates.",
},
"content_commitment": {
Type: schema.TypeBool,
Optional: true,
Description: "The key may be used for cryptographic commitments. Note that this may also be referred to as \"non-repudiation\".",
},
"crl_sign": {
Type: schema.TypeBool,
Optional: true,
Description: "The key may be used sign certificate revocation lists.",
},
"data_encipherment": {
Type: schema.TypeBool,
Optional: true,
Description: "The key may be used to encipher data.",
},
"decipher_only": {
Type: schema.TypeBool,
Optional: true,
Description: "The key may be used to decipher only.",
},
"digital_signature": {
Type: schema.TypeBool,
Optional: true,
Description: "The key may be used for digital signatures.",
},
"encipher_only": {
Type: schema.TypeBool,
Optional: true,
Description: "The key may be used to encipher only.",
},
"key_agreement": {
Type: schema.TypeBool,
Optional: true,
Description: "The key may be used in a key agreement protocol.",
},
"key_encipherment": {
Type: schema.TypeBool,
Optional: true,
Description: "The key may be used to encipher other keys.",
},
},
}
}
func PrivatecaCertificateTemplatePredefinedValuesKeyUsageExtendedKeyUsageSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"client_auth": {
Type: schema.TypeBool,
Optional: true,
Description: "Corresponds to OID 1.3.6.1.5.5.7.3.2. Officially described as \"TLS WWW client authentication\", though regularly used for non-WWW TLS.",
},
"code_signing": {
Type: schema.TypeBool,
Optional: true,
Description: "Corresponds to OID 1.3.6.1.5.5.7.3.3. Officially described as \"Signing of downloadable executable code client authentication\".",
},
"email_protection": {
Type: schema.TypeBool,
Optional: true,
Description: "Corresponds to OID 1.3.6.1.5.5.7.3.4. Officially described as \"Email protection\".",
},
"ocsp_signing": {
Type: schema.TypeBool,
Optional: true,
Description: "Corresponds to OID 1.3.6.1.5.5.7.3.9. Officially described as \"Signing OCSP responses\".",
},
"server_auth": {
Type: schema.TypeBool,
Optional: true,
Description: "Corresponds to OID 1.3.6.1.5.5.7.3.1. Officially described as \"TLS WWW server authentication\", though regularly used for non-WWW TLS.",
},
"time_stamping": {
Type: schema.TypeBool,
Optional: true,
Description: "Corresponds to OID 1.3.6.1.5.5.7.3.8. Officially described as \"Binding the hash of an object to a time\".",
},
},
}
}
func PrivatecaCertificateTemplatePredefinedValuesKeyUsageUnknownExtendedKeyUsagesSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"object_id_path": {
Type: schema.TypeList,
Required: true,
Description: "Required. The parts of an OID path. The most significant parts of the path come first.",
Elem: &schema.Schema{Type: schema.TypeInt},
},
},
}
}
func PrivatecaCertificateTemplatePredefinedValuesPolicyIdsSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"object_id_path": {
Type: schema.TypeList,
Required: true,
Description: "Required. The parts of an OID path. The most significant parts of the path come first.",
Elem: &schema.Schema{Type: schema.TypeInt},
},
},
}
}
func resourcePrivatecaCertificateTemplateCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
obj := &privateca.CertificateTemplate{
Location: dcl.String(d.Get("location").(string)),
Name: dcl.String(d.Get("name").(string)),
Description: dcl.String(d.Get("description").(string)),
IdentityConstraints: expandPrivatecaCertificateTemplateIdentityConstraints(d.Get("identity_constraints")),
Labels: checkStringMap(d.Get("labels")),
PassthroughExtensions: expandPrivatecaCertificateTemplatePassthroughExtensions(d.Get("passthrough_extensions")),
PredefinedValues: expandPrivatecaCertificateTemplatePredefinedValues(d.Get("predefined_values")),
Project: dcl.String(project),
}
id, err := replaceVarsForId(d, config, "projects/{{project}}/locations/{{location}}/certificateTemplates/{{name}}")
if err != nil {
return fmt.Errorf("error constructing id: %s", err)
}
d.SetId(id)
createDirective := 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 := NewDCLPrivatecaClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutCreate))
res, err := client.ApplyCertificateTemplate(context.Background(), obj, createDirective...)
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 CertificateTemplate: %s", err)
}
log.Printf("[DEBUG] Finished creating CertificateTemplate %q: %#v", d.Id(), res)
return resourcePrivatecaCertificateTemplateRead(d, meta)
}
func resourcePrivatecaCertificateTemplateRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
obj := &privateca.CertificateTemplate{
Location: dcl.String(d.Get("location").(string)),
Name: dcl.String(d.Get("name").(string)),
Description: dcl.String(d.Get("description").(string)),
IdentityConstraints: expandPrivatecaCertificateTemplateIdentityConstraints(d.Get("identity_constraints")),
Labels: checkStringMap(d.Get("labels")),
PassthroughExtensions: expandPrivatecaCertificateTemplatePassthroughExtensions(d.Get("passthrough_extensions")),
PredefinedValues: expandPrivatecaCertificateTemplatePredefinedValues(d.Get("predefined_values")),
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 := NewDCLPrivatecaClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutRead))
res, err := client.GetCertificateTemplate(context.Background(), obj)
if err != nil {
resourceName := fmt.Sprintf("PrivatecaCertificateTemplate %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("description", res.Description); err != nil {
return fmt.Errorf("error setting description in state: %s", err)
}
if err = d.Set("identity_constraints", flattenPrivatecaCertificateTemplateIdentityConstraints(res.IdentityConstraints)); err != nil {
return fmt.Errorf("error setting identity_constraints 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("passthrough_extensions", flattenPrivatecaCertificateTemplatePassthroughExtensions(res.PassthroughExtensions)); err != nil {
return fmt.Errorf("error setting passthrough_extensions in state: %s", err)
}
if err = d.Set("predefined_values", flattenPrivatecaCertificateTemplatePredefinedValues(res.PredefinedValues)); err != nil {
return fmt.Errorf("error setting predefined_values 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("update_time", res.UpdateTime); err != nil {
return fmt.Errorf("error setting update_time in state: %s", err)
}
return nil
}
func resourcePrivatecaCertificateTemplateUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
obj := &privateca.CertificateTemplate{
Location: dcl.String(d.Get("location").(string)),
Name: dcl.String(d.Get("name").(string)),
Description: dcl.String(d.Get("description").(string)),
IdentityConstraints: expandPrivatecaCertificateTemplateIdentityConstraints(d.Get("identity_constraints")),
Labels: checkStringMap(d.Get("labels")),
PassthroughExtensions: expandPrivatecaCertificateTemplatePassthroughExtensions(d.Get("passthrough_extensions")),
PredefinedValues: expandPrivatecaCertificateTemplatePredefinedValues(d.Get("predefined_values")),
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 := NewDCLPrivatecaClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutUpdate))
res, err := client.ApplyCertificateTemplate(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 CertificateTemplate: %s", err)
}
log.Printf("[DEBUG] Finished creating CertificateTemplate %q: %#v", d.Id(), res)
return resourcePrivatecaCertificateTemplateRead(d, meta)
}
func resourcePrivatecaCertificateTemplateDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
obj := &privateca.CertificateTemplate{
Location: dcl.String(d.Get("location").(string)),
Name: dcl.String(d.Get("name").(string)),
Description: dcl.String(d.Get("description").(string)),
IdentityConstraints: expandPrivatecaCertificateTemplateIdentityConstraints(d.Get("identity_constraints")),
Labels: checkStringMap(d.Get("labels")),
PassthroughExtensions: expandPrivatecaCertificateTemplatePassthroughExtensions(d.Get("passthrough_extensions")),
PredefinedValues: expandPrivatecaCertificateTemplatePredefinedValues(d.Get("predefined_values")),
Project: dcl.String(project),
}
log.Printf("[DEBUG] Deleting CertificateTemplate %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 := NewDCLPrivatecaClient(config, userAgent, billingProject, d.Timeout(schema.TimeoutDelete))
if err := client.DeleteCertificateTemplate(context.Background(), obj); err != nil {
return fmt.Errorf("Error deleting CertificateTemplate: %s", err)
}
log.Printf("[DEBUG] Finished deleting CertificateTemplate %q", d.Id())
return nil
}
func resourcePrivatecaCertificateTemplateImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)
if err := parseImportId([]string{
"projects/(?P<project>[^/]+)/locations/(?P<location>[^/]+)/certificateTemplates/(?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}}/certificateTemplates/{{name}}")
if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)
return []*schema.ResourceData{d}, nil
}
func expandPrivatecaCertificateTemplateIdentityConstraints(o interface{}) *privateca.CertificateTemplateIdentityConstraints {
if o == nil {
return privateca.EmptyCertificateTemplateIdentityConstraints
}
objArr := o.([]interface{})
if len(objArr) == 0 {
return privateca.EmptyCertificateTemplateIdentityConstraints
}
obj := objArr[0].(map[string]interface{})
return &privateca.CertificateTemplateIdentityConstraints{
AllowSubjectAltNamesPassthrough: dcl.Bool(obj["allow_subject_alt_names_passthrough"].(bool)),
AllowSubjectPassthrough: dcl.Bool(obj["allow_subject_passthrough"].(bool)),
CelExpression: expandPrivatecaCertificateTemplateIdentityConstraintsCelExpression(obj["cel_expression"]),
}
}
func flattenPrivatecaCertificateTemplateIdentityConstraints(obj *privateca.CertificateTemplateIdentityConstraints) interface{} {
if obj == nil || obj.Empty() {
return nil
}
transformed := map[string]interface{}{
"allow_subject_alt_names_passthrough": obj.AllowSubjectAltNamesPassthrough,
"allow_subject_passthrough": obj.AllowSubjectPassthrough,
"cel_expression": flattenPrivatecaCertificateTemplateIdentityConstraintsCelExpression(obj.CelExpression),
}
return []interface{}{transformed}
}
func expandPrivatecaCertificateTemplateIdentityConstraintsCelExpression(o interface{}) *privateca.CertificateTemplateIdentityConstraintsCelExpression {
if o == nil {
return privateca.EmptyCertificateTemplateIdentityConstraintsCelExpression
}
objArr := o.([]interface{})
if len(objArr) == 0 {
return privateca.EmptyCertificateTemplateIdentityConstraintsCelExpression
}
obj := objArr[0].(map[string]interface{})
return &privateca.CertificateTemplateIdentityConstraintsCelExpression{
Description: dcl.String(obj["description"].(string)),
Expression: dcl.String(obj["expression"].(string)),
Location: dcl.String(obj["location"].(string)),
Title: dcl.String(obj["title"].(string)),
}
}
func flattenPrivatecaCertificateTemplateIdentityConstraintsCelExpression(obj *privateca.CertificateTemplateIdentityConstraintsCelExpression) interface{} {
if obj == nil || obj.Empty() {
return nil
}
transformed := map[string]interface{}{
"description": obj.Description,
"expression": obj.Expression,
"location": obj.Location,
"title": obj.Title,
}
return []interface{}{transformed}
}
func expandPrivatecaCertificateTemplatePassthroughExtensions(o interface{}) *privateca.CertificateTemplatePassthroughExtensions {
if o == nil {
return privateca.EmptyCertificateTemplatePassthroughExtensions
}
objArr := o.([]interface{})
if len(objArr) == 0 {
return privateca.EmptyCertificateTemplatePassthroughExtensions
}
obj := objArr[0].(map[string]interface{})
return &privateca.CertificateTemplatePassthroughExtensions{
AdditionalExtensions: expandPrivatecaCertificateTemplatePassthroughExtensionsAdditionalExtensionsArray(obj["additional_extensions"]),
KnownExtensions: expandPrivatecaCertificateTemplatePassthroughExtensionsKnownExtensionsArray(obj["known_extensions"]),
}
}
func flattenPrivatecaCertificateTemplatePassthroughExtensions(obj *privateca.CertificateTemplatePassthroughExtensions) interface{} {
if obj == nil || obj.Empty() {
return nil
}
transformed := map[string]interface{}{
"additional_extensions": flattenPrivatecaCertificateTemplatePassthroughExtensionsAdditionalExtensionsArray(obj.AdditionalExtensions),
"known_extensions": flattenPrivatecaCertificateTemplatePassthroughExtensionsKnownExtensionsArray(obj.KnownExtensions),
}
return []interface{}{transformed}
}
func expandPrivatecaCertificateTemplatePassthroughExtensionsAdditionalExtensionsArray(o interface{}) []privateca.CertificateTemplatePassthroughExtensionsAdditionalExtensions {
if o == nil {
return make([]privateca.CertificateTemplatePassthroughExtensionsAdditionalExtensions, 0)
}
objs := o.([]interface{})
if len(objs) == 0 {
return make([]privateca.CertificateTemplatePassthroughExtensionsAdditionalExtensions, 0)
}
items := make([]privateca.CertificateTemplatePassthroughExtensionsAdditionalExtensions, 0, len(objs))
for _, item := range objs {
i := expandPrivatecaCertificateTemplatePassthroughExtensionsAdditionalExtensions(item)
items = append(items, *i)
}
return items
}
func expandPrivatecaCertificateTemplatePassthroughExtensionsAdditionalExtensions(o interface{}) *privateca.CertificateTemplatePassthroughExtensionsAdditionalExtensions {
if o == nil {
return privateca.EmptyCertificateTemplatePassthroughExtensionsAdditionalExtensions
}
obj := o.(map[string]interface{})
return &privateca.CertificateTemplatePassthroughExtensionsAdditionalExtensions{
ObjectIdPath: expandIntegerArray(obj["object_id_path"]),
}
}
func flattenPrivatecaCertificateTemplatePassthroughExtensionsAdditionalExtensionsArray(objs []privateca.CertificateTemplatePassthroughExtensionsAdditionalExtensions) []interface{} {
if objs == nil {
return nil
}
items := []interface{}{}
for _, item := range objs {
i := flattenPrivatecaCertificateTemplatePassthroughExtensionsAdditionalExtensions(&item)
items = append(items, i)
}
return items
}
func flattenPrivatecaCertificateTemplatePassthroughExtensionsAdditionalExtensions(obj *privateca.CertificateTemplatePassthroughExtensionsAdditionalExtensions) interface{} {
if obj == nil || obj.Empty() {
return nil
}
transformed := map[string]interface{}{
"object_id_path": obj.ObjectIdPath,
}
return transformed
}
func expandPrivatecaCertificateTemplatePredefinedValues(o interface{}) *privateca.CertificateTemplatePredefinedValues {
if o == nil {
return privateca.EmptyCertificateTemplatePredefinedValues
}
objArr := o.([]interface{})
if len(objArr) == 0 {
return privateca.EmptyCertificateTemplatePredefinedValues
}
obj := objArr[0].(map[string]interface{})
return &privateca.CertificateTemplatePredefinedValues{
AdditionalExtensions: expandPrivatecaCertificateTemplatePredefinedValuesAdditionalExtensionsArray(obj["additional_extensions"]),
AiaOcspServers: expandStringArray(obj["aia_ocsp_servers"]),
CaOptions: expandPrivatecaCertificateTemplatePredefinedValuesCaOptions(obj["ca_options"]),
KeyUsage: expandPrivatecaCertificateTemplatePredefinedValuesKeyUsage(obj["key_usage"]),
PolicyIds: expandPrivatecaCertificateTemplatePredefinedValuesPolicyIdsArray(obj["policy_ids"]),
}
}
func flattenPrivatecaCertificateTemplatePredefinedValues(obj *privateca.CertificateTemplatePredefinedValues) interface{} {
if obj == nil || obj.Empty() {
return nil
}
transformed := map[string]interface{}{
"additional_extensions": flattenPrivatecaCertificateTemplatePredefinedValuesAdditionalExtensionsArray(obj.AdditionalExtensions),
"aia_ocsp_servers": obj.AiaOcspServers,
"ca_options": flattenPrivatecaCertificateTemplatePredefinedValuesCaOptions(obj.CaOptions),
"key_usage": flattenPrivatecaCertificateTemplatePredefinedValuesKeyUsage(obj.KeyUsage),
"policy_ids": flattenPrivatecaCertificateTemplatePredefinedValuesPolicyIdsArray(obj.PolicyIds),
}
return []interface{}{transformed}
}
func expandPrivatecaCertificateTemplatePredefinedValuesAdditionalExtensionsArray(o interface{}) []privateca.CertificateTemplatePredefinedValuesAdditionalExtensions {
if o == nil {
return make([]privateca.CertificateTemplatePredefinedValuesAdditionalExtensions, 0)
}
objs := o.([]interface{})
if len(objs) == 0 {
return make([]privateca.CertificateTemplatePredefinedValuesAdditionalExtensions, 0)
}
items := make([]privateca.CertificateTemplatePredefinedValuesAdditionalExtensions, 0, len(objs))
for _, item := range objs {
i := expandPrivatecaCertificateTemplatePredefinedValuesAdditionalExtensions(item)
items = append(items, *i)
}
return items
}
func expandPrivatecaCertificateTemplatePredefinedValuesAdditionalExtensions(o interface{}) *privateca.CertificateTemplatePredefinedValuesAdditionalExtensions {
if o == nil {
return privateca.EmptyCertificateTemplatePredefinedValuesAdditionalExtensions
}
obj := o.(map[string]interface{})
return &privateca.CertificateTemplatePredefinedValuesAdditionalExtensions{
ObjectId: expandPrivatecaCertificateTemplatePredefinedValuesAdditionalExtensionsObjectId(obj["object_id"]),
Value: dcl.String(obj["value"].(string)),
Critical: dcl.Bool(obj["critical"].(bool)),
}
}
func flattenPrivatecaCertificateTemplatePredefinedValuesAdditionalExtensionsArray(objs []privateca.CertificateTemplatePredefinedValuesAdditionalExtensions) []interface{} {
if objs == nil {
return nil
}
items := []interface{}{}
for _, item := range objs {
i := flattenPrivatecaCertificateTemplatePredefinedValuesAdditionalExtensions(&item)
items = append(items, i)
}
return items
}
func flattenPrivatecaCertificateTemplatePredefinedValuesAdditionalExtensions(obj *privateca.CertificateTemplatePredefinedValuesAdditionalExtensions) interface{} {
if obj == nil || obj.Empty() {
return nil
}
transformed := map[string]interface{}{
"object_id": flattenPrivatecaCertificateTemplatePredefinedValuesAdditionalExtensionsObjectId(obj.ObjectId),
"value": obj.Value,
"critical": obj.Critical,
}
return transformed
}
func expandPrivatecaCertificateTemplatePredefinedValuesAdditionalExtensionsObjectId(o interface{}) *privateca.CertificateTemplatePredefinedValuesAdditionalExtensionsObjectId {
if o == nil {
return privateca.EmptyCertificateTemplatePredefinedValuesAdditionalExtensionsObjectId
}
objArr := o.([]interface{})
if len(objArr) == 0 {
return privateca.EmptyCertificateTemplatePredefinedValuesAdditionalExtensionsObjectId
}
obj := objArr[0].(map[string]interface{})
return &privateca.CertificateTemplatePredefinedValuesAdditionalExtensionsObjectId{
ObjectIdPath: expandIntegerArray(obj["object_id_path"]),
}
}
func flattenPrivatecaCertificateTemplatePredefinedValuesAdditionalExtensionsObjectId(obj *privateca.CertificateTemplatePredefinedValuesAdditionalExtensionsObjectId) interface{} {
if obj == nil || obj.Empty() {
return nil
}
transformed := map[string]interface{}{
"object_id_path": obj.ObjectIdPath,
}
return []interface{}{transformed}
}
func expandPrivatecaCertificateTemplatePredefinedValuesCaOptions(o interface{}) *privateca.CertificateTemplatePredefinedValuesCaOptions {
if o == nil {
return privateca.EmptyCertificateTemplatePredefinedValuesCaOptions
}
objArr := o.([]interface{})
if len(objArr) == 0 {
return privateca.EmptyCertificateTemplatePredefinedValuesCaOptions
}
obj := objArr[0].(map[string]interface{})
return &privateca.CertificateTemplatePredefinedValuesCaOptions{
IsCa: dcl.Bool(obj["is_ca"].(bool)),
MaxIssuerPathLength: dcl.Int64(int64(obj["max_issuer_path_length"].(int))),
}
}
func flattenPrivatecaCertificateTemplatePredefinedValuesCaOptions(obj *privateca.CertificateTemplatePredefinedValuesCaOptions) interface{} {
if obj == nil || obj.Empty() {
return nil
}
transformed := map[string]interface{}{
"is_ca": obj.IsCa,
"max_issuer_path_length": obj.MaxIssuerPathLength,
}
return []interface{}{transformed}
}
func expandPrivatecaCertificateTemplatePredefinedValuesKeyUsage(o interface{}) *privateca.CertificateTemplatePredefinedValuesKeyUsage {
if o == nil {
return privateca.EmptyCertificateTemplatePredefinedValuesKeyUsage
}
objArr := o.([]interface{})
if len(objArr) == 0 {
return privateca.EmptyCertificateTemplatePredefinedValuesKeyUsage
}
obj := objArr[0].(map[string]interface{})
return &privateca.CertificateTemplatePredefinedValuesKeyUsage{
BaseKeyUsage: expandPrivatecaCertificateTemplatePredefinedValuesKeyUsageBaseKeyUsage(obj["base_key_usage"]),
ExtendedKeyUsage: expandPrivatecaCertificateTemplatePredefinedValuesKeyUsageExtendedKeyUsage(obj["extended_key_usage"]),
UnknownExtendedKeyUsages: expandPrivatecaCertificateTemplatePredefinedValuesKeyUsageUnknownExtendedKeyUsagesArray(obj["unknown_extended_key_usages"]),
}
}
func flattenPrivatecaCertificateTemplatePredefinedValuesKeyUsage(obj *privateca.CertificateTemplatePredefinedValuesKeyUsage) interface{} {
if obj == nil || obj.Empty() {
return nil
}
transformed := map[string]interface{}{
"base_key_usage": flattenPrivatecaCertificateTemplatePredefinedValuesKeyUsageBaseKeyUsage(obj.BaseKeyUsage),
"extended_key_usage": flattenPrivatecaCertificateTemplatePredefinedValuesKeyUsageExtendedKeyUsage(obj.ExtendedKeyUsage),