-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstructure_access_profile.go
134 lines (112 loc) · 3.23 KB
/
structure_access_profile.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
package main
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
// Flatteners
func flattenAccessProfile(d *schema.ResourceData, in *AccessProfile) error {
if in == nil {
return nil
}
d.SetId(in.ID)
d.Set("name", in.Name)
d.Set("description", in.Description)
d.Set("enabled", in.Enabled)
d.Set("requestable", in.Requestable)
if in.AccessProfileOwner != nil {
v, ok := d.Get("owner").([]interface{})
if !ok {
v = []interface{}{}
}
accessProfileOwnerList := []*ObjectInfo{in.AccessProfileOwner}
d.Set("owner", flattenObjectAccessProfile(accessProfileOwnerList, v))
}
if in.AccessProfileSource != nil {
v, ok := d.Get("source").([]interface{})
if !ok {
v = []interface{}{}
}
accessProfileSourceList := []*ObjectInfo{in.AccessProfileSource}
d.Set("source", flattenObjectAccessProfile(accessProfileSourceList, v))
}
if in.Entitlements != nil {
v, ok := d.Get("entitlements").([]interface{})
if !ok {
v = []interface{}{}
}
d.Set("entitlements", flattenObjectRoles(in.Entitlements, v))
}
return nil
}
func flattenObjectAccessProfile(in []*ObjectInfo, p []interface{}) []interface{} {
if in == nil {
return []interface{}{}
}
out := make([]interface{}, 0, len(in))
for i := range in {
var obj = make(map[string]interface{})
obj["type"] = in[i].Type
obj["id"] = in[i].ID
obj["name"] = in[i].Name
out = append(out, obj)
}
return out
}
// Expanders
func expandAccessProfile(in *schema.ResourceData) (*AccessProfile, error) {
obj := AccessProfile{}
if in == nil {
return nil, fmt.Errorf("[ERROR] Expanding Access Profile: Schema Resource data is nil")
}
obj.Name = in.Get("name").(string)
obj.Description = in.Get("description").(string)
if v, ok := in.Get("enabled").(bool); ok {
obj.Enabled = &v
}
if v, ok := in.Get("owner").([]interface{}); ok && len(v) > 0 {
obj.AccessProfileOwner = expandObjectAccessProfile(v)[0]
}
if v, ok := in.Get("source").([]interface{}); ok && len(v) > 0 {
obj.AccessProfileSource = expandObjectAccessProfile(v)[0]
}
if v, ok := in.Get("entitlements").([]interface{}); ok && len(v) > 0 {
obj.Entitlements = expandObjectRoles(v)
}
return &obj, nil
}
func expandUpdateAccessProfile(in *schema.ResourceData) ([]*UpdateAccessProfile, interface{}, error) {
updatableFields := []string{"name", "description", "enabled", "owner", "entitlements", "requestable", "source"}
var id interface{}
if in == nil {
return nil, nil, fmt.Errorf("[ERROR] Expanding Role: Schema Resource data is nil")
}
if v := in.Id(); len(v) > 0 {
id = v
}
out := []*UpdateAccessProfile{}
for i := range updatableFields {
obj := UpdateAccessProfile{}
if v, ok := in.Get(fmt.Sprintf("/%s", updatableFields[i])).([]interface{}); ok {
obj.Op = "replace"
obj.Path = fmt.Sprintf("/%s", updatableFields[i])
obj.Value = v
}
out = append(out, &obj)
}
return out, id, nil
}
func expandObjectAccessProfile(p []interface{}) []*ObjectInfo {
if len(p) == 0 || p[0] == nil {
return []*ObjectInfo{}
}
out := make([]*ObjectInfo, 0, len(p))
for i := range p {
obj := ObjectInfo{}
in := p[i].(map[string]interface{})
obj.ID = in["id"].(string)
obj.Name = in["name"].(string)
obj.Type = in["type"].(string)
out = append(out, &obj)
}
return out
}