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