blob: 57616c03567579d8d47d92702d665ed7ac5e397b [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 Perritt3860b512016-03-29 12:01:48 -050026func Get(c *gophercloud.ServiceClient, opts GetOptsBuilder) (r GetResult) {
Jon Perrittfea90732016-03-15 02:57:05 -050027 h := make(map[string]string)
Jon Perritt4a59d232014-10-09 20:21:31 -050028 if opts != nil {
Jon Perritte90aced2014-10-12 23:24:06 -050029 headers, err := opts.ToAccountGetMap()
Jon Perritt4a59d232014-10-09 20:21:31 -050030 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -050031 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -050032 return
Jon Perritt4a59d232014-10-09 20:21:31 -050033 }
Jon Perritt4a59d232014-10-09 20:21:31 -050034 for k, v := range headers {
35 h[k] = v
36 }
Jon Perritt816d2a02014-03-11 20:49:46 -050037 }
Jon Perritta33da232016-03-02 04:43:08 -060038 resp, err := c.Request("HEAD", getURL(c), &gophercloud.RequestOpts{
Jon Perritt4a59d232014-10-09 20:21:31 -050039 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -040040 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -050041 })
Jon Perritta2c88b22015-05-18 11:23:30 -060042 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -050043 r.Header = resp.Header
Jon Perritta2c88b22015-05-18 11:23:30 -060044 }
Jon Perrittfea90732016-03-15 02:57:05 -050045 r.Err = err
Jon Perritt4a59d232014-10-09 20:21:31 -050046}
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020047
Jon Perritte90aced2014-10-12 23:24:06 -050048// UpdateOptsBuilder allows extensions to add additional headers to the Update
49// request.
50type UpdateOptsBuilder interface {
51 ToAccountUpdateMap() (map[string]string, error)
52}
53
54// UpdateOpts is a structure that contains parameters for updating, creating, or
55// deleting an account's metadata.
Jon Perritt4a59d232014-10-09 20:21:31 -050056type UpdateOpts struct {
57 Metadata map[string]string
58 ContentType string `h:"Content-Type"`
59 DetectContentType bool `h:"X-Detect-Content-Type"`
60 TempURLKey string `h:"X-Account-Meta-Temp-URL-Key"`
61 TempURLKey2 string `h:"X-Account-Meta-Temp-URL-Key-2"`
62}
63
Jon Perritte90aced2014-10-12 23:24:06 -050064// ToAccountUpdateMap formats an UpdateOpts into a map[string]string of headers.
65func (opts UpdateOpts) ToAccountUpdateMap() (map[string]string, error) {
66 headers, err := gophercloud.BuildHeaders(opts)
67 if err != nil {
68 return nil, err
69 }
70 for k, v := range opts.Metadata {
71 headers["X-Account-Meta-"+k] = v
72 }
73 return headers, err
74}
75
76// Update is a function that creates, updates, or deletes an account's metadata.
Ash Wilsonaf262872014-10-20 09:32:29 -040077// To extract the headers returned, call the Extract method on the UpdateResult.
Jon Perritt3860b512016-03-29 12:01:48 -050078func Update(c *gophercloud.ServiceClient, opts UpdateOptsBuilder) (r UpdateResult) {
Ash Wilson2491b4c2015-02-12 16:13:39 -050079 h := make(map[string]string)
Jon Perritt4a59d232014-10-09 20:21:31 -050080 if opts != nil {
Jon Perritte90aced2014-10-12 23:24:06 -050081 headers, err := opts.ToAccountUpdateMap()
Jon Perritt4a59d232014-10-09 20:21:31 -050082 if err != nil {
Jon Perrittfea90732016-03-15 02:57:05 -050083 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -050084 return
Jon Perritt4a59d232014-10-09 20:21:31 -050085 }
86 for k, v := range headers {
87 h[k] = v
88 }
Jon Perritt4a59d232014-10-09 20:21:31 -050089 }
Jon Perritta33da232016-03-02 04:43:08 -060090 resp, err := c.Request("POST", updateURL(c), &gophercloud.RequestOpts{
Jon Perritt4a59d232014-10-09 20:21:31 -050091 MoreHeaders: h,
Jamie Hannafordc530ba12015-03-23 17:50:46 +010092 OkCodes: []int{201, 202, 204},
Jon Perritt4a59d232014-10-09 20:21:31 -050093 })
Jon Perritta2c88b22015-05-18 11:23:30 -060094 if resp != nil {
Jon Perrittfea90732016-03-15 02:57:05 -050095 r.Header = resp.Header
Jon Perritta2c88b22015-05-18 11:23:30 -060096 }
Jon Perrittfea90732016-03-15 02:57:05 -050097 r.Err = err
Jon Perritt816d2a02014-03-11 20:49:46 -050098}