-
Notifications
You must be signed in to change notification settings - Fork 69
/
include_versions.go
375 lines (311 loc) · 13.4 KB
/
include_versions.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
package papi
import (
"context"
"errors"
"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 (
// CreateIncludeVersionRequest contains parameters used to create a new include version
CreateIncludeVersionRequest struct {
IncludeID string
IncludeVersionRequest
}
// IncludeVersionRequest contains body parameters used to create a new include version
IncludeVersionRequest struct {
CreateFromVersion int `json:"createFromVersion"`
CreateFromVersionEtag string `json:"createFromVersionEtag,omitempty"`
}
// CreateIncludeVersionResponse represents a response object returned by CreateIncludeVersion
CreateIncludeVersionResponse struct {
VersionLink string
Version int
}
// GetIncludeVersionRequest contains parameters used to get the include version
GetIncludeVersionRequest struct {
IncludeID string
Version int
ContractID string
GroupID string
}
// ListIncludeVersionsRequest contains parameters used to list the include versions
ListIncludeVersionsRequest struct {
IncludeID string
ContractID string
GroupID string
}
// GetIncludeVersionResponse represents a response object returned by GetIncludeVersion
GetIncludeVersionResponse struct {
IncludeID string `json:"includeId"`
IncludeName string `json:"includeName"`
AccountID string `json:"accountId"`
ContractID string `json:"contractId"`
GroupID string `json:"groupId"`
AssetID string `json:"assetId"`
IncludeType IncludeType `json:"includeType"`
IncludeVersions Versions `json:"versions"`
IncludeVersion IncludeVersion `json:"-"`
}
// ListIncludeVersionsResponse represents a response object returned by ListIncludeVersions
ListIncludeVersionsResponse struct {
IncludeID string `json:"includeId"`
IncludeName string `json:"includeName"`
AccountID string `json:"accountId"`
ContractID string `json:"contractId"`
GroupID string `json:"groupId"`
AssetID string `json:"assetId"`
IncludeType IncludeType `json:"includeType"`
IncludeVersions Versions `json:"versions"`
}
// Versions represents IncludeVersions object
Versions struct {
Items []IncludeVersion `json:"items"`
}
// IncludeVersion represents an include version object
IncludeVersion struct {
UpdatedByUser string `json:"updatedByUser"`
UpdatedDate string `json:"updatedDate"`
ProductionStatus VersionStatus `json:"productionStatus"`
Etag string `json:"etag"`
ProductID string `json:"productId"`
Note string `json:"note,omitempty"`
RuleFormat string `json:"ruleFormat,omitempty"`
IncludeVersion int `json:"includeVersion"`
StagingStatus VersionStatus `json:"stagingStatus"`
}
// ListAvailableCriteriaRequest contains parameters used to get available include version criteria
ListAvailableCriteriaRequest struct {
IncludeID string
Version int
}
// AvailableCriteriaResponse represents a response object returned by ListIncludeVersionAvailableCriteria
AvailableCriteriaResponse struct {
ContractID string `json:"contractId"`
GroupID string `json:"groupId"`
ProductID string `json:"productId"`
RuleFormat string `json:"ruleFormat"`
AvailableCriteria AvailableCriteria `json:"criteria"`
}
// AvailableCriteria represents list of available criteria for the include version
AvailableCriteria struct {
Items []Criteria `json:"items"`
}
// Criteria represents available criteria object
Criteria struct {
Name string `json:"name"`
SchemaLink string `json:"schemaLink"`
}
// ListAvailableBehaviorsRequest contains parameters used to get available include version behaviors
ListAvailableBehaviorsRequest struct {
IncludeID string
Version int
}
// AvailableBehaviorsResponse represents a response object returned by GetIncludeVersionAvailableBehavior
AvailableBehaviorsResponse struct {
ContractID string `json:"contractId"`
GroupID string `json:"groupId"`
ProductID string `json:"productId"`
RuleFormat string `json:"ruleFormat"`
AvailableBehaviors AvailableBehaviors `json:"behaviors"`
}
// AvailableBehaviors represents list of available behaviors for the include version
AvailableBehaviors struct {
Items []Behavior `json:"items"`
}
// Behavior represents available behavior object
Behavior struct {
Name string `json:"name"`
SchemaLink string `json:"schemaLink"`
}
)
// Validate validates CreateIncludeVersionRequest
func (i CreateIncludeVersionRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"IncludeID": validation.Validate(i.IncludeID, validation.Required),
"CreateFromVersion": validation.Validate(i.CreateFromVersion, validation.Required),
})
}
// Validate validates GetIncludeVersionRequest
func (i GetIncludeVersionRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"IncludeID": validation.Validate(i.IncludeID, validation.Required),
"Version": validation.Validate(i.Version, validation.Required),
"ContractID": validation.Validate(i.ContractID, validation.Required),
"GroupID": validation.Validate(i.GroupID, validation.Required),
})
}
// Validate validates ListIncludeVersionsRequest
func (i ListIncludeVersionsRequest) 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),
})
}
// Validate validates ListAvailableCriteriaRequest
func (i ListAvailableCriteriaRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"IncludeID": validation.Validate(i.IncludeID, validation.Required),
"Version": validation.Validate(i.Version, validation.Required),
})
}
// Validate validates ListAvailableBehaviorsRequest
func (i ListAvailableBehaviorsRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"IncludeID": validation.Validate(i.IncludeID, validation.Required),
"Version": validation.Validate(i.Version, validation.Required),
})
}
var (
// ErrCreateIncludeVersion is returned in case an error occurs on CreateIncludeVersion operation
ErrCreateIncludeVersion = errors.New("create an include version")
// ErrGetIncludeVersion is returned in case an error occurs on GetIncludeVersion operation
ErrGetIncludeVersion = errors.New("get an include version")
// ErrListIncludeVersions is returned in case an error occurs on ListIncludeVersions operation
ErrListIncludeVersions = errors.New("list include versions")
// ErrListIncludeVersionAvailableCriteria is returned in case an error occurs on ListIncludeVersionAvailableCriteria operation
ErrListIncludeVersionAvailableCriteria = errors.New("list include version available criteria")
// ErrListIncludeVersionAvailableBehaviors is returned in case an error occurs on ListIncludeVersionAvailableBehaviors operation
ErrListIncludeVersionAvailableBehaviors = errors.New("list include version available behaviors")
)
func (p *papi) CreateIncludeVersion(ctx context.Context, params CreateIncludeVersionRequest) (*CreateIncludeVersionResponse, error) {
logger := p.Log(ctx)
logger.Debug("CreateIncludeVersion")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrCreateIncludeVersion, ErrStructValidation, err)
}
uri := fmt.Sprintf("/papi/v1/includes/%s/versions", params.IncludeID)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrCreateIncludeVersion, err)
}
var result CreateIncludeVersionResponse
resp, err := p.Exec(req, &result, params.IncludeVersionRequest)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrCreateIncludeVersion, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("%s: %w", ErrCreateIncludeVersion, p.Error(resp))
}
id, err := ResponseLinkParse(result.VersionLink)
if err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrCreateIncludeVersion, ErrInvalidResponseLink, err)
}
result.Version, err = strconv.Atoi(id)
if err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrCreateIncludeVersion, ErrInvalidResponseLink, err)
}
return &result, nil
}
func (p *papi) GetIncludeVersion(ctx context.Context, params GetIncludeVersionRequest) (*GetIncludeVersionResponse, error) {
logger := p.Log(ctx)
logger.Debug("GetIncludeVersion")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrGetIncludeVersion, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/papi/v1/includes/%s/versions/%d", params.IncludeID, params.Version))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrGetIncludeVersion, 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", ErrGetIncludeVersion, err)
}
var result GetIncludeVersionResponse
resp, err := p.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetIncludeVersion, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrGetIncludeVersion, p.Error(resp))
}
if len(result.IncludeVersions.Items) == 0 {
return nil, fmt.Errorf("%s: %w: IncludeID: %s", ErrGetIncludeVersion, ErrNotFound, params.IncludeID)
}
result.IncludeVersion = result.IncludeVersions.Items[0]
return &result, nil
}
func (p *papi) ListIncludeVersions(ctx context.Context, params ListIncludeVersionsRequest) (*ListIncludeVersionsResponse, error) {
logger := p.Log(ctx)
logger.Debug("ListIncludeVersions")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrListIncludeVersions, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/papi/v1/includes/%s/versions", params.IncludeID))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrListIncludeVersions, 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", ErrListIncludeVersions, err)
}
var result ListIncludeVersionsResponse
resp, err := p.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListIncludeVersions, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListIncludeVersions, p.Error(resp))
}
return &result, nil
}
func (p *papi) ListIncludeVersionAvailableCriteria(ctx context.Context, params ListAvailableCriteriaRequest) (*AvailableCriteriaResponse, error) {
logger := p.Log(ctx)
logger.Debug("ListIncludeVersionAvailableCriteria")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrListIncludeVersionAvailableCriteria, ErrStructValidation, err)
}
uri := fmt.Sprintf("/papi/v1/includes/%s/versions/%d/available-criteria", params.IncludeID, params.Version)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrListIncludeVersionAvailableCriteria, err)
}
var result AvailableCriteriaResponse
resp, err := p.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListIncludeVersionAvailableCriteria, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListIncludeVersionAvailableCriteria, p.Error(resp))
}
return &result, nil
}
func (p *papi) ListIncludeVersionAvailableBehaviors(ctx context.Context, params ListAvailableBehaviorsRequest) (*AvailableBehaviorsResponse, error) {
logger := p.Log(ctx)
logger.Debug("ListIncludeVersionAvailableBehaviors")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrListIncludeVersionAvailableBehaviors, ErrStructValidation, err)
}
uri := fmt.Sprintf("/papi/v1/includes/%s/versions/%d/available-behaviors", params.IncludeID, params.Version)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrListIncludeVersionAvailableBehaviors, err)
}
var result AvailableBehaviorsResponse
resp, err := p.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListIncludeVersionAvailableBehaviors, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListIncludeVersionAvailableBehaviors, p.Error(resp))
}
return &result, nil
}