blob: 558a6a8f536ce6094b81372fe88f6908a1d90d47 [file] [log] [blame]
Jon Perritt816d2a02014-03-11 20:49:46 -05001package containers
2
3import (
Jon Perritteb575642014-04-24 15:16:31 -05004 "net/http"
Ash Wilson604320e2014-09-10 16:02:28 -04005
6 "github.com/racker/perigee"
7 "github.com/rackspace/gophercloud"
8 "github.com/rackspace/gophercloud/openstack/utils"
Ash Wilson0faafcc2014-09-16 15:20:17 -04009 "github.com/rackspace/gophercloud/pagination"
Jon Perritt816d2a02014-03-11 20:49:46 -050010)
11
Jon Perrittbef727e2014-05-12 22:41:55 -050012// ListResult is a *http.Response that is returned from a call to the List function.
Ash Wilson0faafcc2014-09-16 15:20:17 -040013type ListResult struct {
14 pagination.MarkerPageBase
15}
16
17// IsEmpty returns true if a ListResult contains no container names.
18func (r ListResult) IsEmpty() (bool, error) {
19 names, err := ExtractNames(r)
20 if err != nil {
21 return true, err
22 }
23 return len(names) == 0, nil
24}
25
26// LastMarker returns the last container name in a ListResult.
27func (r ListResult) LastMarker() (string, error) {
28 names, err := ExtractNames(r)
29 if err != nil {
30 return "", err
31 }
32 if len(names) == 0 {
33 return "", nil
34 }
35 return names[len(names)-1], nil
36}
Jon Perrittbef727e2014-05-12 22:41:55 -050037
38// GetResult is a *http.Response that is returned from a call to the Get function.
Jon Perritteb575642014-04-24 15:16:31 -050039type GetResult *http.Response
Jon Perritt816d2a02014-03-11 20:49:46 -050040
41// List is a function that retrieves all objects in a container. It also returns the details
42// for the account. To extract just the container information or names, pass the ListResult
Jon Perritteb575642014-04-24 15:16:31 -050043// response to the ExtractInfo or ExtractNames function, respectively.
Ash Wilson0faafcc2014-09-16 15:20:17 -040044func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
45 var headers map[string]string
Jon Perritt816d2a02014-03-11 20:49:46 -050046
47 query := utils.BuildQuery(opts.Params)
48
49 if !opts.Full {
Ash Wilson0faafcc2014-09-16 15:20:17 -040050 headers = map[string]string{"Content-Type": "text/plain"}
51 }
52
53 createPage := func(r pagination.LastHTTPResponse) pagination.Page {
54 p := ListResult{pagination.MarkerPageBase{LastHTTPResponse: r}}
55 p.MarkerPageBase.Owner = p
56 return p
Jon Perritt816d2a02014-03-11 20:49:46 -050057 }
58
Jon Perrittfc39b942014-09-10 21:18:19 -050059 url := accountURL(c) + query
60 resp, err := perigee.Request("GET", url, perigee.Options{
61 MoreHeaders: h,
62 Accept: contentType,
63 OkCodes: []int{200, 204},
64 })
65 return &resp.HttpResponse, err
Jon Perritt816d2a02014-03-11 20:49:46 -050066}
67
68// Create is a function that creates a new container.
Ash Wilson604320e2014-09-10 16:02:28 -040069func Create(c *gophercloud.ServiceClient, opts CreateOpts) (Container, error) {
Jon Perrittc19adea2014-04-15 16:56:01 -050070 var ci Container
Jon Perritt816d2a02014-03-11 20:49:46 -050071
Ash Wilson604320e2014-09-10 16:02:28 -040072 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -050073
74 for k, v := range opts.Headers {
75 h[k] = v
76 }
77
78 for k, v := range opts.Metadata {
79 h["X-Container-Meta-"+k] = v
80 }
81
Jon Perrittfc39b942014-09-10 21:18:19 -050082 url := containerURL(c, opts.Name)
Ash Wilsone47ea9e2014-09-10 16:03:44 -040083 _, err := perigee.Request("PUT", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -050084 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -040085 OkCodes: []int{201, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -050086 })
87 if err == nil {
Jon Perrittc19adea2014-04-15 16:56:01 -050088 ci = Container{
89 "name": opts.Name,
Jon Perritt816d2a02014-03-11 20:49:46 -050090 }
91 }
92 return ci, err
93}
94
95// Delete is a function that deletes a container.
Ash Wilson604320e2014-09-10 16:02:28 -040096func Delete(c *gophercloud.ServiceClient, opts DeleteOpts) error {
97 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -050098
99 query := utils.BuildQuery(opts.Params)
100
Jon Perrittfc39b942014-09-10 21:18:19 -0500101 url := containerURL(c, opts.Name) + query
Ash Wilson604320e2014-09-10 16:02:28 -0400102 _, err := perigee.Request("DELETE", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -0500103 MoreHeaders: h,
Ash Wilson604320e2014-09-10 16:02:28 -0400104 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500105 })
106 return err
107}
108
109// Update is a function that creates, updates, or deletes a container's metadata.
Ash Wilson604320e2014-09-10 16:02:28 -0400110func Update(c *gophercloud.ServiceClient, opts UpdateOpts) error {
111 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500112
113 for k, v := range opts.Headers {
114 h[k] = v
115 }
116
117 for k, v := range opts.Metadata {
118 h["X-Container-Meta-"+k] = v
119 }
120
Jon Perrittfc39b942014-09-10 21:18:19 -0500121 url := containerURL(c, opts.Name)
Ash Wilsone47ea9e2014-09-10 16:03:44 -0400122 _, err := perigee.Request("POST", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -0500123 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -0400124 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500125 })
126 return err
127}
128
129// Get is a function that retrieves the metadata of a container. To extract just the custom
Jon Perritteb575642014-04-24 15:16:31 -0500130// metadata, pass the GetResult response to the ExtractMetadata function.
Ash Wilson604320e2014-09-10 16:02:28 -0400131func Get(c *gophercloud.ServiceClient, opts GetOpts) (GetResult, error) {
132 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500133
Jon Perrittfc39b942014-09-10 21:18:19 -0500134 url := containerURL(c, opts.Name)
Jon Perritt816d2a02014-03-11 20:49:46 -0500135 resp, err := perigee.Request("HEAD", url, perigee.Options{
136 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -0400137 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500138 })
Jon Perritteb575642014-04-24 15:16:31 -0500139 return &resp.HttpResponse, err
Jon Perritt816d2a02014-03-11 20:49:46 -0500140}