forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests.go
378 lines (330 loc) · 10.9 KB
/
requests.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package services
import (
"fmt"
"strings"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
)
// ListOptsBuilder allows extensions to add additional parameters to the
// List request.
type ListOptsBuilder interface {
ToCDNServiceListQuery() (string, error)
}
// ListOpts allows the filtering and sorting of paginated collections through
// the API. Marker and Limit are used for pagination.
type ListOpts struct {
Marker string `q:"marker"`
Limit int `q:"limit"`
}
// ToCDNServiceListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToCDNServiceListQuery() (string, error) {
q, err := gophercloud.BuildQueryString(opts)
if err != nil {
return "", err
}
return q.String(), nil
}
// List returns a Pager which allows you to iterate over a collection of
// CDN services. It accepts a ListOpts struct, which allows for pagination via
// marker and limit.
func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
url := listURL(c)
if opts != nil {
query, err := opts.ToCDNServiceListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
createPage := func(r pagination.PageResult) pagination.Page {
p := ServicePage{pagination.MarkerPageBase{PageResult: r}}
p.MarkerPageBase.Owner = p
return p
}
pager := pagination.NewPager(c, url, createPage)
return pager
}
// CreateOptsBuilder is the interface options structs have to satisfy in order
// to be used in the main Create operation in this package. Since many
// extensions decorate or modify the common logic, it is useful for them to
// satisfy a basic interface in order for them to be used.
type CreateOptsBuilder interface {
ToCDNServiceCreateMap() (map[string]interface{}, error)
}
// CreateOpts is the common options struct used in this package's Create
// operation.
type CreateOpts struct {
// REQUIRED. Specifies the name of the service. The minimum length for name is
// 3. The maximum length is 256.
Name string
// REQUIRED. Specifies a list of domains used by users to access their website.
Domains []Domain
// REQUIRED. Specifies a list of origin domains or IP addresses where the
// original assets are stored.
Origins []Origin
// REQUIRED. Specifies the CDN provider flavor ID to use. For a list of
// flavors, see the operation to list the available flavors. The minimum
// length for flavor_id is 1. The maximum length is 256.
FlavorID string
// OPTIONAL. Specifies the TTL rules for the assets under this service. Supports wildcards for fine-grained control.
Caching []CacheRule
// OPTIONAL. Specifies the restrictions that define who can access assets (content from the CDN cache).
Restrictions []Restriction
}
// ToCDNServiceCreateMap casts a CreateOpts struct to a map.
func (opts CreateOpts) ToCDNServiceCreateMap() (map[string]interface{}, error) {
s := make(map[string]interface{})
if opts.Name == "" {
return nil, no("Name")
}
s["name"] = opts.Name
if opts.Domains == nil {
return nil, no("Domains")
}
for _, domain := range opts.Domains {
if domain.Domain == "" {
return nil, no("Domains[].Domain")
}
}
s["domains"] = opts.Domains
if opts.Origins == nil {
return nil, no("Origins")
}
for _, origin := range opts.Origins {
if origin.Origin == "" {
return nil, no("Origins[].Origin")
}
if origin.Rules == nil && len(opts.Origins) > 1 {
return nil, no("Origins[].Rules")
}
for _, rule := range origin.Rules {
if rule.Name == "" {
return nil, no("Origins[].Rules[].Name")
}
if rule.RequestURL == "" {
return nil, no("Origins[].Rules[].RequestURL")
}
}
}
s["origins"] = opts.Origins
if opts.FlavorID == "" {
return nil, no("FlavorID")
}
s["flavor_id"] = opts.FlavorID
if opts.Caching != nil {
for _, cache := range opts.Caching {
if cache.Name == "" {
return nil, no("Caching[].Name")
}
if cache.Rules != nil {
for _, rule := range cache.Rules {
if rule.Name == "" {
return nil, no("Caching[].Rules[].Name")
}
if rule.RequestURL == "" {
return nil, no("Caching[].Rules[].RequestURL")
}
}
}
}
s["caching"] = opts.Caching
}
if opts.Restrictions != nil {
for _, restriction := range opts.Restrictions {
if restriction.Name == "" {
return nil, no("Restrictions[].Name")
}
if restriction.Rules != nil {
for _, rule := range restriction.Rules {
if rule.Name == "" {
return nil, no("Restrictions[].Rules[].Name")
}
}
}
}
s["restrictions"] = opts.Restrictions
}
return s, nil
}
// Create accepts a CreateOpts struct and creates a new CDN service using the
// values provided.
func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
var res CreateResult
reqBody, err := opts.ToCDNServiceCreateMap()
if err != nil {
res.Err = err
return res
}
// Send request to API
resp, err := c.Post(createURL(c), &reqBody, nil, nil)
res.Header = resp.Header
res.Err = err
return res
}
// Get retrieves a specific service based on its URL or its unique ID. For
// example, both "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0" and
// "https://global.cdn.api.rackspacecloud.com/v1.0/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0"
// are valid options for idOrURL.
func Get(c *gophercloud.ServiceClient, idOrURL string) GetResult {
var url string
if strings.Contains(idOrURL, "/") {
url = idOrURL
} else {
url = getURL(c, idOrURL)
}
var res GetResult
_, res.Err = c.Get(url, &res.Body, nil)
return res
}
// Path is a JSON pointer location that indicates which service parameter is being added, replaced,
// or removed.
type Path struct {
baseElement string
}
func (p Path) renderRoot() string {
return "/" + p.baseElement
}
func (p Path) renderDash() string {
return fmt.Sprintf("/%s/-", p.baseElement)
}
func (p Path) renderIndex(index int64) string {
return fmt.Sprintf("/%s/%d", p.baseElement, index)
}
var (
// PathDomains indicates that an update operation is to be performed on a Domain.
PathDomains = Path{baseElement: "domains"}
// PathOrigins indicates that an update operation is to be performed on an Origin.
PathOrigins = Path{baseElement: "origins"}
// PathCaching indicates that an update operation is to be performed on a CacheRule.
PathCaching = Path{baseElement: "caching"}
)
type value interface {
toPatchValue() interface{}
appropriatePath() Path
renderRootOr(func(p Path) string) string
}
// Patch represents a single update to an existing Service. Multiple updates to a service can be
// submitted at the same time.
type Patch interface {
ToCDNServiceUpdateMap() map[string]interface{}
}
// Insertion is a Patch that requests the addition of a value (Domain, Origin, or CacheRule) to
// a Service at a fixed index. Use an Append instead to append the new value to the end of its
// collection. Pass it to the Update function as part of the Patch slice.
type Insertion struct {
Index int64
Value value
}
// ToCDNServiceUpdateMap converts an Insertion into a request body fragment suitable for the
// Update call.
func (i Insertion) ToCDNServiceUpdateMap() map[string]interface{} {
return map[string]interface{}{
"op": "add",
"path": i.Value.renderRootOr(func(p Path) string { return p.renderIndex(i.Index) }),
"value": i.Value.toPatchValue(),
}
}
// Append is a Patch that requests the addition of a value (Domain, Origin, or CacheRule) to a
// Service at the end of its respective collection. Use an Insertion instead to insert the value
// at a fixed index within the collection. Pass this to the Update function as part of its
// Patch slice.
type Append struct {
Value value
}
// ToCDNServiceUpdateMap converts an Append into a request body fragment suitable for the
// Update call.
func (a Append) ToCDNServiceUpdateMap() map[string]interface{} {
return map[string]interface{}{
"op": "add",
"path": a.Value.renderRootOr(func(p Path) string { return p.renderDash() }),
"value": a.Value.toPatchValue(),
}
}
// Replacement is a Patch that alters a specific service parameter (Domain, Origin, or CacheRule)
// in-place by index. Pass it to the Update function as part of the Patch slice.
type Replacement struct {
Value value
Index int64
}
// ToCDNServiceUpdateMap converts a Replacement into a request body fragment suitable for the
// Update call.
func (r Replacement) ToCDNServiceUpdateMap() map[string]interface{} {
return map[string]interface{}{
"op": "replace",
"path": r.Value.renderRootOr(func(p Path) string { return p.renderIndex(r.Index) }),
"value": r.Value.toPatchValue(),
}
}
// NameReplacement specifically updates the Service name. Pass it to the Update function as part
// of the Patch slice.
type NameReplacement struct {
NewName string
}
// ToCDNServiceUpdateMap converts a NameReplacement into a request body fragment suitable for the
// Update call.
func (r NameReplacement) ToCDNServiceUpdateMap() map[string]interface{} {
return map[string]interface{}{
"op": "replace",
"path": "/name",
"value": r.NewName,
}
}
// Removal is a Patch that requests the removal of a service parameter (Domain, Origin, or
// CacheRule) by index. Pass it to the Update function as part of the Patch slice.
type Removal struct {
Path Path
Index int64
All bool
}
// ToCDNServiceUpdateMap converts a Removal into a request body fragment suitable for the
// Update call.
func (r Removal) ToCDNServiceUpdateMap() map[string]interface{} {
result := map[string]interface{}{"op": "remove"}
if r.All {
result["path"] = r.Path.renderRoot()
} else {
result["path"] = r.Path.renderIndex(r.Index)
}
return result
}
type UpdateOpts []Patch
// Update accepts a slice of Patch operations (Insertion, Append, Replacement or Removal) and
// updates an existing CDN service using the values provided. idOrURL can be either the service's
// URL or its ID. For example, both "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0" and
// "https://global.cdn.api.rackspacecloud.com/v1.0/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0"
// are valid options for idOrURL.
func Update(c *gophercloud.ServiceClient, idOrURL string, opts UpdateOpts) UpdateResult {
var url string
if strings.Contains(idOrURL, "/") {
url = idOrURL
} else {
url = updateURL(c, idOrURL)
}
reqBody := make([]map[string]interface{}, len(opts))
for i, patch := range opts {
reqBody[i] = patch.ToCDNServiceUpdateMap()
}
resp, err := c.Request("PATCH", url, gophercloud.RequestOpts{
JSONBody: &reqBody,
OkCodes: []int{202},
})
var result UpdateResult
result.Header = resp.Header
result.Err = err
return result
}
// Delete accepts a service's ID or its URL and deletes the CDN service
// associated with it. For example, both "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0" and
// "https://global.cdn.api.rackspacecloud.com/v1.0/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0"
// are valid options for idOrURL.
func Delete(c *gophercloud.ServiceClient, idOrURL string) DeleteResult {
var url string
if strings.Contains(idOrURL, "/") {
url = idOrURL
} else {
url = deleteURL(c, idOrURL)
}
var res DeleteResult
_, res.Err = c.Delete(url, nil)
return res
}