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 | |
Jamie Hannaford | 93209fe | 2014-10-10 11:54:19 +0200 | [diff] [blame] | 8 | // GetOpts is a structure that contains parameters for getting an account's metadata. |
| 9 | type GetOpts struct { |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame^] | 10 | Newest bool `h:"X-Newest"` |
Jamie Hannaford | 93209fe | 2014-10-10 11:54:19 +0200 | [diff] [blame] | 11 | } |
| 12 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 13 | // Get is a function that retrieves an account's metadata. To extract just the custom |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame^] | 14 | // metadata, call the ExtractMetadata method on the GetResult. To extract all the headers that are |
| 15 | // returned (including the metadata), call the ExtractHeaders method on the GetResult. |
| 16 | func Get(c *gophercloud.ServiceClient, opts *GetOpts) GetResult { |
| 17 | var res GetResult |
| 18 | h := c.Provider.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 19 | |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame^] | 20 | if opts != nil { |
| 21 | headers, err := gophercloud.BuildHeaders(opts) |
| 22 | if err != nil { |
| 23 | res.Err = err |
| 24 | return res |
| 25 | } |
| 26 | |
| 27 | for k, v := range headers { |
| 28 | h[k] = v |
| 29 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 30 | } |
| 31 | |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame^] | 32 | resp, err := perigee.Request("HEAD", getURL(c), perigee.Options{ |
| 33 | MoreHeaders: h, |
Ash Wilson | e47ea9e | 2014-09-10 16:03:44 -0400 | [diff] [blame] | 34 | OkCodes: []int{204}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 35 | }) |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 36 | res.Resp = &resp.HttpResponse |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame^] | 37 | res.Err = err |
| 38 | return res |
| 39 | } |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 40 | |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame^] | 41 | // UpdateOpts is a structure that contains parameters for updating, creating, or deleting an |
| 42 | // account's metadata. |
| 43 | type UpdateOpts struct { |
| 44 | Metadata map[string]string |
| 45 | ContentType string `h:"Content-Type"` |
| 46 | DetectContentType bool `h:"X-Detect-Content-Type"` |
| 47 | TempURLKey string `h:"X-Account-Meta-Temp-URL-Key"` |
| 48 | TempURLKey2 string `h:"X-Account-Meta-Temp-URL-Key-2"` |
| 49 | } |
| 50 | |
| 51 | // Update is a function that creates, updates, or deletes an account's metadata. To extract the |
| 52 | // headers returned, call the ExtractHeaders method on the UpdateResult. |
| 53 | func Update(c *gophercloud.ServiceClient, opts *UpdateOpts) UpdateResult { |
| 54 | var res UpdateResult |
| 55 | h := c.Provider.AuthenticatedHeaders() |
| 56 | |
| 57 | if opts != nil { |
| 58 | headers, err := gophercloud.BuildHeaders(opts) |
| 59 | if err != nil { |
| 60 | res.Err = err |
| 61 | return res |
| 62 | } |
| 63 | for k, v := range headers { |
| 64 | h[k] = v |
| 65 | } |
| 66 | |
| 67 | for k, v := range opts.Metadata { |
| 68 | h["X-Account-Meta-"+k] = v |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | resp, err := perigee.Request("POST", updateURL(c), perigee.Options{ |
| 73 | MoreHeaders: h, |
| 74 | OkCodes: []int{204}, |
| 75 | }) |
| 76 | res.Resp = &resp.HttpResponse |
| 77 | res.Err = err |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 78 | return res |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 79 | } |