-
Notifications
You must be signed in to change notification settings - Fork 69
/
activations.go
253 lines (204 loc) · 7.9 KB
/
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
package edgeworkers
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/session"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
type (
// ListActivationsRequest contains parameters used to list activations
ListActivationsRequest struct {
EdgeWorkerID int
Version string
}
// ActivateVersionRequest contains path parameters and request body used to activate an edge worker
ActivateVersionRequest struct {
EdgeWorkerID int
ActivateVersion
}
// ActivateVersion represents the request body used to activate a version
ActivateVersion struct {
Network ActivationNetwork `json:"network"`
Version string `json:"version"`
Note string `json:"note,omitempty"`
}
// ActivationNetwork represents available activation network types
ActivationNetwork string
// GetActivationRequest contains path parameters used to fetch edge worker activation
GetActivationRequest struct {
EdgeWorkerID int
ActivationID int
}
// ListActivationsResponse represents a response object returned when listing activations
ListActivationsResponse struct {
Activations []Activation `json:"activations"`
}
// CancelActivationRequest contains path parameters used to cancel edge worker activation
CancelActivationRequest struct {
EdgeWorkerID int
ActivationID int
}
// Activation represents an activation object
Activation struct {
AccountID string `json:"accountId"`
ActivationID int `json:"activationId"`
CreatedBy string `json:"createdBy"`
CreatedTime string `json:"createdTime"`
EdgeWorkerID int `json:"edgeWorkerId"`
LastModifiedTime string `json:"lastModifiedTime"`
Network string `json:"network"`
Status string `json:"status"`
Version string `json:"version"`
Note string `json:"note"`
}
)
const (
// ActivationNetworkStaging is the staging network
ActivationNetworkStaging ActivationNetwork = "STAGING"
// ActivationNetworkProduction is the production network
ActivationNetworkProduction ActivationNetwork = "PRODUCTION"
)
// Validate validates ListActivationsRequest
func (r ListActivationsRequest) Validate() error {
return validation.Errors{
"EdgeWorkerID": validation.Validate(r.EdgeWorkerID, validation.Required),
}.Filter()
}
// Validate validates GetActivationRequest
func (r GetActivationRequest) Validate() error {
return validation.Errors{
"EdgeWorkerID": validation.Validate(r.EdgeWorkerID, validation.Required),
"ActivationID": validation.Validate(r.ActivationID, validation.Required),
}.Filter()
}
// Validate validates ActivateVersionRequest
func (r ActivateVersionRequest) Validate() error {
return validation.Errors{
"EdgeWorkerID": validation.Validate(r.EdgeWorkerID, validation.Required),
"ActivateVersion": validation.Validate(&r.ActivateVersion, validation.Required),
}.Filter()
}
// Validate validates ActivateVersion
func (r ActivateVersion) Validate() error {
return validation.Errors{
"Network": validation.Validate(r.Network, validation.Required, validation.In(ActivationNetworkStaging, ActivationNetworkProduction).Error(
fmt.Sprintf("value '%s' is invalid. Must be one of: '%s' or '%s'", r.Network, ActivationNetworkStaging, ActivationNetworkProduction))),
"Version": validation.Validate(r.Version, validation.Required),
}.Filter()
}
// Validate validates CancelActivationRequest
func (r CancelActivationRequest) Validate() error {
return validation.Errors{
"EdgeWorkerID": validation.Validate(r.EdgeWorkerID, validation.Required),
"ActivationID": validation.Validate(r.ActivationID, validation.Required),
}.Filter()
}
var (
// ErrListActivations is returned when ListActivations fails
ErrListActivations = errors.New("list activations")
// ErrGetActivation is returned when GetActivation fails
ErrGetActivation = errors.New("get activation")
// ErrActivateVersion is returned when ActivateVersion fails
ErrActivateVersion = errors.New("activate version")
// ErrCancelActivation is returned when CancelPendingActivation fails
ErrCancelActivation = errors.New("cancel activation")
)
func (e *edgeworkers) ListActivations(ctx context.Context, params ListActivationsRequest) (*ListActivationsResponse, error) {
logger := e.Log(ctx)
logger.Debug("ListActivations")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrListActivations, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/edgeworkers/v1/ids/%d/activations", params.EdgeWorkerID))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrListActivations, err)
}
q := uri.Query()
if params.Version != "" {
q.Add("version", params.Version)
}
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", ErrListActivations, err)
}
var result ListActivationsResponse
resp, err := e.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListActivations, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListActivations, e.Error(resp))
}
return &result, nil
}
func (e *edgeworkers) GetActivation(ctx context.Context, params GetActivationRequest) (*Activation, error) {
logger := e.Log(ctx)
logger.Debug("GetActivation")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrGetActivation, ErrStructValidation, err)
}
uri := fmt.Sprintf("/edgeworkers/v1/ids/%d/activations/%d", params.EdgeWorkerID, params.ActivationID)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetActivation, err)
}
var result Activation
resp, err := e.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetActivation, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrGetActivation, e.Error(resp))
}
return &result, nil
}
func (e *edgeworkers) ActivateVersion(ctx context.Context, params ActivateVersionRequest) (*Activation, error) {
logger := e.Log(ctx)
logger.Debug("ActivateVersion")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrActivateVersion, ErrStructValidation, err)
}
uri := fmt.Sprintf("/edgeworkers/v1/ids/%d/activations", params.EdgeWorkerID)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrActivateVersion, err)
}
var result Activation
resp, err := e.Exec(req, &result, params.ActivateVersion)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrActivateVersion, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("%s: %w", ErrActivateVersion, e.Error(resp))
}
return &result, nil
}
func (e *edgeworkers) CancelPendingActivation(ctx context.Context, params CancelActivationRequest) (*Activation, error) {
logger := e.Log(ctx)
logger.Debug("CancelPendingActivation")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrCancelActivation, ErrStructValidation, err)
}
uri := fmt.Sprintf("/edgeworkers/v1/ids/%d/activations/%d", params.EdgeWorkerID, params.ActivationID)
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrCancelActivation, err)
}
var result Activation
resp, err := e.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrCancelActivation, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrCancelActivation, e.Error(resp))
}
return &result, nil
}