-
Notifications
You must be signed in to change notification settings - Fork 69
/
client_list.go
444 lines (373 loc) · 13.5 KB
/
client_list.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package clientlists
import (
"context"
"fmt"
"net/http"
"net/url"
"strconv"
"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 (
// ClientListType represents client list type
ClientListType string
// GetClientListsRequest contains request parameters for GetClientLists method
GetClientListsRequest struct {
Type []ClientListType
Name string
Search string
IncludeItems bool
IncludeDeprecated bool
IncludeNetworkList bool
Page *int
PageSize *int
Sort []string
}
// GetClientListsResponse contains response parameters from GetClientLists method
GetClientListsResponse struct {
Content []ClientList
}
// ClientList contains list content and items
ClientList struct {
ListContent
Items []ListItemContent
}
// ListContent contains list content
ListContent struct {
Name string `json:"name"`
Type ClientListType `json:"type"`
Notes string `json:"notes"`
Tags []string `json:"tags"`
ListID string `json:"listId"`
Version int64 `json:"version"`
ItemsCount int64 `json:"itemsCount"`
CreateDate string `json:"createDate"`
CreatedBy string `json:"createdBy"`
UpdateDate string `json:"updateDate"`
UpdatedBy string `json:"updatedBy"`
ProductionActivationStatus string `json:"productionActivationStatus"`
StagingActivationStatus string `json:"stagingActivationStatus"`
ProductionActiveVersion int64 `json:"productionActiveVersion"`
StagingActiveVersion int64 `json:"stagingActiveVersion"`
ListType string `json:"listType"`
Shared bool `json:"shared"`
ReadOnly bool `json:"readOnly"`
Deprecated bool `json:"deprecated"`
}
// ListItemContent contains client list item information
ListItemContent struct {
Value string `json:"value"`
Tags []string `json:"tags"`
Description string `json:"description"`
ExpirationDate string `json:"expirationDate"`
CreateDate string `json:"createDate"`
CreatedBy string `json:"createdBy"`
CreatedVersion int64 `json:"createdVersion"`
ProductionStatus string `json:"productionStatus"`
StagingStatus string `json:"stagingStatus"`
Type ClientListType `json:"type"`
UpdateDate string `json:"updateDate"`
UpdatedBy string `json:"updatedBy"`
}
// ListItemPayload contains item's editable fields to use as update/create/delete payload
ListItemPayload struct {
Value string `json:"value"`
Tags []string `json:"tags"`
Description string `json:"description"`
ExpirationDate string `json:"expirationDate"`
}
// GetClientListRequest contains request params for GetClientList method
GetClientListRequest struct {
ListID string
IncludeItems bool
}
// GetClientListResponse contains response from GetClientList method
GetClientListResponse struct {
ListContent
ContractID string `json:"contractId"`
GroupID int64 `json:"groupId"`
GroupName string `json:"groupName"`
Items []ListItemContent `json:"items"`
}
// CreateClientListRequest contains request params for CreateClientList method
CreateClientListRequest struct {
ContractID string `json:"contractId"`
GroupID int64 `json:"groupId"`
Name string `json:"name"`
Type ClientListType `json:"type"`
Notes string `json:"notes"`
Tags []string `json:"tags"`
Items []ListItemPayload `json:"items"`
}
// CreateClientListResponse contains response from CreateClientList method
CreateClientListResponse GetClientListResponse
// UpdateClientListRequest contains request params for UpdateClientList method
UpdateClientListRequest struct {
UpdateClientList
ListID string
}
// UpdateClientList contains the body of client list update request
UpdateClientList struct {
Name string `json:"name"`
Notes string `json:"notes"`
Tags []string `json:"tags"`
}
// UpdateClientListResponse contains response from UpdateClientList method
UpdateClientListResponse struct {
ListContent
ContractID string `json:"contractId"`
GroupName string `json:"groupName"`
GroupID int64 `json:"groupId"`
}
// UpdateClientListItemsRequest contains request params for UpdateClientListItems method
UpdateClientListItemsRequest struct {
UpdateClientListItems
ListID string
}
// UpdateClientListItems contains the body of client list items update request
UpdateClientListItems struct {
Append []ListItemPayload `json:"append"`
Update []ListItemPayload `json:"update"`
Delete []ListItemPayload `json:"delete"`
}
// UpdateClientListItemsResponse contains response from UpdateClientListItems method
UpdateClientListItemsResponse struct {
Appended []ListItemContent `json:"appended"`
Updated []ListItemContent `json:"updated"`
Deleted []ListItemContent `json:"deleted"`
}
// DeleteClientListRequest contains request params for DeleteClientList method
DeleteClientListRequest struct {
ListID string
}
)
func (p *clientlists) GetClientLists(ctx context.Context, params GetClientListsRequest) (*GetClientListsResponse, error) {
logger := p.Log(ctx)
logger.Debug("GetClientLists")
if err := params.validate(); err != nil {
return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
}
uri, err := url.Parse("/client-list/v1/lists")
if err != nil {
return nil, fmt.Errorf("Error parsing URL: %s", err.Error())
}
q := uri.Query()
if params.Name != "" {
q.Add("name", params.Name)
}
if params.Type != nil {
for _, v := range params.Type {
q.Add("type", string(v))
}
}
if params.Search != "" {
q.Add("search", params.Search)
}
if params.IncludeItems {
q.Add("includeItems", strconv.FormatBool(params.IncludeItems))
}
if params.IncludeDeprecated {
q.Add("includeDeprecated", strconv.FormatBool(params.IncludeDeprecated))
}
if params.IncludeNetworkList {
q.Add("includeNetworkList", strconv.FormatBool(params.IncludeNetworkList))
}
if params.Page != nil {
q.Add("page", fmt.Sprintf("%d", *params.Page))
}
if params.PageSize != nil {
q.Add("pageSize", fmt.Sprintf("%d", *params.PageSize))
}
if params.Sort != nil {
for _, v := range params.Sort {
q.Add("sort", string(v))
}
}
uri.RawQuery = q.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("failed to create getClientLists request: %s", err.Error())
}
var rval GetClientListsResponse
resp, err := p.Exec(req, &rval)
if err != nil {
return nil, fmt.Errorf("getClientLists request failed: %s", err.Error())
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, p.Error(resp)
}
return &rval, nil
}
func (p *clientlists) GetClientList(ctx context.Context, params GetClientListRequest) (*GetClientListResponse, error) {
logger := p.Log(ctx)
logger.Debug("GetClientList")
if err := params.validate(); err != nil {
return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
}
uri, err := url.Parse(fmt.Sprintf("/client-list/v1/lists/%s", params.ListID))
if err != nil {
return nil, fmt.Errorf("failed to parse url: %w", err)
}
q := uri.Query()
if params.IncludeItems {
q.Add("includeItems", strconv.FormatBool(params.IncludeItems))
}
uri.RawQuery = q.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("failed to create getClientList request: %s", err.Error())
}
var rval GetClientListResponse
resp, err := p.Exec(req, &rval)
if err != nil {
return nil, fmt.Errorf("getClientList request failed: %s", err.Error())
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, p.Error(resp)
}
return &rval, nil
}
func (p *clientlists) UpdateClientList(ctx context.Context, params UpdateClientListRequest) (*UpdateClientListResponse, error) {
logger := p.Log(ctx)
logger.Debug("UpdateClientList")
if err := params.validate(); err != nil {
return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
}
uri := fmt.Sprintf("/client-list/v1/lists/%s", params.ListID)
req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
if err != nil {
return nil, fmt.Errorf("failed to create updateClientList request: %s", err.Error())
}
var rval UpdateClientListResponse
resp, err := p.Exec(req, &rval, ¶ms.UpdateClientList)
if err != nil {
return nil, fmt.Errorf("updateClientList request failed: %s", err.Error())
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, p.Error(resp)
}
return &rval, nil
}
func (p *clientlists) UpdateClientListItems(ctx context.Context, params UpdateClientListItemsRequest) (*UpdateClientListItemsResponse, error) {
logger := p.Log(ctx)
logger.Debug("UpdateClientListItems")
if err := params.validate(); err != nil {
return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
}
uri := fmt.Sprintf("/client-list/v1/lists/%s/items", params.ListID)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, nil)
if err != nil {
return nil, fmt.Errorf("failed to create UpdateClientListItems request: %s", err.Error())
}
var rval UpdateClientListItemsResponse
resp, err := p.Exec(req, &rval, ¶ms.UpdateClientListItems)
if err != nil {
return nil, fmt.Errorf("UpdateClientListItems request failed: %s", err.Error())
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, p.Error(resp)
}
return &rval, nil
}
func (p *clientlists) CreateClientList(ctx context.Context, params CreateClientListRequest) (*CreateClientListResponse, error) {
logger := p.Log(ctx)
logger.Debug("CreateClientList")
if err := params.validate(); err != nil {
return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, "/client-list/v1/lists", nil)
if err != nil {
return nil, fmt.Errorf("failed to create createClientList request: %s", err.Error())
}
var rval CreateClientListResponse
resp, err := p.Exec(req, &rval, ¶ms)
if err != nil {
return nil, fmt.Errorf("createClientList request failed: %s", err.Error())
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusCreated {
return nil, p.Error(resp)
}
return &rval, nil
}
func (p *clientlists) DeleteClientList(ctx context.Context, params DeleteClientListRequest) error {
logger := p.Log(ctx)
logger.Debug("DeleteClientList")
if err := params.validate(); err != nil {
return fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
}
uri := fmt.Sprintf("%s/%s", "/client-list/v1/lists", params.ListID)
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return fmt.Errorf("failed to create deleteClientList request: %s", err.Error())
}
resp, err := p.Exec(req, nil)
if err != nil {
return fmt.Errorf("deleteClientList request failed: %s", err.Error())
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusNoContent {
return p.Error(resp)
}
return nil
}
func (v GetClientListRequest) validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"ListID": validation.Validate(v.ListID, validation.Required),
})
}
func (v UpdateClientListRequest) validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"ListID": validation.Validate(v.ListID, validation.Required),
"Name": validation.Validate(v.ListID, validation.Required),
})
}
func (v UpdateClientListItemsRequest) validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"ListID": validation.Validate(v.ListID, validation.Required),
})
}
func (v CreateClientListRequest) validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"Name": validation.Validate(v.Name, validation.Required),
"Type": validation.Validate(v.Type, validation.Required),
})
}
func (v DeleteClientListRequest) validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"ListID": validation.Validate(v.ListID, validation.Required),
})
}
func (v GetClientListsRequest) validate() error {
listTypes := getValidListTypesAsInterface()
return edgegriderr.ParseValidationErrors(validation.Errors{
"Type": validation.Validate(v.Type, validation.Each(validation.In(listTypes...).Error(
fmt.Sprintf("Invalid 'type' value(s) provided. Valid values are: %s", listTypes)))),
})
}
const (
// IP for ip type list type
IP ClientListType = "IP"
// GEO for geo/countries list type
GEO ClientListType = "GEO"
// ASN for AS Number list type
ASN ClientListType = "ASN"
// TLSFingerprint for TLS Fingerprint list type
TLSFingerprint ClientListType = "TLS_FINGERPRINT"
// FileHash for file hash type list
FileHash ClientListType = "FILE_HASH"
)
func getValidListTypesAsInterface() []interface{} {
return []interface{}{
IP,
GEO,
ASN,
TLSFingerprint,
FileHash,
}
}