-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathpolicy_property.go
137 lines (114 loc) · 4.37 KB
/
policy_property.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
package cloudlets
import (
"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 (
// GetPolicyPropertiesRequest contains request parameters for GetPolicyPropertiesRequest
GetPolicyPropertiesRequest struct {
PolicyID int64
}
// PolicyProperty contains the response data for a single property
PolicyProperty struct {
GroupID int64 `json:"groupId"`
ID int64 `json:"id"`
Name string `json:"name"`
NewestVersion NetworkStatus `json:"newestVersion"`
Production NetworkStatus `json:"production"`
Staging NetworkStatus `json:"staging"`
}
// NetworkStatus is the type for NetworkStatus of any activation
NetworkStatus struct {
ActivatedBy string `json:"activatedBy"`
ActivationDate string `json:"activationDate"`
Version int64 `json:"version"`
CloudletsOrigins map[string]CloudletsOrigin `json:"cloudletsOrigins"`
ReferencedPolicies []string `json:"referencedPolicies"`
}
//nolint:revive
// CloudletsOrigin is the type for CloudletsOrigins in NetworkStatus
CloudletsOrigin struct {
OriginID string `json:"id"`
Hostname string `json:"hostname"`
Type OriginType `json:"type"`
Checksum string `json:"checksum"`
Description string `json:"description"`
}
// DeletePolicyPropertyRequest contains the request parameters for DeletePolicyProperty
DeletePolicyPropertyRequest struct {
PolicyID int64
PropertyID int64
Network PolicyActivationNetwork
}
)
var (
// ErrGetPolicyProperties is returned when GetPolicyProperties fails
ErrGetPolicyProperties = errors.New("get policy properties")
// ErrDeletePolicyProperty is returned when DeletePolicyProperty fails
ErrDeletePolicyProperty = errors.New("delete policy property")
)
// Validate validates DeletePolicyPropertyRequest
func (r DeletePolicyPropertyRequest) Validate() error {
errs := validation.Errors{
"PolicyID": validation.Validate(r.PolicyID, validation.Required),
"PropertyID": validation.Validate(r.PropertyID, validation.Required),
}
return edgegriderr.ParseValidationErrors(errs)
}
func (c *cloudlets) GetPolicyProperties(ctx context.Context, params GetPolicyPropertiesRequest) (map[string]PolicyProperty, error) {
logger := c.Log(ctx)
logger.Debug("GetPolicyProperties")
uri, err := url.Parse(fmt.Sprintf("/cloudlets/api/v2/policies/%d/properties", params.PolicyID))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrGetPolicyProperties, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetPolicyProperties, err)
}
var result map[string]PolicyProperty
resp, err := c.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetPolicyProperties, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrGetPolicyProperties, c.Error(resp))
}
return result, nil
}
func (c *cloudlets) DeletePolicyProperty(ctx context.Context, params DeletePolicyPropertyRequest) error {
c.Log(ctx).Debug("DeletePolicyProperty")
if err := params.Validate(); err != nil {
return fmt.Errorf("%s: %w:\n%s", ErrDeletePolicyProperty, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/cloudlets/api/v2/policies/%d/properties/%d", params.PolicyID, params.PropertyID))
if err != nil {
return fmt.Errorf("%w: failed to parse url: %s", ErrDeletePolicyProperty, err.Error())
}
q := uri.Query()
q.Set("async", "true")
if params.Network != "" {
q.Set("network", string(params.Network))
}
uri.RawQuery = q.Encode()
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, uri.String(), nil)
if err != nil {
return fmt.Errorf("%w: failed to create request: %s", ErrDeletePolicyProperty, err)
}
resp, err := c.Exec(req, nil)
if err != nil {
return fmt.Errorf("%w: request failed: %s", ErrDeletePolicyProperty, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("%w: %d", ErrDeletePolicyProperty, resp.StatusCode)
}
return nil
}