blob: 013c6ff168ff08bf5188a4370fbf13c9afdb7e07 [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 Perritt8c93a302014-09-28 22:35:57 -050025 query, err := gophercloud.BuildQueryString(opts)
26 if err != nil {
27 return pagination.Pager{Err: err}
28 }
Jon Perritt816d2a02014-03-11 20:49:46 -050029
30 if !opts.Full {
Jon Perrittfdac6e52014-09-29 19:43:45 -050031 headers = map[string]string{"Accept": "text/plain", "Content-Type": "text/plain"}
Ash Wilson0faafcc2014-09-16 15:20:17 -040032 }
33
34 createPage := func(r pagination.LastHTTPResponse) pagination.Page {
Jon Perritt8c93a302014-09-28 22:35:57 -050035 p := ContainerPage{pagination.MarkerPageBase{LastHTTPResponse: r}}
Ash Wilson0faafcc2014-09-16 15:20:17 -040036 p.MarkerPageBase.Owner = p
37 return p
Jon Perritt816d2a02014-03-11 20:49:46 -050038 }
39
Jon Perrittfc39b942014-09-10 21:18:19 -050040 url := accountURL(c) + query
Jon Perritt584c94f2014-09-24 18:00:16 -050041 pager := pagination.NewPager(c, url, createPage)
42 pager.Headers = headers
43 return pager
Jon Perritt816d2a02014-03-11 20:49:46 -050044}
45
Jon Perritt8c93a302014-09-28 22:35:57 -050046// CreateOpts is a structure that holds parameters for creating a container.
47type 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 Perritt816d2a02014-03-11 20:49:46 -050058
Jon Perritt8c93a302014-09-28 22:35:57 -050059// Create is a function that creates a new container.
60func Create(c *gophercloud.ServiceClient, containerName string, opts CreateOpts) (Container, error) {
61 var container Container
Ash Wilson604320e2014-09-10 16:02:28 -040062 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -050063
Jon Perritt8c93a302014-09-28 22:35:57 -050064 headers, err := gophercloud.BuildHeaders(opts)
65 if err != nil {
66 return container, err
67 }
68
69 for k, v := range headers {
Jon Perritt816d2a02014-03-11 20:49:46 -050070 h[k] = v
71 }
72
73 for k, v := range opts.Metadata {
74 h["X-Container-Meta-"+k] = v
75 }
76
Jon Perritt8c93a302014-09-28 22:35:57 -050077 _, err = perigee.Request("PUT", containerURL(c, containerName), perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -050078 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -040079 OkCodes: []int{201, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -050080 })
81 if err == nil {
Jon Perritt8aa40262014-09-29 15:41:32 -050082 container = Container{Name: containerName}
Jon Perritt816d2a02014-03-11 20:49:46 -050083 }
Jon Perritt8c93a302014-09-28 22:35:57 -050084 return container, err
Jon Perritt816d2a02014-03-11 20:49:46 -050085}
86
87// Delete is a function that deletes a container.
Jon Perritt8c93a302014-09-28 22:35:57 -050088func Delete(c *gophercloud.ServiceClient, containerName string) error {
89 _, err := perigee.Request("DELETE", containerURL(c, containerName), perigee.Options{
90 MoreHeaders: c.Provider.AuthenticatedHeaders(),
Ash Wilson604320e2014-09-10 16:02:28 -040091 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -050092 })
93 return err
94}
95
Jon Perritt8c93a302014-09-28 22:35:57 -050096// UpdateOpts is a structure that holds parameters for updating, creating, or deleting a
97// container's metadata.
98type 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 Perritt816d2a02014-03-11 20:49:46 -0500110// Update is a function that creates, updates, or deletes a container's metadata.
Jon Perritt8c93a302014-09-28 22:35:57 -0500111func Update(c *gophercloud.ServiceClient, containerName string, opts UpdateOpts) error {
Ash Wilson604320e2014-09-10 16:02:28 -0400112 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500113
Jon Perritt8c93a302014-09-28 22:35:57 -0500114 headers, err := gophercloud.BuildHeaders(opts)
115 if err != nil {
116 return err
117 }
118
119 for k, v := range headers {
Jon Perritt816d2a02014-03-11 20:49:46 -0500120 h[k] = v
121 }
122
123 for k, v := range opts.Metadata {
124 h["X-Container-Meta-"+k] = v
125 }
126
Jon Perritt8c93a302014-09-28 22:35:57 -0500127 url := containerURL(c, containerName)
128 _, err = perigee.Request("POST", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -0500129 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -0400130 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500131 })
132 return err
133}
134
135// Get is a function that retrieves the metadata of a container. To extract just the custom
Jon Perritteb575642014-04-24 15:16:31 -0500136// metadata, pass the GetResult response to the ExtractMetadata function.
Jon Perritt8c93a302014-09-28 22:35:57 -0500137func 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 Wilsone47ea9e2014-09-10 16:03:44 -0400141 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500142 })
Jon Perritt8c93a302014-09-28 22:35:57 -0500143 gr.Err = err
144 gr.Resp = &resp.HttpResponse
145 return gr
Jon Perritt816d2a02014-03-11 20:49:46 -0500146}