-
Notifications
You must be signed in to change notification settings - Fork 132
/
types_iam.go
192 lines (162 loc) · 5.15 KB
/
types_iam.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
package ovh
type IamReferenceAction struct {
Action string `json:"action"`
Categories []string `json:"categories"`
Description string `json:"description"`
ResourceType string `json:"resourceType"`
}
func (a *IamReferenceAction) ToMap() map[string]any {
out := make(map[string]any, 4)
out["action"] = a.Action
out["categories"] = a.Categories
out["description"] = a.Description
out["resource_type"] = a.ResourceType
return out
}
type IamPolicy struct {
Id string `json:"id,omitempty"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Identities []string `json:"identities"`
Resources []IamResource `json:"resources"`
Permissions IamPermissions `json:"permissions"`
PermissionsGroups []PermissionGroup `json:"permissionsGroups"`
CreatedAt string `json:"createdAt,omitempty"`
UpdatedAt string `json:"updatedAt,omitempty"`
ReadOnly bool `json:"readOnly,omitempty"`
Owner string `json:"owner,omitempty"`
}
type PermissionGroup struct {
Urn string `json:"urn"`
}
func (p IamPolicy) ToMap() map[string]any {
out := make(map[string]any, 0)
out["name"] = p.Name
out["owner"] = p.Owner
out["created_at"] = p.CreatedAt
out["identities"] = p.Identities
var resources []string
for _, r := range p.Resources {
resources = append(resources, r.URN)
}
out["resources"] = resources
// inline allow, except and deny
allow, except, deny := p.Permissions.ToLists()
if len(allow) != 0 {
out["allow"] = allow
}
if len(except) != 0 {
out["except"] = except
}
if len(deny) != 0 {
out["deny"] = deny
}
if len(p.PermissionsGroups) != 0 {
var permGrps []string
for _, grp := range p.PermissionsGroups {
permGrps = append(permGrps, grp.Urn)
}
out["permissions_groups"] = permGrps
}
if p.Description != "" {
out["description"] = p.Description
}
if p.ReadOnly {
out["read_only"] = p.ReadOnly
}
if p.UpdatedAt != "" {
out["updated_at"] = p.UpdatedAt
}
return out
}
// IamResource represent a possible information returned when viewing a policy
type IamResource struct {
// URN is always returned and is the urn of the resource or resource group
// can also be a globing urn, for example "urn:v1:eu:resource:*"
URN string `json:"urn,omitempty"`
// If the urn is a resourceGroup. the details are also present
Group *IamResourceGroup `json:"group,omitempty"`
// If the urn is an IAM resource, the details are also present
Resource *IamResourceDetails `json:"resource,omitempty"`
}
type IamResourceDetails struct {
Id string `json:"id,omitempty"`
URN string `json:"urn,omitempty"`
Name string `json:"name,omitempty"`
DisplayName string `json:"displayName,omitempty"`
Owner string `json:"owner,omitempty"`
Type string `json:"type,omitempty"`
Tags map[string]string `json:"tags,omitempty"`
}
type IamPermissions struct {
Allow []IamAction `json:"allow"`
Except []IamAction `json:"except"`
Deny []IamAction `json:"deny"`
}
func (p IamPermissions) ToLists() ([]string, []string, []string) {
var allow []string
var except []string
var deny []string
for _, r := range p.Allow {
allow = append(allow, r.Action)
}
for _, r := range p.Except {
except = append(except, r.Action)
}
for _, r := range p.Deny {
deny = append(deny, r.Action)
}
return allow, except, deny
}
type IamAction struct {
Action string `json:"action"`
}
type IamResourceGroup struct {
ID string `json:"id,omitempty"`
Name string `json:"name"`
Resources []IamResourceDetails `json:"resources"`
URN string `json:"urn,omitempty"`
Owner string `json:"owner,omitempty"`
CreatedAt string `json:"createdAt,omitempty"`
UpdatedAt string `json:"updatedAt,omitempty"`
ReadOnly bool `json:"readOnly,omitempty"`
}
type IamResourceGroupCreate struct {
Name string `json:"name"`
Resources []IamResourceDetails `json:"resources"`
}
type IamPermissionsGroup struct {
Id string `json:"id,omitempty"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Permissions IamPermissions `json:"permissions"`
CreatedAt string `json:"createdAt,omitempty"`
UpdatedAt string `json:"updatedAt,omitempty"`
Urn string `json:"urn,omitempty"`
Owner string `json:"owner,omitempty"`
}
func (p IamPermissionsGroup) ToMap() map[string]any {
out := make(map[string]any, 0)
out["name"] = p.Name
out["owner"] = p.Owner
out["created_at"] = p.CreatedAt
// inline allow, except and deny
allow, except, deny := p.Permissions.ToLists()
if len(allow) != 0 {
out["allow"] = allow
}
if len(except) != 0 {
out["except"] = except
}
if len(deny) != 0 {
out["deny"] = deny
}
if p.Description != "" {
out["description"] = p.Description
}
if p.UpdatedAt != "" {
out["updated_at"] = p.UpdatedAt
}
out["urn"] = p.Urn
return out
}