-
Notifications
You must be signed in to change notification settings - Fork 928
/
role.go
219 lines (188 loc) · 6.59 KB
/
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
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
package v7action
import (
"code.cloudfoundry.org/cli/actor/actionerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
)
type Role ccv3.Role
func (actor Actor) CreateOrgRole(roleType constant.RoleType, orgGUID string, userNameOrGUID string, userOrigin string, isClient bool) (Warnings, error) {
roleToCreate := ccv3.Role{
Type: roleType,
OrgGUID: orgGUID,
}
if isClient {
err := actor.UAAClient.ValidateClientUser(userNameOrGUID)
if err != nil {
return Warnings{}, err
}
roleToCreate.UserGUID = userNameOrGUID
} else {
roleToCreate.Username = userNameOrGUID
roleToCreate.Origin = userOrigin
}
_, warnings, err := actor.CloudControllerClient.CreateRole(roleToCreate)
return Warnings(warnings), err
}
func (actor Actor) CreateSpaceRole(roleType constant.RoleType, orgGUID string, spaceGUID string, userNameOrGUID string, userOrigin string, isClient bool) (Warnings, error) {
roleToCreate := ccv3.Role{
Type: roleType,
SpaceGUID: spaceGUID,
}
if isClient {
roleToCreate.UserGUID = userNameOrGUID
} else {
roleToCreate.Username = userNameOrGUID
roleToCreate.Origin = userOrigin
}
warnings, err := actor.CreateOrgRole(constant.OrgUserRole, orgGUID, userNameOrGUID, userOrigin, isClient)
if err != nil {
_, isIdempotentError := err.(ccerror.RoleAlreadyExistsError)
_, isForbiddenError := err.(ccerror.ForbiddenError)
_, isUserNotFoundError := err.(actionerror.UserNotFoundError)
if !isIdempotentError && !isForbiddenError && !isUserNotFoundError {
return warnings, err
}
}
_, ccv3Warnings, err := actor.CloudControllerClient.CreateRole(roleToCreate)
warnings = append(warnings, ccv3Warnings...)
return warnings, err
}
func (actor Actor) DeleteOrgRole(roleType constant.RoleType, orgGUID string, userNameOrGUID string, userOrigin string, isClient bool) (Warnings, error) {
var userGUID string
var allWarnings Warnings
userGUID, warnings, err := actor.getUserGuidForDeleteRole(isClient, userNameOrGUID, userOrigin, allWarnings)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return allWarnings, err
}
roleGUID, warnings, err := actor.GetRoleGUID(ccv3.OrganizationGUIDFilter, orgGUID, userGUID, roleType)
allWarnings = append(allWarnings, warnings...)
if err != nil || roleGUID == "" {
return allWarnings, err
}
jobURL, deleteRoleWarnings, err := actor.CloudControllerClient.DeleteRole(roleGUID)
allWarnings = append(allWarnings, deleteRoleWarnings...)
if err != nil {
return allWarnings, err
}
pollJobWarnings, err := actor.CloudControllerClient.PollJob(jobURL)
allWarnings = append(allWarnings, pollJobWarnings...)
if err != nil {
return allWarnings, err
}
return allWarnings, nil
}
func (actor Actor) DeleteSpaceRole(roleType constant.RoleType, spaceGUID string, userNameOrGUID string, userOrigin string, isClient bool) (Warnings, error) {
var userGUID string
var allWarnings Warnings
userGUID, userWarnings, err := actor.getUserGuidForDeleteRole(isClient, userNameOrGUID, userOrigin, allWarnings)
allWarnings = append(allWarnings, userWarnings...)
if err != nil {
return allWarnings, err
}
roleGUID, roleWarnings, err := actor.GetRoleGUID(ccv3.SpaceGUIDFilter, spaceGUID, userGUID, roleType)
allWarnings = append(allWarnings, roleWarnings...)
if err != nil || roleGUID == "" {
return allWarnings, err
}
jobURL, deleteRoleWarnings, err := actor.CloudControllerClient.DeleteRole(roleGUID)
allWarnings = append(allWarnings, deleteRoleWarnings...)
if err != nil {
return allWarnings, err
}
pollJobWarnings, err := actor.CloudControllerClient.PollJob(jobURL)
allWarnings = append(allWarnings, pollJobWarnings...)
if err != nil {
return allWarnings, err
}
return allWarnings, nil
}
func (actor Actor) getUserGuidForDeleteRole(isClient bool, userNameOrGUID string, userOrigin string, allWarnings Warnings) (string, Warnings, error) {
var userGUID string
if isClient {
user, warnings, err := actor.CloudControllerClient.GetUser(userNameOrGUID)
allWarnings = append(allWarnings, warnings...)
if err != nil {
if _, ok := err.(ccerror.UserNotFoundError); ok {
err = actionerror.UserNotFoundError{Username: userNameOrGUID}
}
return "", allWarnings, err
}
userGUID = user.GUID
} else {
ccv3Users, warnings, err := actor.CloudControllerClient.GetUsers(
ccv3.Query{
Key: ccv3.UsernamesFilter,
Values: []string{userNameOrGUID},
},
ccv3.Query{
Key: ccv3.OriginsFilter,
Values: []string{userOrigin},
},
)
allWarnings = append(allWarnings, warnings...)
if err != nil {
return "", allWarnings, err
}
if len(ccv3Users) == 0 {
return "", allWarnings, actionerror.UserNotFoundError{Username: userNameOrGUID, Origin: userOrigin}
}
userGUID = ccv3Users[0].GUID
}
return userGUID, allWarnings, nil
}
func (actor Actor) GetRoleGUID(queryKey ccv3.QueryKey, orgOrSpaceGUID string, userGUID string, roleType constant.RoleType) (string, Warnings, error) {
ccv3Roles, _, warnings, err := actor.CloudControllerClient.GetRoles(
ccv3.Query{
Key: ccv3.UserGUIDFilter,
Values: []string{userGUID},
},
ccv3.Query{
Key: ccv3.RoleTypesFilter,
Values: []string{string(roleType)},
},
ccv3.Query{
Key: queryKey,
Values: []string{orgOrSpaceGUID},
},
)
if err != nil {
return "", Warnings(warnings), err
}
if len(ccv3Roles) == 0 {
return "", Warnings(warnings), nil
}
return ccv3Roles[0].GUID, Warnings(warnings), nil
}
func (actor Actor) GetOrgUsersByRoleType(orgGuid string) (map[constant.RoleType][]User, Warnings, error) {
return actor.getUsersByRoleType(orgGuid, ccv3.OrganizationGUIDFilter)
}
func (actor Actor) GetSpaceUsersByRoleType(spaceGuid string) (map[constant.RoleType][]User, Warnings, error) {
return actor.getUsersByRoleType(spaceGuid, ccv3.SpaceGUIDFilter)
}
func (actor Actor) getUsersByRoleType(guid string, filterKey ccv3.QueryKey) (map[constant.RoleType][]User, Warnings, error) {
ccv3Roles, includes, ccWarnings, err := actor.CloudControllerClient.GetRoles(
ccv3.Query{
Key: filterKey,
Values: []string{guid},
},
ccv3.Query{
Key: ccv3.Include,
Values: []string{"user"},
},
)
if err != nil {
return nil, Warnings(ccWarnings), err
}
usersByGuids := make(map[string]ccv3.User)
for _, user := range includes.Users {
usersByGuids[user.GUID] = user
}
usersByRoleType := make(map[constant.RoleType][]User)
for _, role := range ccv3Roles {
user := User(usersByGuids[role.UserGUID])
usersByRoleType[role.Type] = append(usersByRoleType[role.Type], user)
}
return usersByRoleType, Warnings(ccWarnings), nil
}