blob: 41479a2edf8004ec6f3c1095c0ad1a4e527c4c45 [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 Perrittfea90732016-03-15 02:57:05 -050027 var r GetResult
28 h := make(map[string]string)
Jon Perritt4a59d232014-10-09 20:21:31 -050029 if opts != nil {
Jon Perritte90aced2014-10-12 23:24:06 -050030 headers, err := opts.ToAccountGetMap()
Jon Perritt4a59d232014-10-09 20:21:31 -050031 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -050032 r.Err = err
33 return r
Jon Perritt4a59d232014-10-09 20:21:31 -050034 }
Jon Perritt4a59d232014-10-09 20:21:31 -050035 for k, v := range headers {
36 h[k] = v
37 }
Jon Perritt816d2a02014-03-11 20:49:46 -050038 }
Jon Perritta33da232016-03-02 04:43:08 -060039 resp, err := c.Request("HEAD", getURL(c), &gophercloud.RequestOpts{
Jon Perritt4a59d232014-10-09 20:21:31 -050040 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -040041 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -050042 })
Jon Perritta2c88b22015-05-18 11:23:30 -060043 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -050044 r.Header = resp.Header
Jon Perritta2c88b22015-05-18 11:23:30 -060045 }
Jon Perrittfea90732016-03-15 02:57:05 -050046 r.Err = err
47 return r
Jon Perritt4a59d232014-10-09 20:21:31 -050048}
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020049
Jon Perritte90aced2014-10-12 23:24:06 -050050// UpdateOptsBuilder allows extensions to add additional headers to the Update
51// request.
52type UpdateOptsBuilder interface {
53 ToAccountUpdateMap() (map[string]string, error)
54}
55
56// UpdateOpts is a structure that contains parameters for updating, creating, or
57// deleting an account's metadata.
Jon Perritt4a59d232014-10-09 20:21:31 -050058type UpdateOpts struct {
59 Metadata map[string]string
60 ContentType string `h:"Content-Type"`
61 DetectContentType bool `h:"X-Detect-Content-Type"`
62 TempURLKey string `h:"X-Account-Meta-Temp-URL-Key"`
63 TempURLKey2 string `h:"X-Account-Meta-Temp-URL-Key-2"`
64}
65
Jon Perritte90aced2014-10-12 23:24:06 -050066// ToAccountUpdateMap formats an UpdateOpts into a map[string]string of headers.
67func (opts UpdateOpts) ToAccountUpdateMap() (map[string]string, error) {
68 headers, err := gophercloud.BuildHeaders(opts)
69 if err != nil {
70 return nil, err
71 }
72 for k, v := range opts.Metadata {
73 headers["X-Account-Meta-"+k] = v
74 }
75 return headers, err
76}
77
78// Update is a function that creates, updates, or deletes an account's metadata.
Ash Wilsonaf262872014-10-20 09:32:29 -040079// To extract the headers returned, call the Extract method on the UpdateResult.
Jon Perritte90aced2014-10-12 23:24:06 -050080func Update(c *gophercloud.ServiceClient, opts UpdateOptsBuilder) UpdateResult {
Jon Perrittfea90732016-03-15 02:57:05 -050081 var r UpdateResult
Ash Wilson2491b4c2015-02-12 16:13:39 -050082 h := make(map[string]string)
Jon Perritt4a59d232014-10-09 20:21:31 -050083 if opts != nil {
Jon Perritte90aced2014-10-12 23:24:06 -050084 headers, err := opts.ToAccountUpdateMap()
Jon Perritt4a59d232014-10-09 20:21:31 -050085 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -050086 r.Err = err
87 return r
Jon Perritt4a59d232014-10-09 20:21:31 -050088 }
89 for k, v := range headers {
90 h[k] = v
91 }
Jon Perritt4a59d232014-10-09 20:21:31 -050092 }
Jon Perritta33da232016-03-02 04:43:08 -060093 resp, err := c.Request("POST", updateURL(c), &gophercloud.RequestOpts{
Jon Perritt4a59d232014-10-09 20:21:31 -050094 MoreHeaders: h,
Jamie Hannafordc530ba12015-03-23 17:50:46 +010095 OkCodes: []int{201, 202, 204},
Jon Perritt4a59d232014-10-09 20:21:31 -050096 })
Jon Perritta2c88b22015-05-18 11:23:30 -060097 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -050098 r.Header = resp.Header
Jon Perritta2c88b22015-05-18 11:23:30 -060099 }
Jon Perrittfea90732016-03-15 02:57:05 -0500100 r.Err = err
101 return r
Jon Perritt816d2a02014-03-11 20:49:46 -0500102}