blob: ba119162a9c051aece5bbe8f02c7e28aa43d9a6f [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 Perritt3860b512016-03-29 12:01:48 -050092func Create(c *gophercloud.ServiceClient, containerName string, opts CreateOptsBuilder) (r CreateResult) {
Jon Perrittfea90732016-03-15 02:57:05 -050093 h := make(map[string]string)
Jon Perrittde47eac2014-09-30 15:34:17 -050094 if opts != nil {
Jon Perritt04851d32014-10-14 02:07:13 -050095 headers, err := opts.ToContainerCreateMap()
Jon Perrittde47eac2014-09-30 15:34:17 -050096 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -050097 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -050098 return
Jon Perrittde47eac2014-09-30 15:34:17 -050099 }
Jon Perrittde47eac2014-09-30 15:34:17 -0500100 for k, v := range headers {
101 h[k] = v
102 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500103 }
Jon Perritta33da232016-03-02 04:43:08 -0600104 resp, err := c.Request("PUT", createURL(c, containerName), &gophercloud.RequestOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500105 MoreHeaders: h,
Jon Perritta77ba0d2014-10-17 01:15:29 -0500106 OkCodes: []int{201, 202, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500107 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600108 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500109 r.Header = resp.Header
Jon Perritta2c88b22015-05-18 11:23:30 -0600110 }
Jon Perrittfea90732016-03-15 02:57:05 -0500111 r.Err = err
Jon Perritt816d2a02014-03-11 20:49:46 -0500112}
113
114// Delete is a function that deletes a container.
Jon Perritt3860b512016-03-29 12:01:48 -0500115func Delete(c *gophercloud.ServiceClient, containerName string) (r DeleteResult) {
Jon Perrittfea90732016-03-15 02:57:05 -0500116 _, r.Err = c.Delete(deleteURL(c, containerName), nil)
Jon Perritt816d2a02014-03-11 20:49:46 -0500117}
118
Jon Perritte90aced2014-10-12 23:24:06 -0500119// UpdateOptsBuilder allows extensions to add additional parameters to the
120// Update request.
121type UpdateOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -0500122 ToContainerUpdateMap() (map[string]string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500123}
124
125// UpdateOpts is a structure that holds parameters for updating, creating, or
126// deleting a container's metadata.
Jon Perritt8c93a302014-09-28 22:35:57 -0500127type UpdateOpts struct {
128 Metadata map[string]string
129 ContainerRead string `h:"X-Container-Read"`
130 ContainerSyncTo string `h:"X-Container-Sync-To"`
131 ContainerSyncKey string `h:"X-Container-Sync-Key"`
132 ContainerWrite string `h:"X-Container-Write"`
133 ContentType string `h:"Content-Type"`
134 DetectContentType bool `h:"X-Detect-Content-Type"`
135 RemoveVersionsLocation string `h:"X-Remove-Versions-Location"`
136 VersionsLocation string `h:"X-Versions-Location"`
137}
138
Jon Perritt04851d32014-10-14 02:07:13 -0500139// ToContainerUpdateMap formats a CreateOpts into a map of headers.
140func (opts UpdateOpts) ToContainerUpdateMap() (map[string]string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500141 h, err := gophercloud.BuildHeaders(opts)
142 if err != nil {
143 return nil, err
144 }
145 for k, v := range opts.Metadata {
146 h["X-Container-Meta-"+k] = v
147 }
148 return h, nil
149}
150
151// Update is a function that creates, updates, or deletes a container's
152// metadata.
Jon Perritt3860b512016-03-29 12:01:48 -0500153func Update(c *gophercloud.ServiceClient, containerName string, opts UpdateOptsBuilder) (r UpdateResult) {
Jon Perrittfea90732016-03-15 02:57:05 -0500154 h := make(map[string]string)
Jon Perrittde47eac2014-09-30 15:34:17 -0500155 if opts != nil {
Jon Perritt04851d32014-10-14 02:07:13 -0500156 headers, err := opts.ToContainerUpdateMap()
Jon Perrittde47eac2014-09-30 15:34:17 -0500157 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500158 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500159 return
Jon Perrittde47eac2014-09-30 15:34:17 -0500160 }
161
162 for k, v := range headers {
163 h[k] = v
164 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500165 }
Jon Perritta33da232016-03-02 04:43:08 -0600166 resp, err := c.Request("POST", updateURL(c, containerName), &gophercloud.RequestOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500167 MoreHeaders: h,
Jamie Hannafordc530ba12015-03-23 17:50:46 +0100168 OkCodes: []int{201, 202, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500169 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600170 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500171 r.Header = resp.Header
Jon Perritta2c88b22015-05-18 11:23:30 -0600172 }
Jon Perrittfea90732016-03-15 02:57:05 -0500173 r.Err = err
Jon Perritt816d2a02014-03-11 20:49:46 -0500174}
175
Jon Perritte90aced2014-10-12 23:24:06 -0500176// Get is a function that retrieves the metadata of a container. To extract just
177// the custom metadata, pass the GetResult response to the ExtractMetadata
178// function.
Jon Perritt3860b512016-03-29 12:01:48 -0500179func Get(c *gophercloud.ServiceClient, containerName string) (r GetResult) {
Jon Perritta33da232016-03-02 04:43:08 -0600180 resp, err := c.Request("HEAD", getURL(c, containerName), &gophercloud.RequestOpts{
Ash Wilson59fb6c42015-02-12 16:21:13 -0500181 OkCodes: []int{200, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500182 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600183 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500184 r.Header = resp.Header
Jon Perritta2c88b22015-05-18 11:23:30 -0600185 }
Jon Perrittfea90732016-03-15 02:57:05 -0500186 r.Err = err
Jon Perritt816d2a02014-03-11 20:49:46 -0500187}