blob: d2cec0b34410885820afd109c03cc8c9c6d0eb8d [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
31 url := c.GetAccountURL() + query
32 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,
35 })
Jon Perritteb575642014-04-24 15:16:31 -050036 return &resp.HttpResponse, err
Jon Perritt816d2a02014-03-11 20:49:46 -050037}
38
39// Create is a function that creates a new container.
Ash Wilson604320e2014-09-10 16:02:28 -040040func Create(c *gophercloud.ServiceClient, opts CreateOpts) (Container, error) {
Jon Perrittc19adea2014-04-15 16:56:01 -050041 var ci Container
Jon Perritt816d2a02014-03-11 20:49:46 -050042
Ash Wilson604320e2014-09-10 16:02:28 -040043 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -050044
45 for k, v := range opts.Headers {
46 h[k] = v
47 }
48
49 for k, v := range opts.Metadata {
50 h["X-Container-Meta-"+k] = v
51 }
52
53 url := c.GetContainerURL(opts.Name)
54 _, err = perigee.Request("PUT", url, perigee.Options{
55 MoreHeaders: h,
Jon Perritt816d2a02014-03-11 20:49:46 -050056 })
57 if err == nil {
Jon Perrittc19adea2014-04-15 16:56:01 -050058 ci = Container{
59 "name": opts.Name,
Jon Perritt816d2a02014-03-11 20:49:46 -050060 }
61 }
62 return ci, err
63}
64
65// Delete is a function that deletes a container.
Ash Wilson604320e2014-09-10 16:02:28 -040066func Delete(c *gophercloud.ServiceClient, opts DeleteOpts) error {
67 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -050068
69 query := utils.BuildQuery(opts.Params)
70
Ash Wilson604320e2014-09-10 16:02:28 -040071 url := getContainerURL(c, opts.Name) + query
72 _, err := perigee.Request("DELETE", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -050073 MoreHeaders: h,
Ash Wilson604320e2014-09-10 16:02:28 -040074 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -050075 })
76 return err
77}
78
79// Update is a function that creates, updates, or deletes a container's metadata.
Ash Wilson604320e2014-09-10 16:02:28 -040080func Update(c *gophercloud.ServiceClient, opts UpdateOpts) error {
81 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -050082
83 for k, v := range opts.Headers {
84 h[k] = v
85 }
86
87 for k, v := range opts.Metadata {
88 h["X-Container-Meta-"+k] = v
89 }
90
91 url := c.GetContainerURL(opts.Name)
92 _, err = perigee.Request("POST", url, perigee.Options{
93 MoreHeaders: h,
Jon Perritt816d2a02014-03-11 20:49:46 -050094 })
95 return err
96}
97
98// Get is a function that retrieves the metadata of a container. To extract just the custom
Jon Perritteb575642014-04-24 15:16:31 -050099// metadata, pass the GetResult response to the ExtractMetadata function.
Ash Wilson604320e2014-09-10 16:02:28 -0400100func Get(c *gophercloud.ServiceClient, opts GetOpts) (GetResult, error) {
101 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -0500102
103 for k, v := range opts.Metadata {
104 h["X-Container-Meta-"+k] = v
105 }
106
107 url := c.GetContainerURL(opts.Name)
108 resp, err := perigee.Request("HEAD", url, perigee.Options{
109 MoreHeaders: h,
Jon Perritt816d2a02014-03-11 20:49:46 -0500110 })
Jon Perritteb575642014-04-24 15:16:31 -0500111 return &resp.HttpResponse, err
Jon Perritt816d2a02014-03-11 20:49:46 -0500112}