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" |
Ash Wilson | 0faafcc | 2014-09-16 15:20:17 -0400 | [diff] [blame] | 9 | "github.com/rackspace/gophercloud/pagination" |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 10 | ) |
| 11 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 12 | // ListResult is a *http.Response that is returned from a call to the List function. |
Ash Wilson | 0faafcc | 2014-09-16 15:20:17 -0400 | [diff] [blame] | 13 | type ListResult struct { |
| 14 | pagination.MarkerPageBase |
| 15 | } |
| 16 | |
| 17 | // IsEmpty returns true if a ListResult contains no container names. |
| 18 | func (r ListResult) IsEmpty() (bool, error) { |
| 19 | names, err := ExtractNames(r) |
| 20 | if err != nil { |
| 21 | return true, err |
| 22 | } |
| 23 | return len(names) == 0, nil |
| 24 | } |
| 25 | |
| 26 | // LastMarker returns the last container name in a ListResult. |
| 27 | func (r ListResult) LastMarker() (string, error) { |
| 28 | names, err := ExtractNames(r) |
| 29 | if err != nil { |
| 30 | return "", err |
| 31 | } |
| 32 | if len(names) == 0 { |
| 33 | return "", nil |
| 34 | } |
| 35 | return names[len(names)-1], nil |
| 36 | } |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 37 | |
| 38 | // 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] | 39 | type GetResult *http.Response |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 40 | |
| 41 | // List is a function that retrieves all objects in a container. It also returns the details |
| 42 | // 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] | 43 | // response to the ExtractInfo or ExtractNames function, respectively. |
Ash Wilson | 0faafcc | 2014-09-16 15:20:17 -0400 | [diff] [blame] | 44 | func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager { |
| 45 | var headers map[string]string |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 46 | |
| 47 | query := utils.BuildQuery(opts.Params) |
| 48 | |
| 49 | if !opts.Full { |
Ash Wilson | 0faafcc | 2014-09-16 15:20:17 -0400 | [diff] [blame] | 50 | headers = map[string]string{"Content-Type": "text/plain"} |
| 51 | } |
| 52 | |
| 53 | createPage := func(r pagination.LastHTTPResponse) pagination.Page { |
| 54 | p := ListResult{pagination.MarkerPageBase{LastHTTPResponse: r}} |
| 55 | p.MarkerPageBase.Owner = p |
| 56 | return p |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 57 | } |
| 58 | |
Jon Perritt | fc39b94 | 2014-09-10 21:18:19 -0500 | [diff] [blame] | 59 | url := accountURL(c) + query |
Jon Perritt | 584c94f | 2014-09-24 18:00:16 -0500 | [diff] [blame^] | 60 | pager := pagination.NewPager(c, url, createPage) |
| 61 | pager.Headers = headers |
| 62 | return pager |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // Create is a function that creates a new container. |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 66 | func Create(c *gophercloud.ServiceClient, opts CreateOpts) (Container, error) { |
Jon Perritt | c19adea | 2014-04-15 16:56:01 -0500 | [diff] [blame] | 67 | var ci Container |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 68 | |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 69 | h := c.Provider.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 70 | |
| 71 | for k, v := range opts.Headers { |
| 72 | h[k] = v |
| 73 | } |
| 74 | |
| 75 | for k, v := range opts.Metadata { |
| 76 | h["X-Container-Meta-"+k] = v |
| 77 | } |
| 78 | |
Jon Perritt | fc39b94 | 2014-09-10 21:18:19 -0500 | [diff] [blame] | 79 | url := containerURL(c, opts.Name) |
Ash Wilson | e47ea9e | 2014-09-10 16:03:44 -0400 | [diff] [blame] | 80 | _, err := perigee.Request("PUT", url, perigee.Options{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 81 | MoreHeaders: h, |
Ash Wilson | e47ea9e | 2014-09-10 16:03:44 -0400 | [diff] [blame] | 82 | OkCodes: []int{201, 204}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 83 | }) |
| 84 | if err == nil { |
Jon Perritt | c19adea | 2014-04-15 16:56:01 -0500 | [diff] [blame] | 85 | ci = Container{ |
| 86 | "name": opts.Name, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | return ci, err |
| 90 | } |
| 91 | |
| 92 | // Delete is a function that deletes a container. |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 93 | func Delete(c *gophercloud.ServiceClient, opts DeleteOpts) error { |
| 94 | h := c.Provider.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 95 | |
| 96 | query := utils.BuildQuery(opts.Params) |
| 97 | |
Jon Perritt | fc39b94 | 2014-09-10 21:18:19 -0500 | [diff] [blame] | 98 | url := containerURL(c, opts.Name) + query |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 99 | _, err := perigee.Request("DELETE", url, perigee.Options{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 100 | MoreHeaders: h, |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 101 | OkCodes: []int{204}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 102 | }) |
| 103 | return err |
| 104 | } |
| 105 | |
| 106 | // 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] | 107 | func Update(c *gophercloud.ServiceClient, opts UpdateOpts) error { |
| 108 | h := c.Provider.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 109 | |
| 110 | for k, v := range opts.Headers { |
| 111 | h[k] = v |
| 112 | } |
| 113 | |
| 114 | for k, v := range opts.Metadata { |
| 115 | h["X-Container-Meta-"+k] = v |
| 116 | } |
| 117 | |
Jon Perritt | fc39b94 | 2014-09-10 21:18:19 -0500 | [diff] [blame] | 118 | url := containerURL(c, opts.Name) |
Ash Wilson | e47ea9e | 2014-09-10 16:03:44 -0400 | [diff] [blame] | 119 | _, err := perigee.Request("POST", url, perigee.Options{ |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 120 | MoreHeaders: h, |
Ash Wilson | e47ea9e | 2014-09-10 16:03:44 -0400 | [diff] [blame] | 121 | OkCodes: []int{204}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 122 | }) |
| 123 | return err |
| 124 | } |
| 125 | |
| 126 | // 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] | 127 | // metadata, pass the GetResult response to the ExtractMetadata function. |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 128 | func Get(c *gophercloud.ServiceClient, opts GetOpts) (GetResult, error) { |
| 129 | h := c.Provider.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 130 | |
Jon Perritt | fc39b94 | 2014-09-10 21:18:19 -0500 | [diff] [blame] | 131 | url := containerURL(c, opts.Name) |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 132 | resp, err := perigee.Request("HEAD", url, perigee.Options{ |
| 133 | MoreHeaders: h, |
Ash Wilson | e47ea9e | 2014-09-10 16:03:44 -0400 | [diff] [blame] | 134 | OkCodes: []int{204}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 135 | }) |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 136 | return &resp.HttpResponse, err |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 137 | } |