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