blob: a29d7da5d6815b5a46942d32f824333dc8016972 [file] [log] [blame]
Jon Perritt816d2a02014-03-11 20:49:46 -05001package containers
2
3import (
Ash Wilson604320e2014-09-10 16:02:28 -04004 "github.com/rackspace/gophercloud"
Ash Wilson0faafcc2014-09-16 15:20:17 -04005 "github.com/rackspace/gophercloud/pagination"
Jon Perritt816d2a02014-03-11 20:49:46 -05006)
7
Jon Perritte90aced2014-10-12 23:24:06 -05008// ListOptsBuilder allows extensions to add additional parameters to the List
9// request.
10type ListOptsBuilder interface {
11 ToContainerListParams() (bool, string, error)
12}
13
Jon Perritt8c93a302014-09-28 22:35:57 -050014// ListOpts is a structure that holds options for listing containers.
15type ListOpts struct {
16 Full bool
Jon Perritt04851d32014-10-14 02:07:13 -050017 Limit int `q:"limit"`
18 Marker string `q:"marker"`
19 EndMarker string `q:"end_marker"`
20 Format string `q:"format"`
21 Prefix string `q:"prefix"`
22 Delimiter string `q:"delimiter"`
Ash Wilson0faafcc2014-09-16 15:20:17 -040023}
24
Jon Perritte90aced2014-10-12 23:24:06 -050025// ToContainerListParams formats a ListOpts into a query string and boolean
26// representing whether to list complete information for each container.
27func (opts ListOpts) ToContainerListParams() (bool, string, error) {
28 q, err := gophercloud.BuildQueryString(opts)
29 if err != nil {
30 return false, "", err
31 }
32 return opts.Full, q.String(), nil
33}
34
35// List is a function that retrieves containers associated with the account as
36// well as account metadata. It returns a pager which can be iterated with the
37// EachPage function.
38func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
39 headers := map[string]string{"Accept": "text/plain", "Content-Type": "text/plain"}
Jon Perritt816d2a02014-03-11 20:49:46 -050040
Jon Perrittb3461402014-10-09 21:36:17 -050041 url := listURL(c)
Jon Perrittde47eac2014-09-30 15:34:17 -050042 if opts != nil {
Jon Perritte90aced2014-10-12 23:24:06 -050043 full, query, err := opts.ToContainerListParams()
Jon Perrittde47eac2014-09-30 15:34:17 -050044 if err != nil {
45 return pagination.Pager{Err: err}
46 }
Jon Perritte90aced2014-10-12 23:24:06 -050047 url += query
Jon Perrittde47eac2014-09-30 15:34:17 -050048
Jon Perritte90aced2014-10-12 23:24:06 -050049 if full {
50 headers = map[string]string{"Accept": "application/json", "Content-Type": "application/json"}
Jon Perrittde47eac2014-09-30 15:34:17 -050051 }
Ash Wilson0faafcc2014-09-16 15:20:17 -040052 }
53
Ash Wilsonb8b16f82014-10-20 10:19:49 -040054 createPage := func(r pagination.PageResult) pagination.Page {
55 p := ContainerPage{pagination.MarkerPageBase{PageResult: r}}
Ash Wilson0faafcc2014-09-16 15:20:17 -040056 p.MarkerPageBase.Owner = p
57 return p
Jon Perritt816d2a02014-03-11 20:49:46 -050058 }
59
Jon Perritt584c94f2014-09-24 18:00:16 -050060 pager := pagination.NewPager(c, url, createPage)
61 pager.Headers = headers
62 return pager
Jon Perritt816d2a02014-03-11 20:49:46 -050063}
64
Jon Perritte90aced2014-10-12 23:24:06 -050065// CreateOptsBuilder allows extensions to add additional parameters to the
66// Create request.
67type CreateOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -050068 ToContainerCreateMap() (map[string]string, error)
Jon Perritte90aced2014-10-12 23:24:06 -050069}
70
Jon Perritt8c93a302014-09-28 22:35:57 -050071// CreateOpts is a structure that holds parameters for creating a container.
72type CreateOpts struct {
73 Metadata map[string]string
74 ContainerRead string `h:"X-Container-Read"`
75 ContainerSyncTo string `h:"X-Container-Sync-To"`
76 ContainerSyncKey string `h:"X-Container-Sync-Key"`
77 ContainerWrite string `h:"X-Container-Write"`
78 ContentType string `h:"Content-Type"`
79 DetectContentType bool `h:"X-Detect-Content-Type"`
80 IfNoneMatch string `h:"If-None-Match"`
81 VersionsLocation string `h:"X-Versions-Location"`
82}
Jon Perritt816d2a02014-03-11 20:49:46 -050083
Jon Perritt04851d32014-10-14 02:07:13 -050084// ToContainerCreateMap formats a CreateOpts into a map of headers.
85func (opts CreateOpts) ToContainerCreateMap() (map[string]string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -050086 h, err := gophercloud.BuildHeaders(opts)
87 if err != nil {
88 return nil, err
89 }
90 for k, v := range opts.Metadata {
91 h["X-Container-Meta-"+k] = v
92 }
93 return h, nil
94}
95
Jon Perritt8c93a302014-09-28 22:35:57 -050096// Create is a function that creates a new container.
Jon Perritte90aced2014-10-12 23:24:06 -050097func Create(c *gophercloud.ServiceClient, containerName string, opts CreateOptsBuilder) CreateResult {
Jon Perritt5db08922014-09-30 21:32:48 -050098 var res CreateResult
Ash Wilson77857dc2014-10-22 09:09:02 -040099 h := c.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500100
Jon Perrittde47eac2014-09-30 15:34:17 -0500101 if opts != nil {
Jon Perritt04851d32014-10-14 02:07:13 -0500102 headers, err := opts.ToContainerCreateMap()
Jon Perrittde47eac2014-09-30 15:34:17 -0500103 if err != nil {
Jon Perritt5db08922014-09-30 21:32:48 -0500104 res.Err = err
105 return res
Jon Perrittde47eac2014-09-30 15:34:17 -0500106 }
107
108 for k, v := range headers {
109 h[k] = v
110 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500111 }
112
Ash Wilson59fb6c42015-02-12 16:21:13 -0500113 resp, err := c.Request("PUT", createURL(c, containerName), gophercloud.RequestOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500114 MoreHeaders: h,
Jon Perritta77ba0d2014-10-17 01:15:29 -0500115 OkCodes: []int{201, 202, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500116 })
Ash Wilson59fb6c42015-02-12 16:21:13 -0500117 res.Header = resp.Header
Jon Perritt5db08922014-09-30 21:32:48 -0500118 res.Err = err
119 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500120}
121
122// Delete is a function that deletes a container.
Jon Perritt5db08922014-09-30 21:32:48 -0500123func Delete(c *gophercloud.ServiceClient, containerName string) DeleteResult {
124 var res DeleteResult
Ash Wilson59fb6c42015-02-12 16:21:13 -0500125 _, res.Err = c.Request("DELETE", deleteURL(c, containerName), gophercloud.RequestOpts{
126 OkCodes: []int{202, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500127 })
Jon Perritt5db08922014-09-30 21:32:48 -0500128 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500129}
130
Jon Perritte90aced2014-10-12 23:24:06 -0500131// UpdateOptsBuilder allows extensions to add additional parameters to the
132// Update request.
133type UpdateOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -0500134 ToContainerUpdateMap() (map[string]string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500135}
136
137// UpdateOpts is a structure that holds parameters for updating, creating, or
138// deleting a container's metadata.
Jon Perritt8c93a302014-09-28 22:35:57 -0500139type UpdateOpts struct {
140 Metadata map[string]string
141 ContainerRead string `h:"X-Container-Read"`
142 ContainerSyncTo string `h:"X-Container-Sync-To"`
143 ContainerSyncKey string `h:"X-Container-Sync-Key"`
144 ContainerWrite string `h:"X-Container-Write"`
145 ContentType string `h:"Content-Type"`
146 DetectContentType bool `h:"X-Detect-Content-Type"`
147 RemoveVersionsLocation string `h:"X-Remove-Versions-Location"`
148 VersionsLocation string `h:"X-Versions-Location"`
149}
150
Jon Perritt04851d32014-10-14 02:07:13 -0500151// ToContainerUpdateMap formats a CreateOpts into a map of headers.
152func (opts UpdateOpts) ToContainerUpdateMap() (map[string]string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500153 h, err := gophercloud.BuildHeaders(opts)
154 if err != nil {
155 return nil, err
156 }
157 for k, v := range opts.Metadata {
158 h["X-Container-Meta-"+k] = v
159 }
160 return h, nil
161}
162
163// Update is a function that creates, updates, or deletes a container's
164// metadata.
165func Update(c *gophercloud.ServiceClient, containerName string, opts UpdateOptsBuilder) UpdateResult {
Jon Perritt5db08922014-09-30 21:32:48 -0500166 var res UpdateResult
Ash Wilson77857dc2014-10-22 09:09:02 -0400167 h := c.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500168
Jon Perrittde47eac2014-09-30 15:34:17 -0500169 if opts != nil {
Jon Perritt04851d32014-10-14 02:07:13 -0500170 headers, err := opts.ToContainerUpdateMap()
Jon Perrittde47eac2014-09-30 15:34:17 -0500171 if err != nil {
Jon Perritt5db08922014-09-30 21:32:48 -0500172 res.Err = err
173 return res
Jon Perrittde47eac2014-09-30 15:34:17 -0500174 }
175
176 for k, v := range headers {
177 h[k] = v
178 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500179 }
180
Ash Wilson59fb6c42015-02-12 16:21:13 -0500181 resp, err := c.Request("POST", updateURL(c, containerName), gophercloud.RequestOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500182 MoreHeaders: h,
Jon Perritt33ed7412014-10-17 19:35:32 -0500183 OkCodes: []int{202, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500184 })
Ash Wilson59fb6c42015-02-12 16:21:13 -0500185 res.Header = resp.Header
Jon Perritt5db08922014-09-30 21:32:48 -0500186 res.Err = err
187 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500188}
189
Jon Perritte90aced2014-10-12 23:24:06 -0500190// Get is a function that retrieves the metadata of a container. To extract just
191// the custom metadata, pass the GetResult response to the ExtractMetadata
192// function.
Jon Perritt8c93a302014-09-28 22:35:57 -0500193func Get(c *gophercloud.ServiceClient, containerName string) GetResult {
Jon Perritt5db08922014-09-30 21:32:48 -0500194 var res GetResult
Ash Wilson59fb6c42015-02-12 16:21:13 -0500195 resp, err := c.Request("HEAD", getURL(c, containerName), gophercloud.RequestOpts{
196 OkCodes: []int{200, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500197 })
Ash Wilson59fb6c42015-02-12 16:21:13 -0500198 res.Header = resp.Header
Jon Perritt5db08922014-09-30 21:32:48 -0500199 res.Err = err
200 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500201}