blob: 6cdd7c7af779eff193bc1dad6f9b087aad82a9d6 [file] [log] [blame]
Jon Perritt816d2a02014-03-11 20:49:46 -05001package containers
2
3import (
4 "github.com/racker/perigee"
5 "github.com/rackspace/gophercloud/openstack/storage"
6 "github.com/rackspace/gophercloud/openstack/utils"
7)
8
9type ListResult *perigee.Response
10type GetResult *perigee.Response
11
12// List is a function that retrieves all objects in a container. It also returns the details
13// for the account. To extract just the container information or names, pass the ListResult
14// response to the GetInfo or GetNames function, respectively.
15func List(c *storage.Client, opts ListOpts) (ListResult, error) {
16 contentType := ""
17
18 h, err := c.GetHeaders()
19 if err != nil {
20 return nil, err
21 }
22
23 query := utils.BuildQuery(opts.Params)
24
25 if !opts.Full {
26 contentType = "text/plain"
27 }
28
29 url := c.GetAccountURL() + query
30 resp, err := perigee.Request("GET", url, perigee.Options{
31 Results: true,
32 MoreHeaders: h,
33 OkCodes: []int{200, 204},
34 Accept: contentType,
35 })
36 return resp, err
37}
38
39// Create is a function that creates a new container.
40func Create(c *storage.Client, opts CreateOpts) (*Container, error) {
41 var ci *Container
42
43 h, err := c.GetHeaders()
44 if err != nil {
45 return new(Container), err
46 }
47
48 for k, v := range opts.Headers {
49 h[k] = v
50 }
51
52 for k, v := range opts.Metadata {
53 h["X-Container-Meta-"+k] = v
54 }
55
56 url := c.GetContainerURL(opts.Name)
57 _, err = perigee.Request("PUT", url, perigee.Options{
58 MoreHeaders: h,
59 OkCodes: []int{201, 204},
60 })
61 if err == nil {
62 ci = &Container{
63 Name: opts.Name,
64 }
65 }
66 return ci, err
67}
68
69// Delete is a function that deletes a container.
70func Delete(c *storage.Client, opts DeleteOpts) error {
71 h, err := c.GetHeaders()
72 if err != nil {
73 return err
74 }
75
76 query := utils.BuildQuery(opts.Params)
77
78 url := c.GetContainerURL(opts.Name) + query
79 _, err = perigee.Request("DELETE", url, perigee.Options{
80 MoreHeaders: h,
81 OkCodes: []int{204},
82 })
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,
104 OkCodes: []int{204},
105 })
106 return err
107}
108
109// Get is a function that retrieves the metadata of a container. To extract just the custom
110// metadata, pass the GetResult response to the GetMetadata function.
111func Get(c *storage.Client, opts GetOpts) (GetResult, error) {
112 h, err := c.GetHeaders()
113 if err != nil {
114 return nil, err
115 }
116
117 for k, v := range opts.Metadata {
118 h["X-Container-Meta-"+k] = v
119 }
120
121 url := c.GetContainerURL(opts.Name)
122 resp, err := perigee.Request("HEAD", url, perigee.Options{
123 MoreHeaders: h,
124 OkCodes: []int{204},
125 })
126 return resp, err
127}