Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 1 | package services |
| 2 | |
| 3 | import ( |
Jon Perritt | b0ab0d1 | 2015-01-27 12:12:51 -0700 | [diff] [blame] | 4 | "fmt" |
Jon Perritt | b8713ad | 2015-01-21 15:02:58 -0700 | [diff] [blame] | 5 | "strings" |
| 6 | |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 7 | "github.com/racker/perigee" |
| 8 | "github.com/rackspace/gophercloud" |
| 9 | "github.com/rackspace/gophercloud/pagination" |
| 10 | ) |
| 11 | |
| 12 | // ListOptsBuilder allows extensions to add additional parameters to the |
| 13 | // List request. |
| 14 | type ListOptsBuilder interface { |
| 15 | ToCDNServiceListQuery() (string, error) |
| 16 | } |
| 17 | |
| 18 | // ListOpts allows the filtering and sorting of paginated collections through |
| 19 | // the API. Marker and Limit are used for pagination. |
| 20 | type ListOpts struct { |
| 21 | Marker string `q:"marker"` |
| 22 | Limit int `q:"limit"` |
| 23 | } |
| 24 | |
| 25 | // ToCDNServiceListQuery formats a ListOpts into a query string. |
| 26 | func (opts ListOpts) ToCDNServiceListQuery() (string, error) { |
| 27 | q, err := gophercloud.BuildQueryString(opts) |
| 28 | if err != nil { |
| 29 | return "", err |
| 30 | } |
| 31 | return q.String(), nil |
| 32 | } |
| 33 | |
| 34 | // List returns a Pager which allows you to iterate over a collection of |
| 35 | // CDN services. It accepts a ListOpts struct, which allows for pagination via |
| 36 | // marker and limit. |
| 37 | func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
| 38 | url := listURL(c) |
| 39 | if opts != nil { |
| 40 | query, err := opts.ToCDNServiceListQuery() |
| 41 | if err != nil { |
| 42 | return pagination.Pager{Err: err} |
| 43 | } |
| 44 | url += query |
| 45 | } |
| 46 | |
| 47 | createPage := func(r pagination.PageResult) pagination.Page { |
| 48 | p := ServicePage{pagination.MarkerPageBase{PageResult: r}} |
| 49 | p.MarkerPageBase.Owner = p |
| 50 | return p |
| 51 | } |
| 52 | |
| 53 | pager := pagination.NewPager(c, url, createPage) |
| 54 | return pager |
| 55 | } |
| 56 | |
| 57 | // CreateOptsBuilder is the interface options structs have to satisfy in order |
| 58 | // to be used in the main Create operation in this package. Since many |
| 59 | // extensions decorate or modify the common logic, it is useful for them to |
| 60 | // satisfy a basic interface in order for them to be used. |
| 61 | type CreateOptsBuilder interface { |
| 62 | ToCDNServiceCreateMap() (map[string]interface{}, error) |
| 63 | } |
| 64 | |
| 65 | // CreateOpts is the common options struct used in this package's Create |
| 66 | // operation. |
| 67 | type CreateOpts struct { |
| 68 | // REQUIRED. Specifies the name of the service. The minimum length for name is |
| 69 | // 3. The maximum length is 256. |
| 70 | Name string |
| 71 | // REQUIRED. Specifies a list of domains used by users to access their website. |
| 72 | Domains []Domain |
| 73 | // REQUIRED. Specifies a list of origin domains or IP addresses where the |
| 74 | // original assets are stored. |
| 75 | Origins []Origin |
| 76 | // REQUIRED. Specifies the CDN provider flavor ID to use. For a list of |
| 77 | // flavors, see the operation to list the available flavors. The minimum |
| 78 | // length for flavor_id is 1. The maximum length is 256. |
| 79 | FlavorID string |
| 80 | // OPTIONAL. Specifies the TTL rules for the assets under this service. Supports wildcards for fine-grained control. |
Jon Perritt | 0bd2373 | 2015-01-19 20:58:57 -0700 | [diff] [blame] | 81 | Caching []CacheRule |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 82 | // OPTIONAL. Specifies the restrictions that define who can access assets (content from the CDN cache). |
| 83 | Restrictions []Restriction |
| 84 | } |
| 85 | |
| 86 | // ToCDNServiceCreateMap casts a CreateOpts struct to a map. |
| 87 | func (opts CreateOpts) ToCDNServiceCreateMap() (map[string]interface{}, error) { |
| 88 | s := make(map[string]interface{}) |
| 89 | |
| 90 | if opts.Name == "" { |
| 91 | return nil, no("Name") |
| 92 | } |
| 93 | s["name"] = opts.Name |
| 94 | |
| 95 | if opts.Domains == nil { |
| 96 | return nil, no("Domains") |
| 97 | } |
| 98 | for _, domain := range opts.Domains { |
| 99 | if domain.Domain == "" { |
| 100 | return nil, no("Domains[].Domain") |
| 101 | } |
| 102 | } |
| 103 | s["domains"] = opts.Domains |
| 104 | |
| 105 | if opts.Origins == nil { |
| 106 | return nil, no("Origins") |
| 107 | } |
| 108 | for _, origin := range opts.Origins { |
| 109 | if origin.Origin == "" { |
| 110 | return nil, no("Origins[].Origin") |
| 111 | } |
Jon Perritt | b8713ad | 2015-01-21 15:02:58 -0700 | [diff] [blame] | 112 | if origin.Rules == nil && len(opts.Origins) > 1 { |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 113 | return nil, no("Origins[].Rules") |
| 114 | } |
| 115 | for _, rule := range origin.Rules { |
| 116 | if rule.Name == "" { |
| 117 | return nil, no("Origins[].Rules[].Name") |
| 118 | } |
| 119 | if rule.RequestURL == "" { |
| 120 | return nil, no("Origins[].Rules[].RequestURL") |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | s["origins"] = opts.Origins |
| 125 | |
| 126 | if opts.FlavorID == "" { |
| 127 | return nil, no("FlavorID") |
| 128 | } |
| 129 | s["flavor_id"] = opts.FlavorID |
| 130 | |
| 131 | if opts.Caching != nil { |
| 132 | for _, cache := range opts.Caching { |
| 133 | if cache.Name == "" { |
| 134 | return nil, no("Caching[].Name") |
| 135 | } |
| 136 | if cache.Rules != nil { |
| 137 | for _, rule := range cache.Rules { |
| 138 | if rule.Name == "" { |
| 139 | return nil, no("Caching[].Rules[].Name") |
| 140 | } |
| 141 | if rule.RequestURL == "" { |
| 142 | return nil, no("Caching[].Rules[].RequestURL") |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | s["caching"] = opts.Caching |
| 148 | } |
| 149 | |
| 150 | if opts.Restrictions != nil { |
| 151 | for _, restriction := range opts.Restrictions { |
| 152 | if restriction.Name == "" { |
| 153 | return nil, no("Restrictions[].Name") |
| 154 | } |
| 155 | if restriction.Rules != nil { |
| 156 | for _, rule := range restriction.Rules { |
| 157 | if rule.Name == "" { |
| 158 | return nil, no("Restrictions[].Rules[].Name") |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | s["restrictions"] = opts.Restrictions |
| 164 | } |
| 165 | |
| 166 | return s, nil |
| 167 | } |
| 168 | |
| 169 | // Create accepts a CreateOpts struct and creates a new CDN service using the |
| 170 | // values provided. |
| 171 | func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
| 172 | var res CreateResult |
| 173 | |
| 174 | reqBody, err := opts.ToCDNServiceCreateMap() |
| 175 | if err != nil { |
| 176 | res.Err = err |
| 177 | return res |
| 178 | } |
| 179 | |
| 180 | // Send request to API |
Jon Perritt | d21966f | 2015-01-20 19:22:45 -0700 | [diff] [blame] | 181 | resp, err := perigee.Request("POST", createURL(c), perigee.Options{ |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 182 | MoreHeaders: c.AuthenticatedHeaders(), |
| 183 | ReqBody: &reqBody, |
| 184 | OkCodes: []int{202}, |
| 185 | }) |
Jon Perritt | d21966f | 2015-01-20 19:22:45 -0700 | [diff] [blame] | 186 | res.Header = resp.HttpResponse.Header |
| 187 | res.Err = err |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 188 | return res |
| 189 | } |
| 190 | |
Jon Perritt | b8713ad | 2015-01-21 15:02:58 -0700 | [diff] [blame] | 191 | // Get retrieves a specific service based on its URL or its unique ID. For |
| 192 | // example, both "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0" and |
| 193 | // "https://global.cdn.api.rackspacecloud.com/v1.0/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0" |
| 194 | // are valid options for idOrURL. |
| 195 | func Get(c *gophercloud.ServiceClient, idOrURL string) GetResult { |
| 196 | var url string |
| 197 | if strings.Contains(idOrURL, "/") { |
| 198 | url = idOrURL |
| 199 | } else { |
| 200 | url = getURL(c, idOrURL) |
| 201 | } |
| 202 | |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 203 | var res GetResult |
Jon Perritt | b8713ad | 2015-01-21 15:02:58 -0700 | [diff] [blame] | 204 | _, res.Err = perigee.Request("GET", url, perigee.Options{ |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 205 | MoreHeaders: c.AuthenticatedHeaders(), |
| 206 | Results: &res.Body, |
| 207 | OkCodes: []int{200}, |
| 208 | }) |
| 209 | return res |
| 210 | } |
| 211 | |
Ash Wilson | a623ff7 | 2015-01-28 15:50:37 -0500 | [diff] [blame] | 212 | // Path is a JSON pointer location that indicates which service parameter is being added, replaced, |
| 213 | // or removed. |
| 214 | type Path struct { |
| 215 | baseElement string |
| 216 | } |
| 217 | |
Ash Wilson | 0528070 | 2015-01-29 11:19:25 -0500 | [diff] [blame^] | 218 | func (p Path) renderRoot() string { |
| 219 | return "/" + p.baseElement |
| 220 | } |
| 221 | |
Ash Wilson | a623ff7 | 2015-01-28 15:50:37 -0500 | [diff] [blame] | 222 | func (p Path) renderDash() string { |
| 223 | return fmt.Sprintf("/%s/-", p.baseElement) |
| 224 | } |
| 225 | |
| 226 | func (p Path) renderIndex(index int64) string { |
| 227 | return fmt.Sprintf("/%s/%d", p.baseElement, index) |
| 228 | } |
| 229 | |
| 230 | var ( |
| 231 | // PathDomains indicates that an update operation is to be performed on a Domain. |
| 232 | PathDomains = Path{baseElement: "domains"} |
| 233 | |
| 234 | // PathOrigins indicates that an update operation is to be performed on an Origin. |
| 235 | PathOrigins = Path{baseElement: "origins"} |
| 236 | |
| 237 | // PathCaching indicates that an update operation is to be performed on a CacheRule. |
| 238 | PathCaching = Path{baseElement: "caching"} |
| 239 | ) |
| 240 | |
Ash Wilson | 4ee0501 | 2015-01-28 16:13:43 -0500 | [diff] [blame] | 241 | type value interface { |
Ash Wilson | b47ebed | 2015-01-29 11:08:41 -0500 | [diff] [blame] | 242 | toPatchValue() interface{} |
Ash Wilson | 4ee0501 | 2015-01-28 16:13:43 -0500 | [diff] [blame] | 243 | appropriatePath() Path |
Ash Wilson | 0528070 | 2015-01-29 11:19:25 -0500 | [diff] [blame^] | 244 | renderRootOr(func(p Path) string) string |
Ash Wilson | 4ee0501 | 2015-01-28 16:13:43 -0500 | [diff] [blame] | 245 | } |
| 246 | |
Ash Wilson | 7b72953 | 2015-01-28 16:15:23 -0500 | [diff] [blame] | 247 | // Patch represents a single update to an existing Service. Multiple updates to a service can be |
| 248 | // submitted at the same time. |
| 249 | type Patch interface { |
| 250 | ToCDNServiceUpdateMap() map[string]interface{} |
| 251 | } |
| 252 | |
Ash Wilson | 299363d | 2015-01-29 10:49:40 -0500 | [diff] [blame] | 253 | // Insertion is a Patch that requests the addition of a value (Domain, Origin, or CacheRule) to |
| 254 | // a Service at a fixed index. Use an Append instead to append the new value to the end of its |
| 255 | // collection. Pass it to the Update function as part of the Patch slice. |
| 256 | type Insertion struct { |
| 257 | Index int64 |
Ash Wilson | 334277c | 2015-01-29 09:08:52 -0500 | [diff] [blame] | 258 | Value value |
| 259 | } |
| 260 | |
Ash Wilson | 299363d | 2015-01-29 10:49:40 -0500 | [diff] [blame] | 261 | // ToCDNServiceUpdateMap converts an Insertion into a request body fragment suitable for the |
Ash Wilson | 334277c | 2015-01-29 09:08:52 -0500 | [diff] [blame] | 262 | // Update call. |
Ash Wilson | 299363d | 2015-01-29 10:49:40 -0500 | [diff] [blame] | 263 | func (i Insertion) ToCDNServiceUpdateMap() map[string]interface{} { |
| 264 | return map[string]interface{}{ |
| 265 | "op": "add", |
Ash Wilson | 0528070 | 2015-01-29 11:19:25 -0500 | [diff] [blame^] | 266 | "path": i.Value.renderRootOr(func(p Path) string { return p.renderIndex(i.Index) }), |
Ash Wilson | 299363d | 2015-01-29 10:49:40 -0500 | [diff] [blame] | 267 | "value": i.Value.toPatchValue(), |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | // Append is a Patch that requests the addition of a value (Domain, Origin, or CacheRule) to a |
| 272 | // Service at the end of its respective collection. Use an Insertion instead to insert the value |
| 273 | // at a fixed index within the collection. Pass this to the Update function as part of its |
| 274 | // Patch slice. |
| 275 | type Append struct { |
| 276 | Value value |
| 277 | } |
| 278 | |
| 279 | // ToCDNServiceUpdateMap converts an Append into a request body fragment suitable for the |
| 280 | // Update call. |
| 281 | func (a Append) ToCDNServiceUpdateMap() map[string]interface{} { |
Ash Wilson | 334277c | 2015-01-29 09:08:52 -0500 | [diff] [blame] | 282 | return map[string]interface{}{ |
| 283 | "op": "add", |
Ash Wilson | 0528070 | 2015-01-29 11:19:25 -0500 | [diff] [blame^] | 284 | "path": a.Value.renderRootOr(func(p Path) string { return p.renderDash() }), |
Ash Wilson | 334277c | 2015-01-29 09:08:52 -0500 | [diff] [blame] | 285 | "value": a.Value.toPatchValue(), |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | // Replacement is a Patch that alters a specific service parameter (Domain, Origin, or CacheRule) |
| 290 | // in-place by index. Pass it to the Update function as part of the Patch slice. |
| 291 | type Replacement struct { |
| 292 | Value value |
| 293 | Index int64 |
| 294 | } |
| 295 | |
| 296 | // ToCDNServiceUpdateMap converts a Replacement into a request body fragment suitable for the |
| 297 | // Update call. |
| 298 | func (r Replacement) ToCDNServiceUpdateMap() map[string]interface{} { |
| 299 | return map[string]interface{}{ |
| 300 | "op": "replace", |
Ash Wilson | 0528070 | 2015-01-29 11:19:25 -0500 | [diff] [blame^] | 301 | "path": r.Value.renderRootOr(func(p Path) string { return p.renderIndex(r.Index) }), |
Ash Wilson | 334277c | 2015-01-29 09:08:52 -0500 | [diff] [blame] | 302 | "value": r.Value.toPatchValue(), |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | // Removal is a Patch that requests the removal of a service parameter (Domain, Origin, or |
| 307 | // CacheRule) by index. Pass it to the Update function as part of the Patch slice. |
| 308 | type Removal struct { |
| 309 | Path Path |
| 310 | Index int64 |
| 311 | } |
| 312 | |
| 313 | // ToCDNServiceUpdateMap converts a Removal into a request body fragment suitable for the |
| 314 | // Update call. |
| 315 | func (r Removal) ToCDNServiceUpdateMap() map[string]interface{} { |
| 316 | return map[string]interface{}{ |
| 317 | "op": "remove", |
| 318 | "path": r.Path.renderIndex(r.Index), |
| 319 | } |
| 320 | } |
| 321 | |
Ash Wilson | 299363d | 2015-01-29 10:49:40 -0500 | [diff] [blame] | 322 | // Update accepts a slice of Patch operations (Insertion, Append, Replacement or Removal) and |
| 323 | // updates an existing CDN service using the values provided. idOrURL can be either the service's |
| 324 | // URL or its ID. For example, both "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0" and |
Jon Perritt | b8713ad | 2015-01-21 15:02:58 -0700 | [diff] [blame] | 325 | // "https://global.cdn.api.rackspacecloud.com/v1.0/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0" |
| 326 | // are valid options for idOrURL. |
Ash Wilson | 09d2a28 | 2015-01-29 10:05:53 -0500 | [diff] [blame] | 327 | func Update(c *gophercloud.ServiceClient, idOrURL string, patches []Patch) UpdateResult { |
Jon Perritt | b8713ad | 2015-01-21 15:02:58 -0700 | [diff] [blame] | 328 | var url string |
| 329 | if strings.Contains(idOrURL, "/") { |
| 330 | url = idOrURL |
| 331 | } else { |
| 332 | url = updateURL(c, idOrURL) |
| 333 | } |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 334 | |
Ash Wilson | 09d2a28 | 2015-01-29 10:05:53 -0500 | [diff] [blame] | 335 | reqBody := make([]map[string]interface{}, len(patches)) |
| 336 | for i, patch := range patches { |
| 337 | reqBody[i] = patch.ToCDNServiceUpdateMap() |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Jon Perritt | b8713ad | 2015-01-21 15:02:58 -0700 | [diff] [blame] | 340 | resp, err := perigee.Request("PATCH", url, perigee.Options{ |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 341 | MoreHeaders: c.AuthenticatedHeaders(), |
| 342 | ReqBody: &reqBody, |
| 343 | OkCodes: []int{202}, |
| 344 | }) |
Ash Wilson | 09d2a28 | 2015-01-29 10:05:53 -0500 | [diff] [blame] | 345 | var result UpdateResult |
| 346 | result.Header = resp.HttpResponse.Header |
| 347 | result.Err = err |
| 348 | return result |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Jon Perritt | b8713ad | 2015-01-21 15:02:58 -0700 | [diff] [blame] | 351 | // Delete accepts a service's ID or its URL and deletes the CDN service |
| 352 | // associated with it. For example, both "96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0" and |
| 353 | // "https://global.cdn.api.rackspacecloud.com/v1.0/services/96737ae3-cfc1-4c72-be88-5d0e7cc9a3f0" |
| 354 | // are valid options for idOrURL. |
| 355 | func Delete(c *gophercloud.ServiceClient, idOrURL string) DeleteResult { |
| 356 | var url string |
| 357 | if strings.Contains(idOrURL, "/") { |
| 358 | url = idOrURL |
| 359 | } else { |
| 360 | url = deleteURL(c, idOrURL) |
| 361 | } |
| 362 | |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 363 | var res DeleteResult |
Jon Perritt | b8713ad | 2015-01-21 15:02:58 -0700 | [diff] [blame] | 364 | _, res.Err = perigee.Request("DELETE", url, perigee.Options{ |
Jon Perritt | e7b86d1 | 2015-01-16 20:37:11 -0700 | [diff] [blame] | 365 | MoreHeaders: c.AuthenticatedHeaders(), |
| 366 | OkCodes: []int{202}, |
| 367 | }) |
| 368 | return res |
| 369 | } |