Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 1 | package containers |
| 2 | |
| 3 | import ( |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 4 | "github.com/rackspace/gophercloud" |
Ash Wilson | 0faafcc | 2014-09-16 15:20:17 -0400 | [diff] [blame] | 5 | "github.com/rackspace/gophercloud/pagination" |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 6 | ) |
| 7 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 8 | // ListOptsBuilder allows extensions to add additional parameters to the List |
| 9 | // request. |
| 10 | type ListOptsBuilder interface { |
| 11 | ToContainerListParams() (bool, string, error) |
| 12 | } |
| 13 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 14 | // ListOpts is a structure that holds options for listing containers. |
| 15 | type ListOpts struct { |
| 16 | Full bool |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 17 | Limit int `q:"limit"` |
| 18 | Marker string `q:"marker"` |
| 19 | EndMarker string `q:"end_marker"` |
| 20 | Format string `q:"format"` |
| 21 | Prefix string `q:"prefix"` |
| 22 | Delimiter string `q:"delimiter"` |
Ash Wilson | 0faafcc | 2014-09-16 15:20:17 -0400 | [diff] [blame] | 23 | } |
| 24 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 25 | // ToContainerListParams formats a ListOpts into a query string and boolean |
| 26 | // representing whether to list complete information for each container. |
| 27 | func (opts ListOpts) ToContainerListParams() (bool, string, error) { |
| 28 | q, err := gophercloud.BuildQueryString(opts) |
| 29 | if err != nil { |
| 30 | return false, "", err |
| 31 | } |
| 32 | return opts.Full, q.String(), nil |
| 33 | } |
| 34 | |
| 35 | // List is a function that retrieves containers associated with the account as |
| 36 | // well as account metadata. It returns a pager which can be iterated with the |
| 37 | // EachPage function. |
| 38 | func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
| 39 | headers := map[string]string{"Accept": "text/plain", "Content-Type": "text/plain"} |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 40 | |
Jon Perritt | b346140 | 2014-10-09 21:36:17 -0500 | [diff] [blame] | 41 | url := listURL(c) |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 42 | if opts != nil { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 43 | full, query, err := opts.ToContainerListParams() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 44 | if err != nil { |
| 45 | return pagination.Pager{Err: err} |
| 46 | } |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 47 | url += query |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 48 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 49 | if full { |
| 50 | headers = map[string]string{"Accept": "application/json", "Content-Type": "application/json"} |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 51 | } |
Ash Wilson | 0faafcc | 2014-09-16 15:20:17 -0400 | [diff] [blame] | 52 | } |
| 53 | |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 54 | createPage := func(r pagination.PageResult) pagination.Page { |
| 55 | p := ContainerPage{pagination.MarkerPageBase{PageResult: r}} |
Ash Wilson | 0faafcc | 2014-09-16 15:20:17 -0400 | [diff] [blame] | 56 | p.MarkerPageBase.Owner = p |
| 57 | return p |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 58 | } |
| 59 | |
Jon Perritt | 584c94f | 2014-09-24 18:00:16 -0500 | [diff] [blame] | 60 | pager := pagination.NewPager(c, url, createPage) |
| 61 | pager.Headers = headers |
| 62 | return pager |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 63 | } |
| 64 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 65 | // CreateOptsBuilder allows extensions to add additional parameters to the |
| 66 | // Create request. |
| 67 | type CreateOptsBuilder interface { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 68 | ToContainerCreateMap() (map[string]string, error) |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 69 | } |
| 70 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 71 | // CreateOpts is a structure that holds parameters for creating a container. |
| 72 | type CreateOpts struct { |
| 73 | Metadata map[string]string |
| 74 | ContainerRead string `h:"X-Container-Read"` |
| 75 | ContainerSyncTo string `h:"X-Container-Sync-To"` |
| 76 | ContainerSyncKey string `h:"X-Container-Sync-Key"` |
| 77 | ContainerWrite string `h:"X-Container-Write"` |
| 78 | ContentType string `h:"Content-Type"` |
| 79 | DetectContentType bool `h:"X-Detect-Content-Type"` |
| 80 | IfNoneMatch string `h:"If-None-Match"` |
| 81 | VersionsLocation string `h:"X-Versions-Location"` |
| 82 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 83 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 84 | // ToContainerCreateMap formats a CreateOpts into a map of headers. |
| 85 | func (opts CreateOpts) ToContainerCreateMap() (map[string]string, error) { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 86 | h, err := gophercloud.BuildHeaders(opts) |
| 87 | if err != nil { |
| 88 | return nil, err |
| 89 | } |
| 90 | for k, v := range opts.Metadata { |
| 91 | h["X-Container-Meta-"+k] = v |
| 92 | } |
| 93 | return h, nil |
| 94 | } |
| 95 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 96 | // Create is a function that creates a new container. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 97 | func Create(c *gophercloud.ServiceClient, containerName string, opts CreateOptsBuilder) CreateResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 98 | var res CreateResult |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 99 | h := c.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 100 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 101 | if opts != nil { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 102 | headers, err := opts.ToContainerCreateMap() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 103 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 104 | res.Err = err |
| 105 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | for k, v := range headers { |
| 109 | h[k] = v |
| 110 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 111 | } |
| 112 | |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 113 | resp, err := c.Request("PUT", createURL(c, containerName), gophercloud.RequestOpts{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 114 | MoreHeaders: h, |
Jon Perritt | a77ba0d | 2014-10-17 01:15:29 -0500 | [diff] [blame] | 115 | OkCodes: []int{201, 202, 204}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 116 | }) |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 117 | res.Header = resp.Header |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 118 | res.Err = err |
| 119 | return res |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // Delete is a function that deletes a container. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 123 | func Delete(c *gophercloud.ServiceClient, containerName string) DeleteResult { |
| 124 | var res DeleteResult |
Jamie Hannaford | 1d27afa | 2015-03-24 16:20:45 +0100 | [diff] [blame^] | 125 | _, res.Err = c.Delete(deleteURL(c, containerName), nil) |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 126 | return res |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 127 | } |
| 128 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 129 | // UpdateOptsBuilder allows extensions to add additional parameters to the |
| 130 | // Update request. |
| 131 | type UpdateOptsBuilder interface { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 132 | ToContainerUpdateMap() (map[string]string, error) |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // UpdateOpts is a structure that holds parameters for updating, creating, or |
| 136 | // deleting a container's metadata. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 137 | type UpdateOpts struct { |
| 138 | Metadata map[string]string |
| 139 | ContainerRead string `h:"X-Container-Read"` |
| 140 | ContainerSyncTo string `h:"X-Container-Sync-To"` |
| 141 | ContainerSyncKey string `h:"X-Container-Sync-Key"` |
| 142 | ContainerWrite string `h:"X-Container-Write"` |
| 143 | ContentType string `h:"Content-Type"` |
| 144 | DetectContentType bool `h:"X-Detect-Content-Type"` |
| 145 | RemoveVersionsLocation string `h:"X-Remove-Versions-Location"` |
| 146 | VersionsLocation string `h:"X-Versions-Location"` |
| 147 | } |
| 148 | |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 149 | // ToContainerUpdateMap formats a CreateOpts into a map of headers. |
| 150 | func (opts UpdateOpts) ToContainerUpdateMap() (map[string]string, error) { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 151 | h, err := gophercloud.BuildHeaders(opts) |
| 152 | if err != nil { |
| 153 | return nil, err |
| 154 | } |
| 155 | for k, v := range opts.Metadata { |
| 156 | h["X-Container-Meta-"+k] = v |
| 157 | } |
| 158 | return h, nil |
| 159 | } |
| 160 | |
| 161 | // Update is a function that creates, updates, or deletes a container's |
| 162 | // metadata. |
| 163 | func Update(c *gophercloud.ServiceClient, containerName string, opts UpdateOptsBuilder) UpdateResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 164 | var res UpdateResult |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 165 | h := c.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 166 | |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 167 | if opts != nil { |
Jon Perritt | 04851d3 | 2014-10-14 02:07:13 -0500 | [diff] [blame] | 168 | headers, err := opts.ToContainerUpdateMap() |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 169 | if err != nil { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 170 | res.Err = err |
| 171 | return res |
Jon Perritt | de47eac | 2014-09-30 15:34:17 -0500 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | for k, v := range headers { |
| 175 | h[k] = v |
| 176 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 177 | } |
| 178 | |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 179 | resp, err := c.Request("POST", updateURL(c, containerName), gophercloud.RequestOpts{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 180 | MoreHeaders: h, |
Jamie Hannaford | c530ba1 | 2015-03-23 17:50:46 +0100 | [diff] [blame] | 181 | OkCodes: []int{201, 202, 204}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 182 | }) |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 183 | res.Header = resp.Header |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 184 | res.Err = err |
| 185 | return res |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 186 | } |
| 187 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 188 | // Get is a function that retrieves the metadata of a container. To extract just |
| 189 | // the custom metadata, pass the GetResult response to the ExtractMetadata |
| 190 | // function. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 191 | func Get(c *gophercloud.ServiceClient, containerName string) GetResult { |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 192 | var res GetResult |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 193 | resp, err := c.Request("HEAD", getURL(c, containerName), gophercloud.RequestOpts{ |
| 194 | OkCodes: []int{200, 204}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 195 | }) |
Ash Wilson | 59fb6c4 | 2015-02-12 16:21:13 -0500 | [diff] [blame] | 196 | res.Header = resp.Header |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 197 | res.Err = err |
| 198 | return res |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 199 | } |