blob: 9f3b2af0a6f93897100136b576aef8b6a91d9cfa [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 Perritte90aced2014-10-12 23:24:06 -05009// ListOptsBuilder allows extensions to add additional parameters to the List
10// request.
11type ListOptsBuilder interface {
12 ToContainerListParams() (bool, string, error)
13}
14
Jon Perritt8c93a302014-09-28 22:35:57 -050015// ListOpts is a structure that holds options for listing containers.
16type ListOpts struct {
17 Full bool
Jon Perritt04851d32014-10-14 02:07:13 -050018 Limit int `q:"limit"`
19 Marker string `q:"marker"`
20 EndMarker string `q:"end_marker"`
21 Format string `q:"format"`
22 Prefix string `q:"prefix"`
23 Delimiter string `q:"delimiter"`
Ash Wilson0faafcc2014-09-16 15:20:17 -040024}
25
Jon Perritte90aced2014-10-12 23:24:06 -050026// ToContainerListParams formats a ListOpts into a query string and boolean
27// representing whether to list complete information for each container.
28func (opts ListOpts) ToContainerListParams() (bool, string, error) {
29 q, err := gophercloud.BuildQueryString(opts)
30 if err != nil {
31 return false, "", err
32 }
33 return opts.Full, q.String(), nil
34}
35
36// List is a function that retrieves containers associated with the account as
37// well as account metadata. It returns a pager which can be iterated with the
38// EachPage function.
39func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
40 headers := map[string]string{"Accept": "text/plain", "Content-Type": "text/plain"}
Jon Perritt816d2a02014-03-11 20:49:46 -050041
Jon Perrittb3461402014-10-09 21:36:17 -050042 url := listURL(c)
Jon Perrittde47eac2014-09-30 15:34:17 -050043 if opts != nil {
Jon Perritte90aced2014-10-12 23:24:06 -050044 full, query, err := opts.ToContainerListParams()
Jon Perrittde47eac2014-09-30 15:34:17 -050045 if err != nil {
46 return pagination.Pager{Err: err}
47 }
Jon Perritte90aced2014-10-12 23:24:06 -050048 url += query
Jon Perrittde47eac2014-09-30 15:34:17 -050049
Jon Perritte90aced2014-10-12 23:24:06 -050050 if full {
51 headers = map[string]string{"Accept": "application/json", "Content-Type": "application/json"}
Jon Perrittde47eac2014-09-30 15:34:17 -050052 }
Ash Wilson0faafcc2014-09-16 15:20:17 -040053 }
54
Ash Wilsonb8b16f82014-10-20 10:19:49 -040055 createPage := func(r pagination.PageResult) pagination.Page {
56 p := ContainerPage{pagination.MarkerPageBase{PageResult: r}}
Ash Wilson0faafcc2014-09-16 15:20:17 -040057 p.MarkerPageBase.Owner = p
58 return p
Jon Perritt816d2a02014-03-11 20:49:46 -050059 }
60
Jon Perritt584c94f2014-09-24 18:00:16 -050061 pager := pagination.NewPager(c, url, createPage)
62 pager.Headers = headers
63 return pager
Jon Perritt816d2a02014-03-11 20:49:46 -050064}
65
Jon Perritte90aced2014-10-12 23:24:06 -050066// CreateOptsBuilder allows extensions to add additional parameters to the
67// Create request.
68type CreateOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -050069 ToContainerCreateMap() (map[string]string, error)
Jon Perritte90aced2014-10-12 23:24:06 -050070}
71
Jon Perritt8c93a302014-09-28 22:35:57 -050072// CreateOpts is a structure that holds parameters for creating a container.
73type CreateOpts struct {
74 Metadata map[string]string
75 ContainerRead string `h:"X-Container-Read"`
76 ContainerSyncTo string `h:"X-Container-Sync-To"`
77 ContainerSyncKey string `h:"X-Container-Sync-Key"`
78 ContainerWrite string `h:"X-Container-Write"`
79 ContentType string `h:"Content-Type"`
80 DetectContentType bool `h:"X-Detect-Content-Type"`
81 IfNoneMatch string `h:"If-None-Match"`
82 VersionsLocation string `h:"X-Versions-Location"`
83}
Jon Perritt816d2a02014-03-11 20:49:46 -050084
Jon Perritt04851d32014-10-14 02:07:13 -050085// ToContainerCreateMap formats a CreateOpts into a map of headers.
86func (opts CreateOpts) ToContainerCreateMap() (map[string]string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -050087 h, err := gophercloud.BuildHeaders(opts)
88 if err != nil {
89 return nil, err
90 }
91 for k, v := range opts.Metadata {
92 h["X-Container-Meta-"+k] = v
93 }
94 return h, nil
95}
96
Jon Perritt8c93a302014-09-28 22:35:57 -050097// Create is a function that creates a new container.
Jon Perritte90aced2014-10-12 23:24:06 -050098func Create(c *gophercloud.ServiceClient, containerName string, opts CreateOptsBuilder) CreateResult {
Jon Perritt5db08922014-09-30 21:32:48 -050099 var res CreateResult
Ash Wilson77857dc2014-10-22 09:09:02 -0400100 h := c.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500101
Jon Perrittde47eac2014-09-30 15:34:17 -0500102 if opts != nil {
Jon Perritt04851d32014-10-14 02:07:13 -0500103 headers, err := opts.ToContainerCreateMap()
Jon Perrittde47eac2014-09-30 15:34:17 -0500104 if err != nil {
Jon Perritt5db08922014-09-30 21:32:48 -0500105 res.Err = err
106 return res
Jon Perrittde47eac2014-09-30 15:34:17 -0500107 }
108
109 for k, v := range headers {
110 h[k] = v
111 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500112 }
113
Jon Perrittb3461402014-10-09 21:36:17 -0500114 resp, err := perigee.Request("PUT", createURL(c, containerName), perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -0500115 MoreHeaders: h,
Jon Perritta77ba0d2014-10-17 01:15:29 -0500116 OkCodes: []int{201, 202, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500117 })
Ash Wilson72e4d2c2014-10-20 10:27:30 -0400118 res.Header = resp.HttpResponse.Header
Jon Perritt5db08922014-09-30 21:32:48 -0500119 res.Err = err
120 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500121}
122
123// Delete is a function that deletes a container.
Jon Perritt5db08922014-09-30 21:32:48 -0500124func Delete(c *gophercloud.ServiceClient, containerName string) DeleteResult {
125 var res DeleteResult
Jamie Hannafordaa69b192014-10-27 14:03:11 +0100126 _, res.Err = perigee.Request("DELETE", deleteURL(c, containerName), perigee.Options{
Ash Wilson77857dc2014-10-22 09:09:02 -0400127 MoreHeaders: c.AuthenticatedHeaders(),
Jon Perritt33ed7412014-10-17 19:35:32 -0500128 OkCodes: []int{202, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500129 })
Jon Perritt5db08922014-09-30 21:32:48 -0500130 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500131}
132
Jon Perritte90aced2014-10-12 23:24:06 -0500133// UpdateOptsBuilder allows extensions to add additional parameters to the
134// Update request.
135type UpdateOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -0500136 ToContainerUpdateMap() (map[string]string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500137}
138
139// UpdateOpts is a structure that holds parameters for updating, creating, or
140// deleting a container's metadata.
Jon Perritt8c93a302014-09-28 22:35:57 -0500141type UpdateOpts struct {
142 Metadata map[string]string
143 ContainerRead string `h:"X-Container-Read"`
144 ContainerSyncTo string `h:"X-Container-Sync-To"`
145 ContainerSyncKey string `h:"X-Container-Sync-Key"`
146 ContainerWrite string `h:"X-Container-Write"`
147 ContentType string `h:"Content-Type"`
148 DetectContentType bool `h:"X-Detect-Content-Type"`
149 RemoveVersionsLocation string `h:"X-Remove-Versions-Location"`
150 VersionsLocation string `h:"X-Versions-Location"`
151}
152
Jon Perritt04851d32014-10-14 02:07:13 -0500153// ToContainerUpdateMap formats a CreateOpts into a map of headers.
154func (opts UpdateOpts) ToContainerUpdateMap() (map[string]string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500155 h, err := gophercloud.BuildHeaders(opts)
156 if err != nil {
157 return nil, err
158 }
159 for k, v := range opts.Metadata {
160 h["X-Container-Meta-"+k] = v
161 }
162 return h, nil
163}
164
165// Update is a function that creates, updates, or deletes a container's
166// metadata.
167func Update(c *gophercloud.ServiceClient, containerName string, opts UpdateOptsBuilder) UpdateResult {
Jon Perritt5db08922014-09-30 21:32:48 -0500168 var res UpdateResult
Ash Wilson77857dc2014-10-22 09:09:02 -0400169 h := c.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500170
Jon Perrittde47eac2014-09-30 15:34:17 -0500171 if opts != nil {
Jon Perritt04851d32014-10-14 02:07:13 -0500172 headers, err := opts.ToContainerUpdateMap()
Jon Perrittde47eac2014-09-30 15:34:17 -0500173 if err != nil {
Jon Perritt5db08922014-09-30 21:32:48 -0500174 res.Err = err
175 return res
Jon Perrittde47eac2014-09-30 15:34:17 -0500176 }
177
178 for k, v := range headers {
179 h[k] = v
180 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500181 }
182
Jon Perrittb3461402014-10-09 21:36:17 -0500183 resp, err := perigee.Request("POST", updateURL(c, containerName), perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -0500184 MoreHeaders: h,
Jon Perritt33ed7412014-10-17 19:35:32 -0500185 OkCodes: []int{202, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500186 })
Ash Wilson72e4d2c2014-10-20 10:27:30 -0400187 res.Header = resp.HttpResponse.Header
Jon Perritt5db08922014-09-30 21:32:48 -0500188 res.Err = err
189 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500190}
191
Jon Perritte90aced2014-10-12 23:24:06 -0500192// Get is a function that retrieves the metadata of a container. To extract just
193// the custom metadata, pass the GetResult response to the ExtractMetadata
194// function.
Jon Perritt8c93a302014-09-28 22:35:57 -0500195func Get(c *gophercloud.ServiceClient, containerName string) GetResult {
Jon Perritt5db08922014-09-30 21:32:48 -0500196 var res GetResult
Jon Perrittb3461402014-10-09 21:36:17 -0500197 resp, err := perigee.Request("HEAD", getURL(c, containerName), perigee.Options{
Ash Wilson77857dc2014-10-22 09:09:02 -0400198 MoreHeaders: c.AuthenticatedHeaders(),
Jon Perrittb5c78122014-10-15 20:44:39 -0500199 OkCodes: []int{200, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500200 })
Ash Wilson72e4d2c2014-10-20 10:27:30 -0400201 res.Header = resp.HttpResponse.Header
Jon Perritt5db08922014-09-30 21:32:48 -0500202 res.Err = err
203 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500204}