/
resource_data_catalog_tag_template.go
869 lines (748 loc) · 29.5 KB
/
resource_data_catalog_tag_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
// ----------------------------------------------------------------------------
//
// *** AUTO GENERATED CODE *** Type: MMv1 ***
//
// ----------------------------------------------------------------------------
//
// This file is automatically generated by Magic Modules and manual
// changes will be clobbered when the file is regenerated.
//
// Please read more about how to change this file in
// .github/CONTRIBUTING.md.
//
// ----------------------------------------------------------------------------
package google
import (
"fmt"
"log"
"reflect"
"regexp"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
//Use it to delete TagTemplate Field
func deleteTagTemplateField(d *schema.ResourceData, config *Config, name, billingProject, userAgent string) error {
url_delete, err := replaceVars(d, config, "{{DataCatalogBasePath}}{{name}}/fields/"+name+"?force={{force_delete}}")
if err != nil {
return err
}
var obj map[string]interface{}
res, err := sendRequestWithTimeout(config, "DELETE", billingProject, url_delete, userAgent, obj, d.Timeout(schema.TimeoutDelete))
if err != nil {
return fmt.Errorf("Error deleting TagTemplate Field %v: %s", name, err)
}
log.Printf("[DEBUG] Finished deleting TagTemplate Field %q: %#v", name, res)
return nil
}
//Use it to create TagTemplate Field
func createTagTemplateField(d *schema.ResourceData, config *Config, body map[string]interface{}, name, billingProject, userAgent string) error {
url_create, err := replaceVars(d, config, "{{DataCatalogBasePath}}{{name}}/fields")
if err != nil {
return err
}
url_create, err = addQueryParams(url_create, map[string]string{"tagTemplateFieldId": name})
if err != nil {
return err
}
res_create, err := sendRequestWithTimeout(config, "POST", billingProject, url_create, userAgent, body, d.Timeout(schema.TimeoutCreate))
if err != nil {
return fmt.Errorf("Error creating TagTemplate Field: %s", err)
}
if err != nil {
return fmt.Errorf("Error creating TagTemplate Field %v: %s", name, err)
} else {
log.Printf("[DEBUG] Finished creating TagTemplate Field %v: %#v", name, res_create)
}
return nil
}
func resourceDataCatalogTagTemplate() *schema.Resource {
return &schema.Resource{
Create: resourceDataCatalogTagTemplateCreate,
Read: resourceDataCatalogTagTemplateRead,
Update: resourceDataCatalogTagTemplateUpdate,
Delete: resourceDataCatalogTagTemplateDelete,
Importer: &schema.ResourceImporter{
State: resourceDataCatalogTagTemplateImport,
},
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{
"fields": {
Type: schema.TypeSet,
Required: true,
Description: `Set of tag template field IDs and the settings for the field. This set is an exhaustive list of the allowed fields. This set must contain at least one field and at most 500 fields. The change of field_id will be resulting in re-creating of field. The change of primitive_type will be resulting in re-creating of field, however if the field is a required, you cannot update it.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"field_id": {
Type: schema.TypeString,
Required: true,
},
"type": {
Type: schema.TypeList,
Required: true,
Description: `The type of value this tag field can contain.`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enum_type": {
Type: schema.TypeList,
Optional: true,
Description: `Represents an enum type.
Exactly one of 'primitive_type' or 'enum_type' must be set`,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"allowed_values": {
Type: schema.TypeSet,
Required: true,
Description: `The set of allowed values for this enum. The display names of the
values must be case-insensitively unique within this set. Currently,
enum values can only be added to the list of allowed values. Deletion
and renaming of enum values are not supported.
Can have up to 500 allowed values.`,
Elem: datacatalogTagTemplateFieldsFieldsTypeEnumTypeAllowedValuesSchema(),
// Default schema.HashSchema is used.
},
},
},
},
"primitive_type": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ValidateFunc: validateEnum([]string{"DOUBLE", "STRING", "BOOL", "TIMESTAMP", ""}),
Description: `Represents primitive types - string, bool etc.
Exactly one of 'primitive_type' or 'enum_type' must be set Possible values: ["DOUBLE", "STRING", "BOOL", "TIMESTAMP"]`,
},
},
},
},
"description": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: `A description for this field.`,
},
"display_name": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: `The display name for this field.`,
},
"is_required": {
Type: schema.TypeBool,
Computed: true,
Optional: true,
Description: `Whether this is a required field. Defaults to false.`,
},
"order": {
Type: schema.TypeInt,
Computed: true,
Optional: true,
Description: `The order of this field with respect to other fields in this tag template.
A higher value indicates a more important field. The value can be negative.
Multiple fields can have the same order, and field orders within a tag do not have to be sequential.`,
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: `The resource name of the tag template field in URL format. Example: projects/{project_id}/locations/{location}/tagTemplates/{tagTemplateId}/fields/{field}`,
},
},
},
},
"tag_template_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateRegexp(`^[a-z_][a-z0-9_]{0,63}$`),
Description: `The id of the tag template to create.`,
},
"display_name": {
Type: schema.TypeString,
Optional: true,
Description: `The display name for this template.`,
},
"force_delete": {
Type: schema.TypeBool,
Optional: true,
Description: `This confirms the deletion of any possible tags using this template. Must be set to true in order to delete the tag template.`,
Default: false,
},
"region": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
Description: `Template location region.`,
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: `The resource name of the tag template in URL format. Example: projects/{project_id}/locations/{location}/tagTemplates/{tagTemplateId}`,
},
"project": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
UseJSONNumber: true,
}
}
func datacatalogTagTemplateFieldsFieldsTypeEnumTypeAllowedValuesSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"display_name": {
Type: schema.TypeString,
Required: true,
Description: `The display name of the enum value.`,
},
},
}
}
func resourceDataCatalogTagTemplateCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
obj := make(map[string]interface{})
displayNameProp, err := expandDataCatalogTagTemplateDisplayName(d.Get("display_name"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("display_name"); !isEmptyValue(reflect.ValueOf(displayNameProp)) && (ok || !reflect.DeepEqual(v, displayNameProp)) {
obj["displayName"] = displayNameProp
}
fieldsProp, err := expandDataCatalogTagTemplateFields(d.Get("fields"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("fields"); !isEmptyValue(reflect.ValueOf(fieldsProp)) && (ok || !reflect.DeepEqual(v, fieldsProp)) {
obj["fields"] = fieldsProp
}
url, err := replaceVars(d, config, "{{DataCatalogBasePath}}projects/{{project}}/locations/{{region}}/tagTemplates?tagTemplateId={{tag_template_id}}")
if err != nil {
return err
}
log.Printf("[DEBUG] Creating new TagTemplate: %#v", obj)
billingProject := ""
project, err := getProject(d, config)
if err != nil {
return fmt.Errorf("Error fetching project for TagTemplate: %s", err)
}
billingProject = project
// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}
res, err := sendRequestWithTimeout(config, "POST", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutCreate))
if err != nil {
return fmt.Errorf("Error creating TagTemplate: %s", err)
}
if err := d.Set("name", flattenDataCatalogTagTemplateName(res["name"], d, config)); err != nil {
return fmt.Errorf(`Error setting computed identity field "name": %s`, err)
}
// Store the ID now
id, err := replaceVars(d, config, "{{name}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)
log.Printf("[DEBUG] Finished creating TagTemplate %q: %#v", d.Id(), res)
return resourceDataCatalogTagTemplateRead(d, meta)
}
func resourceDataCatalogTagTemplateRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
url, err := replaceVars(d, config, "{{DataCatalogBasePath}}{{name}}")
if err != nil {
return err
}
billingProject := ""
project, err := getProject(d, config)
if err != nil {
return fmt.Errorf("Error fetching project for TagTemplate: %s", err)
}
billingProject = project
// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}
res, err := sendRequest(config, "GET", billingProject, url, userAgent, nil)
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("DataCatalogTagTemplate %q", d.Id()))
}
if err := d.Set("project", project); err != nil {
return fmt.Errorf("Error reading TagTemplate: %s", err)
}
region, err := getRegion(d, config)
if err != nil {
return err
}
if err := d.Set("region", region); err != nil {
return fmt.Errorf("Error reading TagTemplate: %s", err)
}
if err := d.Set("name", flattenDataCatalogTagTemplateName(res["name"], d, config)); err != nil {
return fmt.Errorf("Error reading TagTemplate: %s", err)
}
if err := d.Set("display_name", flattenDataCatalogTagTemplateDisplayName(res["displayName"], d, config)); err != nil {
return fmt.Errorf("Error reading TagTemplate: %s", err)
}
if err := d.Set("fields", flattenDataCatalogTagTemplateFields(res["fields"], d, config)); err != nil {
return fmt.Errorf("Error reading TagTemplate: %s", err)
}
return nil
}
func resourceDataCatalogTagTemplateUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
billingProject := ""
project, err := getProject(d, config)
if err != nil {
return fmt.Errorf("Error fetching project for TagTemplate: %s", err)
}
billingProject = project
obj := make(map[string]interface{})
displayNameProp, err := expandDataCatalogTagTemplateDisplayName(d.Get("display_name"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("display_name"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, displayNameProp)) {
obj["displayName"] = displayNameProp
}
fieldsProp, err := expandDataCatalogTagTemplateFields(d.Get("fields"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("fields"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, fieldsProp)) {
obj["fields"] = fieldsProp
}
url, err := replaceVars(d, config, "{{DataCatalogBasePath}}{{name}}")
if err != nil {
return err
}
log.Printf("[DEBUG] Updating TagTemplate %q: %#v", d.Id(), obj)
updateMask := []string{}
if d.HasChange("display_name") {
updateMask = append(updateMask, "displayName")
}
// updateMask is a URL parameter but not present in the schema, so replaceVars
// won't set it
url, err = addQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")})
if err != nil {
return err
}
if len(updateMask) > 0 {
// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}
res, err := sendRequestWithTimeout(config, "PATCH", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return fmt.Errorf("Error updating TagTemplate %q: %s", d.Id(), err)
} else {
log.Printf("[DEBUG] Finished updating TagTemplate %q: %#v", d.Id(), res)
}
}
// since fields have a separate endpoint,
// we need to handle it manually
type FieldChange struct {
Old, New map[string]interface{}
}
o, n := d.GetChange("fields")
vals := make(map[string]*FieldChange)
// this will create a dictionary with the value
// of field_id as the key that will contain the
// maps of old and new values
for _, raw := range o.(*schema.Set).List() {
obj := raw.(map[string]interface{})
k := obj["field_id"].(string)
vals[k] = &FieldChange{Old: obj}
}
for _, raw := range n.(*schema.Set).List() {
obj := raw.(map[string]interface{})
k := obj["field_id"].(string)
if _, ok := vals[k]; !ok {
// if key is not present in the vals,
// then create an empty object to hold the new value
vals[k] = &FieldChange{}
}
vals[k].New = obj
}
// fields schema to create schema.set below
dataCatalogTagTemplateFieldsSchema := &schema.Resource{
Schema: resourceDataCatalogTagTemplate().Schema["fields"].Elem.(*schema.Resource).Schema,
}
for name, change := range vals {
// A few different situations to deal with in here:
// - change.Old is nil: create a new role
// - change.New is nil: remove an existing role
// - both are set: test if New is different than Old and update if so
changeOldSet := schema.NewSet(schema.HashResource(dataCatalogTagTemplateFieldsSchema), []interface{}{})
changeOldSet.Add(change.Old)
var changeOldProp map[string]interface{}
if len(change.Old) != 0 {
changeOldProp, _ = expandDataCatalogTagTemplateFields(changeOldSet, nil, nil)
changeOldProp = changeOldProp[name].(map[string]interface{})
}
changeNewSet := schema.NewSet(schema.HashResource(dataCatalogTagTemplateFieldsSchema), []interface{}{})
changeNewSet.Add(change.New)
var changeNewProp map[string]interface{}
if len(change.New) != 0 {
changeNewProp, _ = expandDataCatalogTagTemplateFields(changeNewSet, nil, nil)
changeNewProp = changeNewProp[name].(map[string]interface{})
}
// if old state is empty, then we have a new field to create
if len(change.Old) == 0 {
err := createTagTemplateField(d, config, changeNewProp, name, billingProject, userAgent)
if err != nil {
return err
}
continue
}
// if new state is empty, then we need to delete the current field
if len(change.New) == 0 {
err := deleteTagTemplateField(d, config, name, billingProject, userAgent)
if err != nil {
return err
}
continue
}
// if we have old and new values, but are not equal, update with the new state
if !reflect.DeepEqual(changeOldProp, changeNewProp) {
url1, err := replaceVars(d, config, "{{DataCatalogBasePath}}{{name}}/fields/"+name)
if err != nil {
return err
}
oldType := changeOldProp["type"].(map[string]interface{})
newType := changeNewProp["type"].(map[string]interface{})
if oldType["primitiveType"] != newType["primitiveType"] {
// As primitiveType can't be changed, it is considered as ForceNew which triggers the deletion of old field and recreation of a new field
// Before that, we need to check that is_required is True for the newType or not, as we don't have support to add new required field in the existing TagTemplate,
// So in such cases, we can simply return the error
// Reason for checking the isRequired in changeNewProp -
// Because this changeNewProp check should be ignored when the user wants to update the primitive type and make it optional rather than keeping it required.
if changeNewProp["isRequired"] != nil && changeNewProp["isRequired"].(bool) {
return fmt.Errorf("Updating the primitive type for a required field on an existing tag template is not supported as TagTemplateField %q is required", name)
}
// delete changeOldProp
err_delete := deleteTagTemplateField(d, config, name, billingProject, userAgent)
if err_delete != nil {
return err_delete
}
// recreate changeNewProp
err_create := createTagTemplateField(d, config, changeNewProp, name, billingProject, userAgent)
if err_create != nil {
return err_create
}
log.Printf("[DEBUG] Finished updating TagTemplate Field %q", name)
return resourceDataCatalogTagTemplateRead(d, meta)
}
res, err := sendRequestWithTimeout(config, "PATCH", billingProject, url1, userAgent, changeNewProp, d.Timeout(schema.TimeoutDelete))
if err != nil {
return fmt.Errorf("Error updating TagTemplate Field %v: %s", name, err)
}
log.Printf("[DEBUG] Finished updating TagTemplate Field %q: %#v", name, res)
}
}
return resourceDataCatalogTagTemplateRead(d, meta)
}
func resourceDataCatalogTagTemplateDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
billingProject := ""
project, err := getProject(d, config)
if err != nil {
return fmt.Errorf("Error fetching project for TagTemplate: %s", err)
}
billingProject = project
url, err := replaceVars(d, config, "{{DataCatalogBasePath}}{{name}}?force={{force_delete}}")
if err != nil {
return err
}
var obj map[string]interface{}
log.Printf("[DEBUG] Deleting TagTemplate %q", d.Id())
// err == nil indicates that the billing_project value was found
if bp, err := getBillingProject(d, config); err == nil {
billingProject = bp
}
res, err := sendRequestWithTimeout(config, "DELETE", billingProject, url, userAgent, obj, d.Timeout(schema.TimeoutDelete))
if err != nil {
return handleNotFoundError(err, d, "TagTemplate")
}
log.Printf("[DEBUG] Finished deleting TagTemplate %q: %#v", d.Id(), res)
return nil
}
func resourceDataCatalogTagTemplateImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)
// current import_formats can't import fields with forward slashes in their value
if err := parseImportId([]string{"(?P<name>.+)"}, d, config); err != nil {
return nil, err
}
name := d.Get("name").(string)
egRegex := regexp.MustCompile("projects/(.+)/locations/(.+)/tagTemplates/(.+)")
parts := egRegex.FindStringSubmatch(name)
if len(parts) != 4 {
return nil, fmt.Errorf("tag template name does not fit the format %s", egRegex)
}
if err := d.Set("project", parts[1]); err != nil {
return nil, fmt.Errorf("Error setting project: %s", err)
}
if err := d.Set("region", parts[2]); err != nil {
return nil, fmt.Errorf("Error setting region: %s", err)
}
if err := d.Set("tag_template_id", parts[3]); err != nil {
return nil, fmt.Errorf("Error setting tag_template_id: %s", err)
}
return []*schema.ResourceData{d}, nil
}
func flattenDataCatalogTagTemplateName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}
func flattenDataCatalogTagTemplateDisplayName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}
func flattenDataCatalogTagTemplateFields(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return v
}
l := v.(map[string]interface{})
transformed := make([]interface{}, 0, len(l))
for k, raw := range l {
original := raw.(map[string]interface{})
transformed = append(transformed, map[string]interface{}{
"field_id": k,
"name": flattenDataCatalogTagTemplateFieldsName(original["name"], d, config),
"display_name": flattenDataCatalogTagTemplateFieldsDisplayName(original["displayName"], d, config),
"description": flattenDataCatalogTagTemplateFieldsDescription(original["description"], d, config),
"type": flattenDataCatalogTagTemplateFieldsType(original["type"], d, config),
"is_required": flattenDataCatalogTagTemplateFieldsIsRequired(original["isRequired"], d, config),
"order": flattenDataCatalogTagTemplateFieldsOrder(original["order"], d, config),
})
}
return transformed
}
func flattenDataCatalogTagTemplateFieldsName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}
func flattenDataCatalogTagTemplateFieldsDisplayName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}
func flattenDataCatalogTagTemplateFieldsDescription(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}
func flattenDataCatalogTagTemplateFieldsType(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["primitive_type"] =
flattenDataCatalogTagTemplateFieldsTypePrimitiveType(original["primitiveType"], d, config)
transformed["enum_type"] =
flattenDataCatalogTagTemplateFieldsTypeEnumType(original["enumType"], d, config)
return []interface{}{transformed}
}
func flattenDataCatalogTagTemplateFieldsTypePrimitiveType(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}
func flattenDataCatalogTagTemplateFieldsTypeEnumType(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return nil
}
original := v.(map[string]interface{})
if len(original) == 0 {
return nil
}
transformed := make(map[string]interface{})
transformed["allowed_values"] =
flattenDataCatalogTagTemplateFieldsTypeEnumTypeAllowedValues(original["allowedValues"], d, config)
return []interface{}{transformed}
}
func flattenDataCatalogTagTemplateFieldsTypeEnumTypeAllowedValues(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil {
return v
}
l := v.([]interface{})
transformed := schema.NewSet(schema.HashResource(datacatalogTagTemplateFieldsFieldsTypeEnumTypeAllowedValuesSchema()), []interface{}{})
for _, raw := range l {
original := raw.(map[string]interface{})
if len(original) < 1 {
// Do not include empty json objects coming back from the api
continue
}
transformed.Add(map[string]interface{}{
"display_name": flattenDataCatalogTagTemplateFieldsTypeEnumTypeAllowedValuesDisplayName(original["displayName"], d, config),
})
}
return transformed
}
func flattenDataCatalogTagTemplateFieldsTypeEnumTypeAllowedValuesDisplayName(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}
func flattenDataCatalogTagTemplateFieldsIsRequired(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}
func flattenDataCatalogTagTemplateFieldsOrder(v interface{}, d *schema.ResourceData, config *Config) interface{} {
// Handles the string fixed64 format
if strVal, ok := v.(string); ok {
if intVal, err := stringToFixed64(strVal); err == nil {
return intVal
}
}
// number values are represented as float64
if floatVal, ok := v.(float64); ok {
intVal := int(floatVal)
return intVal
}
return v // let terraform core handle it otherwise
}
func expandDataCatalogTagTemplateDisplayName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
func expandDataCatalogTagTemplateFields(v interface{}, d TerraformResourceData, config *Config) (map[string]interface{}, error) {
if v == nil {
return map[string]interface{}{}, nil
}
m := make(map[string]interface{})
for _, raw := range v.(*schema.Set).List() {
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})
transformedName, err := expandDataCatalogTagTemplateFieldsName(original["name"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedName); val.IsValid() && !isEmptyValue(val) {
transformed["name"] = transformedName
}
transformedDisplayName, err := expandDataCatalogTagTemplateFieldsDisplayName(original["display_name"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedDisplayName); val.IsValid() && !isEmptyValue(val) {
transformed["displayName"] = transformedDisplayName
}
transformedDescription, err := expandDataCatalogTagTemplateFieldsDescription(original["description"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedDescription); val.IsValid() && !isEmptyValue(val) {
transformed["description"] = transformedDescription
}
transformedType, err := expandDataCatalogTagTemplateFieldsType(original["type"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedType); val.IsValid() && !isEmptyValue(val) {
transformed["type"] = transformedType
}
transformedIsRequired, err := expandDataCatalogTagTemplateFieldsIsRequired(original["is_required"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedIsRequired); val.IsValid() && !isEmptyValue(val) {
transformed["isRequired"] = transformedIsRequired
}
transformedOrder, err := expandDataCatalogTagTemplateFieldsOrder(original["order"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedOrder); val.IsValid() && !isEmptyValue(val) {
transformed["order"] = transformedOrder
}
transformedFieldId, err := expandString(original["field_id"], d, config)
if err != nil {
return nil, err
}
m[transformedFieldId] = transformed
}
return m, nil
}
func expandDataCatalogTagTemplateFieldsName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
func expandDataCatalogTagTemplateFieldsDisplayName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
func expandDataCatalogTagTemplateFieldsDescription(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
func expandDataCatalogTagTemplateFieldsType(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})
transformedPrimitiveType, err := expandDataCatalogTagTemplateFieldsTypePrimitiveType(original["primitive_type"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedPrimitiveType); val.IsValid() && !isEmptyValue(val) {
transformed["primitiveType"] = transformedPrimitiveType
}
transformedEnumType, err := expandDataCatalogTagTemplateFieldsTypeEnumType(original["enum_type"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedEnumType); val.IsValid() && !isEmptyValue(val) {
transformed["enumType"] = transformedEnumType
}
return transformed, nil
}
func expandDataCatalogTagTemplateFieldsTypePrimitiveType(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
func expandDataCatalogTagTemplateFieldsTypeEnumType(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})
transformedAllowedValues, err := expandDataCatalogTagTemplateFieldsTypeEnumTypeAllowedValues(original["allowed_values"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedAllowedValues); val.IsValid() && !isEmptyValue(val) {
transformed["allowedValues"] = transformedAllowedValues
}
return transformed, nil
}
func expandDataCatalogTagTemplateFieldsTypeEnumTypeAllowedValues(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
v = v.(*schema.Set).List()
l := v.([]interface{})
req := make([]interface{}, 0, len(l))
for _, raw := range l {
if raw == nil {
continue
}
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})
transformedDisplayName, err := expandDataCatalogTagTemplateFieldsTypeEnumTypeAllowedValuesDisplayName(original["display_name"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedDisplayName); val.IsValid() && !isEmptyValue(val) {
transformed["displayName"] = transformedDisplayName
}
req = append(req, transformed)
}
return req, nil
}
func expandDataCatalogTagTemplateFieldsTypeEnumTypeAllowedValuesDisplayName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
func expandDataCatalogTagTemplateFieldsIsRequired(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
func expandDataCatalogTagTemplateFieldsOrder(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}