-
Notifications
You must be signed in to change notification settings - Fork 69
/
policy_version_activation.go
171 lines (144 loc) · 5.72 KB
/
policy_version_activation.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
package cloudlets
import (
"bytes"
"context"
"errors"
"fmt"
"net/http"
"net/url"
"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 (
// ListPolicyActivationsRequest contains the request parameters for ListPolicyActivations
ListPolicyActivationsRequest struct {
PolicyID int64
Network PolicyActivationNetwork
PropertyName string
}
// ActivatePolicyVersionRequest contains the request parameters for ActivatePolicyVersion
ActivatePolicyVersionRequest struct {
PolicyID int64
Async bool
Version int64
PolicyVersionActivation
}
// PolicyVersionActivation is the body content for an ActivatePolicyVersion request
PolicyVersionActivation struct {
Network PolicyActivationNetwork `json:"network"`
AdditionalPropertyNames []string `json:"additionalPropertyNames,omitempty"`
}
// PolicyActivationNetwork is the activation network type for policy
PolicyActivationNetwork string
)
var (
// ErrListPolicyActivations is returned when ListPolicyActivations fails
ErrListPolicyActivations = errors.New("list policy activations")
// ErrActivatePolicyVersion is returned when ActivatePolicyVersion fails
ErrActivatePolicyVersion = errors.New("activate policy version")
)
const (
// PolicyActivationNetworkStaging is the staging network for policy
PolicyActivationNetworkStaging PolicyActivationNetwork = "staging"
// PolicyActivationNetworkProduction is the production network for policy
PolicyActivationNetworkProduction PolicyActivationNetwork = "prod"
)
// Validate validates ListPolicyActivationsRequest
func (r ListPolicyActivationsRequest) Validate() error {
errs := validation.Errors{
"PolicyID": validation.Validate(r.PolicyID, validation.Required),
"Network": validation.Validate(
r.Network,
validation.In(PolicyActivationNetworkStaging, PolicyActivationNetworkProduction).Error(
fmt.Sprintf("value '%s' is invalid. Must be one of: 'staging', 'prod' or '' (empty)", (&r).Network)),
),
}
return edgegriderr.ParseValidationErrors(errs)
}
// Validate validates ActivatePolicyVersionRequest
func (r ActivatePolicyVersionRequest) Validate() error {
errs := validation.Errors{
"PolicyID": validation.Validate(r.PolicyID, validation.Required),
"Version": validation.Validate(r.Version, validation.Required),
"RequestBody.AdditionalPropertyNames": validation.Validate(r.PolicyVersionActivation.AdditionalPropertyNames, validation.Required),
"RequestBody.Network": validation.Validate(
r.PolicyVersionActivation.Network,
validation.Required,
validation.In(PolicyActivationNetworkStaging, PolicyActivationNetworkProduction).Error(
fmt.Sprintf("value '%s' is invalid. Must be one of: 'staging' or 'prod'", (&r).PolicyVersionActivation.Network)),
),
}
return edgegriderr.ParseValidationErrors(errs)
}
// UnmarshalJSON unifies json network field into well defined values
func (n *PolicyActivationNetwork) UnmarshalJSON(data []byte) error {
d := bytes.Trim(data, "\"")
switch string(d) {
case "STAGING", "staging":
*n = PolicyActivationNetworkStaging
case "PRODUCTION", "production", "prod":
*n = PolicyActivationNetworkProduction
default:
return fmt.Errorf("cannot unmarshall PolicyActivationNetwork: %q", d)
}
return nil
}
func (c *cloudlets) ListPolicyActivations(ctx context.Context, params ListPolicyActivationsRequest) ([]PolicyActivation, error) {
c.Log(ctx).Debug("ListPolicyActivations")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w:\n%s", ErrListPolicyActivations, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/cloudlets/api/v2/policies/%d/activations", params.PolicyID))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrListPolicyActivations, err)
}
q := uri.Query()
if params.Network != "" {
q.Set("network", string(params.Network))
}
if params.PropertyName != "" {
q.Set("propertyName", params.PropertyName)
}
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", ErrListPolicyActivations, err)
}
var result []PolicyActivation
resp, err := c.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListPolicyActivations, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListPolicyActivations, c.Error(resp))
}
return result, nil
}
func (c *cloudlets) ActivatePolicyVersion(ctx context.Context, params ActivatePolicyVersionRequest) ([]PolicyActivation, error) {
c.Log(ctx).Debug("ActivatePolicyVersion")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w:\n%s", ErrActivatePolicyVersion, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf(
"/cloudlets/api/v2/policies/%d/versions/%d/activations",
params.PolicyID, params.Version))
if err != nil {
return nil, fmt.Errorf("%w: failed to create POST URI: %s", ErrActivatePolicyVersion, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create POST request: %s", ErrActivatePolicyVersion, err)
}
var result []PolicyActivation
resp, err := c.Exec(req, &result, params.PolicyVersionActivation)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrActivatePolicyVersion, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode >= http.StatusBadRequest {
return nil, fmt.Errorf("%w: %s", ErrActivatePolicyVersion, c.Error(resp))
}
return result, nil
}