blob: 3a6a2657c117f0ceffae6284b9cea9983fbeeb59 [file] [log] [blame]
Jon Perritt816d2a02014-03-11 20:49:46 -05001package containers
2
3import (
Ash Wilson604320e2014-09-10 16:02:28 -04004 "github.com/racker/perigee"
5 "github.com/rackspace/gophercloud"
Ash Wilson0faafcc2014-09-16 15:20:17 -04006 "github.com/rackspace/gophercloud/pagination"
Jon Perritt816d2a02014-03-11 20:49:46 -05007)
8
Jon Perritt8c93a302014-09-28 22:35:57 -05009// ListOpts is a structure that holds options for listing containers.
10type 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 Wilson0faafcc2014-09-16 15:20:17 -040018}
19
Jon Perritt816d2a02014-03-11 20:49:46 -050020// 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 Perritteb575642014-04-24 15:16:31 -050022// response to the ExtractInfo or ExtractNames function, respectively.
Ash Wilson0faafcc2014-09-16 15:20:17 -040023func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
24 var headers map[string]string
Jon Perritt816d2a02014-03-11 20:49:46 -050025
Jon Perritt8c93a302014-09-28 22:35:57 -050026 query, err := gophercloud.BuildQueryString(opts)
27 if err != nil {
28 return pagination.Pager{Err: err}
29 }
Jon Perritt816d2a02014-03-11 20:49:46 -050030
31 if !opts.Full {
Jon Perritt8c93a302014-09-28 22:35:57 -050032 headers = map[string]string{"Accept": "text/plain"}
Ash Wilson0faafcc2014-09-16 15:20:17 -040033 }
34
35 createPage := func(r pagination.LastHTTPResponse) pagination.Page {
Jon Perritt8c93a302014-09-28 22:35:57 -050036 p := ContainerPage{pagination.MarkerPageBase{LastHTTPResponse: r}}
Ash Wilson0faafcc2014-09-16 15:20:17 -040037 p.MarkerPageBase.Owner = p
38 return p
Jon Perritt816d2a02014-03-11 20:49:46 -050039 }
40
Jon Perrittfc39b942014-09-10 21:18:19 -050041 url := accountURL(c) + query
Jon Perritt584c94f2014-09-24 18:00:16 -050042 pager := pagination.NewPager(c, url, createPage)
43 pager.Headers = headers
44 return pager
Jon Perritt816d2a02014-03-11 20:49:46 -050045}
46
Jon Perritt8c93a302014-09-28 22:35:57 -050047// CreateOpts is a structure that holds parameters for creating a container.
48type 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 Perritt816d2a02014-03-11 20:49:46 -050059
Jon Perritt8c93a302014-09-28 22:35:57 -050060// Create is a function that creates a new container.
61func Create(c *gophercloud.ServiceClient, containerName string, opts CreateOpts) (Container, error) {
62 var container Container
Ash Wilson604320e2014-09-10 16:02:28 -040063 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -050064
Jon Perritt8c93a302014-09-28 22:35:57 -050065 headers, err := gophercloud.BuildHeaders(opts)
66 if err != nil {
67 return container, err
68 }
69
70 for k, v := range headers {
Jon Perritt816d2a02014-03-11 20:49:46 -050071 h[k] = v
72 }
73
74 for k, v := range opts.Metadata {
75 h["X-Container-Meta-"+k] = v
76 }
77
Jon Perritt8c93a302014-09-28 22:35:57 -050078 _, err = perigee.Request("PUT", containerURL(c, containerName), perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -050079 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -040080 OkCodes: []int{201, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -050081 })
82 if err == nil {
Jon Perritt8c93a302014-09-28 22:35:57 -050083 container = Container{"name": containerName}
Jon Perritt816d2a02014-03-11 20:49:46 -050084 }
Jon Perritt8c93a302014-09-28 22:35:57 -050085 return container, err
Jon Perritt816d2a02014-03-11 20:49:46 -050086}
87
88// Delete is a function that deletes a container.
Jon Perritt8c93a302014-09-28 22:35:57 -050089func Delete(c *gophercloud.ServiceClient, containerName string) error {
90 _, err := perigee.Request("DELETE", containerURL(c, containerName), perigee.Options{
91 MoreHeaders: c.Provider.AuthenticatedHeaders(),
Ash Wilson604320e2014-09-10 16:02:28 -040092 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -050093 })
94 return err
95}
96
Jon Perritt8c93a302014-09-28 22:35:57 -050097// UpdateOpts is a structure that holds parameters for updating, creating, or deleting a
98// container's metadata.
99type 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 Perritt816d2a02014-03-11 20:49:46 -0500111// Update is a function that creates, updates, or deletes a container's metadata.
Jon Perritt8c93a302014-09-28 22:35:57 -0500112func Update(c *gophercloud.ServiceClient, containerName string, opts UpdateOpts) error {
Ash Wilson604320e2014-09-10 16:02:28 -0400113 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500114
Jon Perritt8c93a302014-09-28 22:35:57 -0500115 headers, err := gophercloud.BuildHeaders(opts)
116 if err != nil {
117 return err
118 }
119
120 for k, v := range headers {
Jon Perritt816d2a02014-03-11 20:49:46 -0500121 h[k] = v
122 }
123
124 for k, v := range opts.Metadata {
125 h["X-Container-Meta-"+k] = v
126 }
127
Jon Perritt8c93a302014-09-28 22:35:57 -0500128 url := containerURL(c, containerName)
129 _, err = perigee.Request("POST", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -0500130 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -0400131 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500132 })
133 return err
134}
135
136// Get is a function that retrieves the metadata of a container. To extract just the custom
Jon Perritteb575642014-04-24 15:16:31 -0500137// metadata, pass the GetResult response to the ExtractMetadata function.
Jon Perritt8c93a302014-09-28 22:35:57 -0500138func 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 Wilsone47ea9e2014-09-10 16:03:44 -0400142 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500143 })
Jon Perritt8c93a302014-09-28 22:35:57 -0500144 gr.Err = err
145 gr.Resp = &resp.HttpResponse
146 return gr
Jon Perritt816d2a02014-03-11 20:49:46 -0500147}