blob: 846ec0c2b5c24766e6604e5764eb66bd1bc77cba [file] [log] [blame]
Jon Perritt816d2a02014-03-11 20:49:46 -05001package accounts
2
Ash Wilson4bf41a32015-02-12 15:52:44 -05003import "github.com/rackspace/gophercloud"
Jon Perritt816d2a02014-03-11 20:49:46 -05004
Jon Perritte90aced2014-10-12 23:24:06 -05005// GetOptsBuilder allows extensions to add additional headers to the Get
6// request.
7type 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 Hannaford93209fe2014-10-10 11:54:19 +020013type GetOpts struct {
Jon Perritt4a59d232014-10-09 20:21:31 -050014 Newest bool `h:"X-Newest"`
Jamie Hannaford93209fe2014-10-10 11:54:19 +020015}
16
Jon Perritte90aced2014-10-12 23:24:06 -050017// ToAccountGetMap formats a GetOpts into a map[string]string of headers.
18func (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 Perritt9856a342014-10-27 13:44:06 -050025// ExtractHeader method on the GetResult.
Jon Perritte90aced2014-10-12 23:24:06 -050026func Get(c *gophercloud.ServiceClient, opts GetOptsBuilder) GetResult {
Jon Perritt4a59d232014-10-09 20:21:31 -050027 var res GetResult
Ash Wilson77857dc2014-10-22 09:09:02 -040028 h := c.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -050029
Jon Perritt4a59d232014-10-09 20:21:31 -050030 if opts != nil {
Jon Perritte90aced2014-10-12 23:24:06 -050031 headers, err := opts.ToAccountGetMap()
Jon Perritt4a59d232014-10-09 20:21:31 -050032 if err != nil {
33 res.Err = err
34 return res
35 }
36
37 for k, v := range headers {
38 h[k] = v
39 }
Jon Perritt816d2a02014-03-11 20:49:46 -050040 }
41
Ash Wilson4bf41a32015-02-12 15:52:44 -050042 resp, err := c.Request("HEAD", getURL(c), gophercloud.RequestOpts{
Jon Perritt4a59d232014-10-09 20:21:31 -050043 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -040044 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -050045 })
Ash Wilson72e4d2c2014-10-20 10:27:30 -040046 res.Header = resp.HttpResponse.Header
Jon Perritt4a59d232014-10-09 20:21:31 -050047 res.Err = err
48 return res
49}
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020050
Jon Perritte90aced2014-10-12 23:24:06 -050051// UpdateOptsBuilder allows extensions to add additional headers to the Update
52// request.
53type UpdateOptsBuilder interface {
54 ToAccountUpdateMap() (map[string]string, error)
55}
56
57// UpdateOpts is a structure that contains parameters for updating, creating, or
58// deleting an account's metadata.
Jon Perritt4a59d232014-10-09 20:21:31 -050059type UpdateOpts struct {
60 Metadata map[string]string
61 ContentType string `h:"Content-Type"`
62 DetectContentType bool `h:"X-Detect-Content-Type"`
63 TempURLKey string `h:"X-Account-Meta-Temp-URL-Key"`
64 TempURLKey2 string `h:"X-Account-Meta-Temp-URL-Key-2"`
65}
66
Jon Perritte90aced2014-10-12 23:24:06 -050067// ToAccountUpdateMap formats an UpdateOpts into a map[string]string of headers.
68func (opts UpdateOpts) ToAccountUpdateMap() (map[string]string, error) {
69 headers, err := gophercloud.BuildHeaders(opts)
70 if err != nil {
71 return nil, err
72 }
73 for k, v := range opts.Metadata {
74 headers["X-Account-Meta-"+k] = v
75 }
76 return headers, err
77}
78
79// Update is a function that creates, updates, or deletes an account's metadata.
Ash Wilsonaf262872014-10-20 09:32:29 -040080// To extract the headers returned, call the Extract method on the UpdateResult.
Jon Perritte90aced2014-10-12 23:24:06 -050081func Update(c *gophercloud.ServiceClient, opts UpdateOptsBuilder) UpdateResult {
Jon Perritt4a59d232014-10-09 20:21:31 -050082 var res UpdateResult
Ash Wilson77857dc2014-10-22 09:09:02 -040083 h := c.AuthenticatedHeaders()
Jon Perritt4a59d232014-10-09 20:21:31 -050084
85 if opts != nil {
Jon Perritte90aced2014-10-12 23:24:06 -050086 headers, err := opts.ToAccountUpdateMap()
Jon Perritt4a59d232014-10-09 20:21:31 -050087 if err != nil {
88 res.Err = err
89 return res
90 }
91 for k, v := range headers {
92 h[k] = v
93 }
Jon Perritt4a59d232014-10-09 20:21:31 -050094 }
95
Ash Wilson4bf41a32015-02-12 15:52:44 -050096 resp, err := c.Request("POST", updateURL(c), gophercloud.RequestOpts{
Jon Perritt4a59d232014-10-09 20:21:31 -050097 MoreHeaders: h,
98 OkCodes: []int{204},
99 })
Ash Wilson72e4d2c2014-10-20 10:27:30 -0400100 res.Header = resp.HttpResponse.Header
Jon Perritt4a59d232014-10-09 20:21:31 -0500101 res.Err = err
Jamie Hannafordb2e129d2014-10-10 14:06:28 +0200102 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500103}