-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathcustom_fields.go
More file actions
87 lines (69 loc) · 1.74 KB
/
custom_fields.go
File metadata and controls
87 lines (69 loc) · 1.74 KB
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
package netbox
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const customFieldsKey = "custom_fields"
var customFieldsSchema = &schema.Schema{
Type: schema.TypeMap,
Optional: true,
Computed: true,
Default: nil,
Elem: &schema.Schema{
Type: schema.TypeString,
Default: nil,
},
}
var customFieldsSchemaRead = &schema.Schema{
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
}
func normalizeCustomFields(cfm map[string]interface{}) map[string]interface{} {
newcfm := make(map[string]interface{})
for k, v := range cfm {
if v != nil && v != "" {
newcfm[k] = v
}
}
return newcfm
}
func mergeCustomFields(oldcfm, newcfm map[string]interface{}) map[string]interface{} {
if newcfm == nil {
newcfm = make(map[string]interface{})
}
for k, v := range newcfm {
if v == nil {
newcfm[k] = ""
}
}
for k := range oldcfm {
if _, ok := newcfm[k]; !ok {
newcfm[k] = ""
}
}
return newcfm
}
func customFieldsDiff(ctx context.Context, d *schema.ResourceDiff, meta interface{}) error {
cfg := d.GetRawConfig().GetAttr(customFieldsKey)
cfm, ok := d.Get(customFieldsKey).(map[string]interface{})
if cfg.IsNull() || !ok {
d.SetNew(customFieldsKey, nil)
} else {
newcfm := normalizeCustomFields(cfm)
d.SetNew(customFieldsKey, newcfm)
}
return nil
}
func computeCustomFieldsAttr(cf interface{}) map[string]interface{} {
cfm, _ := cf.(map[string]interface{})
return normalizeCustomFields(cfm)
}
func computeCustomFieldsModel(d *schema.ResourceData) interface{} {
oldcf, newcf := d.GetChange(customFieldsKey)
oldcfm, _ := oldcf.(map[string]interface{})
newcfm, _ := newcf.(map[string]interface{})
return mergeCustomFields(oldcfm, newcfm)
}