blob: 5de09da25412f7bebd05444ce4e97fd53961b9a4 [file] [log] [blame]
Jon Perritt816d2a02014-03-11 20:49:46 -05001package accounts
2
3import (
Ash Wilson604320e2014-09-10 16:02:28 -04004 "github.com/racker/perigee"
5 "github.com/rackspace/gophercloud"
Jon Perritt816d2a02014-03-11 20:49:46 -05006)
7
Jamie Hannaford93209fe2014-10-10 11:54:19 +02008// UpdateOpts is a structure that contains parameters for updating, creating, or deleting an
9// account's metadata.
10type UpdateOpts struct {
11 Metadata map[string]string
12 Headers map[string]string
13}
Jon Perritt816d2a02014-03-11 20:49:46 -050014
15// Update is a function that creates, updates, or deletes an account's metadata.
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020016func Update(c *gophercloud.ServiceClient, opts UpdateOpts) UpdateResult {
17 headers := c.Provider.AuthenticatedHeaders()
Ash Wilson604320e2014-09-10 16:02:28 -040018
Jon Perritt816d2a02014-03-11 20:49:46 -050019 for k, v := range opts.Headers {
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020020 headers[k] = v
Jon Perritt816d2a02014-03-11 20:49:46 -050021 }
22
23 for k, v := range opts.Metadata {
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020024 headers["X-Account-Meta-"+k] = v
Jon Perritt816d2a02014-03-11 20:49:46 -050025 }
26
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020027 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 Wilsone47ea9e2014-09-10 16:03:44 -040033 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -050034 })
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020035
36 res.Resp = &resp.HttpResponse
37
38 return res
Jon Perritt816d2a02014-03-11 20:49:46 -050039}
40
Jamie Hannaford93209fe2014-10-10 11:54:19 +020041// GetOpts is a structure that contains parameters for getting an account's metadata.
42type GetOpts struct {
43 Headers map[string]string
44}
45
Jon Perritt816d2a02014-03-11 20:49:46 -050046// Get is a function that retrieves an account's metadata. To extract just the custom
Jon Perritteb575642014-04-24 15:16:31 -050047// metadata, pass the GetResult response to the ExtractMetadata function.
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020048func Get(c *gophercloud.ServiceClient, opts GetOpts) GetResult {
49 headers := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -050050
51 for k, v := range opts.Headers {
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020052 headers[k] = v
Jon Perritt816d2a02014-03-11 20:49:46 -050053 }
54
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020055 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 Wilsone47ea9e2014-09-10 16:03:44 -040061 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -050062 })
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020063
64 res.Resp = &resp.HttpResponse
65
66 return res
Jon Perritt816d2a02014-03-11 20:49:46 -050067}