blob: bbf8cdb952ccb2cdd0dc09b94ef8beea676fc466 [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
Jamie Hannaford1d27afa2015-03-24 16:20:45 +0100125 _, res.Err = c.Delete(deleteURL(c, containerName), nil)
Jon Perritt5db08922014-09-30 21:32:48 -0500126 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500127}
128
Jon Perritte90aced2014-10-12 23:24:06 -0500129// UpdateOptsBuilder allows extensions to add additional parameters to the
130// Update request.
131type UpdateOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -0500132 ToContainerUpdateMap() (map[string]string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500133}
134
135// UpdateOpts is a structure that holds parameters for updating, creating, or
136// deleting a container's metadata.
Jon Perritt8c93a302014-09-28 22:35:57 -0500137type UpdateOpts struct {
138 Metadata map[string]string
139 ContainerRead string `h:"X-Container-Read"`
140 ContainerSyncTo string `h:"X-Container-Sync-To"`
141 ContainerSyncKey string `h:"X-Container-Sync-Key"`
142 ContainerWrite string `h:"X-Container-Write"`
143 ContentType string `h:"Content-Type"`
144 DetectContentType bool `h:"X-Detect-Content-Type"`
145 RemoveVersionsLocation string `h:"X-Remove-Versions-Location"`
146 VersionsLocation string `h:"X-Versions-Location"`
147}
148
Jon Perritt04851d32014-10-14 02:07:13 -0500149// ToContainerUpdateMap formats a CreateOpts into a map of headers.
150func (opts UpdateOpts) ToContainerUpdateMap() (map[string]string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500151 h, err := gophercloud.BuildHeaders(opts)
152 if err != nil {
153 return nil, err
154 }
155 for k, v := range opts.Metadata {
156 h["X-Container-Meta-"+k] = v
157 }
158 return h, nil
159}
160
161// Update is a function that creates, updates, or deletes a container's
162// metadata.
163func Update(c *gophercloud.ServiceClient, containerName string, opts UpdateOptsBuilder) UpdateResult {
Jon Perritt5db08922014-09-30 21:32:48 -0500164 var res UpdateResult
Ash Wilson77857dc2014-10-22 09:09:02 -0400165 h := c.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500166
Jon Perrittde47eac2014-09-30 15:34:17 -0500167 if opts != nil {
Jon Perritt04851d32014-10-14 02:07:13 -0500168 headers, err := opts.ToContainerUpdateMap()
Jon Perrittde47eac2014-09-30 15:34:17 -0500169 if err != nil {
Jon Perritt5db08922014-09-30 21:32:48 -0500170 res.Err = err
171 return res
Jon Perrittde47eac2014-09-30 15:34:17 -0500172 }
173
174 for k, v := range headers {
175 h[k] = v
176 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500177 }
178
Ash Wilson59fb6c42015-02-12 16:21:13 -0500179 resp, err := c.Request("POST", updateURL(c, containerName), gophercloud.RequestOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500180 MoreHeaders: h,
Jamie Hannafordc530ba12015-03-23 17:50:46 +0100181 OkCodes: []int{201, 202, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500182 })
Ash Wilson59fb6c42015-02-12 16:21:13 -0500183 res.Header = resp.Header
Jon Perritt5db08922014-09-30 21:32:48 -0500184 res.Err = err
185 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500186}
187
Jon Perritte90aced2014-10-12 23:24:06 -0500188// Get is a function that retrieves the metadata of a container. To extract just
189// the custom metadata, pass the GetResult response to the ExtractMetadata
190// function.
Jon Perritt8c93a302014-09-28 22:35:57 -0500191func Get(c *gophercloud.ServiceClient, containerName string) GetResult {
Jon Perritt5db08922014-09-30 21:32:48 -0500192 var res GetResult
Ash Wilson59fb6c42015-02-12 16:21:13 -0500193 resp, err := c.Request("HEAD", getURL(c, containerName), gophercloud.RequestOpts{
194 OkCodes: []int{200, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500195 })
Ash Wilson59fb6c42015-02-12 16:21:13 -0500196 res.Header = resp.Header
Jon Perritt5db08922014-09-30 21:32:48 -0500197 res.Err = err
198 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500199}