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