blob: 63bb134ccd6067cf7443685f51acae23369a8a00 [file] [log] [blame]
Jamie Hannaford93209fe2014-10-10 11:54:19 +02001package accounts
2
3import (
Jon Perrittae06ab72014-11-06 18:18:55 -06004 "encoding/json"
Jamie Hannaford93209fe2014-10-10 11:54:19 +02005 "strings"
Jon Perrittae06ab72014-11-06 18:18:55 -06006 "time"
Jamie Hannafordb2e129d2014-10-10 14:06:28 +02007
Jon Perrittae06ab72014-11-06 18:18:55 -06008 "github.com/mitchellh/mapstructure"
Ash Wilsonaf262872014-10-20 09:32:29 -04009 "github.com/rackspace/gophercloud"
Jamie Hannaford93209fe2014-10-10 11:54:19 +020010)
11
Jon Perrittae06ab72014-11-06 18:18:55 -060012// UpdateResult is returned from a call to the Update function.
13type UpdateResult struct {
14 gophercloud.HeaderResult
15}
16
17// UpdateHeader represents the headers returned in the response from an Update request.
18type UpdateHeader struct {
19 ContentLength string `json:"Content-Length"`
20 ContentType []string `json:"Content-Type"`
21 Date time.Time `json:"-"`
22 TransID string `json:"X-Trans_ID"`
23}
24
25// Extract will return a struct of headers returned from a call to Get. To obtain
26// a map of headers, call the ExtractHeader method on the GetResult.
27func (ur UpdateResult) Extract() (UpdateHeader, error) {
28 var uh UpdateHeader
29 if ur.Err != nil {
30 return uh, ur.Err
31 }
32
33 b, err := json.Marshal(ur.Header)
34 if err != nil {
35 return uh, err
36 }
37
38 err = json.Unmarshal(b, &uh)
39 if err != nil {
40 return uh, err
41 }
42
43 date, err := time.Parse(time.RFC1123, ur.Header["Date"][0])
44 if err != nil {
45 return uh, err
46 }
47
48 uh.Date = date
49
50 return uh, nil
51}
52
53// GetHeader represents the headers returned in the response from a Get request.
54type GetHeader struct {
55 BytesUsed int64 `json:"X-Account-Bytes-Used"`
Jon Perritt8314f4e2014-12-01 10:58:40 -070056 ContainerCount int `json:"X-Account-Container-Count"`
Jon Perrittae06ab72014-11-06 18:18:55 -060057 ContentLength int64 `json:"Content-Length"`
58 ContentType string `json:"Content-Type"`
59 Date time.Time `mapstructure:"-" json:"-"`
60 ObjectCount int64 `json:"X-Account-Object-Count"`
61 TransID string `json:"X-Trans-Id"`
62}
63
Ash Wilsonaf262872014-10-20 09:32:29 -040064// GetResult is returned from a call to the Get function.
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020065type GetResult struct {
Jon Perrittd50f93e2014-10-27 14:19:27 -050066 gophercloud.HeaderResult
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020067}
Jamie Hannaford93209fe2014-10-10 11:54:19 +020068
Jon Perrittae06ab72014-11-06 18:18:55 -060069// Extract will return a struct of headers returned from a call to Get. To obtain
70// a map of headers, call the ExtractHeader method on the GetResult.
71func (gr GetResult) Extract() (GetHeader, error) {
Jon Perrittae06ab72014-11-06 18:18:55 -060072 var gh GetHeader
73
74 if err := mapstructure.Decode(gr.Header, &gh); err != nil {
75 return gh, err
76 }
77
Jon Perritt8314f4e2014-12-01 10:58:40 -070078 if date, ok := gr.Header["Date"]; ok && len(date) > 0 {
79 t, err := time.Parse(time.RFC1123, gr.Header["Date"][0])
80 if err != nil {
81 return gh, err
82 }
83 gh.Date = t
Jon Perrittae06ab72014-11-06 18:18:55 -060084 }
Jon Perrittae06ab72014-11-06 18:18:55 -060085
86 return gh, nil
87}
88
Jamie Hannaford93209fe2014-10-10 11:54:19 +020089// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
90// and returns the custom metatdata associated with the account.
Jon Perritt4a59d232014-10-09 20:21:31 -050091func (gr GetResult) ExtractMetadata() (map[string]string, error) {
92 if gr.Err != nil {
93 return nil, gr.Err
94 }
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020095
Jon Perritt4a59d232014-10-09 20:21:31 -050096 metadata := make(map[string]string)
Ash Wilson72e4d2c2014-10-20 10:27:30 -040097 for k, v := range gr.Header {
Jamie Hannaford93209fe2014-10-10 11:54:19 +020098 if strings.HasPrefix(k, "X-Account-Meta-") {
99 key := strings.TrimPrefix(k, "X-Account-Meta-")
100 metadata[key] = v[0]
101 }
102 }
Jon Perritt4a59d232014-10-09 20:21:31 -0500103 return metadata, nil
104}