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 | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame^] | 9 | // ListOpts is a structure that holds options for listing containers. |
| 10 | type ListOpts struct { |
| 11 | Full bool |
| 12 | Limit int `q:"limit"` |
| 13 | Marker string `q:"marker"` |
| 14 | EndMarker string `q:"end_marker"` |
| 15 | Format string `q:"format"` |
| 16 | Prefix string `q:"prefix"` |
| 17 | Delimiter []byte `q:"delimiter"` |
Ash Wilson | 0faafcc | 2014-09-16 15:20:17 -0400 | [diff] [blame] | 18 | } |
| 19 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 20 | // List is a function that retrieves all objects in a container. It also returns the details |
| 21 | // for the account. To extract just the container information or names, pass the ListResult |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 22 | // response to the ExtractInfo or ExtractNames function, respectively. |
Ash Wilson | 0faafcc | 2014-09-16 15:20:17 -0400 | [diff] [blame] | 23 | func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager { |
| 24 | var headers map[string]string |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 25 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame^] | 26 | query, err := gophercloud.BuildQueryString(opts) |
| 27 | if err != nil { |
| 28 | return pagination.Pager{Err: err} |
| 29 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 30 | |
| 31 | if !opts.Full { |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame^] | 32 | headers = map[string]string{"Accept": "text/plain"} |
Ash Wilson | 0faafcc | 2014-09-16 15:20:17 -0400 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | createPage := func(r pagination.LastHTTPResponse) pagination.Page { |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame^] | 36 | p := ContainerPage{pagination.MarkerPageBase{LastHTTPResponse: r}} |
Ash Wilson | 0faafcc | 2014-09-16 15:20:17 -0400 | [diff] [blame] | 37 | p.MarkerPageBase.Owner = p |
| 38 | return p |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 39 | } |
| 40 | |
Jon Perritt | fc39b94 | 2014-09-10 21:18:19 -0500 | [diff] [blame] | 41 | url := accountURL(c) + query |
Jon Perritt | 584c94f | 2014-09-24 18:00:16 -0500 | [diff] [blame] | 42 | pager := pagination.NewPager(c, url, createPage) |
| 43 | pager.Headers = headers |
| 44 | return pager |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 45 | } |
| 46 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame^] | 47 | // CreateOpts is a structure that holds parameters for creating a container. |
| 48 | type CreateOpts struct { |
| 49 | Metadata map[string]string |
| 50 | ContainerRead string `h:"X-Container-Read"` |
| 51 | ContainerSyncTo string `h:"X-Container-Sync-To"` |
| 52 | ContainerSyncKey string `h:"X-Container-Sync-Key"` |
| 53 | ContainerWrite string `h:"X-Container-Write"` |
| 54 | ContentType string `h:"Content-Type"` |
| 55 | DetectContentType bool `h:"X-Detect-Content-Type"` |
| 56 | IfNoneMatch string `h:"If-None-Match"` |
| 57 | VersionsLocation string `h:"X-Versions-Location"` |
| 58 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 59 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame^] | 60 | // Create is a function that creates a new container. |
| 61 | func Create(c *gophercloud.ServiceClient, containerName string, opts CreateOpts) (Container, error) { |
| 62 | var container Container |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 63 | h := c.Provider.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 64 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame^] | 65 | headers, err := gophercloud.BuildHeaders(opts) |
| 66 | if err != nil { |
| 67 | return container, err |
| 68 | } |
| 69 | |
| 70 | for k, v := range headers { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 71 | h[k] = v |
| 72 | } |
| 73 | |
| 74 | for k, v := range opts.Metadata { |
| 75 | h["X-Container-Meta-"+k] = v |
| 76 | } |
| 77 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame^] | 78 | _, err = perigee.Request("PUT", containerURL(c, containerName), perigee.Options{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 79 | MoreHeaders: h, |
Ash Wilson | e47ea9e | 2014-09-10 16:03:44 -0400 | [diff] [blame] | 80 | OkCodes: []int{201, 204}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 81 | }) |
| 82 | if err == nil { |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame^] | 83 | container = Container{"name": containerName} |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 84 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame^] | 85 | return container, err |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | // Delete is a function that deletes a container. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame^] | 89 | func Delete(c *gophercloud.ServiceClient, containerName string) error { |
| 90 | _, err := perigee.Request("DELETE", containerURL(c, containerName), perigee.Options{ |
| 91 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 92 | OkCodes: []int{204}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 93 | }) |
| 94 | return err |
| 95 | } |
| 96 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame^] | 97 | // UpdateOpts is a structure that holds parameters for updating, creating, or deleting a |
| 98 | // container's metadata. |
| 99 | type UpdateOpts struct { |
| 100 | Metadata map[string]string |
| 101 | ContainerRead string `h:"X-Container-Read"` |
| 102 | ContainerSyncTo string `h:"X-Container-Sync-To"` |
| 103 | ContainerSyncKey string `h:"X-Container-Sync-Key"` |
| 104 | ContainerWrite string `h:"X-Container-Write"` |
| 105 | ContentType string `h:"Content-Type"` |
| 106 | DetectContentType bool `h:"X-Detect-Content-Type"` |
| 107 | RemoveVersionsLocation string `h:"X-Remove-Versions-Location"` |
| 108 | VersionsLocation string `h:"X-Versions-Location"` |
| 109 | } |
| 110 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 111 | // Update is a function that creates, updates, or deletes a container's metadata. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame^] | 112 | func Update(c *gophercloud.ServiceClient, containerName string, opts UpdateOpts) error { |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 113 | h := c.Provider.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 114 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame^] | 115 | headers, err := gophercloud.BuildHeaders(opts) |
| 116 | if err != nil { |
| 117 | return err |
| 118 | } |
| 119 | |
| 120 | for k, v := range headers { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 121 | h[k] = v |
| 122 | } |
| 123 | |
| 124 | for k, v := range opts.Metadata { |
| 125 | h["X-Container-Meta-"+k] = v |
| 126 | } |
| 127 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame^] | 128 | url := containerURL(c, containerName) |
| 129 | _, err = perigee.Request("POST", url, perigee.Options{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 130 | MoreHeaders: h, |
Ash Wilson | e47ea9e | 2014-09-10 16:03:44 -0400 | [diff] [blame] | 131 | OkCodes: []int{204}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 132 | }) |
| 133 | return err |
| 134 | } |
| 135 | |
| 136 | // Get is a function that retrieves the metadata of a container. To extract just the custom |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 137 | // metadata, pass the GetResult response to the ExtractMetadata function. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame^] | 138 | func Get(c *gophercloud.ServiceClient, containerName string) GetResult { |
| 139 | var gr GetResult |
| 140 | resp, err := perigee.Request("HEAD", containerURL(c, containerName), perigee.Options{ |
| 141 | MoreHeaders: c.Provider.AuthenticatedHeaders(), |
Ash Wilson | e47ea9e | 2014-09-10 16:03:44 -0400 | [diff] [blame] | 142 | OkCodes: []int{204}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 143 | }) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame^] | 144 | gr.Err = err |
| 145 | gr.Resp = &resp.HttpResponse |
| 146 | return gr |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 147 | } |