Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 1 | package containers |
| 2 | |
| 3 | import ( |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 4 | "net/http" |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame^] | 5 | |
| 6 | "github.com/racker/perigee" |
| 7 | "github.com/rackspace/gophercloud" |
| 8 | "github.com/rackspace/gophercloud/openstack/utils" |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 9 | ) |
| 10 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 11 | // 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] | 12 | type ListResult *http.Response |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 13 | |
| 14 | // 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] | 15 | type GetResult *http.Response |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 16 | |
| 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 Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 19 | // response to the ExtractInfo or ExtractNames function, respectively. |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame^] | 20 | func List(c *gophercloud.ServiceClient, opts ListOpts) (ListResult, error) { |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 21 | contentType := "" |
| 22 | |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame^] | 23 | h := c.Provider.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 24 | |
| 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 33 | MoreHeaders: h, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 34 | Accept: contentType, |
| 35 | }) |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 36 | return &resp.HttpResponse, err |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | // Create is a function that creates a new container. |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame^] | 40 | func Create(c *gophercloud.ServiceClient, opts CreateOpts) (Container, error) { |
Jon Perritt | c19adea | 2014-04-15 16:56:01 -0500 | [diff] [blame] | 41 | var ci Container |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 42 | |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame^] | 43 | h := c.Provider.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 44 | |
| 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 56 | }) |
| 57 | if err == nil { |
Jon Perritt | c19adea | 2014-04-15 16:56:01 -0500 | [diff] [blame] | 58 | ci = Container{ |
| 59 | "name": opts.Name, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | return ci, err |
| 63 | } |
| 64 | |
| 65 | // Delete is a function that deletes a container. |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame^] | 66 | func Delete(c *gophercloud.ServiceClient, opts DeleteOpts) error { |
| 67 | h := c.Provider.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 68 | |
| 69 | query := utils.BuildQuery(opts.Params) |
| 70 | |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame^] | 71 | url := getContainerURL(c, opts.Name) + query |
| 72 | _, err := perigee.Request("DELETE", url, perigee.Options{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 73 | MoreHeaders: h, |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame^] | 74 | OkCodes: []int{204}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 75 | }) |
| 76 | return err |
| 77 | } |
| 78 | |
| 79 | // Update is a function that creates, updates, or deletes a container's metadata. |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame^] | 80 | func Update(c *gophercloud.ServiceClient, opts UpdateOpts) error { |
| 81 | h := c.Provider.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 82 | |
| 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 94 | }) |
| 95 | return err |
| 96 | } |
| 97 | |
| 98 | // 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] | 99 | // metadata, pass the GetResult response to the ExtractMetadata function. |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame^] | 100 | func Get(c *gophercloud.ServiceClient, opts GetOpts) (GetResult, error) { |
| 101 | h := c.Provider.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 102 | |
| 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 Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 110 | }) |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 111 | return &resp.HttpResponse, err |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 112 | } |