blob: 89fa0141c65d449af41601a704f66ae6108e771d [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 Wilson604320e2014-09-10 16:02:28 -0400100 h := c.Provider.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
Jon Perrittb3461402014-10-09 21:36:17 -0500126 resp, err := perigee.Request("DELETE", deleteURL(c, containerName), perigee.Options{
Jon Perritt8c93a302014-09-28 22:35:57 -0500127 MoreHeaders: c.Provider.AuthenticatedHeaders(),
Jon Perritt33ed7412014-10-17 19:35:32 -0500128 OkCodes: []int{202, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500129 })
Ash Wilson72e4d2c2014-10-20 10:27:30 -0400130 res.Header = resp.HttpResponse.Header
Jon Perritt5db08922014-09-30 21:32:48 -0500131 res.Err = err
132 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500133}
134
Jon Perritte90aced2014-10-12 23:24:06 -0500135// UpdateOptsBuilder allows extensions to add additional parameters to the
136// Update request.
137type UpdateOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -0500138 ToContainerUpdateMap() (map[string]string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500139}
140
141// UpdateOpts is a structure that holds parameters for updating, creating, or
142// deleting a container's metadata.
Jon Perritt8c93a302014-09-28 22:35:57 -0500143type UpdateOpts struct {
144 Metadata map[string]string
145 ContainerRead string `h:"X-Container-Read"`
146 ContainerSyncTo string `h:"X-Container-Sync-To"`
147 ContainerSyncKey string `h:"X-Container-Sync-Key"`
148 ContainerWrite string `h:"X-Container-Write"`
149 ContentType string `h:"Content-Type"`
150 DetectContentType bool `h:"X-Detect-Content-Type"`
151 RemoveVersionsLocation string `h:"X-Remove-Versions-Location"`
152 VersionsLocation string `h:"X-Versions-Location"`
153}
154
Jon Perritt04851d32014-10-14 02:07:13 -0500155// ToContainerUpdateMap formats a CreateOpts into a map of headers.
156func (opts UpdateOpts) ToContainerUpdateMap() (map[string]string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500157 h, err := gophercloud.BuildHeaders(opts)
158 if err != nil {
159 return nil, err
160 }
161 for k, v := range opts.Metadata {
162 h["X-Container-Meta-"+k] = v
163 }
164 return h, nil
165}
166
167// Update is a function that creates, updates, or deletes a container's
168// metadata.
169func Update(c *gophercloud.ServiceClient, containerName string, opts UpdateOptsBuilder) UpdateResult {
Jon Perritt5db08922014-09-30 21:32:48 -0500170 var res UpdateResult
Ash Wilson604320e2014-09-10 16:02:28 -0400171 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500172
Jon Perrittde47eac2014-09-30 15:34:17 -0500173 if opts != nil {
Jon Perritt04851d32014-10-14 02:07:13 -0500174 headers, err := opts.ToContainerUpdateMap()
Jon Perrittde47eac2014-09-30 15:34:17 -0500175 if err != nil {
Jon Perritt5db08922014-09-30 21:32:48 -0500176 res.Err = err
177 return res
Jon Perrittde47eac2014-09-30 15:34:17 -0500178 }
179
180 for k, v := range headers {
181 h[k] = v
182 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500183 }
184
Jon Perrittb3461402014-10-09 21:36:17 -0500185 resp, err := perigee.Request("POST", updateURL(c, containerName), perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -0500186 MoreHeaders: h,
Jon Perritt33ed7412014-10-17 19:35:32 -0500187 OkCodes: []int{202, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500188 })
Ash Wilson72e4d2c2014-10-20 10:27:30 -0400189 res.Header = resp.HttpResponse.Header
Jon Perritt5db08922014-09-30 21:32:48 -0500190 res.Err = err
191 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500192}
193
Jon Perritte90aced2014-10-12 23:24:06 -0500194// Get is a function that retrieves the metadata of a container. To extract just
195// the custom metadata, pass the GetResult response to the ExtractMetadata
196// function.
Jon Perritt8c93a302014-09-28 22:35:57 -0500197func Get(c *gophercloud.ServiceClient, containerName string) GetResult {
Jon Perritt5db08922014-09-30 21:32:48 -0500198 var res GetResult
Jon Perrittb3461402014-10-09 21:36:17 -0500199 resp, err := perigee.Request("HEAD", getURL(c, containerName), perigee.Options{
Jon Perritt8c93a302014-09-28 22:35:57 -0500200 MoreHeaders: c.Provider.AuthenticatedHeaders(),
Jon Perrittb5c78122014-10-15 20:44:39 -0500201 OkCodes: []int{200, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500202 })
Ash Wilson72e4d2c2014-10-20 10:27:30 -0400203 res.Header = resp.HttpResponse.Header
Jon Perritt5db08922014-09-30 21:32:48 -0500204 res.Err = err
205 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500206}