-
Notifications
You must be signed in to change notification settings - Fork 1
/
custom_terraform_policy_utils.go
951 lines (912 loc) · 33.6 KB
/
custom_terraform_policy_utils.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
/***************************************************************************
* ========================================================================
* Copyright 2022-2023 VMware, Inc. All rights reserved. VMware Confidential
* SPDX-License-Identifier: MPL-2.0
* ========================================================================
*/
package nsxt
import (
"fmt"
"log"
"net/url"
"strconv"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
nsxtclient "github.com/vmware/terraform-provider-nsxt-virtual-private-cloud/nsxt/clients"
)
/*
Function to create and update NSXT resources. If the resource does not exist it will try to
create it. In case it is present then it updates the resource.
*/
func APICreateOrUpdate(d *schema.ResourceData, meta interface{}, objType string, s map[string]*schema.Schema,
opts ...bool) error {
log.Printf("[DEBUG] start of APICreateOrUpdate")
var robj interface{}
obj := d
nsxtClient := meta.(*nsxtclient.NsxtClient)
var data interface{}
var err error
if data, err = SchemaToNsxtData(obj, s); err == nil {
resourceID := d.Id()
// calculate the policy path of resource
path := ComputePolicyPath(d, objType, false, nsxtClient, false)
pathWithoutQueryParams := strings.Split(path, "?")[0]
// trim policy/api/v1 from pathWithoutQueryParams and save as path, this way path can also be used to refer dependant resource policypath
pathWithoutQueryParamsTrimmed := strings.TrimPrefix(pathWithoutQueryParams, nsxtClient.Config.BasePath)
if resourceID != "" {
// resource with id already present in NSX, this is an update request
log.Printf("[INFO] APICreateOrUpdate: Updating obj %v with path %s\n", objType, path)
err = nsxtClient.NsxtSession.Patch(path, data, &robj)
if err != nil {
log.Printf("[ERROR] APICreateOrUpdate updation failed %v\n", err)
} else {
d.SetId(pathWithoutQueryParamsTrimmed)
d.Set("path", pathWithoutQueryParamsTrimmed)
}
} else {
// resource is new, this is a create request
log.Printf("[INFO] APICreateOrUpdate: Creating obj %v schema %v data %v with path %s\n", objType, d, data, path)
if objType == "SecurityPolicyRule" || objType == "GatewayPolicyRule" {
/* For Rule- source_groups, destination_groups, service, action and scope are optional and defaulted in create, but not defaulted in update. These are
kept as optional in yaml without mentioning defaults. Hence, terraform resource for Rule has optional against them.
Here adding the manual check to match API behaviour. Check each property value given or not. If not given, default it. */
dataMap := data.(map[string]interface{})
if dataMap["action"] == nil {
dataMap["action"] = "ALLOW"
}
keysWithAnyDefaultValue := []string{"source_groups", "destination_groups", "services", "scope"}
for _, key := range keysWithAnyDefaultValue {
if dataMap[key] == nil {
dataMap[key] = []interface{}{"ANY"}
}
}
}
err = nsxtClient.NsxtSession.Patch(path, data, &robj)
if err != nil {
log.Printf("[ERROR] APICreateOrUpdate creation failed %v\n", err)
} else {
d.SetId(pathWithoutQueryParamsTrimmed)
d.Set("path", pathWithoutQueryParamsTrimmed)
}
}
return err
}
log.Printf("[ERROR] APICreateOrUpdate: Error %v", err)
return err
}
/*
Function for reading the current state of the resource in NSX.
Called during multiple phases in lifecycle-
1. Initial Resource Creation: When Terraform creates a new resource for the first time, the Read function is called to fetch the initial state of the resource. This state is then stored in the Terraform state file.
2. Refresh Operation: During a refresh operation, Terraform calls the Read function to retrieve the current state of the resource from NSX. This allows Terraform to compare the current state with the desired state defined in the Terraform configuration and determine if any updates are required.
(The refresh operation in Terraform is triggered explicitly by running the terraform refresh command or as part of other Terraform operations like terraform apply or terraform plan).
3. Post Update operation: To fetch latest changes and update state file accordingly.
4. Post Delete operation: To fetch the final state of the resource, which will typically be null state to indicate that the resource no longer exists, the update the state file with the final state of the resource, ensuring that the state accurately reflects the state of the infrastructure.
*/
func APIRead(d *schema.ResourceData, meta interface{}, objType string, s map[string]*schema.Schema) error {
var obj interface{}
nsxtClient := meta.(*nsxtclient.NsxtClient)
// calculate the policy path of resource
id := d.Id()
policyPath := nsxtClient.Config.BasePath + id
resourceName := d.Get("display_name").(string)
if id != "" {
// Read using ID as complete policy path
log.Printf("[DEBUG] APIRead reading object with id %v\n", id)
err := nsxtClient.NsxtSession.Get(policyPath, &obj)
log.Printf("[DEBUG] json unmarshal response: %v\n", obj)
if err == nil && obj != nil {
d.SetId(id)
// set readonly property _revision for terraform show
objMap, ok := obj.(map[string]interface{})
if !ok {
return fmt.Errorf("want type map[string]interface{}; got %T", objMap)
}
} else {
log.Printf("[INFO] Read not successful for " + objType)
// Set resource ID to "" to signal Terraform that the resource should be treated as non-existent or in an error state.
d.SetId("")
return err
}
} else if resourceName != "" {
// compute policy path, and read using display_name
policyPath := ComputePolicyPath(d, objType, true, nsxtClient, false)
log.Printf("[DEBUG] APIRead reading object with display_name %v\n", resourceName)
err := nsxtClient.NsxtSession.Get(policyPath, &obj)
if err == nil && obj != nil {
d.SetId(policyPath)
// set readonly property _revision for terraform show
objMap, ok := obj.(map[string]interface{})
if !ok {
return fmt.Errorf("want type map[string]interface{}; got %T", objMap)
}
} else {
log.Printf("[INFO] Read not successful for " + objType)
d.SetId("")
return err
}
} else {
log.Printf("[DEBUG] APIRead object ID not present for resource %s\n", objType)
d.SetId("")
}
if localData, err := SchemaToNsxtData(d, s); err == nil {
modAPIRes, err := SetDefaultsInAPIRes(obj, localData, s)
if err != nil {
log.Printf("[ERROR] APIRead in modifying api response object %v\n", err)
return err
}
if _, err := APIDataToSchema(modAPIRes, d, s); err != nil {
log.Printf("[ERROR] APIRead in setting read object %v\n", err)
d.SetId("")
return err
}
} else {
return err
}
return nil
}
func setAttrsInDatasourceSchema(mapObject interface{}, d *schema.ResourceData, objType string) {
for key, value := range mapObject.(map[string]interface{}) {
// Filter based on nsx_id or display_name to get only what datasource desires
if key == "id" {
d.Set("nsx_id", value.(string))
}
if key == "path" {
d.SetId(value.(string)) // because terraform ID is policyPath value
d.Set("path", value.(string))
}
if key == "display_name" {
d.Set("display_name", value.(string))
}
if key == "description" {
d.Set("description", value.(string))
}
if key == "parent_path" && d.Get("parent_path") != nil {
d.Set("parent_path", value.(string))
}
if (objType == "VpcIpAddressAllocation" || objType == "IpAddressAllocation") && key == "allocation_ip" {
d.Set("allocation_ip", value.(string))
}
}
}
// Function to do READ on datasource, using id, display_name, parent_path
//nolint:revive
func DatasourceRead(d *schema.ResourceData, meta interface{}, objType string, s *schema.Resource) error {
var obj interface{}
nsxtClient := meta.(*nsxtclient.NsxtClient)
var contextInfoMap map[string]interface{}
var scope string
var domain string
contextInfo := d.Get("context").([]interface{})
if len(contextInfo) > 0 {
contextInfoMap = contextInfo[0].(map[string]interface{})
}
scope = contextInfoMap["scope"].(string)
domain = contextInfoMap["domain"].(string)
displayName := d.Get("display_name").(string)
nsxID := d.Get("nsx_id").(string)
var uri string
// Some objTypes (should be resource_type value) are differently worded in .yaml, hence correcting them for usage in Search query
switch objType {
case "SecurityPolicyRule":
objType = "Rule"
case "GatewayPolicyRule":
objType = "Rule"
}
// Get the object from NSX using hidden full text search API
uri = nsxtClient.Config.BasePath + "/search?query=resource_type:" + objType
if nsxID != "" {
encodedNsxID := url.QueryEscape(nsxID)
uri += "%20AND%20id:" + encodedNsxID
}
if displayName != "" {
encodedDisplayName := url.QueryEscape(displayName)
uri += "%20AND%20display_name:" + encodedDisplayName
}
if d.Get("parent_path") != nil {
parentPath := d.Get("parent_path").(string)
if parentPath != "" {
uri += "%20AND%20parent_path:%22" + parentPath + "%22"
}
}
if scope == "project" {
// If it is project/infra object, search in context of project
uri += "&context=projects:" + "/orgs/" + nsxtClient.Config.OrgID + "/projects/" + nsxtClient.Config.ProjectID
} else if scope == "vpc" {
// Search in context of Vpc
uri += "&context=vpcs:" + "/orgs/" + nsxtClient.Config.OrgID + "/projects/" + nsxtClient.Config.ProjectID + "/vpcs/" + nsxtClient.Config.VpcID
} else {
// No context for infra based Search, include domain info
if objType == "Group" {
domainPolicyPath := "/infra/domains/" + domain
uri += "%20AND%20parent_path:%22" + domainPolicyPath + "%22"
}
}
err := nsxtClient.NsxtSession.Get(uri, &obj)
if err != nil {
return fmt.Errorf("[ERROR] Search failed for object %s %v", objType, err)
} else {
objMap, ok := obj.(map[string]interface{})
if !ok {
return fmt.Errorf("want type map[string]interface{}; got %T", objMap)
}
// get results of type []interface {} to get id and policypath
results := objMap["results"]
if objMap["result_count"].(float64) == 1 {
setAttrsInDatasourceSchema(results.([]interface{})[0], d, objType)
} else if objMap["result_count"].(float64) == 0 {
// get the entity using listing api, for some VPC resources(eg VpcIpAddressAllocation), search API isn't indexed
uri = ComputePolicyPath(d, objType, false, nsxtClient, true)
// keep maximum allowed page size for pagination
uri += "?page_size=1000"
err := nsxtClient.NsxtSession.Get(uri, &obj)
if err != nil {
return fmt.Errorf("[ERROR] GET failed for object %s %v", objType, err)
} else {
objMap, ok := obj.(map[string]interface{})
if !ok {
return fmt.Errorf("want type map[string]interface{}; got %T", objMap)
}
// get results of type []interface {}
results := objMap["results"]
perfectMatchFound := false
cursor := 0
if objMap["cursor"] != nil {
cursor = objMap["cursor"].(int)
}
for {
for _, itemObject := range results.([]interface{}) {
for key, value := range itemObject.(map[string]interface{}) {
// Filter based on nsx_id or display_name to get only what datasource desires
//TODO: Check how to refine search in case of multiple entries found with same display_name
if (nsxID != "" && key == "id" && value == nsxID) || (displayName != "" && key == "display_name" && value == displayName) {
perfectMatchFound = true
break
}
}
if perfectMatchFound {
setAttrsInDatasourceSchema(itemObject, d, objType)
break
}
}
// If match found, or all records processed, break
if perfectMatchFound || cursor == 0 {
break
} else {
// get next page using cursor
uri += "&cursor=" + strconv.Itoa(cursor)
err = nsxtClient.NsxtSession.Get(uri, &obj)
if err != nil {
return fmt.Errorf("[ERROR] GET failed for object %s %v", objType, err)
} else {
objMap, ok = obj.(map[string]interface{})
if !ok {
return fmt.Errorf("want type map[string]interface{}; got %T", objMap)
}
// get results of type []interface {}
results = objMap["results"]
cursor = objMap["cursor"].(int)
}
}
}
if !perfectMatchFound {
return fmt.Errorf("no record found for %s with id '%s', display_name '%s' in scope '%s'", objType, nsxID, displayName, scope)
}
}
} else {
return fmt.Errorf("either multiple records found for this %s, or the %s is not shared with Project", objType, objType)
}
}
return err
}
func DatasourceReadForVM(d *schema.ResourceData, meta interface{}, objType string, s *schema.Resource) error {
var obj interface{}
nsxtClient := meta.(*nsxtclient.NsxtClient)
displayName := d.Get("display_name").(string)
externalID := d.Get("external_id").(string)
powerState := d.Get("power_state").(string)
var uri string
// Get the object from NSX using hidden full text search API for specific VPC
uri = nsxtClient.Config.BasePath + "/search?query=resource_type:" + objType
if externalID != "" {
uri += "%20AND%20external_id:" + externalID
}
if displayName != "" {
encodedDisplayName := url.QueryEscape(displayName)
uri += "%20AND%20display_name:" + encodedDisplayName
}
if powerState != "" {
uri += "%20AND%20power_state:" + powerState
}
uri += "&context=vpcs:" + "/orgs/" + nsxtClient.Config.OrgID + "/projects/" + nsxtClient.Config.ProjectID + "/vpcs/" + nsxtClient.Config.VpcID
err := nsxtClient.NsxtSession.Get(uri, &obj)
if err != nil {
log.Printf("[ERROR] Search failed for object %s %v\n", objType, err)
} else {
objMap, ok := obj.(map[string]interface{})
if !ok {
return fmt.Errorf("want type map[string]interface{}; got %T", objMap)
}
// get results of type []interface {} to get id and policypath
results := objMap["results"]
if objMap["result_count"].(float64) == 1 {
for key, value := range results.([]interface{})[0].(map[string]interface{}) {
if key == "external_id" {
d.Set("external_id", value.(string))
d.SetId(value.(string)) // set terraform ID
}
if key == "power_state" {
d.Set("power_state", value.(string))
}
if key == "display_name" {
d.Set("display_name", value.(string))
}
}
} else {
return fmt.Errorf("either multiple records found for %s with external_id '%s', display_name '%s', or object is not shared with Project", objType, externalID, displayName)
}
}
return err
}
func convertToSchemaMap(dataMap map[string]interface{}) map[string]*schema.Schema {
schemaMap := make(map[string]*schema.Schema)
for key, value := range dataMap {
// Create a new schema based on the value's type
var fieldSchema *schema.Schema
switch value.(type) {
case string:
fieldSchema = &schema.Schema{Type: schema.TypeString}
case int:
fieldSchema = &schema.Schema{Type: schema.TypeInt}
case float64:
fieldSchema = &schema.Schema{Type: schema.TypeFloat}
case bool:
fieldSchema = &schema.Schema{Type: schema.TypeBool}
// Add additional cases for other types as needed
default:
// Handle unsupported types or custom schema generation logic
// we can set the fieldSchema to a generic schema type or define a custom schema based on requirements
fieldSchema = &schema.Schema{Type: schema.TypeString}
}
// Add the schema to the new schema map
schemaMap[key] = fieldSchema
}
return schemaMap
}
/* Populate data from JSON to terraform schema properties. Check for dataType, chec whether property is present in tf schema, if present, then populate. */
//nolint:typecheck,gosimple
func populateTerraformData(key string, value interface{}, fieldSchema *schema.Schema, terraformDataMap map[string]interface{}, terraformDataData *schema.ResourceData, schemaS map[string]*schema.Schema) error {
switch fieldSchema.Type {
case schema.TypeString:
strValue, ok := value.(string)
if ok {
if terraformDataMap != nil {
terraformDataMap[key] = strValue
} else {
terraformDataData.Set(key, strValue)
}
} else {
return fmt.Errorf("invalid data type for field %s: %T", key, value)
}
case schema.TypeInt:
intValue, ok := value.(int)
if ok {
if terraformDataMap != nil {
terraformDataMap[key] = intValue
} else {
terraformDataData.Set(key, intValue)
}
} else if floatValue, ok := value.(float64); ok {
if terraformDataMap != nil {
terraformDataMap[key] = int(floatValue)
} else {
terraformDataData.Set(key, int(floatValue))
}
} else {
return fmt.Errorf("invalid data type for field %s: %T", key, value)
}
case schema.TypeFloat:
floatValue, ok := value.(float64)
if ok {
if terraformDataMap != nil {
terraformDataMap[key] = floatValue
} else {
terraformDataData.Set(key, floatValue)
}
} else {
return fmt.Errorf("invalid data type for field %s: %T", key, value)
}
case schema.TypeBool:
boolValue, ok := value.(bool)
if ok {
if terraformDataMap != nil {
terraformDataMap[key] = boolValue
} else {
terraformDataData.Set(key, boolValue)
}
} else {
return fmt.Errorf("invalid data type for field %s: %T", key, value)
}
case schema.TypeList:
listValue, ok := value.([]interface{})
if ok {
listData := make([]interface{}, 0)
for _, item := range listValue {
if item != nil {
if terraformDataMap != nil {
if itemSchema, isSchema := item.(*schema.Schema); isSchema {
itemData, err := APIDataToSchema(item, make(map[string]interface{}), map[string]*schema.Schema{
"schema": itemSchema,
})
if err != nil {
return err
}
listData = append(listData, itemData)
} else if itemResource, isResource := item.(*schema.Resource); isResource {
itemData, err := APIDataToSchema(item, make(map[string]interface{}), itemResource.Schema)
if err != nil {
return err
}
listData = append(listData, itemData)
} else {
switch item.(type) {
case string, int, float64, bool:
listData = append(listData, item)
case map[string]interface{}:
itemData, err := APIDataToSchema(item, make(map[string]interface{}), schemaS)
if err != nil {
return err
}
listData = append(listData, itemData)
default:
return fmt.Errorf("invalid data type for field %s: %v", key, item)
}
}
} else {
// process terraformDataData
switch item.(type) {
case string, int, float64, bool:
listData = append(listData, item)
default:
switch fieldSchema.Elem.(type) {
case string, int, float64, bool:
listData = append(listData, fieldSchema.Elem)
case *schema.Schema:
schemaItem := fieldSchema.Elem.(*schema.Schema)
itemData, err := APIDataToSchema(item, make(map[string]interface{}), map[string]*schema.Schema{
"schema": schemaItem,
})
if err != nil {
return err
}
listData = append(listData, itemData)
case *schema.Resource:
resourceItem := fieldSchema.Elem.(*schema.Resource)
itemData, err := APIDataToSchema(item, make(map[string]interface{}), resourceItem.Schema)
if err != nil {
return err
}
listData = append(listData, itemData)
}
}
}
}
}
if terraformDataMap != nil {
terraformDataMap[key] = listData
} else {
terraformDataData.Set(key, listData)
}
} else {
return fmt.Errorf("invalid data type for field %s: %T", key, value)
}
case schema.TypeSet:
setValue, ok := value.([]interface{})
if ok {
setData := make([]interface{}, 0)
for _, item := range setValue {
if item != nil {
switch item.(type) {
case string, int, float64, bool:
setData = append(setData, item)
case map[string]interface{}:
mapSchemaItem := convertToSchemaMap(item.(map[string]interface{}))
// Handle the map type item here, recursively call the APIDataToSchema function on the map item
itemData, err := APIDataToSchema(item, make(map[string]interface{}), mapSchemaItem)
if err != nil {
return err
}
setData = append(setData, itemData)
case *schema.Schema:
schemaItem := item.(*schema.Schema)
itemData, err := APIDataToSchema(item, make(map[string]interface{}), map[string]*schema.Schema{
"schema": schemaItem,
})
if err != nil {
return err
}
setData = append(setData, itemData)
case *schema.Resource:
resourceItem := item.(*schema.Resource)
itemData, err := APIDataToSchema(item, make(map[string]interface{}), resourceItem.Schema)
if err != nil {
return err
}
setData = append(setData, itemData)
default:
return fmt.Errorf("invalid data type for field %s: %T, expected primitive type or map or *schema.Schema or *schema.Resource", key, item)
}
}
}
if terraformDataMap != nil {
terraformDataMap[key] = setData
} else {
terraformDataData.Set(key, setData)
}
}
case schema.TypeMap:
mapValue, ok := value.(map[string]interface{})
if ok {
mapData, err := APIDataToSchema(mapValue, make(map[string]interface{}), fieldSchema.Elem.(*schema.Schema).Elem.(*schema.Resource).Schema)
if err != nil {
return err
}
if terraformDataMap != nil {
terraformDataMap[key] = mapData
} else {
terraformDataData.Set(key, mapData)
}
} else {
return fmt.Errorf("invalid data type for field %s: %T", key, value)
}
default:
return fmt.Errorf("unsupported schema type for field %s: %s", key, fieldSchema.Type)
}
return nil
}
/*
It takes the NSXT JSON data and fills in the terraform data during API read.
It takes input as the top level schema and it uses that to properly create the corresponding terraform resource data
It also checks whether a given nsxt key is defined in the schema before attempting to fill the data.
*/
func APIDataToSchema(jsonData interface{}, terraformData interface{}, schemaS map[string]*schema.Schema) (interface{}, error) {
jsonDataMap, ok := jsonData.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("invalid JSON data type: %T", jsonData)
}
switch v := terraformData.(type) {
case map[string]interface{}:
terraformDataMap := v
for key, value := range jsonDataMap {
if fieldSchema, exists := schemaS[key]; exists {
// Because there is no state during import operation
err := populateTerraformData(key, value, fieldSchema, terraformDataMap, nil, schemaS)
if err != nil {
return nil, err
}
}
}
return terraformDataMap, nil
case *schema.ResourceData:
terraformDataData := v
for key, value := range jsonDataMap {
// Process the key if its present in terraform schema. We don't want all properties from API response, only want the ones in tf resource schema
if fieldSchema, exists := schemaS[key]; exists {
err := populateTerraformData(key, value, fieldSchema, nil, terraformDataData, schemaS)
if err != nil {
return nil, err
}
}
}
return terraformDataData, nil
default:
return nil, fmt.Errorf("invalid Terraform data type: %T", terraformData)
}
}
func CommonHash(v interface{}) int {
return schema.HashString("nsxt")
}
/*
Function that takes the terraform plan data and schema and converts it into NSXT JSON
It recursively resolves the data type of the terraform schema and converts scalar to scalar, Set to dictionary and list to list.
*/
//nolint:typecheck
func SchemaToNsxtData(d interface{}, s interface{}) (interface{}, error) {
switch dType := d.(type) {
default:
case map[string]interface{}:
m := make(map[string]interface{})
for k, v := range d.(map[string]interface{}) {
if obj, err := SchemaToNsxtData(v, s.(map[string]*schema.Schema)[k]); err == nil && obj != nil && obj != "" {
m[k] = obj
} else if err != nil {
log.Printf("[ERROR] SchemaToNsxtData %v in parsing k: %v v: %v type: %v", err, k, v, dType)
}
}
return m, nil
case []interface{}:
var objList []interface{}
varray := d.([]interface{})
var listSchema interface{}
switch sType := s.(*schema.Schema).Elem.(type) {
default:
log.Printf("[DEBUG] Element schema type: %v", sType)
case *schema.Resource:
listSchema = s.(*schema.Schema).Elem.(*schema.Resource).Schema
case *schema.Schema:
listSchema = s.(*schema.Schema).Elem.(*schema.Schema)
}
for i := 0; i < len(varray); i++ {
obj, err := SchemaToNsxtData(varray[i], listSchema)
if err == nil && obj != nil {
objList = append(objList, obj)
}
}
if len(objList) == 0 {
return nil, nil
}
return objList, nil
case *schema.Set:
if len(d.(*schema.Set).List()) == 0 {
return nil, nil
}
/**
Because both Object type and Set type are defined as TypeSet in terraform schema and there is no way to differentiate between the two,
Get maxItems from schema for that Set. If its 1, means its an object type in schema, otherwise a Set with multiple items allowed.
**/
maxItems := s.(*schema.Schema).MaxItems
if maxItems == 1 {
// Its an object
obj, err := SchemaToNsxtData(d.(*schema.Set).List()[0], s.(*schema.Schema).Elem.(*schema.Resource).Schema)
return obj, err
}
// Its a Set
var objList []interface{}
varray := d.(*schema.Set).List()
var setSchema interface{}
switch setSchemaType := s.(*schema.Schema).Elem.(type) {
default:
log.Printf("[DEBUG] Set schema type: %v", setSchemaType)
case *schema.Resource:
setSchema = s.(*schema.Schema).Elem.(*schema.Resource).Schema
case *schema.Schema:
setSchema = s.(*schema.Schema).Elem.(*schema.Schema)
}
for i := 0; i < len(varray); i++ {
obj, err := SchemaToNsxtData(varray[i], setSchema)
if err == nil && obj != nil {
objList = append(objList, obj)
}
}
if len(objList) == 0 {
return nil, nil
}
return objList, nil
case *schema.ResourceData:
// In this case the top level schema should be present.
m := make(map[string]interface{})
r := d.(*schema.ResourceData)
for k, v := range s.(map[string]*schema.Schema) {
if data, ok := r.GetOkExists(k); ok {
if obj, err := SchemaToNsxtData(data, v); err == nil && obj != nil && obj != "" {
m[k] = obj
} else if err != nil {
log.Printf("[ERROR] SchemaToNsxtData %v in converting k: %v v: %v", err, k, v)
}
}
}
return m, nil
}
/** Return the same object as there is nothing special about **/
return d, nil
}
// Function to import an existing entity on NSXT into terraform management, so we can manage it using terraform.
//nolint:typecheck
func ResourceImporter(d *schema.ResourceData, meta interface{}, objType string, s map[string]*schema.Schema, path string) ([]*schema.ResourceData, error) {
log.Printf("[DEBUG] ResourceImporter objType %v using policy path %v\n", objType, d.Id())
nsxtClient := meta.(*nsxtclient.NsxtClient)
if d.Id() != "" {
// return the ID based import
return []*schema.ResourceData{d}, nil
}
var data interface{}
results := make([]*schema.ResourceData, 1)
if path == "" {
return nil, fmt.Errorf("error in computing policy path for object %s in terraform_policy_utils.ResourceImporter()", objType)
}
err1 := nsxtClient.NsxtSession.Get(path, &data)
if err1 != nil || data == nil {
log.Printf("[ERROR] ResourceImporter %v in GET of path %v\n", err1, path)
return nil, err1
}
obj := data.(map[string]interface{})
log.Printf("[DEBUG] ResourceImporter processing obj %v\n", obj)
result := new(schema.ResourceData)
if _, err := APIDataToSchema(obj, result, s); err == nil {
log.Printf("[DEBUG] ResourceImporter Processing obj %v\n", obj)
id := obj["id"].(string)
result.SetId(id)
results[0] = result
}
return results, nil
}
// Calculate policy path for different resources in the provider
//nolint:typecheck
func ComputePolicyPath(d *schema.ResourceData, objType string, isReadRequest bool, nsxtClient *nsxtclient.NsxtClient, isListingRequest bool) string {
var url string
var orgID string
var projectID string
var vpcID string
basePath := nsxtClient.Config.BasePath
orgID = nsxtClient.Config.OrgID
projectID = nsxtClient.Config.ProjectID
vpcID = nsxtClient.Config.VpcID
switch objType {
case "Group":
if isListingRequest {
url = basePath + "/orgs/" + orgID + "/projects/" + projectID + "/vpcs/" + vpcID + "/groups/"
} else {
url = basePath + "/orgs/" + orgID + "/projects/" + projectID + "/vpcs/" + vpcID + "/groups/" + d.Get("nsx_id").(string)
}
case "VpcSubnet":
if isListingRequest {
url = basePath + "/orgs/" + orgID + "/projects/" + projectID + "/vpcs/" + vpcID + "/subnets/"
} else {
url = basePath + "/orgs/" + orgID + "/projects/" + projectID + "/vpcs/" + vpcID + "/subnets/" + d.Get("nsx_id").(string)
}
case "VpcSubnetPort":
if isListingRequest {
url = basePath + d.Get("parent_path").(string) + "/ports/"
} else {
url = basePath + d.Get("parent_path").(string) + "/ports/" + d.Get("nsx_id").(string)
}
case "SecurityPolicy":
if isListingRequest {
url = basePath + "/orgs/" + orgID + "/projects/" + projectID + "/vpcs/" + vpcID + "/security-policies/"
} else {
url = basePath + "/orgs/" + orgID + "/projects/" + projectID + "/vpcs/" + vpcID + "/security-policies/" + d.Get("nsx_id").(string)
}
case "SecurityPolicyRule":
if isListingRequest {
url = basePath + d.Get("parent_path").(string) + "/rules/"
} else {
url = basePath + d.Get("parent_path").(string) + "/rules/" + d.Get("nsx_id").(string)
}
case "GatewayPolicy":
if isListingRequest {
url = basePath + "/orgs/" + orgID + "/projects/" + projectID + "/vpcs/" + vpcID + "/gateway-policies/"
} else {
url = basePath + "/orgs/" + orgID + "/projects/" + projectID + "/vpcs/" + vpcID + "/gateway-policies/" + d.Get("nsx_id").(string)
}
case "GatewayPolicyRule":
if isListingRequest {
url = basePath + d.Get("parent_path").(string) + "/rules/"
} else {
url = basePath + d.Get("parent_path").(string) + "/rules/" + d.Get("nsx_id").(string)
}
case "PolicyVpcNatRule":
if isListingRequest {
url = basePath + d.Get("parent_path").(string) + "/nat-rules/"
} else {
url = basePath + d.Get("parent_path").(string) + "/nat-rules/" + d.Get("nsx_id").(string)
}
case "IpAddressAllocation":
if isListingRequest {
url = basePath + d.Get("parent_path").(string) + "/ip-allocations/"
} else {
url = basePath + d.Get("parent_path").(string) + "/ip-allocations/" + d.Get("nsx_id").(string)
}
case "VpcIpAddressAllocation":
if isListingRequest {
url = basePath + "/orgs/" + orgID + "/projects/" + projectID + "/vpcs/" + vpcID + "/ip-address-allocations/"
} else {
url = basePath + "/orgs/" + orgID + "/projects/" + projectID + "/vpcs/" + vpcID + "/ip-address-allocations/" + d.Get("nsx_id").(string)
}
case "DhcpV4StaticBindingConfig":
if isListingRequest {
url = basePath + d.Get("parent_path").(string) + "/dhcp-static-binding-configs/"
} else {
url = basePath + d.Get("parent_path").(string) + "/dhcp-static-binding-configs/" + d.Get("nsx_id").(string)
}
case "DhcpV6StaticBindingConfig":
if isListingRequest {
url = basePath + d.Get("parent_path").(string) + "/dhcp-static-binding-configs/"
} else {
url = basePath + d.Get("parent_path").(string) + "/dhcp-static-binding-configs/" + d.Get("nsx_id").(string)
}
case "StaticRoutes":
if isListingRequest {
url = basePath + "/orgs/" + orgID + "/projects/" + projectID + "/vpcs/" + vpcID + "/static-routes/"
} else {
url = basePath + "/orgs/" + orgID + "/projects/" + projectID + "/vpcs/" + vpcID + "/static-routes/" + d.Get("nsx_id").(string)
}
}
return url
}
// It sets default values in the terraform resources to avoid diffs for scalars.
//nolint:typecheck
func SetDefaultsInAPIRes(apiRes interface{}, dLocal interface{}, s map[string]*schema.Schema) (interface{}, error) {
if apiRes == nil {
log.Printf("[ERROR] SetDefaultsInAPIRes got nil for %v", s)
return apiRes, nil
}
switch dLocal.(type) {
default:
case map[string]interface{}:
for k, v := range dLocal.(map[string]interface{}) {
switch v.(type) {
// Getting key, value for given dLocal
default:
if _, ok := apiRes.(map[string]interface{})[k]; !ok {
// Cheking if field is present in schema
if dval, ok := s[k]; ok {
// Getting default values from schema
defaultVal, err := dval.DefaultValue()
if err != nil {
log.Printf("[ERROR] SetDefaultsInAPIRes did not get default value from schema err %v %v",
err, defaultVal)
} else {
if defaultVal != nil {
apiRes.(map[string]interface{})[k] = defaultVal
}
}
}
}
// dLocal nested dictionary.
case map[string]interface{}:
if s2, ok := s[k]; ok {
switch s2.Elem.(type) {
default:
case *schema.Resource:
if apiRes.(map[string]interface{})[k] != nil {
apiRes1, err := SetDefaultsInAPIRes(apiRes.(map[string]interface{})[k], v, s2.Elem.(*schema.Resource).Schema)
if err != nil {
log.Printf("[ERROR] SetDefaultsInAPIRes %v", err)
} else {
apiRes.(map[string]interface{})[k] = apiRes1
}
} else {
apiRes.(map[string]interface{})[k] = v
}
}
}
// dLocal is array of dictionaries.
case []interface{}:
var objList []interface{}
if apiRes.(map[string]interface{})[k] != nil {
varray2 := apiRes.(map[string]interface{})[k].([]interface{})
//getting schema for nested object.
s2, err := s[k]
var dst, src []interface{}
//As err returned is boolean value
if !err {
log.Printf("[ERROR] SetDefaultsInAPIRes in fetching k %v err %v", k, err)
}
if len(varray2) > len(v.([]interface{})) {
dst = varray2
src = v.([]interface{})
} else {
dst = v.([]interface{})
src = varray2
}
for x, y := range src {
switch s2.Elem.(type) {
default:
case *schema.Resource:
obj, err := SetDefaultsInAPIRes(dst[x], y, s2.Elem.(*schema.Resource).Schema)
if err != nil {
log.Printf("[ERROR] SetDefaultsInAPIRes err %v in x %v y %v", err, x, y)
} else {
objList = append(objList, obj)
}
case *schema.Schema:
objList = append(objList, src[x])
}
}
}
apiRes.(map[string]interface{})[k] = objList
}
}
}
return apiRes, nil
}