blob: 4797e9641941d07a888b905cf6b8d855e687061d [file] [log] [blame]
Jamie Hannaford93209fe2014-10-10 11:54:19 +02001package accounts
2
3import (
Jon Perrittae06ab72014-11-06 18:18:55 -06004 "encoding/json"
5 "fmt"
Jamie Hannaford93209fe2014-10-10 11:54:19 +02006 "strings"
Jon Perrittae06ab72014-11-06 18:18:55 -06007 "time"
Jamie Hannafordb2e129d2014-10-10 14:06:28 +02008
Jon Perrittae06ab72014-11-06 18:18:55 -06009 "github.com/mitchellh/mapstructure"
Ash Wilsonaf262872014-10-20 09:32:29 -040010 "github.com/rackspace/gophercloud"
Jamie Hannaford93209fe2014-10-10 11:54:19 +020011)
12
Jon Perrittae06ab72014-11-06 18:18:55 -060013// UpdateResult is returned from a call to the Update function.
14type UpdateResult struct {
15 gophercloud.HeaderResult
16}
17
18// UpdateHeader represents the headers returned in the response from an Update request.
19type UpdateHeader struct {
20 ContentLength string `json:"Content-Length"`
21 ContentType []string `json:"Content-Type"`
22 Date time.Time `json:"-"`
23 TransID string `json:"X-Trans_ID"`
24}
25
26// Extract will return a struct of headers returned from a call to Get. To obtain
27// a map of headers, call the ExtractHeader method on the GetResult.
28func (ur UpdateResult) Extract() (UpdateHeader, error) {
29 var uh UpdateHeader
30 if ur.Err != nil {
31 return uh, ur.Err
32 }
33
34 b, err := json.Marshal(ur.Header)
35 if err != nil {
36 return uh, err
37 }
38
39 err = json.Unmarshal(b, &uh)
40 if err != nil {
41 return uh, err
42 }
43
44 date, err := time.Parse(time.RFC1123, ur.Header["Date"][0])
45 if err != nil {
46 return uh, err
47 }
48
49 uh.Date = date
50
51 return uh, nil
52}
53
54// GetHeader represents the headers returned in the response from a Get request.
55type GetHeader struct {
56 BytesUsed int64 `json:"X-Account-Bytes-Used"`
57 ContainerCount int `json:"X-Accound-Container-Count"`
58 ContentLength int64 `json:"Content-Length"`
59 ContentType string `json:"Content-Type"`
60 Date time.Time `mapstructure:"-" json:"-"`
61 ObjectCount int64 `json:"X-Account-Object-Count"`
62 TransID string `json:"X-Trans-Id"`
63}
64
Ash Wilsonaf262872014-10-20 09:32:29 -040065// GetResult is returned from a call to the Get function.
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020066type GetResult struct {
Jon Perrittd50f93e2014-10-27 14:19:27 -050067 gophercloud.HeaderResult
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020068}
Jamie Hannaford93209fe2014-10-10 11:54:19 +020069
Jon Perrittae06ab72014-11-06 18:18:55 -060070// Extract will return a struct of headers returned from a call to Get. To obtain
71// a map of headers, call the ExtractHeader method on the GetResult.
72func (gr GetResult) Extract() (GetHeader, error) {
73 fmt.Printf("raw response header: %+v\n", gr.Header)
74
75 var gh GetHeader
76
77 if err := mapstructure.Decode(gr.Header, &gh); err != nil {
78 return gh, err
79 }
80
81 t, err := time.Parse(time.RFC1123, gr.Header["Date"][0])
82 if err != nil {
83 return gh, err
84 }
85 gh.Date = t
86
87 return gh, nil
88}
89
Jamie Hannaford93209fe2014-10-10 11:54:19 +020090// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
91// and returns the custom metatdata associated with the account.
Jon Perritt4a59d232014-10-09 20:21:31 -050092func (gr GetResult) ExtractMetadata() (map[string]string, error) {
93 if gr.Err != nil {
94 return nil, gr.Err
95 }
Jamie Hannafordb2e129d2014-10-10 14:06:28 +020096
Jon Perritt4a59d232014-10-09 20:21:31 -050097 metadata := make(map[string]string)
Ash Wilson72e4d2c2014-10-20 10:27:30 -040098 for k, v := range gr.Header {
Jamie Hannaford93209fe2014-10-10 11:54:19 +020099 if strings.HasPrefix(k, "X-Account-Meta-") {
100 key := strings.TrimPrefix(k, "X-Account-Meta-")
101 metadata[key] = v[0]
102 }
103 }
Jon Perritt4a59d232014-10-09 20:21:31 -0500104 return metadata, nil
105}