blob: d0d37817f4f6feec95c20edcb7abc004176178b9 [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 Perritt8c93a302014-09-28 22:35:57 -05009// ListOpts is a structure that holds options for listing containers.
10type ListOpts struct {
11 Full bool
Jon Perritt5db08922014-09-30 21:32:48 -050012 Limit int `q:"limit"`
13 Marker string `q:"marker"`
14 EndMarker string `q:"end_marker"`
15 Format string `q:"format"`
16 Prefix string `q:"prefix"`
17 Delimiter [1]byte `q:"delimiter"`
Ash Wilson0faafcc2014-09-16 15:20:17 -040018}
19
Jon Perritt5db08922014-09-30 21:32:48 -050020// List is a function that retrieves containers associated with the account as well as account
21// metadata. It returns a pager which can be iterated with the EachPage function.
Jon Perrittde47eac2014-09-30 15:34:17 -050022func List(c *gophercloud.ServiceClient, opts *ListOpts) pagination.Pager {
Ash Wilson0faafcc2014-09-16 15:20:17 -040023 var headers map[string]string
Jon Perritt816d2a02014-03-11 20:49:46 -050024
Jon Perrittb3461402014-10-09 21:36:17 -050025 url := listURL(c)
Jon Perrittde47eac2014-09-30 15:34:17 -050026 if opts != nil {
27 query, err := gophercloud.BuildQueryString(opts)
28 if err != nil {
29 return pagination.Pager{Err: err}
30 }
31 url += query.String()
32
33 if !opts.Full {
34 headers = map[string]string{"Accept": "text/plain", "Content-Type": "text/plain"}
35 }
Jon Perritt5db08922014-09-30 21:32:48 -050036 } else {
37 headers = map[string]string{"Accept": "text/plain", "Content-Type": "text/plain"}
Ash Wilson0faafcc2014-09-16 15:20:17 -040038 }
39
40 createPage := func(r pagination.LastHTTPResponse) pagination.Page {
Jon Perritt8c93a302014-09-28 22:35:57 -050041 p := ContainerPage{pagination.MarkerPageBase{LastHTTPResponse: r}}
Ash Wilson0faafcc2014-09-16 15:20:17 -040042 p.MarkerPageBase.Owner = p
43 return p
Jon Perritt816d2a02014-03-11 20:49:46 -050044 }
45
Jon Perritt584c94f2014-09-24 18:00:16 -050046 pager := pagination.NewPager(c, url, createPage)
47 pager.Headers = headers
48 return pager
Jon Perritt816d2a02014-03-11 20:49:46 -050049}
50
Jon Perritt8c93a302014-09-28 22:35:57 -050051// CreateOpts is a structure that holds parameters for creating a container.
52type CreateOpts struct {
53 Metadata map[string]string
54 ContainerRead string `h:"X-Container-Read"`
55 ContainerSyncTo string `h:"X-Container-Sync-To"`
56 ContainerSyncKey string `h:"X-Container-Sync-Key"`
57 ContainerWrite string `h:"X-Container-Write"`
58 ContentType string `h:"Content-Type"`
59 DetectContentType bool `h:"X-Detect-Content-Type"`
60 IfNoneMatch string `h:"If-None-Match"`
61 VersionsLocation string `h:"X-Versions-Location"`
62}
Jon Perritt816d2a02014-03-11 20:49:46 -050063
Jon Perritt8c93a302014-09-28 22:35:57 -050064// Create is a function that creates a new container.
Jon Perritt5db08922014-09-30 21:32:48 -050065func Create(c *gophercloud.ServiceClient, containerName string, opts *CreateOpts) CreateResult {
66 var res CreateResult
Ash Wilson604320e2014-09-10 16:02:28 -040067 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -050068
Jon Perrittde47eac2014-09-30 15:34:17 -050069 if opts != nil {
70 headers, err := gophercloud.BuildHeaders(opts)
71 if err != nil {
Jon Perritt5db08922014-09-30 21:32:48 -050072 res.Err = err
73 return res
Jon Perrittde47eac2014-09-30 15:34:17 -050074 }
75
76 for k, v := range headers {
77 h[k] = v
78 }
79
80 for k, v := range opts.Metadata {
81 h["X-Container-Meta-"+k] = v
82 }
Jon Perritt8c93a302014-09-28 22:35:57 -050083 }
84
Jon Perrittb3461402014-10-09 21:36:17 -050085 resp, err := perigee.Request("PUT", createURL(c, containerName), perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -050086 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -040087 OkCodes: []int{201, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -050088 })
Jon Perritt5db08922014-09-30 21:32:48 -050089 res.Resp = &resp.HttpResponse
90 res.Err = err
91 return res
Jon Perritt816d2a02014-03-11 20:49:46 -050092}
93
94// Delete is a function that deletes a container.
Jon Perritt5db08922014-09-30 21:32:48 -050095func Delete(c *gophercloud.ServiceClient, containerName string) DeleteResult {
96 var res DeleteResult
Jon Perrittb3461402014-10-09 21:36:17 -050097 resp, err := perigee.Request("DELETE", deleteURL(c, containerName), perigee.Options{
Jon Perritt8c93a302014-09-28 22:35:57 -050098 MoreHeaders: c.Provider.AuthenticatedHeaders(),
Ash Wilson604320e2014-09-10 16:02:28 -040099 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500100 })
Jon Perritt5db08922014-09-30 21:32:48 -0500101 res.Resp = &resp.HttpResponse
102 res.Err = err
103 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500104}
105
Jon Perritt8c93a302014-09-28 22:35:57 -0500106// UpdateOpts is a structure that holds parameters for updating, creating, or deleting a
107// container's metadata.
108type UpdateOpts struct {
109 Metadata map[string]string
110 ContainerRead string `h:"X-Container-Read"`
111 ContainerSyncTo string `h:"X-Container-Sync-To"`
112 ContainerSyncKey string `h:"X-Container-Sync-Key"`
113 ContainerWrite string `h:"X-Container-Write"`
114 ContentType string `h:"Content-Type"`
115 DetectContentType bool `h:"X-Detect-Content-Type"`
116 RemoveVersionsLocation string `h:"X-Remove-Versions-Location"`
117 VersionsLocation string `h:"X-Versions-Location"`
118}
119
Jon Perritt816d2a02014-03-11 20:49:46 -0500120// Update is a function that creates, updates, or deletes a container's metadata.
Jon Perritt5db08922014-09-30 21:32:48 -0500121func Update(c *gophercloud.ServiceClient, containerName string, opts *UpdateOpts) UpdateResult {
122 var res UpdateResult
Ash Wilson604320e2014-09-10 16:02:28 -0400123 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500124
Jon Perrittde47eac2014-09-30 15:34:17 -0500125 if opts != nil {
126 headers, err := gophercloud.BuildHeaders(opts)
127 if err != nil {
Jon Perritt5db08922014-09-30 21:32:48 -0500128 res.Err = err
129 return res
Jon Perrittde47eac2014-09-30 15:34:17 -0500130 }
131
132 for k, v := range headers {
133 h[k] = v
134 }
135
136 for k, v := range opts.Metadata {
137 h["X-Container-Meta-"+k] = v
138 }
Jon Perritt8c93a302014-09-28 22:35:57 -0500139 }
140
Jon Perrittb3461402014-10-09 21:36:17 -0500141 resp, err := perigee.Request("POST", updateURL(c, containerName), perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -0500142 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -0400143 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500144 })
Jon Perritt5db08922014-09-30 21:32:48 -0500145 res.Resp = &resp.HttpResponse
146 res.Err = err
147 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500148}
149
150// Get is a function that retrieves the metadata of a container. To extract just the custom
Jon Perritteb575642014-04-24 15:16:31 -0500151// metadata, pass the GetResult response to the ExtractMetadata function.
Jon Perritt8c93a302014-09-28 22:35:57 -0500152func Get(c *gophercloud.ServiceClient, containerName string) GetResult {
Jon Perritt5db08922014-09-30 21:32:48 -0500153 var res GetResult
Jon Perrittb3461402014-10-09 21:36:17 -0500154 resp, err := perigee.Request("HEAD", getURL(c, containerName), perigee.Options{
Jon Perritt8c93a302014-09-28 22:35:57 -0500155 MoreHeaders: c.Provider.AuthenticatedHeaders(),
Ash Wilsone47ea9e2014-09-10 16:03:44 -0400156 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500157 })
Jon Perritt5db08922014-09-30 21:32:48 -0500158 res.Resp = &resp.HttpResponse
159 res.Err = err
160 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500161}