blob: b30439c70b12adc944e405e31e0cbff125182ff8 [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
Jamie Hannaford93209fe2014-10-10 11:54:19 +02008// GetOpts is a structure that contains parameters for getting an account's metadata.
9type GetOpts struct {
Jon Perritt4a59d232014-10-09 20:21:31 -050010 Newest bool `h:"X-Newest"`
Jamie Hannaford93209fe2014-10-10 11:54:19 +020011}
12
Jon Perritt816d2a02014-03-11 20:49:46 -050013// Get is a function that retrieves an account's metadata. To extract just the custom
Jon Perritt4a59d232014-10-09 20:21:31 -050014// metadata, call the ExtractMetadata method on the GetResult. To extract all the headers that are
15// returned (including the metadata), call the ExtractHeaders method on the GetResult.
16func Get(c *gophercloud.ServiceClient, opts *GetOpts) GetResult {
17 var res GetResult
18 h := c.Provider.AuthenticatedHeaders()
Jon Perritt816d2a02014-03-11 20:49:46 -050019
Jon Perritt4a59d232014-10-09 20:21:31 -050020 if opts != nil {
21 headers, err := gophercloud.BuildHeaders(opts)
22 if err != nil {
23 res.Err = err
24 return res
25 }
26
27 for k, v := range headers {
28 h[k] = v
29 }
Jon Perritt816d2a02014-03-11 20:49:46 -050030 }
31
Jon Perritt4a59d232014-10-09 20:21:31 -050032 resp, err := perigee.Request("HEAD", getURL(c), perigee.Options{
33 MoreHeaders: h,
Ash Wilsone47ea9e2014-09-10 16:03:44 -040034 OkCodes: []int{204},
Jon Perritt816d2a02014-03-11 20:49:46 -050035 })
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020036 res.Resp = &resp.HttpResponse
Jon Perritt4a59d232014-10-09 20:21:31 -050037 res.Err = err
38 return res
39}
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020040
Jon Perritt4a59d232014-10-09 20:21:31 -050041// UpdateOpts is a structure that contains parameters for updating, creating, or deleting an
42// account's metadata.
43type UpdateOpts struct {
44 Metadata map[string]string
45 ContentType string `h:"Content-Type"`
46 DetectContentType bool `h:"X-Detect-Content-Type"`
47 TempURLKey string `h:"X-Account-Meta-Temp-URL-Key"`
48 TempURLKey2 string `h:"X-Account-Meta-Temp-URL-Key-2"`
49}
50
51// Update is a function that creates, updates, or deletes an account's metadata. To extract the
52// headers returned, call the ExtractHeaders method on the UpdateResult.
53func Update(c *gophercloud.ServiceClient, opts *UpdateOpts) UpdateResult {
54 var res UpdateResult
55 h := c.Provider.AuthenticatedHeaders()
56
57 if opts != nil {
58 headers, err := gophercloud.BuildHeaders(opts)
59 if err != nil {
60 res.Err = err
61 return res
62 }
63 for k, v := range headers {
64 h[k] = v
65 }
66
67 for k, v := range opts.Metadata {
68 h["X-Account-Meta-"+k] = v
69 }
70 }
71
72 resp, err := perigee.Request("POST", updateURL(c), perigee.Options{
73 MoreHeaders: h,
74 OkCodes: []int{204},
75 })
76 res.Resp = &resp.HttpResponse
77 res.Err = err
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020078 return res
Jon Perritt816d2a02014-03-11 20:49:46 -050079}