blob: a66867394c713142a567bc2da3e17789f58fbb1c [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
jrperritt29ae6b32016-04-13 12:59:37 -0500112 return
Jon Perritt816d2a02014-03-11 20:49:46 -0500113}
114
115// Delete is a function that deletes a container.
Jon Perritt3860b512016-03-29 12:01:48 -0500116func Delete(c *gophercloud.ServiceClient, containerName string) (r DeleteResult) {
Jon Perrittfea90732016-03-15 02:57:05 -0500117 _, r.Err = c.Delete(deleteURL(c, containerName), nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500118 return
Jon Perritt816d2a02014-03-11 20:49:46 -0500119}
120
Jon Perritte90aced2014-10-12 23:24:06 -0500121// UpdateOptsBuilder allows extensions to add additional parameters to the
122// Update request.
123type UpdateOptsBuilder interface {
Jon Perritt04851d32014-10-14 02:07:13 -0500124 ToContainerUpdateMap() (map[string]string, error)
Jon Perritte90aced2014-10-12 23:24:06 -0500125}
126
127// UpdateOpts is a structure that holds parameters for updating, creating, or
128// deleting a container's metadata.
Jon Perritt8c93a302014-09-28 22:35:57 -0500129type UpdateOpts struct {
130 Metadata map[string]string
131 ContainerRead string `h:"X-Container-Read"`
132 ContainerSyncTo string `h:"X-Container-Sync-To"`
133 ContainerSyncKey string `h:"X-Container-Sync-Key"`
134 ContainerWrite string `h:"X-Container-Write"`
135 ContentType string `h:"Content-Type"`
136 DetectContentType bool `h:"X-Detect-Content-Type"`
137 RemoveVersionsLocation string `h:"X-Remove-Versions-Location"`
138 VersionsLocation string `h:"X-Versions-Location"`
139}
140
Jon Perritt04851d32014-10-14 02:07:13 -0500141// ToContainerUpdateMap formats a CreateOpts into a map of headers.
142func (opts UpdateOpts) ToContainerUpdateMap() (map[string]string, error) {
Jon Perritte90aced2014-10-12 23:24:06 -0500143 h, err := gophercloud.BuildHeaders(opts)
144 if err != nil {
145 return nil, err
146 }
147 for k, v := range opts.Metadata {
148 h["X-Container-Meta-"+k] = v
149 }
150 return h, nil
151}
152
153// Update is a function that creates, updates, or deletes a container's
154// metadata.
Jon Perritt3860b512016-03-29 12:01:48 -0500155func Update(c *gophercloud.ServiceClient, containerName string, opts UpdateOptsBuilder) (r UpdateResult) {
Jon Perrittfea90732016-03-15 02:57:05 -0500156 h := make(map[string]string)
Jon Perrittde47eac2014-09-30 15:34:17 -0500157 if opts != nil {
Jon Perritt04851d32014-10-14 02:07:13 -0500158 headers, err := opts.ToContainerUpdateMap()
Jon Perrittde47eac2014-09-30 15:34:17 -0500159 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500160 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500161 return
Jon Perrittde47eac2014-09-30 15:34:17 -0500162 }
163
164 for k, v := range headers {
165 h[k] = v
166 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500167 }
Jon Perritta33da232016-03-02 04:43:08 -0600168 resp, err := c.Request("POST", updateURL(c, containerName), &gophercloud.RequestOpts{
Jon Perritt816d2a02014-03-11 20:49:46 -0500169 MoreHeaders: h,
Jamie Hannafordc530ba12015-03-23 17:50:46 +0100170 OkCodes: []int{201, 202, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500171 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600172 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500173 r.Header = resp.Header
Jon Perritta2c88b22015-05-18 11:23:30 -0600174 }
Jon Perrittfea90732016-03-15 02:57:05 -0500175 r.Err = err
jrperritt29ae6b32016-04-13 12:59:37 -0500176 return
Jon Perritt816d2a02014-03-11 20:49:46 -0500177}
178
Jon Perritte90aced2014-10-12 23:24:06 -0500179// Get is a function that retrieves the metadata of a container. To extract just
180// the custom metadata, pass the GetResult response to the ExtractMetadata
181// function.
Jon Perritt3860b512016-03-29 12:01:48 -0500182func Get(c *gophercloud.ServiceClient, containerName string) (r GetResult) {
Jon Perritta33da232016-03-02 04:43:08 -0600183 resp, err := c.Request("HEAD", getURL(c, containerName), &gophercloud.RequestOpts{
Ash Wilson59fb6c42015-02-12 16:21:13 -0500184 OkCodes: []int{200, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500185 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600186 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -0500187 r.Header = resp.Header
Jon Perritta2c88b22015-05-18 11:23:30 -0600188 }
Jon Perrittfea90732016-03-15 02:57:05 -0500189 r.Err = err
jrperritt29ae6b32016-04-13 12:59:37 -0500190 return
Jon Perritt816d2a02014-03-11 20:49:46 -0500191}