-
Notifications
You must be signed in to change notification settings - Fork 69
/
edgeworker_version.go
262 lines (214 loc) · 9.22 KB
/
edgeworker_version.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
package edgeworkers
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/session"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
type (
// GetEdgeWorkerVersionRequest contains parameters used to get an EdgeWorkerVersion
GetEdgeWorkerVersionRequest EdgeWorkerVersionRequest
// EdgeWorkerVersion represents an EdgeWorkerVersion object
EdgeWorkerVersion struct {
EdgeWorkerID int `json:"edgeWorkerId"`
Version string `json:"version"`
AccountID string `json:"accountId"`
Checksum string `json:"checksum"`
SequenceNumber int `json:"sequenceNumber"`
CreatedBy string `json:"createdBy"`
CreatedTime string `json:"createdTime"`
}
// ListEdgeWorkerVersionsRequest contains query parameters used to list EdgeWorkerVersions
ListEdgeWorkerVersionsRequest struct {
EdgeWorkerID int
}
// ListEdgeWorkerVersionsResponse represents a response object returned by ListEdgeWorkerVersions
ListEdgeWorkerVersionsResponse struct {
EdgeWorkerVersions []EdgeWorkerVersion `json:"versions"`
}
// GetEdgeWorkerVersionContentRequest contains parameters used to get content bundle of an EdgeWorkerVersion
GetEdgeWorkerVersionContentRequest EdgeWorkerVersionRequest
// CreateEdgeWorkerVersionRequest contains parameters used to create EdgeWorkerVersion
CreateEdgeWorkerVersionRequest struct {
EdgeWorkerID int
ContentBundle Bundle
}
// DeleteEdgeWorkerVersionRequest contains parameters used to delete an EdgeWorkerVersion
DeleteEdgeWorkerVersionRequest EdgeWorkerVersionRequest
// EdgeWorkerVersionRequest contains request parameters used by GetEdgeWorkerVersion, GetEdgeWorkerVersionContent and DeleteEdgeWorkerVersion
EdgeWorkerVersionRequest struct {
EdgeWorkerID int
Version string
}
// Bundle is the type for content bundle of an Edgeworker Version
Bundle struct {
io.Reader
}
)
// Validate validates GetEdgeWorkerVersionRequest
func (g GetEdgeWorkerVersionRequest) Validate() error {
return validation.Errors{
"EdgeWorkerID": validation.Validate(g.EdgeWorkerID, validation.Required),
"Version": validation.Validate(g.Version, validation.Required),
}.Filter()
}
// Validate validates ListEdgeWorkerVersionsRequest
func (g ListEdgeWorkerVersionsRequest) Validate() error {
return validation.Errors{
"EdgeWorkerID": validation.Validate(g.EdgeWorkerID, validation.Required),
}.Filter()
}
// Validate validates CreateEdgeWorkerVersionRequest
func (g CreateEdgeWorkerVersionRequest) Validate() error {
return validation.Errors{
"EdgeWorkerID": validation.Validate(g.EdgeWorkerID, validation.Required),
"ContentBundle": validation.Validate(g.ContentBundle.Reader, validation.Required),
}.Filter()
}
// Validate validates GetEdgeWorkerVersionContentRequest
func (g GetEdgeWorkerVersionContentRequest) Validate() error {
return validation.Errors{
"EdgeWorkerID": validation.Validate(g.EdgeWorkerID, validation.Required),
"Version": validation.Validate(g.Version, validation.Required),
}.Filter()
}
// Validate validates DeleteEdgeWorkerVersionRequest
func (g DeleteEdgeWorkerVersionRequest) Validate() error {
return validation.Errors{
"EdgeWorkerID": validation.Validate(g.EdgeWorkerID, validation.Required),
"Version": validation.Validate(g.Version, validation.Required),
}.Filter()
}
var (
// ErrGetEdgeWorkerVersion is returned in case an error occurs on GetEdgeWorkerVersion operation
ErrGetEdgeWorkerVersion = errors.New("get an EdgeWorker Version")
// ErrListEdgeWorkerVersions is returned in case an error occurs on ListEdgeWorkerVersions operation
ErrListEdgeWorkerVersions = errors.New("list EdgeWorkers Versions")
// ErrGetEdgeWorkerVersionContent is returned in case an error occurs on GetEdgeWorkerVersionContent operation
ErrGetEdgeWorkerVersionContent = errors.New("get an EdgeWorker Version Content Bundle")
// ErrCreateEdgeWorkerVersion is returned in case an error occurs on CreateEdgeWorkerVersion operation
ErrCreateEdgeWorkerVersion = errors.New("create an EdgeWorker Version")
// ErrDeleteEdgeWorkerVersion is returned in case an error occurs on DeleteEdgeWorkerVersion operation
ErrDeleteEdgeWorkerVersion = errors.New("delete an EdgeWorker Version")
)
func (e *edgeworkers) GetEdgeWorkerVersion(ctx context.Context, params GetEdgeWorkerVersionRequest) (*EdgeWorkerVersion, error) {
logger := e.Log(ctx)
logger.Debug("GetEdgeWorkerVersion")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrGetEdgeWorkerVersion, ErrStructValidation, err)
}
uri := fmt.Sprintf("/edgeworkers/v1/ids/%d/versions/%s", params.EdgeWorkerID, params.Version)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetEdgeWorkerVersion, err)
}
var result EdgeWorkerVersion
resp, err := e.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetEdgeWorkerVersion, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrGetEdgeWorkerVersion, e.Error(resp))
}
return &result, nil
}
func (e *edgeworkers) ListEdgeWorkerVersions(ctx context.Context, params ListEdgeWorkerVersionsRequest) (*ListEdgeWorkerVersionsResponse, error) {
logger := e.Log(ctx)
logger.Debug("ListEdgeWorkerVersions")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrListEdgeWorkerVersions, ErrStructValidation, err)
}
uri := fmt.Sprintf("/edgeworkers/v1/ids/%d/versions", params.EdgeWorkerID)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrListEdgeWorkerVersions, err)
}
var result ListEdgeWorkerVersionsResponse
resp, err := e.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListEdgeWorkerVersions, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListEdgeWorkerVersions, e.Error(resp))
}
return &result, nil
}
func (e *edgeworkers) GetEdgeWorkerVersionContent(ctx context.Context, params GetEdgeWorkerVersionContentRequest) (*Bundle, error) {
logger := e.Log(ctx)
logger.Debug("GetEdgeWorkerVersionContent")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrGetEdgeWorkerVersionContent, ErrStructValidation, err)
}
uri := fmt.Sprintf("/edgeworkers/v1/ids/%d/versions/%s/content", params.EdgeWorkerID, params.Version)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetEdgeWorkerVersionContent, err)
}
req.Header.Add("Accept", "application/gzip")
resp, err := e.Exec(req, nil)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetEdgeWorkerVersionContent, err)
}
defer session.CloseResponseBody(resp)
var result Bundle
data, err := ioutil.ReadAll(resp.Body)
result.Reader = bytes.NewBuffer(data)
if err != nil {
return nil, fmt.Errorf("%w: failed to read response body: %s", ErrGetEdgeWorkerVersionContent, err)
}
resp.Body = ioutil.NopCloser(bytes.NewBuffer(data))
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrGetEdgeWorkerVersionContent, e.Error(resp))
}
return &result, nil
}
func (e *edgeworkers) CreateEdgeWorkerVersion(ctx context.Context, params CreateEdgeWorkerVersionRequest) (*EdgeWorkerVersion, error) {
logger := e.Log(ctx)
logger.Debug("CreateEdgeWorkerVersion")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w:\n%s", ErrCreateEdgeWorkerVersion, ErrStructValidation, err)
}
uri := fmt.Sprintf("/edgeworkers/v1/ids/%d/versions", params.EdgeWorkerID)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, ioutil.NopCloser(params.ContentBundle))
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrCreateEdgeWorkerVersion, err)
}
req.Header.Add("Content-Type", "application/gzip")
var result EdgeWorkerVersion
resp, err := e.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrCreateEdgeWorkerVersion, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("%s: %w", ErrCreateEdgeWorkerVersion, e.Error(resp))
}
return &result, nil
}
func (e *edgeworkers) DeleteEdgeWorkerVersion(ctx context.Context, params DeleteEdgeWorkerVersionRequest) error {
e.Log(ctx).Debug("DeleteEdgeWorkerVersion")
if err := params.Validate(); err != nil {
return fmt.Errorf("%s: %w:\n%s", ErrDeleteEdgeWorkerVersion, ErrStructValidation, err)
}
uri := fmt.Sprintf("/edgeworkers/v1/ids/%d/versions/%s", params.EdgeWorkerID, params.Version)
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return fmt.Errorf("%w: failed to create request: %s", ErrDeleteEdgeWorkerVersion, err)
}
resp, err := e.Exec(req, nil)
if err != nil {
return fmt.Errorf("%w: request failed: %s", ErrDeleteEdgeWorkerVersion, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("%s: %w", ErrDeleteEdgeWorkerVersion, e.Error(resp))
}
return nil
}