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