blob: 5837e0be2df4d8c92986b78e8afbcde6d64fc3ad [file] [log] [blame]
Jamie Hannaford93209fe2014-10-10 11:54:19 +02001package accounts
2
3import (
Jamie Hannaford93209fe2014-10-10 11:54:19 +02004 "strings"
Jon Perrittae06ab72014-11-06 18:18:55 -06005 "time"
Jamie Hannafordb2e129d2014-10-10 14:06:28 +02006
Jon Perrittae06ab72014-11-06 18:18:55 -06007 "github.com/mitchellh/mapstructure"
Ash Wilsonaf262872014-10-20 09:32:29 -04008 "github.com/rackspace/gophercloud"
Jamie Hannaford93209fe2014-10-10 11:54:19 +02009)
10
Jon Perrittae06ab72014-11-06 18:18:55 -060011// UpdateResult is returned from a call to the Update function.
12type UpdateResult struct {
13 gophercloud.HeaderResult
14}
15
16// UpdateHeader represents the headers returned in the response from an Update request.
17type UpdateHeader struct {
Jon Perritt8c31b2a2014-12-03 10:21:11 -070018 ContentLength string `mapstructure:"Content-Length"`
19 ContentType string `mapstructure:"Content-Type"`
20 Date time.Time `mapstructure:"-"`
21 TransID string `mapstructure:"X-Trans_ID"`
Jon Perrittae06ab72014-11-06 18:18:55 -060022}
23
24// Extract will return a struct of headers returned from a call to Get. To obtain
25// a map of headers, call the ExtractHeader method on the GetResult.
26func (ur UpdateResult) Extract() (UpdateHeader, error) {
27 var uh UpdateHeader
28 if ur.Err != nil {
29 return uh, ur.Err
30 }
31
Jon Perritt8c31b2a2014-12-03 10:21:11 -070032 if err := mapstructure.Decode(ur.Header, &uh); err != nil {
Jon Perrittae06ab72014-11-06 18:18:55 -060033 return uh, err
34 }
35
Jon Perritt8c31b2a2014-12-03 10:21:11 -070036 if date, ok := ur.Header["Date"]; ok && len(date) > 0 {
37 t, err := time.Parse(time.RFC1123, ur.Header["Date"][0])
38 if err != nil {
39 return uh, err
40 }
41 uh.Date = t
Jon Perrittae06ab72014-11-06 18:18:55 -060042 }
43
Jon Perrittae06ab72014-11-06 18:18:55 -060044 return uh, nil
45}
46
47// GetHeader represents the headers returned in the response from a Get request.
48type GetHeader struct {
Jon Perritt8c31b2a2014-12-03 10:21:11 -070049 BytesUsed int64 `mapstructure:"X-Account-Bytes-Used"`
50 ContainerCount int `mapstructure:"X-Account-Container-Count"`
51 ContentLength int64 `mapstructure:"Content-Length"`
52 ContentType string `mapstructure:"Content-Type"`
53 Date time.Time `mapstructure:"-"`
54 ObjectCount int64 `mapstructure:"X-Account-Object-Count"`
55 TransID string `mapstructure:"X-Trans-Id"`
Jon Perrittae06ab72014-11-06 18:18:55 -060056}
57
Ash Wilsonaf262872014-10-20 09:32:29 -040058// GetResult is returned from a call to the Get function.
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020059type GetResult struct {
Jon Perrittd50f93e2014-10-27 14:19:27 -050060 gophercloud.HeaderResult
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020061}
Jamie Hannaford93209fe2014-10-10 11:54:19 +020062
Jon Perrittae06ab72014-11-06 18:18:55 -060063// Extract will return a struct of headers returned from a call to Get. To obtain
64// a map of headers, call the ExtractHeader method on the GetResult.
65func (gr GetResult) Extract() (GetHeader, error) {
Jon Perrittae06ab72014-11-06 18:18:55 -060066 var gh GetHeader
Jon Perritt8c31b2a2014-12-03 10:21:11 -070067 if gr.Err != nil {
68 return gh, gr.Err
69 }
Jon Perrittae06ab72014-11-06 18:18:55 -060070
71 if err := mapstructure.Decode(gr.Header, &gh); err != nil {
72 return gh, err
73 }
74
Jon Perritt8314f4e2014-12-01 10:58:40 -070075 if date, ok := gr.Header["Date"]; ok && len(date) > 0 {
76 t, err := time.Parse(time.RFC1123, gr.Header["Date"][0])
77 if err != nil {
78 return gh, err
79 }
80 gh.Date = t
Jon Perrittae06ab72014-11-06 18:18:55 -060081 }
Jon Perrittae06ab72014-11-06 18:18:55 -060082
83 return gh, nil
84}
85
Jamie Hannaford93209fe2014-10-10 11:54:19 +020086// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
87// and returns the custom metatdata associated with the account.
Jon Perritt4a59d232014-10-09 20:21:31 -050088func (gr GetResult) ExtractMetadata() (map[string]string, error) {
89 if gr.Err != nil {
90 return nil, gr.Err
91 }
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020092
Jon Perritt4a59d232014-10-09 20:21:31 -050093 metadata := make(map[string]string)
Ash Wilson72e4d2c2014-10-20 10:27:30 -040094 for k, v := range gr.Header {
Jamie Hannaford93209fe2014-10-10 11:54:19 +020095 if strings.HasPrefix(k, "X-Account-Meta-") {
96 key := strings.TrimPrefix(k, "X-Account-Meta-")
97 metadata[key] = v[0]
98 }
99 }
Jon Perritt4a59d232014-10-09 20:21:31 -0500100 return metadata, nil
101}