-
Notifications
You must be signed in to change notification settings - Fork 69
/
properties.go
175 lines (145 loc) · 5.06 KB
/
properties.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
package cloudwrapper
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v7/pkg/edgegriderr"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
type (
// Properties is a CloudWrapper properties API interface
Properties interface {
// ListProperties lists unused properties
//
// See: https://techdocs.akamai.com/cloud-wrapper/reference/get-properties
ListProperties(context.Context, ListPropertiesRequest) (*ListPropertiesResponse, error)
// ListOrigins lists property origins
//
// See: https://techdocs.akamai.com/cloud-wrapper/reference/get-origins
ListOrigins(context.Context, ListOriginsRequest) (*ListOriginsResponse, error)
}
// ListPropertiesRequest holds parameters for ListProperties
ListPropertiesRequest struct {
Unused bool
ContractIDs []string
}
// ListOriginsRequest holds parameters for ListOrigins
ListOriginsRequest struct {
PropertyID int64
ContractID string
GroupID int64
}
// ListPropertiesResponse contains response from ListProperties
ListPropertiesResponse struct {
Properties []Property `json:"properties"`
}
// ListOriginsResponse contains response from ListOrigins
ListOriginsResponse struct {
Children []Child `json:"children"`
Default []Behavior `json:"default"`
}
// Child represents children rules in a property
Child struct {
Name string `json:"name"`
Behaviors []Behavior `json:"behaviors"`
}
// Behavior contains behavior information
Behavior struct {
Hostname string `json:"hostname"`
OriginType OriginType `json:"originType"`
}
// Property represents property object
Property struct {
GroupID int64 `json:"groupId"`
ContractID string `json:"contractId"`
PropertyID int64 `json:"propertyId"`
PropertyName string `json:"propertyName"`
Type PropertyType `json:"type"`
}
// OriginType represents the type of the origin
OriginType string
// PropertyType represents the type of the property
PropertyType string
)
const (
// PropertyTypeWeb is the web type of the property
PropertyTypeWeb PropertyType = "WEB"
// PropertyTypeMedia is the media type of the property
PropertyTypeMedia PropertyType = "MEDIA"
// OriginTypeCustomer is the customer type of the origin
OriginTypeCustomer OriginType = "CUSTOMER"
// OriginTypeNetStorage is the net storage type of the origin
OriginTypeNetStorage OriginType = "NET_STORAGE"
)
// Validate validates ListOriginsRequest
func (r ListOriginsRequest) Validate() error {
return edgegriderr.ParseValidationErrors(validation.Errors{
"PropertyID": validation.Validate(r.PropertyID, validation.Required),
"ContractID": validation.Validate(r.ContractID, validation.Required),
"GroupID": validation.Validate(r.GroupID, validation.Required),
})
}
var (
// ErrListProperties is returned when ListProperties fails
ErrListProperties = errors.New("list properties")
// ErrListOrigins is returned when ListOrigins fails
ErrListOrigins = errors.New("list origins")
)
func (c *cloudwrapper) ListProperties(ctx context.Context, params ListPropertiesRequest) (*ListPropertiesResponse, error) {
logger := c.Log(ctx)
logger.Debug("ListProperties")
uri, err := url.Parse("/cloud-wrapper/v1/properties")
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrListProperties, err)
}
q := uri.Query()
q.Add("unused", strconv.FormatBool(params.Unused))
for _, ctr := range params.ContractIDs {
q.Add("contractIds", ctr)
}
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", ErrListProperties, err)
}
var result ListPropertiesResponse
resp, err := c.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListProperties, err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListProperties, c.Error(resp))
}
return &result, nil
}
func (c *cloudwrapper) ListOrigins(ctx context.Context, params ListOriginsRequest) (*ListOriginsResponse, error) {
logger := c.Log(ctx)
logger.Debug("ListOrigins")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrListOrigins, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/cloud-wrapper/v1/properties/%d/origins", params.PropertyID))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrListOrigins, err)
}
q := uri.Query()
q.Add("contractId", params.ContractID)
q.Add("groupId", strconv.FormatInt(params.GroupID, 10))
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", ErrListOrigins, err)
}
var result ListOriginsResponse
resp, err := c.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListOrigins, err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListOrigins, c.Error(resp))
}
return &result, nil
}