blob: 0db691cc8a8a6c439ed796edec6f4af785016fe1 [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"
Jon Perritt816d2a02014-03-11 20:49:46 -05009)
10
Jon Perrittbef727e2014-05-12 22:41:55 -050011// ListResult is a *http.Response that is returned from a call to the List function.
Jon Perritteb575642014-04-24 15:16:31 -050012type ListResult *http.Response
Jon Perrittbef727e2014-05-12 22:41:55 -050013
14// GetResult is a *http.Response that is returned from a call to the Get function.
Jon Perritteb575642014-04-24 15:16:31 -050015type GetResult *http.Response
Jon Perritt816d2a02014-03-11 20:49:46 -050016
17// List is a function that retrieves all objects in a container. It also returns the details
18// for the account. To extract just the container information or names, pass the ListResult
Jon Perritteb575642014-04-24 15:16:31 -050019// response to the ExtractInfo or ExtractNames function, respectively.
Ash Wilson604320e2014-09-10 16:02:28 -040020func List(c *gophercloud.ServiceClient, opts ListOpts) (ListResult, error) {
Jon Perritt816d2a02014-03-11 20:49:46 -050021 contentType := ""
22
Ash Wilson604320e2014-09-10 16:02:28 -040023 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -050024
25 query := utils.BuildQuery(opts.Params)
26
27 if !opts.Full {
28 contentType = "text/plain"
29 }
30
Ash Wilsone47ea9e2014-09-10 16:03:44 -040031 url := getAccountURL(c) + query
Jon Perritt816d2a02014-03-11 20:49:46 -050032 resp, err := perigee.Request("GET", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -050033 MoreHeaders: h,
Jon Perritt816d2a02014-03-11 20:49:46 -050034 Accept: contentType,
Ash Wilsone47ea9e2014-09-10 16:03:44 -040035 OkCodes: []int{200, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -050036 })
Jon Perritteb575642014-04-24 15:16:31 -050037 return &resp.HttpResponse, err
Jon Perritt816d2a02014-03-11 20:49:46 -050038}
39
40// Create is a function that creates a new container.
Ash Wilson604320e2014-09-10 16:02:28 -040041func Create(c *gophercloud.ServiceClient, opts CreateOpts) (Container, error) {
Jon Perrittc19adea2014-04-15 16:56:01 -050042 var ci Container
Jon Perritt816d2a02014-03-11 20:49:46 -050043
Ash Wilson604320e2014-09-10 16:02:28 -040044 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -050045
46 for k, v := range opts.Headers {
47 h[k] = v
48 }
49
50 for k, v := range opts.Metadata {
51 h["X-Container-Meta-"+k] = v
52 }
53
Ash Wilsone47ea9e2014-09-10 16:03:44 -040054 url := getContainerURL(c, opts.Name)
55 _, err := perigee.Request("PUT", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -050056 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -040057 OkCodes: []int{201, 204},
Jon Perritt816d2a02014-03-11 20:49:46 -050058 })
59 if err == nil {
Jon Perrittc19adea2014-04-15 16:56:01 -050060 ci = Container{
61 "name": opts.Name,
Jon Perritt816d2a02014-03-11 20:49:46 -050062 }
63 }
64 return ci, err
65}
66
67// Delete is a function that deletes a container.
Ash Wilson604320e2014-09-10 16:02:28 -040068func Delete(c *gophercloud.ServiceClient, opts DeleteOpts) error {
69 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -050070
71 query := utils.BuildQuery(opts.Params)
72
Ash Wilson604320e2014-09-10 16:02:28 -040073 url := getContainerURL(c, opts.Name) + query
74 _, err := perigee.Request("DELETE", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -050075 MoreHeaders: h,
Ash Wilson604320e2014-09-10 16:02:28 -040076 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -050077 })
78 return err
79}
80
81// Update is a function that creates, updates, or deletes a container's metadata.
Ash Wilson604320e2014-09-10 16:02:28 -040082func Update(c *gophercloud.ServiceClient, opts UpdateOpts) error {
83 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -050084
85 for k, v := range opts.Headers {
86 h[k] = v
87 }
88
89 for k, v := range opts.Metadata {
90 h["X-Container-Meta-"+k] = v
91 }
92
Ash Wilsone47ea9e2014-09-10 16:03:44 -040093 url := getContainerURL(c, opts.Name)
94 _, err := perigee.Request("POST", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -050095 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -040096 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -050097 })
98 return err
99}
100
101// Get is a function that retrieves the metadata of a container. To extract just the custom
Jon Perritteb575642014-04-24 15:16:31 -0500102// metadata, pass the GetResult response to the ExtractMetadata function.
Ash Wilson604320e2014-09-10 16:02:28 -0400103func Get(c *gophercloud.ServiceClient, opts GetOpts) (GetResult, error) {
104 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500105
106 for k, v := range opts.Metadata {
107 h["X-Container-Meta-"+k] = v
108 }
109
Ash Wilsone47ea9e2014-09-10 16:03:44 -0400110 url := getContainerURL(c, opts.Name)
Jon Perritt816d2a02014-03-11 20:49:46 -0500111 resp, err := perigee.Request("HEAD", url, perigee.Options{
112 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -0400113 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -0500114 })
Jon Perritteb575642014-04-24 15:16:31 -0500115 return &resp.HttpResponse, err
Jon Perritt816d2a02014-03-11 20:49:46 -0500116}