-
Notifications
You must be signed in to change notification settings - Fork 7
/
structure_role.go
129 lines (107 loc) · 2.81 KB
/
structure_role.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
package main
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
// Flatteners
func flattenRole(d *schema.ResourceData, in *Role) error {
if in == nil {
return nil
}
d.SetId(in.ID)
d.Set("description", in.Description)
d.Set("name", in.Name)
d.Set("requestable", in.Requestable)
d.Set("enabled", in.Enabled)
if in.RoleOwner != nil {
v, ok := d.Get("owner").([]interface{})
if !ok {
v = []interface{}{}
}
roleOwnerList := []*ObjectInfo{in.RoleOwner}
d.Set("owner", flattenObjectRoles(roleOwnerList, v))
}
if in.AccessProfiles != nil {
v, ok := d.Get("access_profiles").([]interface{})
if !ok {
v = []interface{}{}
}
d.Set("access_profiles", flattenObjectRoles(in.AccessProfiles, v))
}
return nil
}
func flattenObjectRoles(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 expandRole(in *schema.ResourceData) (*Role, error) {
obj := Role{}
if in == nil {
return nil, fmt.Errorf("[ERROR] Expanding Role: Schema Resource data is nil")
}
if v := in.Id(); len(v) > 0 {
obj.ID = v
}
obj.Description = in.Get("description").(string)
obj.Name = in.Get("name").(string)
if v, ok := in.Get("requestable").(bool); ok {
obj.Requestable = &v
}
if v, ok := in.Get("owner").([]interface{}); ok && len(v) > 0 {
obj.RoleOwner = expandObjectRoles(v)[0]
}
if v, ok := in.Get("access_profiles").([]interface{}); ok && len(v) > 0 {
obj.AccessProfiles = expandObjectRoles(v)
}
if v, ok := in.Get("enabled").(bool); ok {
obj.Enabled = &v
}
return &obj, nil
}
func expandUpdateRole(in *schema.ResourceData) ([]*UpdateRole, interface{}, error) {
updatableFields := []string{"name", "description", "enabled", "owner", "accessProfiles", "requestable"}
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 := []*UpdateRole{}
for i := range updatableFields {
obj := UpdateRole{}
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 expandObjectRoles(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
}