github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/iam/user_lock.go (about) 1 package iam 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "net/http" 8 "net/url" 9 10 validation "github.com/go-ozzo/ozzo-validation/v4" 11 ) 12 13 type ( 14 // UserLock is the IAM user lock/unlock API interface 15 UserLock interface { 16 // LockUser lock the user 17 // 18 // See: https://techdocs.akamai.com/iam-user-admin/reference/post-ui-identity-lock 19 LockUser(context.Context, LockUserRequest) error 20 21 // UnlockUser release the lock on a user's account 22 // 23 // See: https://techdocs.akamai.com/iam-user-admin/reference/post-ui-identity-unlock 24 UnlockUser(context.Context, UnlockUserRequest) error 25 } 26 27 // LockUserRequest contains the request parameters of the lock user endpoint 28 LockUserRequest struct { 29 IdentityID string 30 } 31 32 // UnlockUserRequest contains the request parameters of the unlock user endpoint 33 UnlockUserRequest struct { 34 IdentityID string 35 } 36 ) 37 38 var ( 39 // ErrLockUser is returned when LockUser fails 40 ErrLockUser = errors.New("lock user") 41 42 // ErrUnlockUser is returned when UnlockUser fails 43 ErrUnlockUser = errors.New("unlock user") 44 ) 45 46 // Validate validates LockUserRequest 47 func (r LockUserRequest) Validate() error { 48 return validation.Errors{ 49 "IdentityID": validation.Validate(r.IdentityID, validation.Required), 50 }.Filter() 51 } 52 53 // Validate validates UnlockUserRequest 54 func (r UnlockUserRequest) Validate() error { 55 return validation.Errors{ 56 "IdentityID": validation.Validate(r.IdentityID, validation.Required), 57 }.Filter() 58 } 59 60 func (i *iam) LockUser(ctx context.Context, params LockUserRequest) error { 61 if err := params.Validate(); err != nil { 62 return fmt.Errorf("%s: %w:\n%s", ErrLockUser, ErrStructValidation, err) 63 } 64 65 u, err := url.Parse(fmt.Sprintf("/identity-management/v2/user-admin/ui-identities/%s/lock", params.IdentityID)) 66 if err != nil { 67 return fmt.Errorf("%w: failed to create request: %s", ErrLockUser, err) 68 } 69 70 req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), nil) 71 if err != nil { 72 return fmt.Errorf("%w: failed to create request: %s", ErrLockUser, err) 73 } 74 75 resp, err := i.Exec(req, nil) 76 if err != nil { 77 return fmt.Errorf("%w: request failed: %s", ErrLockUser, err) 78 } 79 80 if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent { 81 return fmt.Errorf("%s: %w", ErrLockUser, i.Error(resp)) 82 } 83 84 return nil 85 } 86 87 func (i *iam) UnlockUser(ctx context.Context, params UnlockUserRequest) error { 88 if err := params.Validate(); err != nil { 89 return fmt.Errorf("%s: %w:\n%s", ErrUnlockUser, ErrStructValidation, err) 90 } 91 92 u, err := url.Parse(fmt.Sprintf("/identity-management/v2/user-admin/ui-identities/%s/unlock", params.IdentityID)) 93 if err != nil { 94 return fmt.Errorf("%w: failed to create request: %s", ErrUnlockUser, err) 95 } 96 97 req, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), nil) 98 if err != nil { 99 return fmt.Errorf("%w: failed to create request: %s", ErrUnlockUser, err) 100 } 101 102 resp, err := i.Exec(req, nil) 103 if err != nil { 104 return fmt.Errorf("%w: request failed: %s", ErrUnlockUser, err) 105 } 106 107 if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent { 108 return fmt.Errorf("%s: %w", ErrUnlockUser, i.Error(resp)) 109 } 110 111 return nil 112 }