/
api_account.go
186 lines (155 loc) · 6.13 KB
/
api_account.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
package client
import (
"context"
"fmt"
"net/http"
)
// CreateAccount struct.
type CreateAccount struct {
Name string `json:"name"`
ID string `json:"id"`
Password string `json:"password"`
FullName string `json:"fullName,omitempty"`
IsActive bool `json:"isActive,omitempty"`
IsAdmin bool `json:"isAdmin,omitempty"`
IsOrg bool `json:"isOrg,omitempty"`
SearchLDAP bool `json:"searchLDAP,omitempty"`
}
// UpdateAccount struct.
type UpdateAccount struct {
FullName string `json:"fullName,omitempty"`
IsActive bool `json:"isActive,omitempty"`
IsAdmin bool `json:"isAdmin,omitempty"`
}
// ResponseAccount struct.
type ResponseAccount struct {
Name string `json:"name"`
ID string `json:"id"`
FullName string `json:"fullName,omitempty"`
IsActive bool `json:"isActive"`
IsAdmin bool `json:"isAdmin"`
IsOrg bool `json:"isOrg"`
IsImported bool `json:"isImported"`
OnDemand bool `json:"onDemand"`
OtpEnabled bool `json:"otpEnabled"`
MembersCount int `json:"membersCount"`
TeamsCount int `json:"teamsCount"`
}
// ResponseAccounts struct.
type ResponseAccounts struct {
UsersCount int `json:"usersCount"`
OrgsCount int `json:"orgsCount"`
ResourceCount int `json:"resourceCount"`
NextPageStart string `json:"nextPageStart"`
Accounts []ResponseAccount `json:"accounts"`
}
// Account filters enum.
type AccountFilter string
const (
AccountFilterUsers AccountFilter = "user"
AccountFilterOrgs AccountFilter = "orgs"
AccountFilterAdmins AccountFilter = "admins"
AccountFilterNonAdmins AccountFilter = "non-admins"
AccountFilterActiveUsers AccountFilter = "active-users"
AccountFilterInactiveUsers AccountFilter = "inactive-users"
URLTargetForAccounts = "accounts"
)
// APIFormOfFilter is a string readable form of the AccountFilters enum.
func (accF AccountFilter) APIFormOfFilter() string {
filters := [...]string{"users", "orgs", "admins", "non-admins", "active-users"}
x := string(accF)
for _, v := range filters {
if v == x {
return x
}
}
return "all"
}
// CreateAccount method - checking the MKE health endpoint.
func (c *Client) ApiCreateAccount(ctx context.Context, acc CreateAccount) (ResponseAccount, error) {
if (acc == CreateAccount{}) {
return ResponseAccount{}, fmt.Errorf("creating account failed. %w: %+v", ErrEmptyStruct, acc)
}
req, err := c.RequestFromTargetAndJSONBody(ctx, http.MethodPost, URLTargetForAccounts, acc)
if err != nil {
return ResponseAccount{}, fmt.Errorf("creating account %s failed. %w: %s", acc.Name, ErrRequestCreation, err)
}
resp, err := c.doAuthorizedRequest(req)
if err != nil {
return ResponseAccount{}, fmt.Errorf("creating account %s failed. %w", acc.Name, err)
}
resAcc := ResponseAccount{}
if err := resp.JSONMarshallBody(&resAcc); err != nil {
return ResponseAccount{}, fmt.Errorf("creating account %s failed. %w: %s", acc.Name, ErrUnmarshaling, err)
}
return resAcc, nil
}
// DeleteAccount deletes a user from in Enzi.
func (c *Client) ApiDeleteAccount(ctx context.Context, id string) error {
url := fmt.Sprintf("%s/%s", URLTargetForAccounts, id)
req, err := c.RequestFromTargetAndBytesBody(ctx, http.MethodDelete, url, []byte{})
if err != nil {
return fmt.Errorf("deleting account %s failed. %w: %s", id, ErrRequestCreation, err)
}
if _, err = c.doAuthorizedRequest(req); err != nil {
return fmt.Errorf("deleting account %s failed. %w", id, err)
}
return nil
}
// ReadAccount method retrieves a user from the enzi endpoint.
func (c *Client) ApiReadAccount(ctx context.Context, id string) (ResponseAccount, error) {
url := fmt.Sprintf("%s/%s", URLTargetForAccounts, id)
req, err := c.RequestFromTargetAndBytesBody(ctx, http.MethodGet, url, []byte{})
if err != nil {
return ResponseAccount{}, fmt.Errorf("reading account %s failed. %w: %s", id, ErrRequestCreation, err)
}
resp, err := c.doAuthorizedRequest(req)
if err != nil {
return ResponseAccount{}, fmt.Errorf("reading account %s failed. %w", id, err)
}
resAcc := ResponseAccount{}
if err := resp.JSONMarshallBody(&resAcc); err != nil {
return ResponseAccount{}, fmt.Errorf("reading account %s failed. %w: %s", id, ErrUnmarshaling, err)
}
return resAcc, nil
}
// UpdateAccount updates a user in the enzi endpoint.
func (c *Client) ApiUpdateAccount(ctx context.Context, id string, acc UpdateAccount) (ResponseAccount, error) {
url := fmt.Sprintf("%s/%s", URLTargetForAccounts, id)
req, err := c.RequestFromTargetAndJSONBody(ctx, http.MethodPatch, url, acc)
if err != nil {
return ResponseAccount{}, fmt.Errorf("updating account %s failed. %w: %s", id, ErrRequestCreation, err)
}
resp, err := c.doAuthorizedRequest(req)
if err != nil {
return ResponseAccount{}, fmt.Errorf("updating account %s failed. %w", id, err)
}
resAcc := ResponseAccount{}
if err := resp.JSONMarshallBody(&resAcc); err != nil {
return ResponseAccount{}, fmt.Errorf("updating account %s failed. %w: %s", id, ErrUnmarshaling, err)
}
return resAcc, nil
}
// ReadAccounts method retrieves all accounts depending on the filter passed from the enzi endpoint.
func (c *Client) ApiReadAccounts(ctx context.Context, accFilter AccountFilter) ([]ResponseAccount, error) {
// req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.createEnziUrl("accounts"), nil)
req, err := c.RequestFromTargetAndBytesBody(ctx, http.MethodGet, URLTargetForAccounts, []byte{})
if err != nil {
return []ResponseAccount{}, fmt.Errorf("reading accounts in bulk '%s' failed. %w: %s",
accFilter.APIFormOfFilter(), ErrRequestCreation, err)
}
q := req.URL.Query()
q.Add("filter", accFilter.APIFormOfFilter())
req.URL.RawQuery = q.Encode()
resp, err := c.doAuthorizedRequest(req)
if err != nil {
return []ResponseAccount{}, fmt.Errorf("reading accounts in bulk '%s' failed. %w",
accFilter.APIFormOfFilter(), err)
}
var accs ResponseAccounts
if err := resp.JSONMarshallBody(&accs); err != nil {
return []ResponseAccount{}, fmt.Errorf("reading accounts in bulk '%s' failed. %w: %s",
accFilter.APIFormOfFilter(), ErrUnmarshaling, err)
}
return accs.Accounts, nil
}