blob: 55fcb05ffd84714d474efda0feb288eec1ff6548 [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
Jon Perritte90aced2014-10-12 23:24:06 -05008// GetOptsBuilder allows extensions to add additional headers to the Get
9// request.
10type GetOptsBuilder interface {
11 ToAccountGetMap() (map[string]string, error)
12}
13
14// GetOpts is a structure that contains parameters for getting an account's
15// metadata.
Jamie Hannaford93209fe2014-10-10 11:54:19 +020016type GetOpts struct {
Jon Perritt4a59d232014-10-09 20:21:31 -050017 Newest bool `h:"X-Newest"`
Jamie Hannaford93209fe2014-10-10 11:54:19 +020018}
19
Jon Perritte90aced2014-10-12 23:24:06 -050020// ToAccountGetMap formats a GetOpts into a map[string]string of headers.
21func (opts GetOpts) ToAccountGetMap() (map[string]string, error) {
22 return gophercloud.BuildHeaders(opts)
23}
24
25// Get is a function that retrieves an account's metadata. To extract just the
26// custom metadata, call the ExtractMetadata method on the GetResult. To extract
27// all the headers that are returned (including the metadata), call the
28// ExtractHeaders method on the GetResult.
29func Get(c *gophercloud.ServiceClient, opts GetOptsBuilder) GetResult {
Jon Perritt4a59d232014-10-09 20:21:31 -050030 var res GetResult
31 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -050032
Jon Perritt4a59d232014-10-09 20:21:31 -050033 if opts != nil {
Jon Perritte90aced2014-10-12 23:24:06 -050034 headers, err := opts.ToAccountGetMap()
Jon Perritt4a59d232014-10-09 20:21:31 -050035 if err != nil {
36 res.Err = err
37 return res
38 }
39
40 for k, v := range headers {
41 h[k] = v
42 }
Jon Perritt816d2a02014-03-11 20:49:46 -050043 }
44
Jon Perritt4a59d232014-10-09 20:21:31 -050045 resp, err := perigee.Request("HEAD", getURL(c), perigee.Options{
46 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -040047 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -050048 })
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020049 res.Resp = &resp.HttpResponse
Jon Perritt4a59d232014-10-09 20:21:31 -050050 res.Err = err
51 return res
52}
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020053
Jon Perritte90aced2014-10-12 23:24:06 -050054// UpdateOptsBuilder allows extensions to add additional headers to the Update
55// request.
56type UpdateOptsBuilder interface {
57 ToAccountUpdateMap() (map[string]string, error)
58}
59
60// UpdateOpts is a structure that contains parameters for updating, creating, or
61// deleting an account's metadata.
Jon Perritt4a59d232014-10-09 20:21:31 -050062type UpdateOpts struct {
63 Metadata map[string]string
64 ContentType string `h:"Content-Type"`
65 DetectContentType bool `h:"X-Detect-Content-Type"`
66 TempURLKey string `h:"X-Account-Meta-Temp-URL-Key"`
67 TempURLKey2 string `h:"X-Account-Meta-Temp-URL-Key-2"`
68}
69
Jon Perritte90aced2014-10-12 23:24:06 -050070// ToAccountUpdateMap formats an UpdateOpts into a map[string]string of headers.
71func (opts UpdateOpts) ToAccountUpdateMap() (map[string]string, error) {
72 headers, err := gophercloud.BuildHeaders(opts)
73 if err != nil {
74 return nil, err
75 }
76 for k, v := range opts.Metadata {
77 headers["X-Account-Meta-"+k] = v
78 }
79 return headers, err
80}
81
82// Update is a function that creates, updates, or deletes an account's metadata.
83// To extract the headers returned, call the ExtractHeaders method on the
84// UpdateResult.
85func Update(c *gophercloud.ServiceClient, opts UpdateOptsBuilder) UpdateResult {
Jon Perritt4a59d232014-10-09 20:21:31 -050086 var res UpdateResult
87 h := c.Provider.AuthenticatedHeaders()
88
89 if opts != nil {
Jon Perritte90aced2014-10-12 23:24:06 -050090 headers, err := opts.ToAccountUpdateMap()
Jon Perritt4a59d232014-10-09 20:21:31 -050091 if err != nil {
92 res.Err = err
93 return res
94 }
95 for k, v := range headers {
96 h[k] = v
97 }
Jon Perritt4a59d232014-10-09 20:21:31 -050098 }
99
100 resp, err := perigee.Request("POST", updateURL(c), perigee.Options{
101 MoreHeaders: h,
102 OkCodes: []int{204},
103 })
104 res.Resp = &resp.HttpResponse
105 res.Err = err
Jamie Hannafordb2e129d2014-10-10 14:06:28 +0200106 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500107}