/
support.go
271 lines (208 loc) · 8.04 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
package iam
import (
"context"
"errors"
"fmt"
"net/http"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
type (
// Support is a list of IAM supported objects API interfaces
Support interface {
// ListProducts lists products a user can subscribe to and receive notifications for on the account
//
// See: https://techdocs.akamai.com/iam-user-admin/reference/get-common-notification-products
ListProducts(context.Context) ([]string, error)
// ListStates lists U.S. states or Canadian provinces
//
// See: https://techdocs.akamai.com/iam-user-admin/reference/get-common-states
ListStates(context.Context, ListStatesRequest) ([]string, error)
// ListTimeoutPolicies lists all the possible session timeout policies
//
// See: https://techdocs.akamai.com/iam-user-admin/reference/get-common-timeout-policies
ListTimeoutPolicies(context.Context) ([]TimeoutPolicy, error)
// SupportedContactTypes lists supported contact types
//
// See: https://techdocs.akamai.com/iam-user-admin/reference/get-common-contact-types
SupportedContactTypes(context.Context) ([]string, error)
// SupportedCountries lists supported countries
//
// See: https://techdocs.akamai.com/iam-user-admin/reference/get-common-countries
SupportedCountries(context.Context) ([]string, error)
// SupportedLanguages lists supported languages
//
// See: https://techdocs.akamai.com/iam-user-admin/reference/get-common-languages
SupportedLanguages(context.Context) ([]string, error)
// SupportedTimezones lists supported timezones
//
// See: https://techdocs.akamai.com/iam-user-admin/reference/get-common-timezones
SupportedTimezones(context.Context) ([]Timezone, error)
}
// TimeoutPolicy encapsulates the response of the list timeout policies endpoint
TimeoutPolicy struct {
Name string `json:"name"`
Value int64 `json:"value"`
}
// ListStatesRequest contains the country request parameter for the list states endpoint
ListStatesRequest struct {
Country string
}
// Timezone contains the response of the list supported timezones endpoint
Timezone struct {
Description string `json:"description"`
Offset string `json:"offset"`
Posix string `json:"posix"`
Timezone string `json:"timezone"`
}
)
var (
// 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")
// 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 validation.Errors{
"country": validation.Validate(r.Country, validation.Required),
}.Filter()
}
func (i *iam) ListProducts(ctx context.Context) ([]string, error) {
logger := i.Log(ctx)
logger.Debug("ListProducts")
getURL := "/identity-management/v2/user-admin/common/notification-products"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrListProducts, err)
}
var rval []string
resp, err := i.Exec(req, &rval)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListProducts, err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListProducts, i.Error(resp))
}
return rval, 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)
}
getURL := fmt.Sprintf("/identity-management/v2/user-admin/common/countries/%s/states", params.Country)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrListStates, err)
}
var rval []string
resp, err := i.Exec(req, &rval)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListStates, err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListStates, i.Error(resp))
}
return rval, nil
}
func (i *iam) ListTimeoutPolicies(ctx context.Context) ([]TimeoutPolicy, error) {
logger := i.Log(ctx)
logger.Debug("ListTimeoutPolicies")
getURL := "/identity-management/v2/user-admin/common/timeout-policies"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrListTimeoutPolicies, err)
}
var rval []TimeoutPolicy
resp, err := i.Exec(req, &rval)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListTimeoutPolicies, err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListTimeoutPolicies, i.Error(resp))
}
return rval, nil
}
func (i *iam) SupportedContactTypes(ctx context.Context) ([]string, error) {
logger := i.Log(ctx)
logger.Debug("SupportedContactTypes")
getURL := "/identity-management/v2/user-admin/common/contact-types"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrSupportedContactTypes, err)
}
var rval []string
resp, err := i.Exec(req, &rval)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrSupportedContactTypes, err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrSupportedContactTypes, i.Error(resp))
}
return rval, nil
}
func (i *iam) SupportedCountries(ctx context.Context) ([]string, error) {
logger := i.Log(ctx)
logger.Debug("SupportedCountries")
getURL := "/identity-management/v2/user-admin/common/countries"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrSupportedCountries, err)
}
var rval []string
resp, err := i.Exec(req, &rval)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrSupportedCountries, err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrSupportedCountries, i.Error(resp))
}
return rval, nil
}
func (i *iam) SupportedLanguages(ctx context.Context) ([]string, error) {
logger := i.Log(ctx)
logger.Debug("SupportedLanguages")
getURL := "/identity-management/v2/user-admin/common/supported-languages"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrSupportedLanguages, err)
}
var rval []string
resp, err := i.Exec(req, &rval)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrSupportedLanguages, err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrSupportedLanguages, i.Error(resp))
}
return rval, nil
}
func (i *iam) SupportedTimezones(ctx context.Context) ([]Timezone, error) {
logger := i.Log(ctx)
logger.Debug("SupportedTimezones")
getURL := "/identity-management/v2/user-admin/common/timezones"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrSupportedTimezones, err)
}
var rval []Timezone
resp, err := i.Exec(req, &rval)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrSupportedTimezones, err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrSupportedTimezones, i.Error(resp))
}
return rval, nil
}