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