Jon Perritt | b5c7812 | 2014-10-15 20:44:39 -0500 | [diff] [blame] | 1 | package cdncontainers |
| 2 | |
| 3 | import ( |
| 4 | "github.com/racker/perigee" |
| 5 | "github.com/rackspace/gophercloud" |
| 6 | ) |
| 7 | |
| 8 | // EnableOptsBuilder allows extensions to add additional parameters to the Enable |
| 9 | // request. |
| 10 | type EnableOptsBuilder interface { |
| 11 | ToCDNContainerEnableMap() (map[string]string, error) |
| 12 | } |
| 13 | |
| 14 | // EnableOpts is a structure that holds options for enabling a CDN container. |
| 15 | type EnableOpts struct { |
| 16 | // CDNEnabled indicates whether or not the container is CDN enabled. Set to |
| 17 | // `true` to enable the container. Note that changing this setting from true |
| 18 | // to false will disable the container in the CDN but only after the TTL has |
| 19 | // expired. |
| 20 | CDNEnabled bool `h:"X-Cdn-Enabled"` |
| 21 | // TTL is the time-to-live for the container (in seconds). |
| 22 | TTL int `h:"X-Ttl"` |
| 23 | } |
| 24 | |
| 25 | // ToCDNContainerEnableMap formats an EnableOpts into a map of headers. |
| 26 | func (opts EnableOpts) ToCDNContainerEnableMap() (map[string]string, error) { |
| 27 | h, err := gophercloud.BuildHeaders(opts) |
| 28 | if err != nil { |
| 29 | return nil, err |
| 30 | } |
| 31 | return h, nil |
| 32 | } |
| 33 | |
| 34 | // Enable is a function that enables/disables a CDN container. |
| 35 | func Enable(c *gophercloud.ServiceClient, containerName string, opts EnableOptsBuilder) EnableResult { |
| 36 | var res EnableResult |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 37 | h := c.AuthenticatedHeaders() |
Jon Perritt | b5c7812 | 2014-10-15 20:44:39 -0500 | [diff] [blame] | 38 | |
| 39 | if opts != nil { |
| 40 | headers, err := opts.ToCDNContainerEnableMap() |
| 41 | if err != nil { |
| 42 | res.Err = err |
| 43 | return res |
| 44 | } |
| 45 | |
| 46 | for k, v := range headers { |
| 47 | h[k] = v |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | resp, err := perigee.Request("PUT", enableURL(c, containerName), perigee.Options{ |
| 52 | MoreHeaders: h, |
| 53 | OkCodes: []int{201, 202, 204}, |
| 54 | }) |
Jon Perritt | d846fe0 | 2014-10-20 17:54:07 -0500 | [diff] [blame] | 55 | res.Header = resp.HttpResponse.Header |
Jon Perritt | b5c7812 | 2014-10-15 20:44:39 -0500 | [diff] [blame] | 56 | res.Err = err |
| 57 | return res |
| 58 | } |