blob: 6ab1a230617451ecdb5854e5eec2fe0bf03a227f [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
Ash Wilsonaf262872014-10-20 09:32:29 -04007 "github.com/rackspace/gophercloud"
Jamie Hannaford93209fe2014-10-10 11:54:19 +02008)
9
Jon Perrittae06ab72014-11-06 18:18:55 -060010// UpdateResult is returned from a call to the Update function.
11type UpdateResult struct {
12 gophercloud.HeaderResult
13}
14
15// UpdateHeader represents the headers returned in the response from an Update request.
16type UpdateHeader struct {
Jon Perritt8c31b2a2014-12-03 10:21:11 -070017 ContentLength string `mapstructure:"Content-Length"`
18 ContentType string `mapstructure:"Content-Type"`
19 Date time.Time `mapstructure:"-"`
Jon Perritt60af2f62015-02-02 10:40:45 -070020 TransID string `mapstructure:"X-Trans-Id"`
Jon Perrittae06ab72014-11-06 18:18:55 -060021}
22
23// Extract will return a struct of headers returned from a call to Get. To obtain
24// a map of headers, call the ExtractHeader method on the GetResult.
25func (ur UpdateResult) Extract() (UpdateHeader, error) {
26 var uh UpdateHeader
27 if ur.Err != nil {
28 return uh, ur.Err
29 }
30
Jon Perritt63e7a482014-12-04 09:47:23 -070031 if err := gophercloud.DecodeHeader(ur.Header, &uh); err != nil {
Jon Perrittae06ab72014-11-06 18:18:55 -060032 return uh, err
33 }
34
Jon Perritt8c31b2a2014-12-03 10:21:11 -070035 if date, ok := ur.Header["Date"]; ok && len(date) > 0 {
36 t, err := time.Parse(time.RFC1123, ur.Header["Date"][0])
37 if err != nil {
38 return uh, err
39 }
40 uh.Date = t
Jon Perrittae06ab72014-11-06 18:18:55 -060041 }
42
Jon Perrittae06ab72014-11-06 18:18:55 -060043 return uh, nil
44}
45
46// GetHeader represents the headers returned in the response from a Get request.
47type GetHeader struct {
Jon Perritt8c31b2a2014-12-03 10:21:11 -070048 BytesUsed int64 `mapstructure:"X-Account-Bytes-Used"`
49 ContainerCount int `mapstructure:"X-Account-Container-Count"`
50 ContentLength int64 `mapstructure:"Content-Length"`
51 ContentType string `mapstructure:"Content-Type"`
52 Date time.Time `mapstructure:"-"`
53 ObjectCount int64 `mapstructure:"X-Account-Object-Count"`
54 TransID string `mapstructure:"X-Trans-Id"`
Jon Perritt3828d5a2015-02-01 17:01:59 -070055 TempURLKey string `mapstructure:"X-Account-Meta-Temp-URL-Key"`
56 TempURLKey2 string `mapstructure:"X-Account-Meta-Temp-URL-Key-2"`
Jon Perrittae06ab72014-11-06 18:18:55 -060057}
58
Ash Wilsonaf262872014-10-20 09:32:29 -040059// GetResult is returned from a call to the Get function.
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020060type GetResult struct {
Jon Perrittd50f93e2014-10-27 14:19:27 -050061 gophercloud.HeaderResult
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020062}
Jamie Hannaford93209fe2014-10-10 11:54:19 +020063
Jon Perrittae06ab72014-11-06 18:18:55 -060064// Extract will return a struct of headers returned from a call to Get. To obtain
65// a map of headers, call the ExtractHeader method on the GetResult.
66func (gr GetResult) Extract() (GetHeader, error) {
Jon Perrittae06ab72014-11-06 18:18:55 -060067 var gh GetHeader
Jon Perritt8c31b2a2014-12-03 10:21:11 -070068 if gr.Err != nil {
69 return gh, gr.Err
70 }
Jon Perrittae06ab72014-11-06 18:18:55 -060071
Jon Perritt63e7a482014-12-04 09:47:23 -070072 if err := gophercloud.DecodeHeader(gr.Header, &gh); err != nil {
Jon Perrittae06ab72014-11-06 18:18:55 -060073 return gh, err
74 }
75
Jon Perritt8314f4e2014-12-01 10:58:40 -070076 if date, ok := gr.Header["Date"]; ok && len(date) > 0 {
77 t, err := time.Parse(time.RFC1123, gr.Header["Date"][0])
78 if err != nil {
79 return gh, err
80 }
81 gh.Date = t
Jon Perrittae06ab72014-11-06 18:18:55 -060082 }
Jon Perrittae06ab72014-11-06 18:18:55 -060083
84 return gh, nil
85}
86
Jamie Hannaford93209fe2014-10-10 11:54:19 +020087// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
88// and returns the custom metatdata associated with the account.
Jon Perritt4a59d232014-10-09 20:21:31 -050089func (gr GetResult) ExtractMetadata() (map[string]string, error) {
90 if gr.Err != nil {
91 return nil, gr.Err
92 }
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020093
Jon Perritt4a59d232014-10-09 20:21:31 -050094 metadata := make(map[string]string)
Ash Wilson72e4d2c2014-10-20 10:27:30 -040095 for k, v := range gr.Header {
Jamie Hannaford93209fe2014-10-10 11:54:19 +020096 if strings.HasPrefix(k, "X-Account-Meta-") {
97 key := strings.TrimPrefix(k, "X-Account-Meta-")
98 metadata[key] = v[0]
99 }
100 }
Jon Perritt4a59d232014-10-09 20:21:31 -0500101 return metadata, nil
102}