blob: 9bc83404771fb48ba2f20f8ade044a0a64b60340 [file] [log] [blame]
Jamie Hannaford93209fe2014-10-10 11:54:19 +02001package accounts
2
3import (
jrperritt655245a2016-08-31 15:30:27 -05004 "encoding/json"
jrperritt655245a2016-08-31 15:30:27 -05005 "strconv"
Jamie Hannaford93209fe2014-10-10 11:54:19 +02006 "strings"
jrperritt98d01622017-01-12 14:24:42 -06007 "time"
Jamie Hannafordb2e129d2014-10-10 14:06:28 +02008
Jon Perritt27249f42016-02-18 10:35:59 -06009 "github.com/gophercloud/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 {
jrperritt98d01622017-01-12 14:24:42 -060019 ContentLength int64 `json:"-"`
20 ContentType string `json:"Content-Type"`
21 TransID string `json:"X-Trans-Id"`
22 Date time.Time `json:"-"`
jrperritt655245a2016-08-31 15:30:27 -050023}
24
jrperritt98d01622017-01-12 14:24:42 -060025func (r *UpdateHeader) UnmarshalJSON(b []byte) error {
jrperritt655245a2016-08-31 15:30:27 -050026 type tmp UpdateHeader
jrperritt98d01622017-01-12 14:24:42 -060027 var s struct {
jrperritt655245a2016-08-31 15:30:27 -050028 tmp
jrperritt98d01622017-01-12 14:24:42 -060029 ContentLength string `json:"Content-Length"`
30 Date gophercloud.JSONRFC1123 `json:"Date"`
jrperritt655245a2016-08-31 15:30:27 -050031 }
jrperritt98d01622017-01-12 14:24:42 -060032 err := json.Unmarshal(b, &s)
jrperritt655245a2016-08-31 15:30:27 -050033 if err != nil {
34 return err
35 }
36
jrperritt98d01622017-01-12 14:24:42 -060037 *r = UpdateHeader(s.tmp)
jrperritt655245a2016-08-31 15:30:27 -050038
jrperritt98d01622017-01-12 14:24:42 -060039 switch s.ContentLength {
jrperritt655245a2016-08-31 15:30:27 -050040 case "":
jrperritt98d01622017-01-12 14:24:42 -060041 r.ContentLength = 0
jrperritt655245a2016-08-31 15:30:27 -050042 default:
jrperritt98d01622017-01-12 14:24:42 -060043 r.ContentLength, err = strconv.ParseInt(s.ContentLength, 10, 64)
jrperritt655245a2016-08-31 15:30:27 -050044 if err != nil {
45 return err
46 }
47 }
48
jrperritt98d01622017-01-12 14:24:42 -060049 r.Date = time.Time(s.Date)
50
51 return err
Jon Perrittae06ab72014-11-06 18:18:55 -060052}
53
54// Extract will return a struct of headers returned from a call to Get. To obtain
55// a map of headers, call the ExtractHeader method on the GetResult.
jrperritt98d01622017-01-12 14:24:42 -060056func (r UpdateResult) Extract() (*UpdateHeader, error) {
57 var s *UpdateHeader
58 err := r.ExtractInto(&s)
59 return s, err
Jon Perrittae06ab72014-11-06 18:18:55 -060060}
61
62// GetHeader represents the headers returned in the response from a Get request.
63type GetHeader struct {
jrperritt98d01622017-01-12 14:24:42 -060064 BytesUsed int64 `json:"-"`
65 ContainerCount int64 `json:"-"`
66 ContentLength int64 `json:"-"`
67 ObjectCount int64 `json:"-"`
68 ContentType string `json:"Content-Type"`
69 TransID string `json:"X-Trans-Id"`
70 TempURLKey string `json:"X-Account-Meta-Temp-URL-Key"`
71 TempURLKey2 string `json:"X-Account-Meta-Temp-URL-Key-2"`
72 Date time.Time `json:"-"`
jrperritt655245a2016-08-31 15:30:27 -050073}
74
jrperritt98d01622017-01-12 14:24:42 -060075func (r *GetHeader) UnmarshalJSON(b []byte) error {
jrperritt655245a2016-08-31 15:30:27 -050076 type tmp GetHeader
jrperritt98d01622017-01-12 14:24:42 -060077 var s struct {
jrperritt655245a2016-08-31 15:30:27 -050078 tmp
79 BytesUsed string `json:"X-Account-Bytes-Used"`
80 ContentLength string `json:"Content-Length"`
81 ContainerCount string `json:"X-Account-Container-Count"`
82 ObjectCount string `json:"X-Account-Object-Count"`
jrperritt98d01622017-01-12 14:24:42 -060083 Date string `json:"Date"`
jrperritt655245a2016-08-31 15:30:27 -050084 }
jrperritt98d01622017-01-12 14:24:42 -060085 err := json.Unmarshal(b, &s)
jrperritt655245a2016-08-31 15:30:27 -050086 if err != nil {
87 return err
88 }
89
jrperritt98d01622017-01-12 14:24:42 -060090 *r = GetHeader(s.tmp)
jrperritt655245a2016-08-31 15:30:27 -050091
jrperritt98d01622017-01-12 14:24:42 -060092 switch s.BytesUsed {
jrperritt655245a2016-08-31 15:30:27 -050093 case "":
jrperritt98d01622017-01-12 14:24:42 -060094 r.BytesUsed = 0
jrperritt655245a2016-08-31 15:30:27 -050095 default:
jrperritt98d01622017-01-12 14:24:42 -060096 r.BytesUsed, err = strconv.ParseInt(s.BytesUsed, 10, 64)
jrperritt655245a2016-08-31 15:30:27 -050097 if err != nil {
98 return err
99 }
100 }
101
jrperritt98d01622017-01-12 14:24:42 -0600102 switch s.ContentLength {
jrperritt655245a2016-08-31 15:30:27 -0500103 case "":
jrperritt98d01622017-01-12 14:24:42 -0600104 r.ContentLength = 0
jrperritt655245a2016-08-31 15:30:27 -0500105 default:
jrperritt98d01622017-01-12 14:24:42 -0600106 r.ContentLength, err = strconv.ParseInt(s.ContentLength, 10, 64)
jrperritt655245a2016-08-31 15:30:27 -0500107 if err != nil {
108 return err
109 }
110 }
111
jrperritt98d01622017-01-12 14:24:42 -0600112 switch s.ObjectCount {
jrperritt655245a2016-08-31 15:30:27 -0500113 case "":
jrperritt98d01622017-01-12 14:24:42 -0600114 r.ObjectCount = 0
jrperritt655245a2016-08-31 15:30:27 -0500115 default:
jrperritt98d01622017-01-12 14:24:42 -0600116 r.ObjectCount, err = strconv.ParseInt(s.ObjectCount, 10, 64)
jrperritt655245a2016-08-31 15:30:27 -0500117 if err != nil {
118 return err
119 }
120 }
121
jrperritt98d01622017-01-12 14:24:42 -0600122 switch s.ContainerCount {
jrperritt655245a2016-08-31 15:30:27 -0500123 case "":
jrperritt98d01622017-01-12 14:24:42 -0600124 r.ContainerCount = 0
jrperritt655245a2016-08-31 15:30:27 -0500125 default:
jrperritt98d01622017-01-12 14:24:42 -0600126 r.ContainerCount, err = strconv.ParseInt(s.ContainerCount, 10, 64)
jrperritt655245a2016-08-31 15:30:27 -0500127 if err != nil {
128 return err
129 }
130 }
131
jrperritt98d01622017-01-12 14:24:42 -0600132 if s.Date != "" {
133 r.Date, err = time.Parse(time.RFC1123, s.Date)
134 }
135
136 return err
Jon Perrittae06ab72014-11-06 18:18:55 -0600137}
138
Ash Wilsonaf262872014-10-20 09:32:29 -0400139// GetResult is returned from a call to the Get function.
Jamie Hannafordb2e129d2014-10-10 14:06:28 +0200140type GetResult struct {
Jon Perrittd50f93e2014-10-27 14:19:27 -0500141 gophercloud.HeaderResult
Jamie Hannafordb2e129d2014-10-10 14:06:28 +0200142}
Jamie Hannaford93209fe2014-10-10 11:54:19 +0200143
Jon Perrittae06ab72014-11-06 18:18:55 -0600144// Extract will return a struct of headers returned from a call to Get. To obtain
145// a map of headers, call the ExtractHeader method on the GetResult.
Jon Perritt31b66462016-02-25 22:25:30 -0600146func (r GetResult) Extract() (*GetHeader, error) {
147 var s *GetHeader
148 err := r.ExtractInto(&s)
149 return s, err
Jon Perrittae06ab72014-11-06 18:18:55 -0600150}
151
jrperritt98d01622017-01-12 14:24:42 -0600152// ExtractMetadata is a function that takes a GetResult (of type *htts.Response)
Jamie Hannaford93209fe2014-10-10 11:54:19 +0200153// and returns the custom metatdata associated with the account.
Jon Perritt31b66462016-02-25 22:25:30 -0600154func (r GetResult) ExtractMetadata() (map[string]string, error) {
155 if r.Err != nil {
156 return nil, r.Err
Jon Perritt4a59d232014-10-09 20:21:31 -0500157 }
Jamie Hannafordb2e129d2014-10-10 14:06:28 +0200158
Jon Perritt4a59d232014-10-09 20:21:31 -0500159 metadata := make(map[string]string)
Jon Perritt31b66462016-02-25 22:25:30 -0600160 for k, v := range r.Header {
Jamie Hannaford93209fe2014-10-10 11:54:19 +0200161 if strings.HasPrefix(k, "X-Account-Meta-") {
162 key := strings.TrimPrefix(k, "X-Account-Meta-")
163 metadata[key] = v[0]
164 }
165 }
Jon Perritt4a59d232014-10-09 20:21:31 -0500166 return metadata, nil
167}