-
Notifications
You must be signed in to change notification settings - Fork 70
/
policy_version.go
301 lines (249 loc) · 10.3 KB
/
policy_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
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
package cloudlets
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 (
// PolicyVersion is response returned by GetPolicyVersion, CreatePolicyVersion or UpdatePolicyVersion
PolicyVersion struct {
Location string `json:"location"`
RevisionID int64 `json:"revisionId"`
PolicyID int64 `json:"policyId"`
Version int64 `json:"version"`
Description string `json:"description"`
CreatedBy string `json:"createdBy"`
CreateDate int64 `json:"createDate"`
LastModifiedBy string `json:"lastModifiedBy"`
LastModifiedDate int64 `json:"lastModifiedDate"`
RulesLocked bool `json:"rulesLocked"`
Activations []PolicyActivation `json:"activations"`
MatchRules MatchRules `json:"matchRules"`
MatchRuleFormat MatchRuleFormat `json:"matchRuleFormat"`
Deleted bool `json:"deleted,omitempty"`
Warnings []Warning `json:"warnings,omitempty"`
}
// ListPolicyVersionsRequest describes the parameters needed to list policy versions
ListPolicyVersionsRequest struct {
PolicyID int64
IncludeRules bool
IncludeDeleted bool
IncludeActivations bool
Offset int
PageSize *int
}
// GetPolicyVersionRequest describes the parameters needed to get policy version
GetPolicyVersionRequest struct {
PolicyID int64
Version int64
OmitRules bool
}
// CreatePolicyVersionRequest describes the body of the create policy request
CreatePolicyVersionRequest struct {
CreatePolicyVersion
PolicyID int64
}
// CreatePolicyVersion describes the body of the create policy request
CreatePolicyVersion struct {
Description string `json:"description,omitempty"`
MatchRuleFormat MatchRuleFormat `json:"matchRuleFormat,omitempty"`
MatchRules MatchRules `json:"matchRules"`
}
// UpdatePolicyVersion describes the body of the update policy version request
UpdatePolicyVersion struct {
Description string `json:"description,omitempty"`
MatchRuleFormat MatchRuleFormat `json:"matchRuleFormat,omitempty"`
MatchRules MatchRules `json:"matchRules"`
Deleted bool `json:"deleted"`
}
// DeletePolicyVersionRequest describes the parameters of the delete policy version request
DeletePolicyVersionRequest struct {
PolicyID int64
Version int64
}
// UpdatePolicyVersionRequest describes the parameters of the update policy version request
UpdatePolicyVersionRequest struct {
UpdatePolicyVersion
PolicyID int64
Version int64
}
)
// Validate validates ListPolicyVersionsRequest
func (c ListPolicyVersionsRequest) Validate() error {
errs := validation.Errors{
"PolicyID": validation.Validate(c.PolicyID, validation.Required),
"Offset": validation.Validate(c.Offset, validation.Min(0)),
}
return edgegriderr.ParseValidationErrors(errs)
}
// Validate validates CreatePolicyVersionRequest
func (c CreatePolicyVersionRequest) Validate() error {
errs := validation.Errors{
"Description": validation.Validate(c.Description, validation.Length(0, 255)),
"MatchRuleFormat": validation.Validate(c.MatchRuleFormat, validation.In(MatchRuleFormat10).Error(
fmt.Sprintf("value '%s' is invalid. Must be one of: '1.0' or '' (empty)", (&c).MatchRuleFormat))),
"MatchRules": validation.Validate(c.MatchRules, validation.Length(0, 5000)),
}
return edgegriderr.ParseValidationErrors(errs)
}
// Validate validates UpdatePolicyVersionRequest
func (o UpdatePolicyVersionRequest) Validate() error {
errs := validation.Errors{
"Description": validation.Validate(o.Description, validation.Length(0, 255)),
"MatchRuleFormat": validation.Validate(o.MatchRuleFormat, validation.In(MatchRuleFormat10).Error(
fmt.Sprintf("value '%s' is invalid. Must be one of: '1.0' or '' (empty)", (&o).MatchRuleFormat))),
"MatchRules": validation.Validate(o.MatchRules, validation.Length(0, 5000)),
}
return edgegriderr.ParseValidationErrors(errs)
}
var (
// ErrListPolicyVersions is returned when ListPolicyVersions fails
ErrListPolicyVersions = errors.New("list policy versions")
// ErrGetPolicyVersion is returned when GetPolicyVersion fails
ErrGetPolicyVersion = errors.New("get policy versions")
// ErrCreatePolicyVersion is returned when CreatePolicyVersion fails
ErrCreatePolicyVersion = errors.New("create policy versions")
// ErrDeletePolicyVersion is returned when DeletePolicyVersion fails
ErrDeletePolicyVersion = errors.New("delete policy versions")
// ErrUpdatePolicyVersion is returned when UpdatePolicyVersion fails
ErrUpdatePolicyVersion = errors.New("update policy versions")
)
func (c *cloudlets) ListPolicyVersions(ctx context.Context, params ListPolicyVersionsRequest) ([]PolicyVersion, error) {
logger := c.Log(ctx)
logger.Debug("ListPolicyVersions")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w:\n%s", ErrListPolicyVersions, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/cloudlets/api/v2/policies/%d/versions", params.PolicyID))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrListPolicyVersions, err)
}
q := uri.Query()
q.Add("offset", fmt.Sprintf("%d", params.Offset))
q.Add("includeRules", strconv.FormatBool(params.IncludeRules))
q.Add("includeDeleted", strconv.FormatBool(params.IncludeDeleted))
q.Add("includeActivations", strconv.FormatBool(params.IncludeActivations))
if params.PageSize != nil {
q.Add("pageSize", fmt.Sprintf("%d", *params.PageSize))
}
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", ErrListPolicyVersions, err)
}
var result []PolicyVersion
resp, err := c.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListPolicyVersions, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListPolicyVersions, c.Error(resp))
}
return result, nil
}
func (c *cloudlets) GetPolicyVersion(ctx context.Context, params GetPolicyVersionRequest) (*PolicyVersion, error) {
logger := c.Log(ctx)
logger.Debug("GetPolicyVersion")
var result PolicyVersion
uri, err := url.Parse(fmt.Sprintf(
"/cloudlets/api/v2/policies/%d/versions/%d",
params.PolicyID, params.Version),
)
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrGetPolicyVersion, err)
}
q := uri.Query()
q.Add("omitRules", strconv.FormatBool(params.OmitRules))
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", ErrGetPolicyVersion, err)
}
resp, err := c.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetPolicyVersion, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrGetPolicyVersion, c.Error(resp))
}
return &result, nil
}
func (c *cloudlets) CreatePolicyVersion(ctx context.Context, params CreatePolicyVersionRequest) (*PolicyVersion, error) {
logger := c.Log(ctx)
logger.Debug("CreatePolicyVersion")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w:\n%s", ErrCreatePolicyVersion, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/cloudlets/api/v2/policies/%d/versions", params.PolicyID))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrCreatePolicyVersion, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrCreatePolicyVersion, err)
}
var result PolicyVersion
resp, err := c.Exec(req, &result, params.CreatePolicyVersion)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrCreatePolicyVersion, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("%s: %w", ErrCreatePolicyVersion, c.Error(resp))
}
return &result, nil
}
func (c *cloudlets) DeletePolicyVersion(ctx context.Context, params DeletePolicyVersionRequest) error {
logger := c.Log(ctx)
logger.Debug("DeletePolicyVersion")
uri, err := url.Parse(fmt.Sprintf("/cloudlets/api/v2/policies/%d/versions/%d", params.PolicyID, params.Version))
if err != nil {
return fmt.Errorf("%w: failed to parse url: %s", ErrDeletePolicyVersion, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, uri.String(), nil)
if err != nil {
return fmt.Errorf("%w: failed to create request: %s", ErrDeletePolicyVersion, err)
}
resp, err := c.Exec(req, nil)
if err != nil {
return fmt.Errorf("%w: request failed: %s", ErrDeletePolicyVersion, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("%s: %w", ErrDeletePolicyVersion, c.Error(resp))
}
return nil
}
func (c *cloudlets) UpdatePolicyVersion(ctx context.Context, params UpdatePolicyVersionRequest) (*PolicyVersion, error) {
logger := c.Log(ctx)
logger.Debug("UpdatePolicyVersion")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w:\n%s", ErrUpdatePolicyVersion, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/cloudlets/api/v2/policies/%d/versions/%d", params.PolicyID, params.Version))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrUpdatePolicyVersion, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrUpdatePolicyVersion, err)
}
var result PolicyVersion
resp, err := c.Exec(req, &result, params.UpdatePolicyVersion)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrUpdatePolicyVersion, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrUpdatePolicyVersion, c.Error(resp))
}
return &result, nil
}