-
Notifications
You must be signed in to change notification settings - Fork 69
/
groups.go
377 lines (308 loc) · 11.6 KB
/
groups.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package iam
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/edgegriderr"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/session"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
type (
// GetGroupRequest describes the request parameters for the GetGroup endpoint.
GetGroupRequest struct {
GroupID int64
Actions bool
}
// Group describes the response from the ListGroups endpoint.
Group struct {
Actions *GroupActions `json:"actions,omitempty"`
CreatedBy string `json:"createdBy"`
CreatedDate time.Time `json:"createdDate"`
GroupID int64 `json:"groupId"`
GroupName string `json:"groupName"`
ModifiedBy string `json:"modifiedBy"`
ModifiedDate time.Time `json:"modifiedDate"`
ParentGroupID int64 `json:"parentGroupId"`
SubGroups []Group `json:"subGroups,omitempty"`
}
// GroupActions encapsulates permissions available to the user for this group.
GroupActions struct {
Delete bool `json:"delete"`
Edit bool `json:"edit"`
}
// GroupUser describes the response from the ListAffectedUsers endpoint.
GroupUser struct {
AccountID string `json:"accountId"`
Email string `json:"email"`
FirstName string `json:"firstName"`
IdentityID string `json:"uiIdentityId"`
LastLoginDate time.Time `json:"lastLoginDate"`
LastName string `json:"lastName"`
UserName string `json:"uiUserName"`
}
// GroupRequest describes the request and body parameters for creating new group or updating a group name endpoint.
GroupRequest struct {
GroupID int64 `json:"-"`
GroupName string `json:"groupName"`
}
// MoveGroupRequest describes the request body for the MoveGroup endpoint.
MoveGroupRequest struct {
SourceGroupID int64 `json:"sourceGroupId"`
DestinationGroupID int64 `json:"destinationGroupId"`
}
// ListAffectedUsersRequest describes the request and body parameters of the ListAffectedUsers endpoint.
ListAffectedUsersRequest struct {
DestinationGroupID int64
SourceGroupID int64
UserType string
}
// ListGroupsRequest describes the request parameters of the ListGroups endpoint.
ListGroupsRequest struct {
Actions bool
}
// RemoveGroupRequest describes the request parameter for the RemoveGroup endpoint.
RemoveGroupRequest struct {
GroupID int64
}
)
const (
// LostAccessUsers with a userType of lostAccess lose their access to the source group.
LostAccessUsers = "lostAccess"
// GainAccessUsers with a userType of gainAccess gain their access to the source group.
GainAccessUsers = "gainAccess"
)
var (
// ErrCreateGroup is returned when CreateGroup fails.
ErrCreateGroup = errors.New("create group")
// ErrGetGroup is returned when GetGroup fails.
ErrGetGroup = errors.New("get group")
// ErrListAffectedUsers is returned when ListAffectedUsers fails.
ErrListAffectedUsers = errors.New("list affected users")
// ErrListGroups is returned when ListGroups fails.
ErrListGroups = errors.New("list groups")
// ErrUpdateGroupName is returned when UpdateGroupName fails.
ErrUpdateGroupName = errors.New("update group name")
// ErrRemoveGroup is returned when RemoveGroup fails.
ErrRemoveGroup = errors.New("remove group")
// ErrMoveGroup is returned when MoveGroup fails.
ErrMoveGroup = errors.New("move group")
)
// Validate validates GetGroupRequest.
func (r GetGroupRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"GroupID": validation.Validate(r.GroupID, validation.Required),
})
}
// Validate validates GroupRequest.
func (r GroupRequest) Validate() error {
return validation.Errors{
"GroupID": validation.Validate(r.GroupID, validation.Required),
"GroupName": validation.Validate(r.GroupName, validation.Required),
}.Filter()
}
// Validate validates MoveGroupRequest.
func (r MoveGroupRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"DestinationGroupID": validation.Validate(r.DestinationGroupID, validation.Required),
"SourceGroupID": validation.Validate(r.SourceGroupID, validation.Required),
})
}
// Validate validates ListAffectedUsersRequest.
func (r ListAffectedUsersRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"DestinationGroupID": validation.Validate(r.DestinationGroupID, validation.Required),
"SourceGroupID": validation.Validate(r.SourceGroupID, validation.Required),
"UserType": validation.Validate(r.UserType, validation.In(LostAccessUsers, GainAccessUsers)),
})
}
// Validate validates RemoveGroupRequest.
func (r RemoveGroupRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"GroupID": validation.Validate(r.GroupID, validation.Required),
})
}
func (i *iam) CreateGroup(ctx context.Context, params GroupRequest) (*Group, error) {
logger := i.Log(ctx)
logger.Debug("CreateGroup")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w:\n%s", ErrCreateGroup, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/identity-management/v3/user-admin/groups/%d", params.GroupID))
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrCreateGroup, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrCreateGroup, err)
}
var result Group
resp, err := i.Exec(req, &result, params)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrCreateGroup, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("%s: %w", ErrCreateGroup, i.Error(resp))
}
return &result, nil
}
func (i *iam) GetGroup(ctx context.Context, params GetGroupRequest) (*Group, error) {
logger := i.Log(ctx)
logger.Debug("GetGroup")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w:\n%s", ErrGetGroup, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/identity-management/v3/user-admin/groups/%d", params.GroupID))
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetGroup, err)
}
q := uri.Query()
q.Add("actions", strconv.FormatBool(params.Actions))
uri.RawQuery = q.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetGroup, err)
}
var result Group
resp, err := i.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetGroup, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrGetGroup, i.Error(resp))
}
return &result, nil
}
func (i *iam) ListAffectedUsers(ctx context.Context, params ListAffectedUsersRequest) ([]GroupUser, error) {
logger := i.Log(ctx)
logger.Debug("ListAffectedUsers")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w:\n%s", ErrListAffectedUsers, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/identity-management/v3/user-admin/groups/move/%d/%d/affected-users", params.SourceGroupID, params.DestinationGroupID))
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrListAffectedUsers, err)
}
if params.UserType != "" {
q := uri.Query()
q.Add("userType", params.UserType)
uri.RawQuery = q.Encode()
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrListAffectedUsers, err)
}
var result []GroupUser
resp, err := i.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListAffectedUsers, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListAffectedUsers, i.Error(resp))
}
return result, nil
}
func (i *iam) ListGroups(ctx context.Context, params ListGroupsRequest) ([]Group, error) {
logger := i.Log(ctx)
logger.Debug("ListGroups")
uri, err := url.Parse("/identity-management/v3/user-admin/groups")
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrListGroups, err)
}
q := uri.Query()
q.Add("actions", strconv.FormatBool(params.Actions))
uri.RawQuery = q.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrListGroups, err)
}
var result []Group
resp, err := i.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListGroups, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListGroups, i.Error(resp))
}
return result, nil
}
func (i *iam) RemoveGroup(ctx context.Context, params RemoveGroupRequest) error {
logger := i.Log(ctx)
logger.Debug("RemoveGroup")
if err := params.Validate(); err != nil {
return fmt.Errorf("%s: %w:\n%s", ErrRemoveGroup, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/identity-management/v3/user-admin/groups/%d", params.GroupID))
if err != nil {
return fmt.Errorf("%w: failed to create request: %s", ErrRemoveGroup, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, uri.String(), nil)
if err != nil {
return fmt.Errorf("%w: failed to create request: %s", ErrRemoveGroup, err)
}
resp, err := i.Exec(req, nil)
if err != nil {
return fmt.Errorf("%w: request failed: %s", ErrRemoveGroup, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("%s: %w", ErrRemoveGroup, i.Error(resp))
}
return nil
}
func (i *iam) UpdateGroupName(ctx context.Context, params GroupRequest) (*Group, error) {
logger := i.Log(ctx)
logger.Debug("UpdateGroupName")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w:\n%s", ErrUpdateGroupName, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/identity-management/v3/user-admin/groups/%d", params.GroupID))
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrUpdateGroupName, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrUpdateGroupName, err)
}
var result Group
resp, err := i.Exec(req, &result, params)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrUpdateGroupName, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrUpdateGroupName, i.Error(resp))
}
return &result, nil
}
func (i *iam) MoveGroup(ctx context.Context, params MoveGroupRequest) error {
logger := i.Log(ctx)
logger.Debug("MoveGroup")
if err := params.Validate(); err != nil {
return fmt.Errorf("%s: %w:\n%s", ErrMoveGroup, ErrStructValidation, err)
}
uri, err := url.Parse("/identity-management/v3/user-admin/groups/move")
if err != nil {
return fmt.Errorf("%w: failed to parse url: %s", ErrMoveGroup, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri.String(), nil)
if err != nil {
return fmt.Errorf("%w: failed to create request: %s", ErrMoveGroup, err)
}
resp, err := i.Exec(req, nil, params)
if err != nil {
return fmt.Errorf("%w: request failed: %s", ErrMoveGroup, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("%w: %s", ErrMoveGroup, i.Error(resp))
}
return nil
}