Jon Perritt | b5c7812 | 2014-10-15 20:44:39 -0500 | [diff] [blame] | 1 | package cdncontainers |
| 2 | |
| 3 | import ( |
Jon Perritt | a3de08f | 2014-12-17 22:08:19 -0700 | [diff] [blame] | 4 | "strconv" |
| 5 | |
Jon Perritt | b5c7812 | 2014-10-15 20:44:39 -0500 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
| 7 | ) |
| 8 | |
| 9 | // EnableOptsBuilder allows extensions to add additional parameters to the Enable |
| 10 | // request. |
| 11 | type EnableOptsBuilder interface { |
| 12 | ToCDNContainerEnableMap() (map[string]string, error) |
| 13 | } |
| 14 | |
| 15 | // EnableOpts is a structure that holds options for enabling a CDN container. |
| 16 | type EnableOpts struct { |
| 17 | // CDNEnabled indicates whether or not the container is CDN enabled. Set to |
| 18 | // `true` to enable the container. Note that changing this setting from true |
| 19 | // to false will disable the container in the CDN but only after the TTL has |
| 20 | // expired. |
| 21 | CDNEnabled bool `h:"X-Cdn-Enabled"` |
| 22 | // TTL is the time-to-live for the container (in seconds). |
| 23 | TTL int `h:"X-Ttl"` |
| 24 | } |
| 25 | |
| 26 | // ToCDNContainerEnableMap formats an EnableOpts into a map of headers. |
| 27 | func (opts EnableOpts) ToCDNContainerEnableMap() (map[string]string, error) { |
| 28 | h, err := gophercloud.BuildHeaders(opts) |
| 29 | if err != nil { |
| 30 | return nil, err |
| 31 | } |
| 32 | return h, nil |
| 33 | } |
| 34 | |
| 35 | // Enable is a function that enables/disables a CDN container. |
| 36 | func Enable(c *gophercloud.ServiceClient, containerName string, opts EnableOptsBuilder) EnableResult { |
| 37 | var res EnableResult |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 38 | h := c.AuthenticatedHeaders() |
Jon Perritt | b5c7812 | 2014-10-15 20:44:39 -0500 | [diff] [blame] | 39 | |
| 40 | if opts != nil { |
| 41 | headers, err := opts.ToCDNContainerEnableMap() |
| 42 | if err != nil { |
| 43 | res.Err = err |
| 44 | return res |
| 45 | } |
| 46 | |
| 47 | for k, v := range headers { |
| 48 | h[k] = v |
| 49 | } |
| 50 | } |
| 51 | |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame^] | 52 | resp, err := c.Request("PUT", enableURL(c, containerName), gophercloud.RequestOpts{ |
Jon Perritt | b5c7812 | 2014-10-15 20:44:39 -0500 | [diff] [blame] | 53 | MoreHeaders: h, |
| 54 | OkCodes: []int{201, 202, 204}, |
| 55 | }) |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame^] | 56 | res.Header = resp.Header |
Jon Perritt | b5c7812 | 2014-10-15 20:44:39 -0500 | [diff] [blame] | 57 | res.Err = err |
| 58 | return res |
| 59 | } |
Jon Perritt | a3de08f | 2014-12-17 22:08:19 -0700 | [diff] [blame] | 60 | |
| 61 | // Get is a function that retrieves the metadata of a container. To extract just |
| 62 | // the custom metadata, pass the GetResult response to the ExtractMetadata |
| 63 | // function. |
| 64 | func Get(c *gophercloud.ServiceClient, containerName string) GetResult { |
| 65 | var res GetResult |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame^] | 66 | resp, err := c.Request("HEAD", getURL(c, containerName), gophercloud.RequestOpts{ |
| 67 | OkCodes: []int{200, 204}, |
Jon Perritt | a3de08f | 2014-12-17 22:08:19 -0700 | [diff] [blame] | 68 | }) |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame^] | 69 | res.Header = resp.Header |
Jon Perritt | a3de08f | 2014-12-17 22:08:19 -0700 | [diff] [blame] | 70 | res.Err = err |
| 71 | return res |
| 72 | } |
| 73 | |
| 74 | // State is the state of an option. It is a pointer to a boolean to enable checking for |
| 75 | // a zero-value of nil instead of false, which is a valid option. |
| 76 | type State *bool |
| 77 | |
| 78 | var ( |
| 79 | iTrue = true |
| 80 | iFalse = false |
| 81 | |
| 82 | // Enabled is used for a true value for options in request bodies. |
| 83 | Enabled State = &iTrue |
| 84 | // Disabled is used for a false value for options in request bodies. |
| 85 | Disabled State = &iFalse |
| 86 | ) |
| 87 | |
| 88 | // UpdateOptsBuilder allows extensions to add additional parameters to the |
| 89 | // Update request. |
| 90 | type UpdateOptsBuilder interface { |
| 91 | ToContainerUpdateMap() (map[string]string, error) |
| 92 | } |
| 93 | |
| 94 | // UpdateOpts is a structure that holds parameters for updating, creating, or |
| 95 | // deleting a container's metadata. |
| 96 | type UpdateOpts struct { |
| 97 | // Whether or not to CDN-enable a container. Prefer using XCDNEnabled, which |
| 98 | // is of type *bool underneath. |
| 99 | // TODO v2.0: change type to Enabled/Disabled (*bool) |
| 100 | CDNEnabled bool `h:"X-Cdn-Enabled"` |
| 101 | // Whether or not to enable log retention. Prefer using XLogRetention, which |
| 102 | // is of type *bool underneath. |
| 103 | // TODO v2.0: change type to Enabled/Disabled (*bool) |
| 104 | LogRetention bool `h:"X-Log-Retention"` |
| 105 | XCDNEnabled *bool |
| 106 | XLogRetention *bool |
| 107 | TTL int `h:"X-Ttl"` |
| 108 | } |
| 109 | |
| 110 | // ToContainerUpdateMap formats a CreateOpts into a map of headers. |
| 111 | func (opts UpdateOpts) ToContainerUpdateMap() (map[string]string, error) { |
| 112 | h, err := gophercloud.BuildHeaders(opts) |
| 113 | if err != nil { |
| 114 | return nil, err |
| 115 | } |
| 116 | h["X-Cdn-Enabled"] = strconv.FormatBool(opts.CDNEnabled) |
| 117 | h["X-Log-Retention"] = strconv.FormatBool(opts.LogRetention) |
| 118 | |
| 119 | if opts.XCDNEnabled != nil { |
| 120 | h["X-Cdn-Enabled"] = strconv.FormatBool(*opts.XCDNEnabled) |
| 121 | } |
| 122 | |
| 123 | if opts.XLogRetention != nil { |
| 124 | h["X-Log-Retention"] = strconv.FormatBool(*opts.XLogRetention) |
| 125 | } |
| 126 | |
| 127 | return h, nil |
| 128 | } |
| 129 | |
| 130 | // Update is a function that creates, updates, or deletes a container's |
| 131 | // metadata. |
| 132 | func Update(c *gophercloud.ServiceClient, containerName string, opts UpdateOptsBuilder) UpdateResult { |
| 133 | var res UpdateResult |
| 134 | h := c.AuthenticatedHeaders() |
| 135 | |
| 136 | if opts != nil { |
| 137 | headers, err := opts.ToContainerUpdateMap() |
| 138 | if err != nil { |
| 139 | res.Err = err |
| 140 | return res |
| 141 | } |
| 142 | |
| 143 | for k, v := range headers { |
| 144 | h[k] = v |
| 145 | } |
| 146 | } |
| 147 | |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame^] | 148 | resp, err := c.Request("POST", updateURL(c, containerName), gophercloud.RequestOpts{ |
Jon Perritt | a3de08f | 2014-12-17 22:08:19 -0700 | [diff] [blame] | 149 | MoreHeaders: h, |
| 150 | OkCodes: []int{202, 204}, |
| 151 | }) |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame^] | 152 | res.Header = resp.Header |
Jon Perritt | a3de08f | 2014-12-17 22:08:19 -0700 | [diff] [blame] | 153 | res.Err = err |
| 154 | return res |
| 155 | } |