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