-
Notifications
You must be signed in to change notification settings - Fork 1
/
groups_service.go
269 lines (233 loc) · 8.38 KB
/
groups_service.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package warrant
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"github.com/pivotal-cf-experimental/warrant/internal/documents"
"github.com/pivotal-cf-experimental/warrant/internal/network"
)
// TODO: Pagination for List
// GroupsService provides access to common group actions. Using this service,
// you can create, delete, fetch and list group resources.
type GroupsService struct {
config Config
}
// NewGroupsService returns a GroupsService initialized with the given Config.
func NewGroupsService(config Config) GroupsService {
return GroupsService{
config: config,
}
}
// Create will make a request to UAA to create a new group resource with the given
// DisplayName. A token with the "scim.write" scope is required.
func (gs GroupsService) Create(displayName, token string) (Group, error) {
resp, err := newNetworkClient(gs.config).MakeRequest(network.Request{
Method: "POST",
Path: "/Groups",
Authorization: network.NewTokenAuthorization(token),
Body: network.NewJSONRequestBody(documents.CreateGroupRequest{
DisplayName: displayName,
}),
AcceptableStatusCodes: []int{http.StatusCreated},
})
if err != nil {
return Group{}, translateError(err)
}
var response documents.GroupResponse
err = json.Unmarshal(resp.Body, &response)
if err != nil {
return Group{}, MalformedResponseError{err}
}
return newGroupFromResponse(gs.config, response), nil
}
// Update will make a request to UAA to update the matching group resource.
// A token with the "scim.write" or "groups.update" scope is required.
func (gs GroupsService) Update(group Group, token string) (Group, error) {
resp, err := newNetworkClient(gs.config).MakeRequest(network.Request{
Method: "PUT",
Path: fmt.Sprintf("/Groups/%s", group.ID),
Authorization: network.NewTokenAuthorization(token),
IfMatch: strconv.Itoa(group.Version),
Body: network.NewJSONRequestBody(newUpdateGroupDocumentFromGroup(group)),
AcceptableStatusCodes: []int{http.StatusOK},
})
if err != nil {
return Group{}, translateError(err)
}
var response documents.GroupResponse
err = json.Unmarshal(resp.Body, &response)
if err != nil {
return Group{}, MalformedResponseError{err}
}
return newGroupFromResponse(gs.config, response), nil
}
// AddMember will make a request to UAA to add a member to the group resource with the matching id.
// A token with the "scim.write" scope is required.
func (gs GroupsService) AddMember(groupID, memberID, token string) (Member, error) {
resp, err := newNetworkClient(gs.config).MakeRequest(network.Request{
Method: "POST",
Path: fmt.Sprintf("/Groups/%s/members", groupID),
Authorization: network.NewTokenAuthorization(token),
Body: network.NewJSONRequestBody(documents.CreateMemberRequest{
Origin: "uaa",
Type: "USER",
Value: memberID,
}),
AcceptableStatusCodes: []int{http.StatusCreated},
})
if err != nil {
return Member{}, translateError(err)
}
var response documents.MemberResponse
err = json.Unmarshal(resp.Body, &response)
if err != nil {
return Member{}, MalformedResponseError{err}
}
return newMemberFromResponse(gs.config, response), nil
}
// CheckMembership will make a request to UAA to fetch a member resource from a group resource.
// A token with the "scim.read" scope is required.
func (gs GroupsService) CheckMembership(groupID, memberID, token string) (Member, bool, error) {
resp, err := newNetworkClient(gs.config).MakeRequest(network.Request{
Method: "GET",
Path: fmt.Sprintf("/Groups/%s/members/%s", groupID, memberID),
Authorization: network.NewTokenAuthorization(token),
AcceptableStatusCodes: []int{http.StatusOK, http.StatusNotFound},
})
if err != nil {
return Member{}, false, translateError(err)
}
if resp.Code == http.StatusNotFound {
return Member{}, false, nil
}
var response documents.MemberResponse
err = json.Unmarshal(resp.Body, &response)
if err != nil {
return Member{}, false, MalformedResponseError{err}
}
return newMemberFromResponse(gs.config, response), true, nil
}
// ListMembers will make a request to UAA to fetch the members of a group resource with the matching id.
// A token with the "scim.read" scope is required.
func (gs GroupsService) ListMembers(groupID, token string) ([]Member, error) {
resp, err := newNetworkClient(gs.config).MakeRequest(network.Request{
Method: "GET",
Path: fmt.Sprintf("/Groups/%s/members", groupID),
Authorization: network.NewTokenAuthorization(token),
AcceptableStatusCodes: []int{http.StatusOK},
})
if err != nil {
return []Member{}, translateError(err)
}
var response []documents.MemberResponse
err = json.Unmarshal(resp.Body, &response)
if err != nil {
return []Member{}, MalformedResponseError{err}
}
var memberList []Member
for _, memberResponse := range response {
memberList = append(memberList, newMemberFromResponse(gs.config, memberResponse))
}
return memberList, nil
}
// RemoveMember will make a request to UAA to remove a member from a group resource.
// A token with the "scim.write" scope is required.
func (gs GroupsService) RemoveMember(groupID, memberID, token string) error {
_, err := newNetworkClient(gs.config).MakeRequest(network.Request{
Method: "DELETE",
Path: fmt.Sprintf("/Groups/%s/members/%s", groupID, memberID),
Authorization: network.NewTokenAuthorization(token),
AcceptableStatusCodes: []int{http.StatusOK},
})
if err != nil {
return translateError(err)
}
return nil
}
// Get will make a request to UAA to fetch the group resource with the matching id.
// A token with the "scim.read" scope is required.
func (gs GroupsService) Get(id, token string) (Group, error) {
resp, err := newNetworkClient(gs.config).MakeRequest(network.Request{
Method: "GET",
Path: fmt.Sprintf("/Groups/%s", id),
Authorization: network.NewTokenAuthorization(token),
AcceptableStatusCodes: []int{http.StatusOK},
})
if err != nil {
return Group{}, translateError(err)
}
var response documents.GroupResponse
err = json.Unmarshal(resp.Body, &response)
if err != nil {
return Group{}, MalformedResponseError{err}
}
return newGroupFromResponse(gs.config, response), nil
}
// List wil make a request to UAA to list the groups that match the given Query.
// A token with the "scim.read" scope is required.
func (gs GroupsService) List(query Query, token string) ([]Group, error) {
requestPath := url.URL{
Path: "/Groups",
RawQuery: url.Values{
"filter": []string{query.Filter},
"sortBy": []string{query.SortBy},
}.Encode(),
}
resp, err := newNetworkClient(gs.config).MakeRequest(network.Request{
Method: "GET",
Path: requestPath.String(),
Authorization: network.NewTokenAuthorization(token),
AcceptableStatusCodes: []int{http.StatusOK},
})
if err != nil {
return []Group{}, translateError(err)
}
var response documents.GroupListResponse
err = json.Unmarshal(resp.Body, &response)
if err != nil {
return []Group{}, MalformedResponseError{err}
}
var groupList []Group
for _, groupResponse := range response.Resources {
groupList = append(groupList, newGroupFromResponse(gs.config, groupResponse))
}
return groupList, err
}
// Delete will make a request to UAA to delete the group resource with the matching id.
// A token with the "scim.write" scope is required.
func (gs GroupsService) Delete(id, token string) error {
_, err := newNetworkClient(gs.config).MakeRequest(network.Request{
Method: "DELETE",
Path: fmt.Sprintf("/Groups/%s", id),
Authorization: network.NewTokenAuthorization(token),
AcceptableStatusCodes: []int{http.StatusOK},
})
if err != nil {
return translateError(err)
}
return nil
}
func newUpdateGroupDocumentFromGroup(group Group) documents.CreateUpdateGroupRequest {
var members []documents.CreateMemberRequest
for _, member := range group.Members {
members = append(members, documents.CreateMemberRequest{
Origin: member.Origin,
Type: member.Type,
Value: member.Value,
})
}
return documents.CreateUpdateGroupRequest{
Schemas: schemas,
ID: group.ID,
DisplayName: group.DisplayName,
Description: group.Description,
Members: members,
Meta: documents.Meta{
Version: group.Version,
Created: group.CreatedAt,
LastModified: group.UpdatedAt,
},
}
}