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