Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 1 | package accounts |
| 2 | |
| 3 | import ( |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 4 | "github.com/racker/perigee" |
| 5 | "github.com/rackspace/gophercloud" |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 6 | ) |
| 7 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 8 | // GetOptsBuilder allows extensions to add additional headers to the Get |
| 9 | // request. |
| 10 | type GetOptsBuilder interface { |
| 11 | ToAccountGetMap() (map[string]string, error) |
| 12 | } |
| 13 | |
| 14 | // GetOpts is a structure that contains parameters for getting an account's |
| 15 | // metadata. |
Jamie Hannaford | 93209fe | 2014-10-10 11:54:19 +0200 | [diff] [blame] | 16 | type GetOpts struct { |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame] | 17 | Newest bool `h:"X-Newest"` |
Jamie Hannaford | 93209fe | 2014-10-10 11:54:19 +0200 | [diff] [blame] | 18 | } |
| 19 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 20 | // ToAccountGetMap formats a GetOpts into a map[string]string of headers. |
| 21 | func (opts GetOpts) ToAccountGetMap() (map[string]string, error) { |
| 22 | return gophercloud.BuildHeaders(opts) |
| 23 | } |
| 24 | |
| 25 | // Get is a function that retrieves an account's metadata. To extract just the |
| 26 | // custom metadata, call the ExtractMetadata method on the GetResult. To extract |
| 27 | // all the headers that are returned (including the metadata), call the |
| 28 | // ExtractHeaders method on the GetResult. |
| 29 | func Get(c *gophercloud.ServiceClient, opts GetOptsBuilder) GetResult { |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame] | 30 | var res GetResult |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame^] | 31 | h := c.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 32 | |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame] | 33 | if opts != nil { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 34 | headers, err := opts.ToAccountGetMap() |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame] | 35 | if err != nil { |
| 36 | res.Err = err |
| 37 | return res |
| 38 | } |
| 39 | |
| 40 | for k, v := range headers { |
| 41 | h[k] = v |
| 42 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 43 | } |
| 44 | |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame] | 45 | resp, err := perigee.Request("HEAD", getURL(c), perigee.Options{ |
| 46 | MoreHeaders: h, |
Ash Wilson | e47ea9e | 2014-09-10 16:03:44 -0400 | [diff] [blame] | 47 | OkCodes: []int{204}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 48 | }) |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 49 | res.Header = resp.HttpResponse.Header |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame] | 50 | res.Err = err |
| 51 | return res |
| 52 | } |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 53 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 54 | // UpdateOptsBuilder allows extensions to add additional headers to the Update |
| 55 | // request. |
| 56 | type UpdateOptsBuilder interface { |
| 57 | ToAccountUpdateMap() (map[string]string, error) |
| 58 | } |
| 59 | |
| 60 | // UpdateOpts is a structure that contains parameters for updating, creating, or |
| 61 | // deleting an account's metadata. |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame] | 62 | type UpdateOpts struct { |
| 63 | Metadata map[string]string |
| 64 | ContentType string `h:"Content-Type"` |
| 65 | DetectContentType bool `h:"X-Detect-Content-Type"` |
| 66 | TempURLKey string `h:"X-Account-Meta-Temp-URL-Key"` |
| 67 | TempURLKey2 string `h:"X-Account-Meta-Temp-URL-Key-2"` |
| 68 | } |
| 69 | |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 70 | // ToAccountUpdateMap formats an UpdateOpts into a map[string]string of headers. |
| 71 | func (opts UpdateOpts) ToAccountUpdateMap() (map[string]string, error) { |
| 72 | headers, err := gophercloud.BuildHeaders(opts) |
| 73 | if err != nil { |
| 74 | return nil, err |
| 75 | } |
| 76 | for k, v := range opts.Metadata { |
| 77 | headers["X-Account-Meta-"+k] = v |
| 78 | } |
| 79 | return headers, err |
| 80 | } |
| 81 | |
| 82 | // Update is a function that creates, updates, or deletes an account's metadata. |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 83 | // To extract the headers returned, call the Extract method on the UpdateResult. |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 84 | func Update(c *gophercloud.ServiceClient, opts UpdateOptsBuilder) UpdateResult { |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame] | 85 | var res UpdateResult |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame^] | 86 | h := c.AuthenticatedHeaders() |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame] | 87 | |
| 88 | if opts != nil { |
Jon Perritt | e90aced | 2014-10-12 23:24:06 -0500 | [diff] [blame] | 89 | headers, err := opts.ToAccountUpdateMap() |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame] | 90 | if err != nil { |
| 91 | res.Err = err |
| 92 | return res |
| 93 | } |
| 94 | for k, v := range headers { |
| 95 | h[k] = v |
| 96 | } |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | resp, err := perigee.Request("POST", updateURL(c), perigee.Options{ |
| 100 | MoreHeaders: h, |
| 101 | OkCodes: []int{204}, |
| 102 | }) |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 103 | res.Header = resp.HttpResponse.Header |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame] | 104 | res.Err = err |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 105 | return res |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 106 | } |