-
Notifications
You must be signed in to change notification settings - Fork 70
/
properties.go
419 lines (342 loc) · 13.5 KB
/
properties.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
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 (
// ListPropertiesRequest contains the request parameters for the ListProperties endpoint.
ListPropertiesRequest struct {
GroupID int64
Actions bool
}
// ListUsersForPropertyRequest contains the request parameters for the ListUsersForProperty endpoint.
ListUsersForPropertyRequest struct {
PropertyID int64
UserType PropertyUserType
}
// GetPropertyRequest contains the request parameters for the GetProperty endpoint.
GetPropertyRequest struct {
PropertyID int64
GroupID int64
}
// MapPropertyNameToIDRequest is the argument for MapPropertyNameToID.
MapPropertyNameToIDRequest string
// BlockUsersRequest contains the request parameters for the BlockUsers endpoint.
BlockUsersRequest struct {
PropertyID int64
Body BlockUsersRequestBody
}
// ListPropertiesResponse holds the response data from the ListProperties endpoint.
ListPropertiesResponse []Property
// ListUsersForPropertyResponse holds the response data from the ListUsersForProperty endpoint.
ListUsersForPropertyResponse []UsersForProperty
// GetPropertyResponse holds the response data from the GetProperty endpoint.
GetPropertyResponse struct {
ARLConfigFile string `json:"arlConfigFile"`
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"`
PropertyID int64 `json:"propertyId"`
PropertyName string `json:"propertyName"`
}
// BlockUsersResponse holds the response data from the BlockUsers endpoint.
BlockUsersResponse []UsersForProperty
// MovePropertyRequest contains the request parameters for the MoveProperty endpoint.
MovePropertyRequest struct {
PropertyID int64
Body MovePropertyRequestBody
}
// MovePropertyRequestBody contains body parameters for the MoveProperty endpoint.
MovePropertyRequestBody struct {
DestinationGroupID int64 `json:"destinationGroupId"`
SourceGroupID int64 `json:"sourceGroupId"`
}
// BlockUsersRequestBody hold the request body parameters for the BlockUsers endpoint.
BlockUsersRequestBody []BlockUserItem
// BlockUserItem contains body parameters for the BlockUsers endpoint.
BlockUserItem struct {
UIIdentityID string `json:"uiIdentityId"`
}
// Property holds the property details.
Property struct {
PropertyID int64 `json:"propertyId"`
PropertyName string `json:"propertyName"`
PropertyTypeDescription string `json:"propertyTypeDescription"`
GroupID int64 `json:"groupId"`
GroupName string `json:"groupName"`
Actions PropertyActions `json:"actions"`
}
// PropertyActions specifies activities available for the property.
PropertyActions struct {
Move bool `json:"move"`
}
// MapPropertyIDToNameRequest is the argument for MapPropertyIDToName.
MapPropertyIDToNameRequest struct {
PropertyID int64
GroupID int64
}
// UsersForProperty holds details about the users accessing the property.
UsersForProperty struct {
FirstName string `json:"firstName"`
IsBlocked bool `json:"isBlocked"`
LastName string `json:"lastName"`
UIIdentityID string `json:"uiIdentityId"`
UIUserName string `json:"uiUserName"`
}
// PropertyUserType filters property users based on their access to the property.
PropertyUserType string
)
const (
// PropertyUserTypeAll selects all property users.
PropertyUserTypeAll PropertyUserType = "all"
// PropertyUserTypeAssigned selects users that have access to the property.
PropertyUserTypeAssigned PropertyUserType = "assigned"
// PropertyUserTypeBlocked selects users whose access to the property is blocked.
PropertyUserTypeBlocked PropertyUserType = "blocked"
)
// Validate validates PropertyUserType.
func (p PropertyUserType) Validate() error {
return validation.In(PropertyUserTypeAll, PropertyUserTypeAssigned, PropertyUserTypeBlocked).
Error(fmt.Sprintf("value '%s' is invalid. Must be one of: '%s', '%s' or '%s'",
p, PropertyUserTypeAll, PropertyUserTypeAssigned, PropertyUserTypeBlocked)).
Validate(p)
}
// Validate validates ListUsersForPropertyRequest.
func (r ListUsersForPropertyRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"PropertyID": validation.Validate(r.PropertyID, validation.Required),
"UserType": r.UserType.Validate(),
})
}
// Validate validates GetPropertyRequest.
func (r GetPropertyRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"PropertyID": validation.Validate(r.PropertyID, validation.Required),
"GroupID": validation.Validate(r.GroupID, validation.Required),
})
}
// Validate validates MapPropertyIDToNameRequest.
func (r MapPropertyIDToNameRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"PropertyID": validation.Validate(r.PropertyID, validation.Required),
"GroupID": validation.Validate(r.GroupID, validation.Required),
})
}
// Validate validates MovePropertyRequest.
func (r MovePropertyRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"PropertyID": validation.Validate(r.PropertyID, validation.Required),
"Body": validation.Validate(r.Body, validation.Required),
})
}
// Validate validates MovePropertyRequestBody.
func (r MovePropertyRequestBody) Validate() error {
return validation.Errors{
"DestinationGroupID": validation.Validate(r.DestinationGroupID, validation.Required),
"SourceGroupID": validation.Validate(r.SourceGroupID, validation.Required),
}.Filter()
}
// Validate validates BlockUsersRequest.
func (r BlockUsersRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"PropertyID": validation.Validate(r.PropertyID, validation.Required),
"Body": validation.Validate(r.Body, validation.Required),
})
}
// Validate validates BlockUserItem.
func (r BlockUserItem) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"UIIdentityID": validation.Validate(r.UIIdentityID, validation.Required),
})
}
var (
// ErrListProperties is returned when ListProperties fails.
ErrListProperties = errors.New("list properties")
// ErrListUsersForProperty is returned when ListUsersForProperty fails.
ErrListUsersForProperty = errors.New("list users for property")
// ErrGetProperty is returned when GetProperty fails.
ErrGetProperty = errors.New("get property")
// ErrMoveProperty is returned when MoveProperty fails.
ErrMoveProperty = errors.New("move property")
// ErrMapPropertyIDToName is returned when MapPropertyIDToName fails.
ErrMapPropertyIDToName = errors.New("map property by id")
// ErrMapPropertyNameToID is returned when MapPropertyNameToID fails.
ErrMapPropertyNameToID = errors.New("map property by name")
// ErrNoProperty is returned when MapPropertyNameToID did not find given property.
ErrNoProperty = errors.New("no such property")
// ErrBlockUsers is returned when BlockUsers fails.
ErrBlockUsers = errors.New("block users")
)
func (i *iam) ListProperties(ctx context.Context, params ListPropertiesRequest) (ListPropertiesResponse, error) {
logger := i.Log(ctx)
logger.Debug("ListProperties")
uri, err := url.Parse("/identity-management/v3/user-admin/properties")
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrListProperties, err)
}
q := uri.Query()
q.Add("actions", strconv.FormatBool(params.Actions))
if params.GroupID != 0 {
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", ErrListProperties, err)
}
var result ListPropertiesResponse
resp, err := i.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListProperties, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListProperties, i.Error(resp))
}
return result, nil
}
func (i *iam) ListUsersForProperty(ctx context.Context, params ListUsersForPropertyRequest) (ListUsersForPropertyResponse, error) {
logger := i.Log(ctx)
logger.Debug("ListUsersForProperty")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w:\n%s", ErrListUsersForProperty, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/identity-management/v3/user-admin/properties/%d/users", params.PropertyID))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrListUsersForProperty, err)
}
if params.UserType != "" {
q := uri.Query()
q.Add("userType", string(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", ErrListUsersForProperty, err)
}
var result ListUsersForPropertyResponse
resp, err := i.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListUsersForProperty, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListUsersForProperty, i.Error(resp))
}
return result, nil
}
func (i *iam) GetProperty(ctx context.Context, params GetPropertyRequest) (*GetPropertyResponse, error) {
logger := i.Log(ctx)
logger.Debug("GetProperty")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w:\n%s", ErrGetProperty, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/identity-management/v3/user-admin/properties/%d", params.PropertyID))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrGetProperty, err)
}
q := uri.Query()
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", ErrGetProperty, err)
}
var result GetPropertyResponse
resp, err := i.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetProperty, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrGetProperty, i.Error(resp))
}
return &result, nil
}
func (i *iam) MoveProperty(ctx context.Context, params MovePropertyRequest) error {
logger := i.Log(ctx)
logger.Debug("MoveProperty")
if err := params.Validate(); err != nil {
return fmt.Errorf("%s: %w: %s", ErrMoveProperty, ErrStructValidation, err)
}
uri := fmt.Sprintf("/identity-management/v3/user-admin/properties/%d", params.PropertyID)
req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
if err != nil {
return fmt.Errorf("%w: failed to create request: %s", ErrMoveProperty, err)
}
resp, err := i.Exec(req, nil, params.Body)
if err != nil {
return fmt.Errorf("%w: request failed: %s", ErrMoveProperty, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("%s: %w", ErrMoveProperty, i.Error(resp))
}
return nil
}
func (i *iam) MapPropertyIDToName(ctx context.Context, params MapPropertyIDToNameRequest) (*string, error) {
logger := i.Log(ctx)
logger.Debug("MapPropertyIDToName")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w:\n%s", ErrMapPropertyIDToName, ErrStructValidation, err)
}
req := GetPropertyRequest{
PropertyID: params.PropertyID,
GroupID: params.GroupID,
}
property, err := i.GetProperty(ctx, req)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrMapPropertyIDToName, err)
}
return &property.PropertyName, nil
}
func (i *iam) MapPropertyNameToID(ctx context.Context, name MapPropertyNameToIDRequest) (*int64, error) {
logger := i.Log(ctx)
logger.Debug("MapPropertyNameToID")
if name == "" {
return nil, fmt.Errorf("%s: %w:\n name cannot be blank", ErrMapPropertyNameToID, ErrStructValidation)
}
properties, err := i.ListProperties(ctx, ListPropertiesRequest{})
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrMapPropertyNameToID, err)
}
for _, property := range properties {
if property.PropertyName == string(name) {
return &property.PropertyID, nil
}
}
return nil, fmt.Errorf("%w: %s", ErrNoProperty, name)
}
func (i *iam) BlockUsers(ctx context.Context, params BlockUsersRequest) (*BlockUsersResponse, error) {
logger := i.Log(ctx)
logger.Debug("BlockUsers")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrBlockUsers, ErrStructValidation, err)
}
uri := fmt.Sprintf("/identity-management/v3/user-admin/properties/%d/users/block", params.PropertyID)
req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrBlockUsers, err)
}
var result BlockUsersResponse
resp, err := i.Exec(req, &result, params.Body)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrBlockUsers, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrBlockUsers, i.Error(resp))
}
return &result, nil
}