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 | // UpdateOpts is a structure that contains parameters for updating, creating, or deleting an |
| 9 | // account's metadata. |
| 10 | type UpdateOpts struct { |
| 11 | Metadata map[string]string |
| 12 | Headers map[string]string |
| 13 | } |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 14 | |
| 15 | // Update is a function that creates, updates, or deletes an account's metadata. |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 16 | func Update(c *gophercloud.ServiceClient, opts UpdateOpts) UpdateResult { |
| 17 | headers := c.Provider.AuthenticatedHeaders() |
Ash Wilson | 604320e | 2014-09-10 16:02:28 -0400 | [diff] [blame] | 18 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 19 | for k, v := range opts.Headers { |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 20 | headers[k] = v |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | for k, v := range opts.Metadata { |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 24 | headers["X-Account-Meta-"+k] = v |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 25 | } |
| 26 | |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 27 | var res UpdateResult |
| 28 | |
| 29 | var resp *perigee.Response |
| 30 | |
| 31 | resp, res.Err = perigee.Request("POST", accountURL(c), perigee.Options{ |
| 32 | MoreHeaders: headers, |
Ash Wilson | e47ea9e | 2014-09-10 16:03:44 -0400 | [diff] [blame] | 33 | OkCodes: []int{204}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 34 | }) |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 35 | |
| 36 | res.Resp = &resp.HttpResponse |
| 37 | |
| 38 | return res |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 39 | } |
| 40 | |
Jamie Hannaford | 93209fe | 2014-10-10 11:54:19 +0200 | [diff] [blame] | 41 | // GetOpts is a structure that contains parameters for getting an account's metadata. |
| 42 | type GetOpts struct { |
| 43 | Headers map[string]string |
| 44 | } |
| 45 | |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 46 | // Get is a function that retrieves an account's metadata. To extract just the custom |
Jon Perritt | eb57564 | 2014-04-24 15:16:31 -0500 | [diff] [blame] | 47 | // metadata, pass the GetResult response to the ExtractMetadata function. |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 48 | func Get(c *gophercloud.ServiceClient, opts GetOpts) GetResult { |
| 49 | headers := c.Provider.AuthenticatedHeaders() |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 50 | |
| 51 | for k, v := range opts.Headers { |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 52 | headers[k] = v |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 53 | } |
| 54 | |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 55 | var res GetResult |
| 56 | var resp *perigee.Response |
| 57 | |
| 58 | resp, res.Err = perigee.Request("HEAD", accountURL(c), perigee.Options{ |
| 59 | MoreHeaders: headers, |
| 60 | Results: &res.Resp, |
Ash Wilson | e47ea9e | 2014-09-10 16:03:44 -0400 | [diff] [blame] | 61 | OkCodes: []int{204}, |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 62 | }) |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 63 | |
| 64 | res.Resp = &resp.HttpResponse |
| 65 | |
| 66 | return res |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 67 | } |