blob: 8b54c813f5c56df590db6eeab4cb957d4601b43d [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.
Jon Perrittde47eac2014-09-30 15:34:17 -050023func List(c *gophercloud.ServiceClient, opts *ListOpts) pagination.Pager {
Ash Wilson0faafcc2014-09-16 15:20:17 -040024 var headers map[string]string
Jon Perritt816d2a02014-03-11 20:49:46 -050025
Jon Perrittde47eac2014-09-30 15:34:17 -050026 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 Wilson0faafcc2014-09-16 15:20:17 -040037 }
38
39 createPage := func(r pagination.LastHTTPResponse) pagination.Page {
Jon Perritt8c93a302014-09-28 22:35:57 -050040 p := ContainerPage{pagination.MarkerPageBase{LastHTTPResponse: r}}
Ash Wilson0faafcc2014-09-16 15:20:17 -040041 p.MarkerPageBase.Owner = p
42 return p
Jon Perritt816d2a02014-03-11 20:49:46 -050043 }
44
Jon Perritt584c94f2014-09-24 18:00:16 -050045 pager := pagination.NewPager(c, url, createPage)
46 pager.Headers = headers
47 return pager
Jon Perritt816d2a02014-03-11 20:49:46 -050048}
49
Jon Perritt8c93a302014-09-28 22:35:57 -050050// CreateOpts is a structure that holds parameters for creating a container.
51type 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 Perritt816d2a02014-03-11 20:49:46 -050062
Jon Perritt8c93a302014-09-28 22:35:57 -050063// Create is a function that creates a new container.
Jon Perrittde47eac2014-09-30 15:34:17 -050064func Create(c *gophercloud.ServiceClient, containerName string, opts *CreateOpts) (Container, error) {
Jon Perritt8c93a302014-09-28 22:35:57 -050065 var container Container
Ash Wilson604320e2014-09-10 16:02:28 -040066 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -050067
Jon Perrittde47eac2014-09-30 15:34:17 -050068 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 Perritt8c93a302014-09-28 22:35:57 -050081 }
82
Jon Perrittde47eac2014-09-30 15:34:17 -050083 _, err := perigee.Request("PUT", containerURL(c, containerName), perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -050084 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -040085 OkCodes: []int{201, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -050086 })
87 if err == nil {
Jon Perritt8aa40262014-09-29 15:41:32 -050088 container = Container{Name: containerName}
Jon Perritt816d2a02014-03-11 20:49:46 -050089 }
Jon Perritt8c93a302014-09-28 22:35:57 -050090 return container, err
Jon Perritt816d2a02014-03-11 20:49:46 -050091}
92
93// Delete is a function that deletes a container.
Jon Perritt8c93a302014-09-28 22:35:57 -050094func Delete(c *gophercloud.ServiceClient, containerName string) error {
95 _, err := perigee.Request("DELETE", containerURL(c, containerName), perigee.Options{
96 MoreHeaders: c.Provider.AuthenticatedHeaders(),
Ash Wilson604320e2014-09-10 16:02:28 -040097 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -050098 })
99 return err
100}
101
Jon Perritt8c93a302014-09-28 22:35:57 -0500102// UpdateOpts is a structure that holds parameters for updating, creating, or deleting a
103// container's metadata.
104type 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 Perritt816d2a02014-03-11 20:49:46 -0500116// Update is a function that creates, updates, or deletes a container's metadata.
Jon Perrittde47eac2014-09-30 15:34:17 -0500117func Update(c *gophercloud.ServiceClient, containerName string, opts *UpdateOpts) error {
Ash Wilson604320e2014-09-10 16:02:28 -0400118 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500119
Jon Perrittde47eac2014-09-30 15:34:17 -0500120 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 Perritt8c93a302014-09-28 22:35:57 -0500133 }
134
Jon Perrittde47eac2014-09-30 15:34:17 -0500135 _, err := perigee.Request("POST", containerURL(c, containerName), perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -0500136 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -0400137 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500138 })
139 return err
140}
141
142// Get is a function that retrieves the metadata of a container. To extract just the custom
Jon Perritteb575642014-04-24 15:16:31 -0500143// metadata, pass the GetResult response to the ExtractMetadata function.
Jon Perritt8c93a302014-09-28 22:35:57 -0500144func 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 Wilsone47ea9e2014-09-10 16:03:44 -0400148 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500149 })
Jon Perritt8c93a302014-09-28 22:35:57 -0500150 gr.Err = err
151 gr.Resp = &resp.HttpResponse
152 return gr
Jon Perritt816d2a02014-03-11 20:49:46 -0500153}