-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathloadbalancer.go
240 lines (196 loc) · 7.35 KB
/
loadbalancer.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
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 (
// OriginResponse is an Origin returned in ListOrigins
OriginResponse struct {
Hostname string `json:"hostname"`
Origin
}
// OriginType is a type for Origin Type
OriginType string
// ListOriginsRequest describes the parameters of the ListOrigins request
ListOriginsRequest struct {
Type OriginType
}
// GetOriginRequest describes the parameters of the get origins request
GetOriginRequest struct {
OriginID string
}
// CreateOriginRequest describes the parameters of the create origin request
CreateOriginRequest struct {
OriginID string `json:"originId"`
Description
}
// UpdateOriginRequest describes the parameters of the update origin request
UpdateOriginRequest struct {
OriginID string
Description
}
// Description describes description for the Origin
Description struct {
Description string `json:"description,omitempty"`
}
// Origin is a response returned by CreateOrigin
Origin struct {
OriginID string `json:"originId"`
Description string `json:"description"`
Akamaized bool `json:"akamaized"`
Checksum string `json:"checksum"`
Type OriginType `json:"type"`
}
)
const (
// OriginTypeAll is a value to use when you want ListOrigins to return origins of all types
OriginTypeAll OriginType = ""
// OriginTypeCustomer is a value to use when you want ListOrigins to return only origins of CUSTOMER type
OriginTypeCustomer OriginType = "CUSTOMER"
// OriginTypeApplicationLoadBalancer is a value to use when you want ListOrigins to return only origins of APPLICATION_LOAD_BALANCER type
OriginTypeApplicationLoadBalancer OriginType = "APPLICATION_LOAD_BALANCER"
// OriginTypeNetStorage is a value to use when you want ListOrigins to return only origins of NETSTORAGE type
OriginTypeNetStorage OriginType = "NETSTORAGE"
)
var (
// ErrListOrigins is returned when ListOrigins fails
ErrListOrigins = errors.New("list origins")
// ErrGetOrigin is returned when GetOrigin fails
ErrGetOrigin = errors.New("get origin")
// ErrCreateOrigin is returned when CreateOrigin fails
ErrCreateOrigin = errors.New("create origin")
// ErrUpdateOrigin is returned when UpdateOrigin fails
ErrUpdateOrigin = errors.New("update origin")
)
// Validate validates ListOriginsRequest
func (v ListOriginsRequest) Validate() error {
errs := validation.Errors{
"Type": validation.Validate(v.Type, validation.In(OriginTypeCustomer, OriginTypeApplicationLoadBalancer, OriginTypeNetStorage, OriginTypeAll).Error(
fmt.Sprintf("value '%s' is invalid. Must be one of: 'CUSTOMER', 'APPLICATION_LOAD_BALANCER', 'NETSTORAGE' or '' (empty)", (&v).Type))),
}
return edgegriderr.ParseValidationErrors(errs)
}
// Validate validates CreateOriginRequest
func (v CreateOriginRequest) Validate() error {
errs := validation.Errors{
"OriginID": validation.Validate(v.OriginID, validation.Required, validation.Length(2, 63)),
"Description": validation.Validate(v.Description.Description, validation.Length(0, 255)),
}
return edgegriderr.ParseValidationErrors(errs)
}
// Validate validates UpdateOriginRequest
func (v UpdateOriginRequest) Validate() error {
errs := validation.Errors{
"OriginID": validation.Validate(v.OriginID, validation.Required, validation.Length(2, 63)),
"Description": validation.Validate(v.Description.Description, validation.Length(0, 255)),
}
return edgegriderr.ParseValidationErrors(errs)
}
func (c *cloudlets) ListOrigins(ctx context.Context, params ListOriginsRequest) ([]OriginResponse, error) {
logger := c.Log(ctx)
logger.Debug("ListOrigins")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w:\n%s", ErrListOrigins, ErrStructValidation, err)
}
uri, err := url.Parse("/cloudlets/api/v2/origins")
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrListOrigins, err)
}
if params.Type != OriginTypeAll {
q := uri.Query()
q.Add("type", string(params.Type))
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 []OriginResponse
resp, err := c.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListOrigins, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListOrigins, c.Error(resp))
}
return result, nil
}
func (c *cloudlets) GetOrigin(ctx context.Context, params GetOriginRequest) (*Origin, error) {
logger := c.Log(ctx)
logger.Debug("GetOrigin")
uri, err := url.Parse(fmt.Sprintf("/cloudlets/api/v2/origins/%s", params.OriginID))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrGetOrigin, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetOrigin, err)
}
var result Origin
resp, err := c.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetOrigin, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrGetOrigin, c.Error(resp))
}
return &result, nil
}
func (c *cloudlets) CreateOrigin(ctx context.Context, params CreateOriginRequest) (*Origin, error) {
logger := c.Log(ctx)
logger.Debug("CreateOrigin")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w:\n%s", ErrCreateOrigin, ErrStructValidation, err)
}
uri, err := url.Parse("/cloudlets/api/v2/origins")
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrCreateOrigin, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrCreateOrigin, err)
}
var result Origin
resp, err := c.Exec(req, &result, params)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrCreateOrigin, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("%s: %w", ErrCreateOrigin, c.Error(resp))
}
return &result, nil
}
func (c *cloudlets) UpdateOrigin(ctx context.Context, params UpdateOriginRequest) (*Origin, error) {
logger := c.Log(ctx)
logger.Debug("UpdateOrigin")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w:\n%s", ErrUpdateOrigin, ErrStructValidation, err)
}
uri, err := url.Parse(fmt.Sprintf("/cloudlets/api/v2/origins/%s", params.OriginID))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrUpdateOrigin, err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri.String(), nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrUpdateOrigin, err)
}
var result Origin
resp, err := c.Exec(req, &result, params.Description)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrUpdateOrigin, err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrUpdateOrigin, c.Error(resp))
}
return &result, nil
}