-
Notifications
You must be signed in to change notification settings - Fork 69
/
edgekv_namespaces.go
268 lines (224 loc) · 9.36 KB
/
edgekv_namespaces.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
package edgeworkers
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/session"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
type (
// ListEdgeKVNamespacesRequest contains path parameters used to list namespaces
ListEdgeKVNamespacesRequest struct {
Network NamespaceNetwork
Details bool
}
// GetEdgeKVNamespaceRequest contains path parameters used to feth a namespace
GetEdgeKVNamespaceRequest struct {
Network NamespaceNetwork
Name string
}
// CreateEdgeKVNamespaceRequest contains path parameter and request body used to create a namespace
CreateEdgeKVNamespaceRequest struct {
Network NamespaceNetwork
Namespace
}
// UpdateEdgeKVNamespaceRequest contains path parameters and request body used to update a namespace
UpdateEdgeKVNamespaceRequest struct {
Network NamespaceNetwork
UpdateNamespace
}
// ListEdgeKVNamespacesResponse represents a response object returned when listing namespaces
ListEdgeKVNamespacesResponse struct {
Namespaces []Namespace `json:"namespaces"`
}
// Namespace represents a namespace object and a request body used to create a namespace
Namespace struct {
Name string `json:"namespace"`
GeoLocation string `json:"geoLocation,omitempty"`
Retention *int `json:"retentionInSeconds,omitempty"`
GroupID *int `json:"groupId,omitempty"`
}
// UpdateNamespace represents a request body used to update a namespace
UpdateNamespace struct {
Name string `json:"namespace"`
Retention *int `json:"retentionInSeconds"`
GroupID *int `json:"groupId"`
}
// NamespaceNetwork represents available namespace network types
NamespaceNetwork string
)
const (
// NamespaceStagingNetwork is the staging network
NamespaceStagingNetwork NamespaceNetwork = "staging"
// NamespaceProductionNetwork is the production network
NamespaceProductionNetwork NamespaceNetwork = "production"
)
// Validate validates ListEdgeKVNamespacesRequest
func (r ListEdgeKVNamespacesRequest) Validate() error {
return validation.Errors{
"Network": validation.Validate(r.Network, validation.Required, validation.In(NamespaceStagingNetwork, NamespaceProductionNetwork).Error(
fmt.Sprintf("value '%s' is invalid. Must be one of: '%s' or '%s'", r.Network, NamespaceStagingNetwork, NamespaceProductionNetwork))),
}.Filter()
}
// Validate validates GetEdgeKVNamespaceRequest
func (r GetEdgeKVNamespaceRequest) Validate() error {
return validation.Errors{
"Network": validation.Validate(r.Network, validation.Required, validation.In(NamespaceStagingNetwork, NamespaceProductionNetwork).Error(
fmt.Sprintf("value '%s' is invalid. Must be one of: '%s' or '%s'", r.Network, NamespaceStagingNetwork, NamespaceProductionNetwork))),
"Name": validation.Validate(r.Name, validation.Required, validation.Length(1, 32)),
}.Filter()
}
// Validate validates CreateEdgeKVNamespaceRequest
func (r CreateEdgeKVNamespaceRequest) Validate() error {
return validation.Errors{
"Network": validation.Validate(r.Network, validation.Required, validation.In(NamespaceStagingNetwork, NamespaceProductionNetwork).Error(
fmt.Sprintf("value '%s' is invalid. Must be one of: '%s' or '%s'", r.Network, NamespaceStagingNetwork, NamespaceProductionNetwork))),
"Name": validation.Validate(r.Name, validation.Required, validation.Length(1, 32)),
"Retention": validation.Validate(r.Retention, validation.By(validateRetention)),
"GroupID": validation.Validate(r.GroupID, validation.By(validateGroupID)),
}.Filter()
}
// Validate validates UpdateEdgeKVNamespaceRequest
func (r UpdateEdgeKVNamespaceRequest) Validate() error {
return validation.Errors{
"Network": validation.Validate(r.Network, validation.Required, validation.In(NamespaceStagingNetwork, NamespaceProductionNetwork).Error(
fmt.Sprintf("value '%s' is invalid. Must be one of: '%s' or '%s'", r.Network, NamespaceStagingNetwork, NamespaceProductionNetwork))),
"Name": validation.Validate(r.Name, validation.Required, validation.Length(1, 32)),
"Retention": validation.Validate(r.Retention, validation.By(validateRetention)),
"GroupID": validation.Validate(r.GroupID, validation.By(validateGroupID)),
}.Filter()
}
func validateRetention(value interface{}) error {
v, ok := value.(*int)
if !ok {
return fmt.Errorf("type %T is invalid. Must be *int", value)
}
if v == nil {
return fmt.Errorf("cannot be blank")
}
if (*v < 86400 && *v != 0) || *v > 315360000 {
return fmt.Errorf("a non zero value specified for retention period cannot be less than 86400 or more than 315360000")
}
return nil
}
func validateGroupID(value interface{}) error {
v, ok := value.(*int)
if !ok {
return fmt.Errorf("type %T is invalid. Must be *int", value)
}
if v == nil {
return fmt.Errorf("cannot be blank")
}
if *v < 0 {
return fmt.Errorf("cannot be less than 0")
}
return nil
}
var (
// ErrListEdgeKVNamespace is returned when ListEdgeKVNamespaces fails
ErrListEdgeKVNamespace = errors.New("list EdgeKV namespaces")
// ErrGetEdgeKVNamespace is returned when GetEdgeKVNamespace fails
ErrGetEdgeKVNamespace = errors.New("get an EdgeKV namespace")
// ErrCreateEdgeKVNamespace is returned when CreateEdgeKVNamespace fails
ErrCreateEdgeKVNamespace = errors.New("create an EdgeKV namespace")
// ErrUpdateEdgeKVNamespace is returned when UpdateEdgeKVNamespace fails
ErrUpdateEdgeKVNamespace = errors.New("update an EdgeKV namespace")
)
func (e *edgeworkers) ListEdgeKVNamespaces(ctx context.Context, params ListEdgeKVNamespacesRequest) (*ListEdgeKVNamespacesResponse, error) {
logger := e.Log(ctx)
logger.Debug("ListEdgeKVNamespaces")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrListEdgeKVNamespace, ErrStructValidation, err.Error())
}
uri, err := url.Parse(fmt.Sprintf("/edgekv/v1/networks/%s/namespaces", params.Network))
if err != nil {
return nil, fmt.Errorf("%w: failed to parse url: %s", ErrListEdgeKVNamespace, err.Error())
}
if params.Details {
q := uri.Query()
q.Add("details", "on")
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", ErrListEdgeKVNamespace, err.Error())
}
var result ListEdgeKVNamespacesResponse
resp, err := e.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrListEdgeKVNamespace, err.Error())
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrListEdgeKVNamespace, e.Error(resp))
}
return &result, nil
}
func (e *edgeworkers) GetEdgeKVNamespace(ctx context.Context, params GetEdgeKVNamespaceRequest) (*Namespace, error) {
logger := e.Log(ctx)
logger.Debug("GetEdgeKVNamespace")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrGetEdgeKVNamespace, ErrStructValidation, err.Error())
}
uri := fmt.Sprintf("/edgekv/v1/networks/%s/namespaces/%s", params.Network, params.Name)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetEdgeKVNamespace, err.Error())
}
var result Namespace
resp, err := e.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrGetEdgeKVNamespace, err.Error())
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrGetEdgeKVNamespace, e.Error(resp))
}
return &result, nil
}
func (e *edgeworkers) CreateEdgeKVNamespace(ctx context.Context, params CreateEdgeKVNamespaceRequest) (*Namespace, error) {
logger := e.Log(ctx)
logger.Debug("CreateEdgeKVNamespace")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrCreateEdgeKVNamespace, ErrStructValidation, err.Error())
}
uri := fmt.Sprintf("/edgekv/v1/networks/%s/namespaces", params.Network)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrCreateEdgeKVNamespace, err.Error())
}
var result Namespace
resp, err := e.Exec(req, &result, params.Namespace)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrCreateEdgeKVNamespace, err.Error())
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrCreateEdgeKVNamespace, e.Error(resp))
}
return &result, nil
}
func (e *edgeworkers) UpdateEdgeKVNamespace(ctx context.Context, params UpdateEdgeKVNamespaceRequest) (*Namespace, error) {
logger := e.Log(ctx)
logger.Debug("UpdateEdgeKVNamespace")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%s: %w: %s", ErrUpdateEdgeKVNamespace, ErrStructValidation, err.Error())
}
uri := fmt.Sprintf("/edgekv/v1/networks/%s/namespaces/%s", params.Network, params.Name)
req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
if err != nil {
return nil, fmt.Errorf("%w: failed to create request: %s", ErrUpdateEdgeKVNamespace, err.Error())
}
var result Namespace
resp, err := e.Exec(req, &result, params.UpdateNamespace)
if err != nil {
return nil, fmt.Errorf("%w: request failed: %s", ErrUpdateEdgeKVNamespace, err.Error())
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%s: %w", ErrUpdateEdgeKVNamespace, e.Error(resp))
}
return &result, nil
}