blob: 57d6a6dab0ceb0debd0b2a512d3331c4aabf4664 [file] [log] [blame]
Jon Perritt816d2a02014-03-11 20:49:46 -05001package accounts
2
Jon Perritt27249f42016-02-18 10:35:59 -06003import "github.com/gophercloud/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 })
Jon Perritta2c88b22015-05-18 11:23:30 -060046 if resp != nil {
47 res.Header = resp.Header
48 }
Jon Perritt4a59d232014-10-09 20:21:31 -050049 res.Err = err
50 return res
51}
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020052
Jon Perritte90aced2014-10-12 23:24:06 -050053// UpdateOptsBuilder allows extensions to add additional headers to the Update
54// request.
55type UpdateOptsBuilder interface {
56 ToAccountUpdateMap() (map[string]string, error)
57}
58
59// UpdateOpts is a structure that contains parameters for updating, creating, or
60// deleting an account's metadata.
Jon Perritt4a59d232014-10-09 20:21:31 -050061type UpdateOpts struct {
62 Metadata map[string]string
63 ContentType string `h:"Content-Type"`
64 DetectContentType bool `h:"X-Detect-Content-Type"`
65 TempURLKey string `h:"X-Account-Meta-Temp-URL-Key"`
66 TempURLKey2 string `h:"X-Account-Meta-Temp-URL-Key-2"`
67}
68
Jon Perritte90aced2014-10-12 23:24:06 -050069// ToAccountUpdateMap formats an UpdateOpts into a map[string]string of headers.
70func (opts UpdateOpts) ToAccountUpdateMap() (map[string]string, error) {
71 headers, err := gophercloud.BuildHeaders(opts)
72 if err != nil {
73 return nil, err
74 }
75 for k, v := range opts.Metadata {
76 headers["X-Account-Meta-"+k] = v
77 }
78 return headers, err
79}
80
81// Update is a function that creates, updates, or deletes an account's metadata.
Ash Wilsonaf262872014-10-20 09:32:29 -040082// To extract the headers returned, call the Extract method on the UpdateResult.
Jon Perritte90aced2014-10-12 23:24:06 -050083func Update(c *gophercloud.ServiceClient, opts UpdateOptsBuilder) UpdateResult {
Jon Perritt4a59d232014-10-09 20:21:31 -050084 var res UpdateResult
Ash Wilson2491b4c2015-02-12 16:13:39 -050085 h := make(map[string]string)
Jon Perritt4a59d232014-10-09 20:21:31 -050086
87 if opts != nil {
Jon Perritte90aced2014-10-12 23:24:06 -050088 headers, err := opts.ToAccountUpdateMap()
Jon Perritt4a59d232014-10-09 20:21:31 -050089 if err != nil {
90 res.Err = err
91 return res
92 }
93 for k, v := range headers {
94 h[k] = v
95 }
Jon Perritt4a59d232014-10-09 20:21:31 -050096 }
97
Ash Wilson4bf41a32015-02-12 15:52:44 -050098 resp, err := c.Request("POST", updateURL(c), gophercloud.RequestOpts{
Jon Perritt4a59d232014-10-09 20:21:31 -050099 MoreHeaders: h,
Jamie Hannafordc530ba12015-03-23 17:50:46 +0100100 OkCodes: []int{201, 202, 204},
Jon Perritt4a59d232014-10-09 20:21:31 -0500101 })
Jon Perritta2c88b22015-05-18 11:23:30 -0600102 if resp != nil {
103 res.Header = resp.Header
104 }
Jon Perritt4a59d232014-10-09 20:21:31 -0500105 res.Err = err
Jamie Hannafordb2e129d2014-10-10 14:06:28 +0200106 return res
Jon Perritt816d2a02014-03-11 20:49:46 -0500107}