Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 1 | package containers |
| 2 | |
| 3 | import ( |
| 4 | "github.com/racker/perigee" |
Jon Perritt | b6b1d02 | 2014-04-14 21:50:45 -0500 | [diff] [blame] | 5 | storage "github.com/rackspace/gophercloud/openstack/storage/v1" |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud/openstack/utils" |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 7 | "net/http" |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 8 | ) |
| 9 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 10 | // ListResult is a *http.Response that is returned from a call to the List function. |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 11 | type ListResult *http.Response |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 12 | |
| 13 | // GetResult is a *http.Response that is returned from a call to the Get function. |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 14 | type GetResult *http.Response |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 15 | |
| 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 Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 18 | // response to the ExtractInfo or ExtractNames function, respectively. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 19 | func 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 35 | MoreHeaders: h, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 36 | Accept: contentType, |
| 37 | }) |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 38 | return &resp.HttpResponse, err |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | // Create is a function that creates a new container. |
Jon Perritt | c19adea | 2014-04-15 16:56:01 -0500 | [diff] [blame] | 42 | func Create(c *storage.Client, opts CreateOpts) (Container, error) { |
| 43 | var ci Container |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 44 | |
| 45 | h, err := c.GetHeaders() |
| 46 | if err != nil { |
Jon Perritt | c19adea | 2014-04-15 16:56:01 -0500 | [diff] [blame] | 47 | return Container{}, err |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 48 | } |
| 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 61 | }) |
| 62 | if err == nil { |
Jon Perritt | c19adea | 2014-04-15 16:56:01 -0500 | [diff] [blame] | 63 | ci = Container{ |
| 64 | "name": opts.Name, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | return ci, err |
| 68 | } |
| 69 | |
| 70 | // Delete is a function that deletes a container. |
| 71 | func 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 82 | }) |
| 83 | return err |
| 84 | } |
| 85 | |
| 86 | // Update is a function that creates, updates, or deletes a container's metadata. |
| 87 | func 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 104 | }) |
| 105 | return err |
| 106 | } |
| 107 | |
| 108 | // Get is a function that retrieves the metadata of a container. To extract just the custom |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 109 | // metadata, pass the GetResult response to the ExtractMetadata function. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 110 | func 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 123 | }) |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 124 | return &resp.HttpResponse, err |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 125 | } |