blob: b6d3a8940f8bfc28f554de9e9d3d54c2af6cee9a [file] [log] [blame]
Jon Perritt816d2a02014-03-11 20:49:46 -05001package containers
2
3import (
4 "github.com/racker/perigee"
Jon Perrittb6b1d022014-04-14 21:50:45 -05005 storage "github.com/rackspace/gophercloud/openstack/storage/v1"
Jon Perritt816d2a02014-03-11 20:49:46 -05006 "github.com/rackspace/gophercloud/openstack/utils"
Jon Perritteb575642014-04-24 15:16:31 -05007 "net/http"
Jon Perritt816d2a02014-03-11 20:49:46 -05008)
9
Jon Perrittbef727e2014-05-12 22:41:55 -050010// ListResult is a *http.Response that is returned from a call to the List function.
Jon Perritteb575642014-04-24 15:16:31 -050011type ListResult *http.Response
Jon Perrittbef727e2014-05-12 22:41:55 -050012
13// GetResult is a *http.Response that is returned from a call to the Get function.
Jon Perritteb575642014-04-24 15:16:31 -050014type GetResult *http.Response
Jon Perritt816d2a02014-03-11 20:49:46 -050015
16// List is a function that retrieves all objects in a container. It also returns the details
17// for the account. To extract just the container information or names, pass the ListResult
Jon Perritteb575642014-04-24 15:16:31 -050018// response to the ExtractInfo or ExtractNames function, respectively.
Jon Perritt816d2a02014-03-11 20:49:46 -050019func List(c *storage.Client, opts ListOpts) (ListResult, error) {
20 contentType := ""
21
22 h, err := c.GetHeaders()
23 if err != nil {
24 return nil, err
25 }
26
27 query := utils.BuildQuery(opts.Params)
28
29 if !opts.Full {
30 contentType = "text/plain"
31 }
32
33 url := c.GetAccountURL() + query
34 resp, err := perigee.Request("GET", url, perigee.Options{
Jon Perritt816d2a02014-03-11 20:49:46 -050035 MoreHeaders: h,
Jon Perritt816d2a02014-03-11 20:49:46 -050036 Accept: contentType,
37 })
Jon Perritteb575642014-04-24 15:16:31 -050038 return &resp.HttpResponse, err
Jon Perritt816d2a02014-03-11 20:49:46 -050039}
40
41// Create is a function that creates a new container.
Jon Perrittc19adea2014-04-15 16:56:01 -050042func Create(c *storage.Client, opts CreateOpts) (Container, error) {
43 var ci Container
Jon Perritt816d2a02014-03-11 20:49:46 -050044
45 h, err := c.GetHeaders()
46 if err != nil {
Jon Perrittc19adea2014-04-15 16:56:01 -050047 return Container{}, err
Jon Perritt816d2a02014-03-11 20:49:46 -050048 }
49
50 for k, v := range opts.Headers {
51 h[k] = v
52 }
53
54 for k, v := range opts.Metadata {
55 h["X-Container-Meta-"+k] = v
56 }
57
58 url := c.GetContainerURL(opts.Name)
59 _, err = perigee.Request("PUT", url, perigee.Options{
60 MoreHeaders: h,
Jon Perritt816d2a02014-03-11 20:49:46 -050061 })
62 if err == nil {
Jon Perrittc19adea2014-04-15 16:56:01 -050063 ci = Container{
64 "name": opts.Name,
Jon Perritt816d2a02014-03-11 20:49:46 -050065 }
66 }
67 return ci, err
68}
69
70// Delete is a function that deletes a container.
71func Delete(c *storage.Client, opts DeleteOpts) error {
72 h, err := c.GetHeaders()
73 if err != nil {
74 return err
75 }
76
77 query := utils.BuildQuery(opts.Params)
78
79 url := c.GetContainerURL(opts.Name) + query
80 _, err = perigee.Request("DELETE", url, perigee.Options{
81 MoreHeaders: h,
Jon Perritt816d2a02014-03-11 20:49:46 -050082 })
83 return err
84}
85
86// Update is a function that creates, updates, or deletes a container's metadata.
87func Update(c *storage.Client, opts UpdateOpts) error {
88 h, err := c.GetHeaders()
89 if err != nil {
90 return err
91 }
92
93 for k, v := range opts.Headers {
94 h[k] = v
95 }
96
97 for k, v := range opts.Metadata {
98 h["X-Container-Meta-"+k] = v
99 }
100
101 url := c.GetContainerURL(opts.Name)
102 _, err = perigee.Request("POST", url, perigee.Options{
103 MoreHeaders: h,
Jon Perritt816d2a02014-03-11 20:49:46 -0500104 })
105 return err
106}
107
108// Get is a function that retrieves the metadata of a container. To extract just the custom
Jon Perritteb575642014-04-24 15:16:31 -0500109// metadata, pass the GetResult response to the ExtractMetadata function.
Jon Perritt816d2a02014-03-11 20:49:46 -0500110func Get(c *storage.Client, opts GetOpts) (GetResult, error) {
111 h, err := c.GetHeaders()
112 if err != nil {
113 return nil, err
114 }
115
116 for k, v := range opts.Metadata {
117 h["X-Container-Meta-"+k] = v
118 }
119
120 url := c.GetContainerURL(opts.Name)
121 resp, err := perigee.Request("HEAD", url, perigee.Options{
122 MoreHeaders: h,
Jon Perritt816d2a02014-03-11 20:49:46 -0500123 })
Jon Perritteb575642014-04-24 15:16:31 -0500124 return &resp.HttpResponse, err
Jon Perritt816d2a02014-03-11 20:49:46 -0500125}