-
Notifications
You must be signed in to change notification settings - Fork 70
/
roles.go
354 lines (289 loc) · 10.6 KB
/
roles.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
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 (
// RoleRequest describes request parameters for the CreateRole and UpdateRole endpoints.
RoleRequest struct {
Name string `json:"roleName,omitempty"`
Description string `json:"roleDescription,omitempty"`
GrantedRoles []GrantedRoleID `json:"grantedRoles,omitempty"`
}
// CreateRoleRequest describes the request parameters for the CreateRole endpoint.
CreateRoleRequest RoleRequest
// GrantedRoleID describes a unique identifier for a granted role.
GrantedRoleID struct {
ID int64 `json:"grantedRoleId"`
}
// GetRoleRequest describes the request parameters for the GetRole endpoint.
GetRoleRequest struct {
ID int64
Actions bool
GrantedRoles bool
Users bool
}
// UpdateRoleRequest describes the request parameters for the UpdateRole endpoint.
// It works as patch request. You need to provide only fields which you want to update.
UpdateRoleRequest struct {
ID int64
RoleRequest
}
// DeleteRoleRequest describes the request parameters for the DeleteRole endpoint.
DeleteRoleRequest struct {
ID int64
}
// ListRolesRequest describes the request parameters for the ListRoles endpoint.
ListRolesRequest struct {
GroupID *int64
Actions bool
IgnoreContext bool
Users bool
}
// RoleAction encapsulates permissions available to the user for this role.
RoleAction struct {
Delete bool `json:"delete"`
Edit bool `json:"edit"`
}
// RoleGrantedRole is a list of granted roles, giving the user access to objects in a group.
RoleGrantedRole struct {
Description string `json:"grantedRoleDescription,omitempty"`
RoleID int64 `json:"grantedRoleId"`
RoleName string `json:"grantedRoleName"`
}
// RoleUser user who shares the same role.
RoleUser struct {
AccountID string `json:"accountId"`
Email string `json:"email"`
FirstName string `json:"firstName"`
LastLoginDate time.Time `json:"lastLoginDate"`
LastName string `json:"lastName"`
UIIdentityID string `json:"uiIdentityId"`
}
// Role encapsulates the response from the ListRoles endpoint.
Role struct {
Actions *RoleAction `json:"actions,omitempty"`
CreatedBy string `json:"createdBy"`
CreatedDate time.Time `json:"createdDate"`
GrantedRoles []RoleGrantedRole `json:"grantedRoles,omitempty"`
ModifiedBy string `json:"modifiedBy"`
ModifiedDate time.Time `json:"modifiedDate"`
RoleDescription string `json:"roleDescription"`
RoleID int64 `json:"roleId"`
RoleName string `json:"roleName"`
Users []RoleUser `json:"users,omitempty"`
RoleType RoleType `json:"type"`
}
// RoleType is an enum of role types.
RoleType string
)
// Validate validates CreateRoleRequest.
func (r CreateRoleRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"Name": validation.Validate(r.Name, validation.Required),
"Description": validation.Validate(r.Description, validation.Required),
"GrantedRoles": validation.Validate(r.GrantedRoles, validation.Required),
})
}
// Validate validates GetRoleRequest.
func (r GetRoleRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"ID": validation.Validate(r.ID, validation.Required),
})
}
// Validate validates UpdateRoleRequest.
func (r UpdateRoleRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"ID": validation.Validate(r.ID, validation.Required),
})
}
// Validate validates DeleteRoleRequest.
func (r DeleteRoleRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"ID": validation.Validate(r.ID, validation.Required),
})
}
var (
// RoleTypeStandard is a standard type provided by Akamai.
RoleTypeStandard RoleType = "standard"
// RoleTypeCustom is a custom role provided by the account.
RoleTypeCustom RoleType = "custom"
)
var (
// ErrCreateRole is returned when CreateRole fails.
ErrCreateRole = errors.New("create a role")
// ErrGetRole is returned when GetRole fails.
ErrGetRole = errors.New("get a role")
// ErrUpdateRole is returned when UpdateRole fails.
ErrUpdateRole = errors.New("update a role")
// ErrDeleteRole is returned when DeleteRole fails.
ErrDeleteRole = errors.New("delete a role")
// ErrListRoles is returned when ListRoles fails.
ErrListRoles = errors.New("list roles")
// ErrListGrantableRoles is returned when ListGrantableRoles fails.
ErrListGrantableRoles = errors.New("list grantable roles")
)
func (i *iam) CreateRole(ctx context.Context, params CreateRoleRequest) (*Role, error) {
logger := i.Log(ctx)
logger.Debug("CreateRole")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w:\n%s", ErrCreateRole, ErrStructValidation, err)
}
uri, err := url.Parse("/identity-management/v3/user-admin/roles")
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrCreateRole, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrCreateRole, err)
}
var result Role
resp, err := i.Exec(req, &result, params)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrCreateRole, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("%s: %w", ErrCreateRole, i.Error(resp))
}
return &result, nil
}
func (i *iam) GetRole(ctx context.Context, params GetRoleRequest) (*Role, error) {
logger := i.Log(ctx)
logger.Debug("GetRole")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w:\n%s", ErrGetRole, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/identity-management/v3/user-admin/roles/%d", params.ID))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrGetRole, err)
}
q := uri.Query()
q.Add("actions", strconv.FormatBool(params.Actions))
q.Add("grantedRoles", strconv.FormatBool(params.GrantedRoles))
q.Add("users", strconv.FormatBool(params.Users))
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", ErrGetRole, err)
}
var result Role
resp, err := i.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetRole, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrGetRole, i.Error(resp))
}
return &result, nil
}
func (i *iam) UpdateRole(ctx context.Context, params UpdateRoleRequest) (*Role, error) {
logger := i.Log(ctx)
logger.Debug("UpdateRole")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w:\n%s", ErrUpdateRole, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/identity-management/v3/user-admin/roles/%d", params.ID))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrUpdateRole, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrUpdateRole, err)
}
var result Role
resp, err := i.Exec(req, &result, params.RoleRequest)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrUpdateRole, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrUpdateRole, i.Error(resp))
}
return &result, nil
}
func (i *iam) DeleteRole(ctx context.Context, params DeleteRoleRequest) error {
logger := i.Log(ctx)
logger.Debug("DeleteRole")
if err := params.Validate(); err != nil {
return fmt.Errorf("%s: %w:\n%s", ErrDeleteRole, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/identity-management/v3/user-admin/roles/%d", params.ID))
if err != nil {
return fmt.Errorf("%w: failed to parse url: %s", ErrDeleteRole, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, uri.String(), nil)
if err != nil {
return fmt.Errorf("%w: failed to create request: %s", ErrDeleteRole, err)
}
resp, err := i.Exec(req, nil)
if err != nil {
return fmt.Errorf("%w: request failed: %s", ErrDeleteRole, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("%s: %w", ErrDeleteRole, i.Error(resp))
}
return nil
}
func (i *iam) ListRoles(ctx context.Context, params ListRolesRequest) ([]Role, error) {
logger := i.Log(ctx)
logger.Debug("ListRoles")
uri, err := url.Parse("/identity-management/v3/user-admin/roles")
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrListRoles, err)
}
q := uri.Query()
q.Add("actions", strconv.FormatBool(params.Actions))
q.Add("ignoreContext", strconv.FormatBool(params.IgnoreContext))
q.Add("users", strconv.FormatBool(params.Users))
if params.GroupID != nil {
q.Add("groupId", strconv.FormatInt(*params.GroupID, 10))
}
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", ErrListRoles, err)
}
var result []Role
resp, err := i.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListRoles, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListRoles, i.Error(resp))
}
return result, nil
}
func (i *iam) ListGrantableRoles(ctx context.Context) ([]RoleGrantedRole, error) {
logger := i.Log(ctx)
logger.Debug("ListGrantableRoles")
uri, err := url.Parse("/identity-management/v3/user-admin/roles/grantable-roles")
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrListGrantableRoles, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrListGrantableRoles, err)
}
var result []RoleGrantedRole
resp, err := i.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListGrantableRoles, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListGrantableRoles, i.Error(resp))
}
return result, nil
}