blob: 2aa263ad27aace8f6d3af48e72b11c0b4a8015c8 [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
Ash Wilsone47ea9e2014-09-10 16:03:44 -040059 url := getAccountURL(c) + query
Ash Wilson0faafcc2014-09-16 15:20:17 -040060 pager := pagination.NewPager(c, url, createPage)
61 pager.Headers = headers
62 return pager
Jon Perritt816d2a02014-03-11 20:49:46 -050063}
64
65// Create is a function that creates a new container.
Ash Wilson604320e2014-09-10 16:02:28 -040066func Create(c *gophercloud.ServiceClient, opts CreateOpts) (Container, error) {
Jon Perrittc19adea2014-04-15 16:56:01 -050067 var ci Container
Jon Perritt816d2a02014-03-11 20:49:46 -050068
Ash Wilson604320e2014-09-10 16:02:28 -040069 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -050070
71 for k, v := range opts.Headers {
72 h[k] = v
73 }
74
75 for k, v := range opts.Metadata {
76 h["X-Container-Meta-"+k] = v
77 }
78
Ash Wilsone47ea9e2014-09-10 16:03:44 -040079 url := getContainerURL(c, opts.Name)
80 _, err := perigee.Request("PUT", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -050081 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -040082 OkCodes: []int{201, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -050083 })
84 if err == nil {
Jon Perrittc19adea2014-04-15 16:56:01 -050085 ci = Container{
86 "name": opts.Name,
Jon Perritt816d2a02014-03-11 20:49:46 -050087 }
88 }
89 return ci, err
90}
91
92// Delete is a function that deletes a container.
Ash Wilson604320e2014-09-10 16:02:28 -040093func Delete(c *gophercloud.ServiceClient, opts DeleteOpts) error {
94 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -050095
96 query := utils.BuildQuery(opts.Params)
97
Ash Wilson604320e2014-09-10 16:02:28 -040098 url := getContainerURL(c, opts.Name) + query
99 _, err := perigee.Request("DELETE", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -0500100 MoreHeaders: h,
Ash Wilson604320e2014-09-10 16:02:28 -0400101 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500102 })
103 return err
104}
105
106// Update is a function that creates, updates, or deletes a container's metadata.
Ash Wilson604320e2014-09-10 16:02:28 -0400107func Update(c *gophercloud.ServiceClient, opts UpdateOpts) error {
108 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500109
110 for k, v := range opts.Headers {
111 h[k] = v
112 }
113
114 for k, v := range opts.Metadata {
115 h["X-Container-Meta-"+k] = v
116 }
117
Ash Wilsone47ea9e2014-09-10 16:03:44 -0400118 url := getContainerURL(c, opts.Name)
119 _, err := perigee.Request("POST", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -0500120 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -0400121 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500122 })
123 return err
124}
125
126// Get is a function that retrieves the metadata of a container. To extract just the custom
Jon Perritteb575642014-04-24 15:16:31 -0500127// metadata, pass the GetResult response to the ExtractMetadata function.
Ash Wilson604320e2014-09-10 16:02:28 -0400128func Get(c *gophercloud.ServiceClient, opts GetOpts) (GetResult, error) {
129 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500130
131 for k, v := range opts.Metadata {
132 h["X-Container-Meta-"+k] = v
133 }
134
Ash Wilsone47ea9e2014-09-10 16:03:44 -0400135 url := getContainerURL(c, opts.Name)
Jon Perritt816d2a02014-03-11 20:49:46 -0500136 resp, err := perigee.Request("HEAD", url, perigee.Options{
137 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -0400138 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500139 })
Jon Perritteb575642014-04-24 15:16:31 -0500140 return &resp.HttpResponse, err
Jon Perritt816d2a02014-03-11 20:49:46 -0500141}