-
Notifications
You must be signed in to change notification settings - Fork 70
/
support.go
340 lines (264 loc) · 10.1 KB
/
support.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
package iam
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"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 (
// GetPasswordPolicyResponse holds the response data from the GetPasswordPolicy endpoint.
GetPasswordPolicyResponse struct {
CaseDiff int64 `json:"caseDif"`
MaxRepeating int64 `json:"maxRepeating"`
MinDigits int64 `json:"minDigits"`
MinLength int64 `json:"minLength"`
MinLetters int64 `json:"minLetters"`
MinNonAlpha int64 `json:"minNonAlpha"`
MinReuse int64 `json:"minReuse"`
PwClass string `json:"pwclass"`
RotateFrequency int64 `json:"rotateFrequency"`
}
// TimeoutPolicy encapsulates the response from the ListTimeoutPolicies endpoint.
TimeoutPolicy struct {
Name string `json:"name"`
Value int64 `json:"value"`
}
// ListStatesRequest contains the country request parameter for the ListStates endpoint.
ListStatesRequest struct {
Country string
}
// ListAccountSwitchKeysRequest contains the request parameters for the ListAccountSwitchKeys endpoint.
ListAccountSwitchKeysRequest struct {
ClientID string
Search string
}
// ListAccountSwitchKeysResponse holds the response data from the ListAccountSwitchKeys endpoint.
ListAccountSwitchKeysResponse []AccountSwitchKey
// AccountSwitchKey contains information about account switch key.
AccountSwitchKey struct {
AccountName string `json:"accountName"`
AccountSwitchKey string `json:"accountSwitchKey"`
}
// Timezone contains the response from the ListSupportedTimezones endpoint.
Timezone struct {
Description string `json:"description"`
Offset string `json:"offset"`
Posix string `json:"posix"`
Timezone string `json:"timezone"`
}
)
var (
// ErrGetPasswordPolicy is returned when GetPasswordPolicy fails.
ErrGetPasswordPolicy = errors.New("get password policy")
// ErrListProducts is returned when ListProducts fails.
ErrListProducts = errors.New("list products")
// ErrListStates is returned when ListStates fails.
ErrListStates = errors.New("list states")
// ErrListTimeoutPolicies is returned when ListTimeoutPolicies fails.
ErrListTimeoutPolicies = errors.New("list timeout policies")
// ErrListAccountSwitchKeys is returned when ListAccountSwitchKeys fails.
ErrListAccountSwitchKeys = errors.New("list account switch keys")
// ErrSupportedContactTypes is returned when SupportedContactTypes fails.
ErrSupportedContactTypes = errors.New("supported contact types")
// ErrSupportedCountries is returned when SupportedCountries fails.
ErrSupportedCountries = errors.New("supported countries")
// ErrSupportedLanguages is returned when SupportedLanguages fails.
ErrSupportedLanguages = errors.New("supported languages")
// ErrSupportedTimezones is returned when SupportedTimezones fails.
ErrSupportedTimezones = errors.New("supported timezones")
)
// Validate validates ListStatesRequest.
func (r ListStatesRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"Country": validation.Validate(r.Country, validation.Required),
})
}
func (i *iam) GetPasswordPolicy(ctx context.Context) (*GetPasswordPolicyResponse, error) {
logger := i.Log(ctx)
logger.Debug("GetPasswordPolicy")
uri := "/identity-management/v3/user-admin/common/password-policy"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetPasswordPolicy, err)
}
var result GetPasswordPolicyResponse
resp, err := i.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetPasswordPolicy, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrGetPasswordPolicy, i.Error(resp))
}
return &result, nil
}
func (i *iam) ListProducts(ctx context.Context) ([]string, error) {
logger := i.Log(ctx)
logger.Debug("ListProducts")
uri := "/identity-management/v3/user-admin/common/notification-products"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrListProducts, err)
}
var result []string
resp, err := i.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListProducts, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListProducts, i.Error(resp))
}
return result, nil
}
func (i *iam) ListStates(ctx context.Context, params ListStatesRequest) ([]string, error) {
logger := i.Log(ctx)
logger.Debug("ListStates")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w:\n%s", ErrListStates, ErrStructValidation, err)
}
uri := fmt.Sprintf("/identity-management/v3/user-admin/common/countries/%s/states", params.Country)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrListStates, err)
}
var result []string
resp, err := i.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListStates, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListStates, i.Error(resp))
}
return result, nil
}
func (i *iam) ListTimeoutPolicies(ctx context.Context) ([]TimeoutPolicy, error) {
logger := i.Log(ctx)
logger.Debug("ListTimeoutPolicies")
uri := "/identity-management/v3/user-admin/common/timeout-policies"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrListTimeoutPolicies, err)
}
var result []TimeoutPolicy
resp, err := i.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListTimeoutPolicies, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListTimeoutPolicies, i.Error(resp))
}
return result, nil
}
func (i *iam) ListAccountSwitchKeys(ctx context.Context, params ListAccountSwitchKeysRequest) (ListAccountSwitchKeysResponse, error) {
logger := i.Log(ctx)
logger.Debug("ListAccountSwitchKeys")
if params.ClientID == "" {
params.ClientID = "self"
}
uri, err := url.Parse(fmt.Sprintf("/identity-management/v3/api-clients/%s/account-switch-keys", params.ClientID))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrListAccountSwitchKeys, err)
}
if params.Search != "" {
q := uri.Query()
q.Add("search", params.Search)
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", ErrListAccountSwitchKeys, err)
}
var result ListAccountSwitchKeysResponse
resp, err := i.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListAccountSwitchKeys, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListAccountSwitchKeys, i.Error(resp))
}
return result, nil
}
func (i *iam) SupportedContactTypes(ctx context.Context) ([]string, error) {
logger := i.Log(ctx)
logger.Debug("SupportedContactTypes")
uri := "/identity-management/v3/user-admin/common/contact-types"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrSupportedContactTypes, err)
}
var result []string
resp, err := i.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrSupportedContactTypes, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrSupportedContactTypes, i.Error(resp))
}
return result, nil
}
func (i *iam) SupportedCountries(ctx context.Context) ([]string, error) {
logger := i.Log(ctx)
logger.Debug("SupportedCountries")
uri := "/identity-management/v3/user-admin/common/countries"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrSupportedCountries, err)
}
var result []string
resp, err := i.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrSupportedCountries, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrSupportedCountries, i.Error(resp))
}
return result, nil
}
func (i *iam) SupportedLanguages(ctx context.Context) ([]string, error) {
logger := i.Log(ctx)
logger.Debug("SupportedLanguages")
uri := "/identity-management/v3/user-admin/common/supported-languages"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrSupportedLanguages, err)
}
var result []string
resp, err := i.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrSupportedLanguages, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrSupportedLanguages, i.Error(resp))
}
return result, nil
}
func (i *iam) SupportedTimezones(ctx context.Context) ([]Timezone, error) {
logger := i.Log(ctx)
logger.Debug("SupportedTimezones")
uri := "/identity-management/v3/user-admin/common/timezones"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrSupportedTimezones, err)
}
var result []Timezone
resp, err := i.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrSupportedTimezones, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrSupportedTimezones, i.Error(resp))
}
return result, nil
}