-
Notifications
You must be signed in to change notification settings - Fork 70
/
include_activations.go
569 lines (478 loc) · 20.3 KB
/
include_activations.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
package papi
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/edgegriderr"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/ptr"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/session"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
type (
// ActivateIncludeRequest contains parameters used to activate include
ActivateIncludeRequest ActivateOrDeactivateIncludeRequest
// DeactivateIncludeRequest contains parameters used to deactivate include
DeactivateIncludeRequest ActivateOrDeactivateIncludeRequest
// ActivateOrDeactivateIncludeRequest contains parameters used to activate or deactivate include
ActivateOrDeactivateIncludeRequest struct {
IncludeID string `json:"-"`
Version int `json:"includeVersion"`
Network ActivationNetwork `json:"network"`
Note string `json:"note"`
NotifyEmails []string `json:"notifyEmails"`
AcknowledgeWarnings []string `json:"acknowledgeWarnings,omitempty"`
AcknowledgeAllWarnings bool `json:"acknowledgeAllWarnings"`
IgnoreHTTPErrors *bool `json:"ignoreHttpErrors,omitempty"`
ComplianceRecord complianceRecord `json:"complianceRecord,omitempty"`
}
// CancelIncludeActivationRequest contains parameters used to cancel pending activation of include
CancelIncludeActivationRequest struct {
ContractID string
GroupID string
IncludeID string
ActivationID string
}
// CancelIncludeActivationResponse represents a response object returned by CancelIncludeActivation operation
CancelIncludeActivationResponse ListIncludeActivationsResponse
// ActivationIncludeResponse represents a response object returned by ActivateInclude operation
ActivationIncludeResponse struct {
ActivationID string `json:"-"`
ActivationLink string `json:"activationLink"`
}
// DeactivationIncludeResponse represents a response object returned by DeactivateInclude operation
DeactivationIncludeResponse struct {
ActivationID string `json:"-"`
ActivationLink string `json:"activationLink"`
}
// GetIncludeActivationRequest contains parameters used to get the include activation
GetIncludeActivationRequest struct {
IncludeID string
ActivationID string
}
// GetIncludeActivationResponse represents a response object returned by GetIncludeActivation
GetIncludeActivationResponse struct {
AccountID string `json:"accountId"`
ContractID string `json:"contractId"`
GroupID string `json:"groupId"`
Activations IncludeActivationsRes `json:"activations"`
Validations *Validations `json:"validations,omitempty"`
Activation IncludeActivation `json:"-"`
}
// Validations represent include activation validation object
Validations struct {
ValidationSummary ValidationSummary `json:"validationSummary"`
ValidationProgressItemList ValidationProgress `json:"validationProgressItemList"`
Network ActivationNetwork `json:"network"`
}
// ValidationSummary represent include activation validation summary object
ValidationSummary struct {
CompletePercent float64 `json:"completePercent"`
HasValidationError bool `json:"hasValidationError"`
HasValidationWarning bool `json:"hasValidationWarning"`
HasSystemError bool `json:"hasSystemError"`
HasClientError bool `json:"hasClientError"`
MessageState string `json:"messageState"`
ErrorMessage string `json:"errorMessage"`
}
// ValidationProgress represents include activation validation progress object
ValidationProgress struct {
ErrorItems []ErrorItem `json:"errorItemsList"`
}
// ErrorItem represents validation progress error item object
ErrorItem struct {
VersionID int `json:"versionId"`
PropertyName string `json:"propertyName"`
VersionNumber int `json:"versionNumber"`
HasValidationError bool `json:"hasValidationError"`
HasValidationWarning bool `json:"hasValidationWarning"`
ValidationResultsLink string `json:"validationResultsLink"`
}
// ListIncludeActivationsRequest contains parameters used to list the include activations
ListIncludeActivationsRequest struct {
IncludeID string
ContractID string
GroupID string
}
// ListIncludeActivationsResponse represents a response object returned by ListIncludeActivations
ListIncludeActivationsResponse struct {
AccountID string `json:"accountId"`
ContractID string `json:"contractId"`
GroupID string `json:"groupId"`
Activations IncludeActivationsRes `json:"activations"`
}
// IncludeActivationsRes represents Activations object
IncludeActivationsRes struct {
Items []IncludeActivation `json:"items"`
}
// IncludeActivation represents an include activation object
IncludeActivation struct {
ActivationID string `json:"activationId"`
Network ActivationNetwork `json:"network"`
ActivationType ActivationType `json:"activationType"`
Status ActivationStatus `json:"status"`
SubmitDate string `json:"submitDate"`
UpdateDate string `json:"updateDate"`
Note string `json:"note"`
NotifyEmails []string `json:"notifyEmails"`
FMAActivationState string `json:"fmaActivationState"`
FallbackInfo *ActivationFallbackInfo `json:"fallbackInfo"`
IncludeID string `json:"includeId"`
IncludeName string `json:"includeName"`
IncludeType IncludeType `json:"includeType"`
IncludeVersion int `json:"includeVersion"`
IncludeActivationID string `json:"includeActivationId"`
}
// complianceRecord is an interface for ComplianceRecord data type
complianceRecord interface {
noncomplianceReasonType() string
}
// ComplianceRecordNone holds data relevant for ComplianceRecord with noncomplianceReason 'None'
ComplianceRecordNone struct {
CustomerEmail string `json:"customerEmail"`
PeerReviewedBy string `json:"peerReviewedBy"`
UnitTested bool `json:"unitTested"`
TicketID string `json:"ticketId,omitempty"`
}
// ComplianceRecordOther holds data relevant for ComplianceRecord with noncomplianceReason 'Other'
ComplianceRecordOther struct {
OtherNoncomplianceReason string `json:"otherNoncomplianceReason"`
TicketID string `json:"ticketId,omitempty"`
}
// ComplianceRecordNoProductionTraffic holds data relevant for ComplianceRecord with noncomplianceReason 'NoProductionTraffic'
ComplianceRecordNoProductionTraffic struct {
TicketID string `json:"ticketId,omitempty"`
}
// ComplianceRecordEmergency holds data relevant for ComplianceRecord with noncomplianceReason 'Emergency'
ComplianceRecordEmergency struct {
TicketID string `json:"ticketId,omitempty"`
}
)
const (
// NoncomplianceReasonNoProductionTraffic is noncompliance reason type for compliance record
NoncomplianceReasonNoProductionTraffic = "NO_PRODUCTION_TRAFFIC"
// NoncomplianceReasonOther is noncompliance reason type for compliance record
NoncomplianceReasonOther = "OTHER"
// NoncomplianceReasonEmergency is noncompliance reason type for compliance record
NoncomplianceReasonEmergency = "EMERGENCY"
// NoncomplianceReasonNone is noncompliance reason type for compliance record
NoncomplianceReasonNone = "NONE"
)
// Validate validates ActivateIncludeRequest
func (i ActivateIncludeRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"IncludeID": validation.Validate(i.IncludeID, validation.Required),
"Version": validation.Validate(i.Version, validation.Required),
"Network": validation.Validate(i.Network, validation.Required),
"NotifyEmails": validation.Validate(i.NotifyEmails, validation.Required),
"ComplianceRecord": validation.Validate(i.ComplianceRecord,
validation.When(i.Network == ActivationNetworkProduction, validation.By(unitTestedFieldValidationRule))),
})
}
func unitTestedFieldValidationRule(value interface{}) error {
switch value.(type) {
case *ComplianceRecordNone:
if value.(*ComplianceRecordNone).UnitTested == false {
return errors.New("for PRODUCTION activation network and nonComplianceRecord, UnitTested value has to be set to true, otherwise API will not work correctly")
}
}
return nil
}
func (c *ComplianceRecordNone) noncomplianceReasonType() string {
return NoncomplianceReasonNone
}
// Validate validates ComplianceRecordNone
func (c *ComplianceRecordNone) Validate() error {
return validation.Errors{
"CustomerEmail": validation.Validate(c.CustomerEmail, validation.Required),
"PeerReviewedBy": validation.Validate(c.PeerReviewedBy, validation.Required),
}.Filter()
}
// MarshalJSON is a custom marshaller for ComplianceRecordNone struct
func (c ComplianceRecordNone) MarshalJSON() ([]byte, error) {
type ComplianceRecord ComplianceRecordNone
v := struct {
ComplianceRecord
NoncomplianceReason string `json:"noncomplianceReason"`
}{
ComplianceRecord(c),
c.noncomplianceReasonType(),
}
return json.Marshal(v)
}
func (c *ComplianceRecordOther) noncomplianceReasonType() string {
return NoncomplianceReasonOther
}
// Validate validates ComplianceRecordOther
func (c *ComplianceRecordOther) Validate() error {
return validation.Errors{
"OtherNoncomplianceReason": validation.Validate(c.OtherNoncomplianceReason, validation.Required),
}.Filter()
}
// MarshalJSON is a custom marshaller for ComplianceRecordOther struct
func (c ComplianceRecordOther) MarshalJSON() ([]byte, error) {
type ComplianceRecord ComplianceRecordOther
v := struct {
ComplianceRecord
NoncomplianceReason string `json:"noncomplianceReason"`
}{
ComplianceRecord(c),
c.noncomplianceReasonType(),
}
return json.Marshal(v)
}
func (c *ComplianceRecordNoProductionTraffic) noncomplianceReasonType() string {
return NoncomplianceReasonNoProductionTraffic
}
// MarshalJSON is a custom marshaller for ComplianceRecordNoProductionTraffic struct
func (c ComplianceRecordNoProductionTraffic) MarshalJSON() ([]byte, error) {
type ComplianceRecord ComplianceRecordNoProductionTraffic
v := struct {
ComplianceRecord
NoncomplianceReason string `json:"noncomplianceReason"`
}{
ComplianceRecord(c),
c.noncomplianceReasonType(),
}
return json.Marshal(v)
}
func (c *ComplianceRecordEmergency) noncomplianceReasonType() string {
return NoncomplianceReasonEmergency
}
// MarshalJSON is a custom marshaller for ComplianceRecordEmergency struct
func (c ComplianceRecordEmergency) MarshalJSON() ([]byte, error) {
type ComplianceRecord ComplianceRecordEmergency
v := struct {
ComplianceRecord
NoncomplianceReason string `json:"noncomplianceReason"`
}{
ComplianceRecord(c),
c.noncomplianceReasonType(),
}
return json.Marshal(v)
}
// Validate validates DeactivateIncludeRequest
func (i DeactivateIncludeRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"IncludeID": validation.Validate(i.IncludeID, validation.Required),
"Version": validation.Validate(i.Version, validation.Required),
"Network": validation.Validate(i.Network, validation.Required),
"NotifyEmails": validation.Validate(i.NotifyEmails, validation.Required),
})
}
// Validate validates GetIncludeActivationRequest
func (i GetIncludeActivationRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"IncludeID": validation.Validate(i.IncludeID, validation.Required),
"ActivationID": validation.Validate(i.ActivationID, validation.Required),
})
}
// Validate validates CancelIncludeActivationRequest
func (i CancelIncludeActivationRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"ContractID": validation.Validate(i.ContractID, validation.Required),
"GroupID": validation.Validate(i.GroupID, validation.Required),
"IncludeID": validation.Validate(i.IncludeID, validation.Required),
"ActivationID": validation.Validate(i.ActivationID, validation.Required),
})
}
// Validate validates ListIncludeActivationsRequest
func (i ListIncludeActivationsRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"IncludeID": validation.Validate(i.IncludeID, validation.Required),
"ContractID": validation.Validate(i.ContractID, validation.Required),
"GroupID": validation.Validate(i.GroupID, validation.Required),
})
}
var (
// ErrActivateInclude is returned in case an error occurs on ActivateInclude operation
ErrActivateInclude = errors.New("activate include")
// ErrDeactivateInclude is returned in case an error occurs on DeactivateInclude operation
ErrDeactivateInclude = errors.New("deactivate include")
// ErrCancelIncludeActivation is returned in case an error occurs on CancelIncludeActivation operation
ErrCancelIncludeActivation = errors.New("cancel include activation")
// ErrGetIncludeActivation is returned in case an error occurs on GetIncludeActivation operation
ErrGetIncludeActivation = errors.New("get include activation")
// ErrListIncludeActivations is returned in case an error occurs on ListIncludeActivations operation
ErrListIncludeActivations = errors.New("list include activations")
)
func (p *papi) ActivateInclude(ctx context.Context, params ActivateIncludeRequest) (*ActivationIncludeResponse, error) {
logger := p.Log(ctx)
logger.Debug("ActivateInclude")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrActivateInclude, ErrStructValidation, err)
}
if params.IgnoreHTTPErrors == nil {
params.IgnoreHTTPErrors = ptr.To(true)
}
requestBody := struct {
ActivateIncludeRequest
ActivationType ActivationType `json:"activationType"`
}{
params,
ActivationTypeActivate,
}
uri := fmt.Sprintf("/papi/v1/includes/%s/activations", params.IncludeID)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrActivateInclude, err)
}
var result ActivationIncludeResponse
resp, err := p.Exec(req, &result, requestBody)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrActivateInclude, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("%s: %w", ErrActivateInclude, p.Error(resp))
}
id, err := ResponseLinkParse(result.ActivationLink)
if err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrActivateInclude, ErrInvalidResponseLink, err)
}
result.ActivationID = id
return &result, nil
}
func (p *papi) DeactivateInclude(ctx context.Context, params DeactivateIncludeRequest) (*DeactivationIncludeResponse, error) {
logger := p.Log(ctx)
logger.Debug("DeactivateInclude")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrDeactivateInclude, ErrStructValidation, err)
}
if params.IgnoreHTTPErrors == nil {
params.IgnoreHTTPErrors = ptr.To(true)
}
requestBody := struct {
DeactivateIncludeRequest
ActivationType ActivationType `json:"activationType"`
}{
params,
ActivationTypeDeactivate,
}
uri := fmt.Sprintf("/papi/v1/includes/%s/activations", params.IncludeID)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrDeactivateInclude, err)
}
var result DeactivationIncludeResponse
resp, err := p.Exec(req, &result, requestBody)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrDeactivateInclude, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("%s: %w", ErrDeactivateInclude, p.Error(resp))
}
id, err := ResponseLinkParse(result.ActivationLink)
if err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrDeactivateInclude, ErrInvalidResponseLink, err)
}
result.ActivationID = id
return &result, nil
}
func (p *papi) CancelIncludeActivation(ctx context.Context, params CancelIncludeActivationRequest) (*CancelIncludeActivationResponse, error) {
logger := p.Log(ctx)
logger.Debug("CancelIncludeActivation")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrCancelIncludeActivation, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/papi/v1/includes/%s/activations/%s", params.IncludeID, params.ActivationID))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrCancelIncludeActivation, err)
}
q := uri.Query()
q.Add("contractId", params.ContractID)
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", ErrCancelIncludeActivation, err)
}
var result CancelIncludeActivationResponse
resp, err := p.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrCancelIncludeActivation, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrCancelIncludeActivation, p.Error(resp))
}
return &result, nil
}
func (p *papi) GetIncludeActivation(ctx context.Context, params GetIncludeActivationRequest) (*GetIncludeActivationResponse, error) {
logger := p.Log(ctx)
logger.Debug("GetIncludeActivation")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrGetIncludeActivation, ErrStructValidation, err)
}
uri := fmt.Sprintf("/papi/v1/includes/%s/activations/%s", params.IncludeID, params.ActivationID)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetIncludeActivation, err)
}
var result GetIncludeActivationResponse
resp, err := p.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetIncludeActivation, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrGetIncludeActivation, p.Error(resp))
}
if result.Validations != nil {
val := result.Validations.ValidationSummary
if val.HasClientError || val.HasValidationError || val.HasSystemError {
if err = extractError(val.ErrorMessage); err != nil {
return nil, fmt.Errorf("%s: %w", ErrGetIncludeActivation, err)
}
}
}
if len(result.Activations.Items) == 0 {
return nil, fmt.Errorf("%s: %w: ActivationID: %s", ErrGetIncludeActivation, ErrNotFound, params.ActivationID)
}
result.Activation = result.Activations.Items[0]
return &result, nil
}
func (p *papi) ListIncludeActivations(ctx context.Context, params ListIncludeActivationsRequest) (*ListIncludeActivationsResponse, error) {
logger := p.Log(ctx)
logger.Debug("ListIncludeActivations")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrListIncludeActivations, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/papi/v1/includes/%s/activations", params.IncludeID))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrListIncludeActivations, 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", ErrListIncludeActivations, err)
}
var result ListIncludeActivationsResponse
resp, err := p.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListIncludeActivations, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListIncludeActivations, p.Error(resp))
}
return &result, nil
}
// extractError extracts error from validation object in GetIncludeActivation response if it is present
func extractError(rawError string) error {
startIndex := strings.Index(rawError, "{")
endIndex := strings.LastIndex(rawError, "}") + 1
formattedError := rawError[startIndex:endIndex]
var e ActivationError
if err := json.Unmarshal([]byte(formattedError), &e); err != nil {
return err
}
return &e
}