-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathinclude.go
439 lines (361 loc) · 13.9 KB
/
include.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
package papi
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 (
// ListIncludesRequest contains parameters used to list includes
ListIncludesRequest struct {
ContractID string
GroupID string
}
// ListIncludesResponse represents a response object returned by ListIncludes
ListIncludesResponse struct {
Includes IncludeItems `json:"includes"`
}
// ListIncludeParentsRequest contains parameters used to list parents of an include
ListIncludeParentsRequest struct {
ContractID string
GroupID string
IncludeID string
}
// ListIncludeParentsResponse represents a response object returned by ListIncludeParents
ListIncludeParentsResponse struct {
Properties ParentPropertyItems `json:"properties"`
}
// GetIncludeRequest contains parameters used to fetch an include
GetIncludeRequest struct {
ContractID string
GroupID string
IncludeID string
}
// GetIncludeResponse represents a response object returned by GetInclude
GetIncludeResponse struct {
Includes IncludeItems `json:"includes"`
Include Include `json:"-"`
}
// CreateIncludeRequest contains parameters used to create an include
CreateIncludeRequest struct {
ContractID string `json:"-"`
GroupID string `json:"-"`
IncludeName string `json:"includeName"`
IncludeType IncludeType `json:"includeType"`
ProductID string `json:"productId"`
RuleFormat string `json:"ruleFormat,omitempty"`
CloneIncludeFrom *CloneIncludeFrom `json:"cloneFrom,omitempty"`
}
// CreateIncludeResponse represents a response object returned by CreateInclude
CreateIncludeResponse struct {
IncludeID string `json:"-"`
IncludeLink string `json:"includeLink"`
ResponseHeaders CreateIncludeResponseHeaders
}
// DeleteIncludeRequest contains parameters used to delete an include
DeleteIncludeRequest struct {
ContractID string
GroupID string
IncludeID string
}
// DeleteIncludeResponse represents a response object returned by DeleteInclude
DeleteIncludeResponse struct {
Message string `json:"message"`
}
// CloneIncludeFrom optionally identifies another include instance to clone when making a request to create new include
CloneIncludeFrom struct {
CloneFromVersionEtag string `json:"cloneFromVersionEtag,omitempty"`
IncludeID string `json:"includeId"`
Version int `json:"version"`
}
// CreateIncludeResponseHeaders contains information received in response headers when making a request to create new include
CreateIncludeResponseHeaders struct {
IncludesLimitTotal string
IncludesLimitRemaining string
}
// Include represents an Include object
Include struct {
AccountID string `json:"accountId"`
AssetID string `json:"assetId"`
ContractID string `json:"contractId"`
GroupID string `json:"groupId"`
IncludeID string `json:"includeId"`
IncludeName string `json:"includeName"`
IncludeType IncludeType `json:"includeType"`
LatestVersion int `json:"latestVersion"`
ProductionVersion *int `json:"productionVersion"`
PropertyType *string `json:"propertyType"`
StagingVersion *int `json:"stagingVersion"`
}
// IncludeItems represents a list of Include objects
IncludeItems struct {
Items []Include `json:"items"`
}
// ParentProperty represents an include parent object
ParentProperty struct {
AccountID string `json:"accountId"`
AssetID string `json:"assetId"`
ContractID string `json:"contractId"`
GroupID string `json:"groupId"`
ProductionVersion *int `json:"productionVersion,omitempty"`
PropertyID string `json:"propertyId"`
PropertyName string `json:"propertyName"`
StagingVersion *int `json:"stagingVersion,omitempty"`
}
// ParentPropertyItems represents a list of ParentProperty objects
ParentPropertyItems struct {
Items []ParentProperty `json:"items"`
}
// IncludeType is type of include
IncludeType string
)
const (
// IncludeTypeMicroServices is used for creating a new microservices include
IncludeTypeMicroServices IncludeType = "MICROSERVICES"
// IncludeTypeCommonSettings is used for creating a new common_settings include
IncludeTypeCommonSettings IncludeType = "COMMON_SETTINGS"
)
// Validate validates ListIncludesRequest
func (i ListIncludesRequest) Validate() error {
return validation.Errors{
"ContractID": validation.Validate(i.ContractID, validation.Required),
}.Filter()
}
// Validate validates ListIncludeParentsRequest
func (i ListIncludeParentsRequest) Validate() error {
return validation.Errors{
"IncludeID": validation.Validate(i.IncludeID, validation.Required),
}.Filter()
}
// Validate validates GetIncludeRequest
func (i GetIncludeRequest) Validate() error {
errs := validation.Errors{
"ContractID": validation.Validate(i.ContractID, validation.Required),
"GroupID": validation.Validate(i.GroupID, validation.Required),
"IncludeID": validation.Validate(i.IncludeID, validation.Required),
}
return edgegriderr.ParseValidationErrors(errs)
}
// Validate validates CreateIncludeRequest
func (i CreateIncludeRequest) Validate() error {
errs := validation.Errors{
"ContractID": validation.Validate(i.ContractID, validation.Required),
"GroupID": validation.Validate(i.GroupID, validation.Required),
"IncludeName": validation.Validate(i.IncludeName, validation.Required),
"IncludeType": validation.Validate(i.IncludeType, validation.Required, validation.In(IncludeTypeMicroServices, IncludeTypeCommonSettings)),
"ProductID": validation.Validate(i.ProductID, validation.Required),
"CloneIncludeFrom.IncludeID": validation.Validate(i.CloneIncludeFrom, validation.When(i.CloneIncludeFrom != nil, validation.By(validateCloneIncludeID))),
"CloneIncludeFrom.Version": validation.Validate(i.CloneIncludeFrom, validation.When(i.CloneIncludeFrom != nil, validation.By(validateCloneVersion))),
}
return edgegriderr.ParseValidationErrors(errs)
}
// Validate validates DeleteIncludeRequest
func (i DeleteIncludeRequest) Validate() error {
return validation.Errors{
"IncludeID": validation.Validate(i.IncludeID, validation.Required),
}.Filter()
}
// validateCloneIncludeID validates IncludeID under CloneIncludeFrom
func validateCloneIncludeID(value interface{}) error {
v, ok := value.(*CloneIncludeFrom)
if !ok {
return fmt.Errorf("type %T is invalid. Must be *CloneIncludeFrom", value)
}
if v.IncludeID == "" {
return fmt.Errorf("cannot be blank")
}
return nil
}
// validateCloneVersion validates Version under CloneIncludeFrom
func validateCloneVersion(value interface{}) error {
v, ok := value.(*CloneIncludeFrom)
if !ok {
return fmt.Errorf("type %T is invalid. Must be *CloneIncludeFrom", value)
}
if v.Version == 0 {
return fmt.Errorf("cannot be blank")
}
return nil
}
var (
// ErrListIncludes is returned in case an error occurs on ListIncludes operation
ErrListIncludes = errors.New("list Includes")
// ErrListIncludeParents is returned in case an error occurs on ListIncludeParents operation
ErrListIncludeParents = errors.New("list Include Parents")
// ErrGetInclude is returned in case an error occurs on GetInclude operation
ErrGetInclude = errors.New("get an Include")
// ErrCreateInclude is returned in case an error occurs on CreateInclude operation
ErrCreateInclude = errors.New("create an Include")
// ErrDeleteInclude is returned in case an error occurs on DeleteInclude operation
ErrDeleteInclude = errors.New("delete an Include")
)
func (p *papi) ListIncludes(ctx context.Context, params ListIncludesRequest) (*ListIncludesResponse, error) {
logger := p.Log(ctx)
logger.Debug("ListIncludes")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrListIncludes, ErrStructValidation, err)
}
uri, err := url.Parse("/papi/v1/includes")
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrListIncludes, err)
}
q := uri.Query()
q.Add("contractId", params.ContractID)
if params.GroupID != "" {
q.Add("groupId", params.GroupID)
}
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", ErrListIncludes, err)
}
var result ListIncludesResponse
resp, err := p.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%s: request failed: %w", ErrListIncludes, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListIncludes, p.Error(resp))
}
return &result, nil
}
func (p *papi) ListIncludeParents(ctx context.Context, params ListIncludeParentsRequest) (*ListIncludeParentsResponse, error) {
logger := p.Log(ctx)
logger.Debug("ListIncludeParents")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrListIncludeParents, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/papi/v1/includes/%s/parents", params.IncludeID))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrListIncludeParents, err)
}
q := uri.Query()
if params.ContractID != "" {
q.Add("contractId", params.ContractID)
}
if params.GroupID != "" {
q.Add("groupId", params.GroupID)
}
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", ErrListIncludeParents, err)
}
var result ListIncludeParentsResponse
resp, err := p.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListIncludeParents, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListIncludeParents, p.Error(resp))
}
return &result, nil
}
func (p *papi) GetInclude(ctx context.Context, params GetIncludeRequest) (*GetIncludeResponse, error) {
logger := p.Log(ctx)
logger.Debug("GetInclude")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrGetInclude, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/papi/v1/includes/%s", params.IncludeID))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrGetInclude, err)
}
q := uri.Query()
q.Add("contractId", params.ContractID)
q.Add("groupId", params.GroupID)
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", ErrGetInclude, err)
}
var result GetIncludeResponse
resp, err := p.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetInclude, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrGetInclude, p.Error(resp))
}
if len(result.Includes.Items) == 0 {
return nil, fmt.Errorf("%s: %w: IncludeID: %s", ErrGetInclude, ErrNotFound, params.IncludeID)
}
result.Include = result.Includes.Items[0]
return &result, nil
}
func (p *papi) CreateInclude(ctx context.Context, params CreateIncludeRequest) (*CreateIncludeResponse, error) {
logger := p.Log(ctx)
logger.Debug("CreateInclude")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrCreateInclude, ErrStructValidation, err)
}
uri, err := url.Parse("/papi/v1/includes")
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrCreateInclude, err)
}
q := uri.Query()
q.Add("contractId", params.ContractID)
q.Add("groupId", params.GroupID)
uri.RawQuery = q.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrCreateInclude, err)
}
var result CreateIncludeResponse
resp, err := p.Exec(req, &result, params)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrCreateInclude, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("%s: %w", ErrCreateInclude, p.Error(resp))
}
result.ResponseHeaders.IncludesLimitTotal = resp.Header.Get("x-limit-includes-per-contract-limit")
result.ResponseHeaders.IncludesLimitRemaining = resp.Header.Get("x-limit-includes-per-contract-remaining")
id, err := ResponseLinkParse(result.IncludeLink)
if err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrCreateInclude, ErrInvalidResponseLink, err)
}
result.IncludeID = id
return &result, nil
}
func (p *papi) DeleteInclude(ctx context.Context, params DeleteIncludeRequest) (*DeleteIncludeResponse, error) {
logger := p.Log(ctx)
logger.Debug("DeleteInclude")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrDeleteInclude, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/papi/v1/includes/%s", params.IncludeID))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrDeleteInclude, err)
}
q := uri.Query()
if params.ContractID != "" {
q.Add("contractId", params.ContractID)
}
if params.GroupID != "" {
q.Add("groupId", params.GroupID)
}
uri.RawQuery = q.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrDeleteInclude, err)
}
var result DeleteIncludeResponse
resp, err := p.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrDeleteInclude, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrDeleteInclude, p.Error(resp))
}
return &result, nil
}