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 | }) |
Jon Perritt | a2c88b2 | 2015-05-18 11:23:30 -0600 | [diff] [blame] | 56 | if resp != nil { |
| 57 | res.Header = resp.Header |
| 58 | } |
Jon Perritt | b5c7812 | 2014-10-15 20:44:39 -0500 | [diff] [blame] | 59 | res.Err = err |
| 60 | return res |
| 61 | } |
Jon Perritt | a3de08f | 2014-12-17 22:08:19 -0700 | [diff] [blame] | 62 | |
| 63 | // Get is a function that retrieves the metadata of a container. To extract just |
| 64 | // the custom metadata, pass the GetResult response to the ExtractMetadata |
| 65 | // function. |
| 66 | func Get(c *gophercloud.ServiceClient, containerName string) GetResult { |
| 67 | var res GetResult |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 68 | resp, err := c.Request("HEAD", getURL(c, containerName), gophercloud.RequestOpts{ |
| 69 | OkCodes: []int{200, 204}, |
Jon Perritt | a3de08f | 2014-12-17 22:08:19 -0700 | [diff] [blame] | 70 | }) |
Jon Perritt | a2c88b2 | 2015-05-18 11:23:30 -0600 | [diff] [blame] | 71 | if resp != nil { |
| 72 | res.Header = resp.Header |
| 73 | } |
Jon Perritt | a3de08f | 2014-12-17 22:08:19 -0700 | [diff] [blame] | 74 | res.Err = err |
| 75 | return res |
| 76 | } |
| 77 | |
| 78 | // State is the state of an option. It is a pointer to a boolean to enable checking for |
| 79 | // a zero-value of nil instead of false, which is a valid option. |
| 80 | type State *bool |
| 81 | |
| 82 | var ( |
| 83 | iTrue = true |
| 84 | iFalse = false |
| 85 | |
| 86 | // Enabled is used for a true value for options in request bodies. |
| 87 | Enabled State = &iTrue |
| 88 | // Disabled is used for a false value for options in request bodies. |
| 89 | Disabled State = &iFalse |
| 90 | ) |
| 91 | |
| 92 | // UpdateOptsBuilder allows extensions to add additional parameters to the |
| 93 | // Update request. |
| 94 | type UpdateOptsBuilder interface { |
| 95 | ToContainerUpdateMap() (map[string]string, error) |
| 96 | } |
| 97 | |
| 98 | // UpdateOpts is a structure that holds parameters for updating, creating, or |
| 99 | // deleting a container's metadata. |
| 100 | type UpdateOpts struct { |
| 101 | // Whether or not to CDN-enable a container. Prefer using XCDNEnabled, which |
| 102 | // is of type *bool underneath. |
| 103 | // TODO v2.0: change type to Enabled/Disabled (*bool) |
| 104 | CDNEnabled bool `h:"X-Cdn-Enabled"` |
| 105 | // Whether or not to enable log retention. Prefer using XLogRetention, which |
| 106 | // is of type *bool underneath. |
| 107 | // TODO v2.0: change type to Enabled/Disabled (*bool) |
| 108 | LogRetention bool `h:"X-Log-Retention"` |
| 109 | XCDNEnabled *bool |
| 110 | XLogRetention *bool |
| 111 | TTL int `h:"X-Ttl"` |
| 112 | } |
| 113 | |
| 114 | // ToContainerUpdateMap formats a CreateOpts into a map of headers. |
| 115 | func (opts UpdateOpts) ToContainerUpdateMap() (map[string]string, error) { |
| 116 | h, err := gophercloud.BuildHeaders(opts) |
| 117 | if err != nil { |
| 118 | return nil, err |
| 119 | } |
| 120 | h["X-Cdn-Enabled"] = strconv.FormatBool(opts.CDNEnabled) |
| 121 | h["X-Log-Retention"] = strconv.FormatBool(opts.LogRetention) |
| 122 | |
| 123 | if opts.XCDNEnabled != nil { |
| 124 | h["X-Cdn-Enabled"] = strconv.FormatBool(*opts.XCDNEnabled) |
| 125 | } |
| 126 | |
| 127 | if opts.XLogRetention != nil { |
| 128 | h["X-Log-Retention"] = strconv.FormatBool(*opts.XLogRetention) |
| 129 | } |
| 130 | |
| 131 | return h, nil |
| 132 | } |
| 133 | |
| 134 | // Update is a function that creates, updates, or deletes a container's |
| 135 | // metadata. |
| 136 | func Update(c *gophercloud.ServiceClient, containerName string, opts UpdateOptsBuilder) UpdateResult { |
| 137 | var res UpdateResult |
| 138 | h := c.AuthenticatedHeaders() |
| 139 | |
| 140 | if opts != nil { |
| 141 | headers, err := opts.ToContainerUpdateMap() |
| 142 | if err != nil { |
| 143 | res.Err = err |
| 144 | return res |
| 145 | } |
| 146 | |
| 147 | for k, v := range headers { |
| 148 | h[k] = v |
| 149 | } |
| 150 | } |
| 151 | |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 152 | resp, err := c.Request("POST", updateURL(c, containerName), gophercloud.RequestOpts{ |
Jon Perritt | a3de08f | 2014-12-17 22:08:19 -0700 | [diff] [blame] | 153 | MoreHeaders: h, |
| 154 | OkCodes: []int{202, 204}, |
| 155 | }) |
Jon Perritt | a2c88b2 | 2015-05-18 11:23:30 -0600 | [diff] [blame] | 156 | if resp != nil { |
| 157 | res.Header = resp.Header |
| 158 | } |
Jon Perritt | a3de08f | 2014-12-17 22:08:19 -0700 | [diff] [blame] | 159 | res.Err = err |
| 160 | return res |
| 161 | } |