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 | |
Ash Wilson | e47ea9e | 2014-09-10 16:03:44 -0400 | [diff] [blame] | 31 | url := getAccountURL(c) + query |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 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, |
Ash Wilson | e47ea9e | 2014-09-10 16:03:44 -0400 | [diff] [blame] | 35 | OkCodes: []int{200, 204}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 36 | }) |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 37 | return &resp.HttpResponse, err |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | // Create is a function that creates a new container. |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 41 | func Create(c *gophercloud.ServiceClient, opts CreateOpts) (Container, error) { |
Jon Perritt | c19adea | 2014-04-15 16:56:01 -0500 | [diff] [blame] | 42 | var ci Container |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 43 | |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 44 | h := c.Provider.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 45 | |
| 46 | for k, v := range opts.Headers { |
| 47 | h[k] = v |
| 48 | } |
| 49 | |
| 50 | for k, v := range opts.Metadata { |
| 51 | h["X-Container-Meta-"+k] = v |
| 52 | } |
| 53 | |
Ash Wilson | e47ea9e | 2014-09-10 16:03:44 -0400 | [diff] [blame] | 54 | url := getContainerURL(c, opts.Name) |
| 55 | _, err := perigee.Request("PUT", url, perigee.Options{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 56 | MoreHeaders: h, |
Ash Wilson | e47ea9e | 2014-09-10 16:03:44 -0400 | [diff] [blame] | 57 | OkCodes: []int{201, 204}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 58 | }) |
| 59 | if err == nil { |
Jon Perritt | c19adea | 2014-04-15 16:56:01 -0500 | [diff] [blame] | 60 | ci = Container{ |
| 61 | "name": opts.Name, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | return ci, err |
| 65 | } |
| 66 | |
| 67 | // Delete is a function that deletes a container. |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 68 | func Delete(c *gophercloud.ServiceClient, opts DeleteOpts) error { |
| 69 | h := c.Provider.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 70 | |
| 71 | query := utils.BuildQuery(opts.Params) |
| 72 | |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 73 | url := getContainerURL(c, opts.Name) + query |
| 74 | _, err := perigee.Request("DELETE", url, perigee.Options{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 75 | MoreHeaders: h, |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 76 | OkCodes: []int{204}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 77 | }) |
| 78 | return err |
| 79 | } |
| 80 | |
| 81 | // 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] | 82 | func Update(c *gophercloud.ServiceClient, opts UpdateOpts) error { |
| 83 | h := c.Provider.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 84 | |
| 85 | for k, v := range opts.Headers { |
| 86 | h[k] = v |
| 87 | } |
| 88 | |
| 89 | for k, v := range opts.Metadata { |
| 90 | h["X-Container-Meta-"+k] = v |
| 91 | } |
| 92 | |
Ash Wilson | e47ea9e | 2014-09-10 16:03:44 -0400 | [diff] [blame] | 93 | url := getContainerURL(c, opts.Name) |
| 94 | _, err := perigee.Request("POST", url, perigee.Options{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 95 | MoreHeaders: h, |
Ash Wilson | e47ea9e | 2014-09-10 16:03:44 -0400 | [diff] [blame] | 96 | OkCodes: []int{204}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 97 | }) |
| 98 | return err |
| 99 | } |
| 100 | |
| 101 | // 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] | 102 | // metadata, pass the GetResult response to the ExtractMetadata function. |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 103 | func Get(c *gophercloud.ServiceClient, opts GetOpts) (GetResult, error) { |
| 104 | h := c.Provider.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 105 | |
| 106 | for k, v := range opts.Metadata { |
| 107 | h["X-Container-Meta-"+k] = v |
| 108 | } |
| 109 | |
Ash Wilson | e47ea9e | 2014-09-10 16:03:44 -0400 | [diff] [blame] | 110 | url := getContainerURL(c, opts.Name) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 111 | resp, err := perigee.Request("HEAD", url, perigee.Options{ |
| 112 | MoreHeaders: h, |
Ash Wilson | e47ea9e | 2014-09-10 16:03:44 -0400 | [diff] [blame] | 113 | OkCodes: []int{204}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 114 | }) |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 115 | return &resp.HttpResponse, err |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 116 | } |