blob: f575ea7da367685d46b44ced5de4b0cce87b89fe [file] [log] [blame]
Jon Perritt816d2a02014-03-11 20:49:46 -05001package containers
2
3import (
Jon Perritt27249f42016-02-18 10:35:59 -06004 "github.com/gophercloud/gophercloud"
5 "github.com/gophercloud/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)
Jon Perrittfea90732016-03-15 02:57:05 -050029 return opts.Full, q.String(), err
Jon Perritte90aced2014-10-12 23:24:06 -050030}
31
32// List is a function that retrieves containers associated with the account as
33// well as account metadata. It returns a pager which can be iterated with the
34// EachPage function.
35func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
36 headers := map[string]string{"Accept": "text/plain", "Content-Type": "text/plain"}
Jon Perritt816d2a02014-03-11 20:49:46 -050037
Jon Perrittb3461402014-10-09 21:36:17 -050038 url := listURL(c)
Jon Perrittde47eac2014-09-30 15:34:17 -050039 if opts != nil {
Jon Perritte90aced2014-10-12 23:24:06 -050040 full, query, err := opts.ToContainerListParams()
Jon Perrittde47eac2014-09-30 15:34:17 -050041 if err != nil {
42 return pagination.Pager{Err: err}
43 }
Jon Perritte90aced2014-10-12 23:24:06 -050044 url += query
Jon Perrittde47eac2014-09-30 15:34:17 -050045
Jon Perritte90aced2014-10-12 23:24:06 -050046 if full {
47 headers = map[string]string{"Accept": "application/json", "Content-Type": "application/json"}
Jon Perrittde47eac2014-09-30 15:34:17 -050048 }
Ash Wilson0faafcc2014-09-16 15:20:17 -040049 }
50
Jon Perrittfea90732016-03-15 02:57:05 -050051 pager := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
Ash Wilsonb8b16f82014-10-20 10:19:49 -040052 p := ContainerPage{pagination.MarkerPageBase{PageResult: r}}
Ash Wilson0faafcc2014-09-16 15:20:17 -040053 p.MarkerPageBase.Owner = p
54 return p
Jon Perrittfea90732016-03-15 02:57:05 -050055 })
Jon Perritt584c94f2014-09-24 18:00:16 -050056 pager.Headers = headers
57 return pager
Jon Perritt816d2a02014-03-11 20:49:46 -050058}
59
Jon Perritte90aced2014-10-12 23:24:06 -050060// CreateOptsBuilder allows extensions to add additional parameters to the
61// Create request.
62type CreateOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -050063 ToContainerCreateMap() (map[string]string, error)
Jon Perritte90aced2014-10-12 23:24:06 -050064}
65
Jon Perritt8c93a302014-09-28 22:35:57 -050066// CreateOpts is a structure that holds parameters for creating a container.
67type CreateOpts struct {
68 Metadata map[string]string
69 ContainerRead string `h:"X-Container-Read"`
70 ContainerSyncTo string `h:"X-Container-Sync-To"`
71 ContainerSyncKey string `h:"X-Container-Sync-Key"`
72 ContainerWrite string `h:"X-Container-Write"`
73 ContentType string `h:"Content-Type"`
74 DetectContentType bool `h:"X-Detect-Content-Type"`
75 IfNoneMatch string `h:"If-None-Match"`
76 VersionsLocation string `h:"X-Versions-Location"`
77}
Jon Perritt816d2a02014-03-11 20:49:46 -050078
Jon Perritt04851d32014-10-14 02:07:13 -050079// ToContainerCreateMap formats a CreateOpts into a map of headers.
80func (opts CreateOpts) ToContainerCreateMap() (map[string]string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -050081 h, err := gophercloud.BuildHeaders(opts)
82 if err != nil {
83 return nil, err
84 }
85 for k, v := range opts.Metadata {
86 h["X-Container-Meta-"+k] = v
87 }
88 return h, nil
89}
90
Jon Perritt8c93a302014-09-28 22:35:57 -050091// Create is a function that creates a new container.
Jon Perritte90aced2014-10-12 23:24:06 -050092func Create(c *gophercloud.ServiceClient, containerName string, opts CreateOptsBuilder) CreateResult {
Jon Perrittfea90732016-03-15 02:57:05 -050093 var r CreateResult
94 h := make(map[string]string)
Jon Perrittde47eac2014-09-30 15:34:17 -050095 if opts != nil {
Jon Perritt04851d32014-10-14 02:07:13 -050096 headers, err := opts.ToContainerCreateMap()
Jon Perrittde47eac2014-09-30 15:34:17 -050097 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -050098 r.Err = err
99 return r
Jon Perrittde47eac2014-09-30 15:34:17 -0500100 }
Jon Perrittde47eac2014-09-30 15:34:17 -0500101 for k, v := range headers {
102 h[k] = v
103 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500104 }
Jon Perritta33da232016-03-02 04:43:08 -0600105 resp, err := c.Request("PUT", createURL(c, containerName), &gophercloud.RequestOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500106 MoreHeaders: h,
Jon Perritta77ba0d2014-10-17 01:15:29 -0500107 OkCodes: []int{201, 202, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500108 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600109 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500110 r.Header = resp.Header
Jon Perritta2c88b22015-05-18 11:23:30 -0600111 }
Jon Perrittfea90732016-03-15 02:57:05 -0500112 r.Err = err
113 return r
Jon Perritt816d2a02014-03-11 20:49:46 -0500114}
115
116// Delete is a function that deletes a container.
Jon Perritt5db08922014-09-30 21:32:48 -0500117func Delete(c *gophercloud.ServiceClient, containerName string) DeleteResult {
Jon Perrittfea90732016-03-15 02:57:05 -0500118 var r DeleteResult
119 _, r.Err = c.Delete(deleteURL(c, containerName), nil)
120 return r
Jon Perritt816d2a02014-03-11 20:49:46 -0500121}
122
Jon Perritte90aced2014-10-12 23:24:06 -0500123// UpdateOptsBuilder allows extensions to add additional parameters to the
124// Update request.
125type UpdateOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -0500126 ToContainerUpdateMap() (map[string]string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500127}
128
129// UpdateOpts is a structure that holds parameters for updating, creating, or
130// deleting a container's metadata.
Jon Perritt8c93a302014-09-28 22:35:57 -0500131type UpdateOpts struct {
132 Metadata map[string]string
133 ContainerRead string `h:"X-Container-Read"`
134 ContainerSyncTo string `h:"X-Container-Sync-To"`
135 ContainerSyncKey string `h:"X-Container-Sync-Key"`
136 ContainerWrite string `h:"X-Container-Write"`
137 ContentType string `h:"Content-Type"`
138 DetectContentType bool `h:"X-Detect-Content-Type"`
139 RemoveVersionsLocation string `h:"X-Remove-Versions-Location"`
140 VersionsLocation string `h:"X-Versions-Location"`
141}
142
Jon Perritt04851d32014-10-14 02:07:13 -0500143// ToContainerUpdateMap formats a CreateOpts into a map of headers.
144func (opts UpdateOpts) ToContainerUpdateMap() (map[string]string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500145 h, err := gophercloud.BuildHeaders(opts)
146 if err != nil {
147 return nil, err
148 }
149 for k, v := range opts.Metadata {
150 h["X-Container-Meta-"+k] = v
151 }
152 return h, nil
153}
154
155// Update is a function that creates, updates, or deletes a container's
156// metadata.
157func Update(c *gophercloud.ServiceClient, containerName string, opts UpdateOptsBuilder) UpdateResult {
Jon Perrittfea90732016-03-15 02:57:05 -0500158 var r UpdateResult
159 h := make(map[string]string)
Jon Perrittde47eac2014-09-30 15:34:17 -0500160 if opts != nil {
Jon Perritt04851d32014-10-14 02:07:13 -0500161 headers, err := opts.ToContainerUpdateMap()
Jon Perrittde47eac2014-09-30 15:34:17 -0500162 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500163 r.Err = err
164 return r
Jon Perrittde47eac2014-09-30 15:34:17 -0500165 }
166
167 for k, v := range headers {
168 h[k] = v
169 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500170 }
Jon Perritta33da232016-03-02 04:43:08 -0600171 resp, err := c.Request("POST", updateURL(c, containerName), &gophercloud.RequestOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500172 MoreHeaders: h,
Jamie Hannafordc530ba12015-03-23 17:50:46 +0100173 OkCodes: []int{201, 202, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500174 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600175 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500176 r.Header = resp.Header
Jon Perritta2c88b22015-05-18 11:23:30 -0600177 }
Jon Perrittfea90732016-03-15 02:57:05 -0500178 r.Err = err
179 return r
Jon Perritt816d2a02014-03-11 20:49:46 -0500180}
181
Jon Perritte90aced2014-10-12 23:24:06 -0500182// Get is a function that retrieves the metadata of a container. To extract just
183// the custom metadata, pass the GetResult response to the ExtractMetadata
184// function.
Jon Perritt8c93a302014-09-28 22:35:57 -0500185func Get(c *gophercloud.ServiceClient, containerName string) GetResult {
Jon Perrittfea90732016-03-15 02:57:05 -0500186 var r GetResult
Jon Perritta33da232016-03-02 04:43:08 -0600187 resp, err := c.Request("HEAD", getURL(c, containerName), &gophercloud.RequestOpts{
Ash Wilson59fb6c42015-02-12 16:21:13 -0500188 OkCodes: []int{200, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500189 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600190 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500191 r.Header = resp.Header
Jon Perritta2c88b22015-05-18 11:23:30 -0600192 }
Jon Perrittfea90732016-03-15 02:57:05 -0500193 r.Err = err
194 return r
Jon Perritt816d2a02014-03-11 20:49:46 -0500195}