blob: 4c27ec6a892f9c9c22e70d101cfa4b97819094a4 [file] [log] [blame]
Jon Perritt8c93a302014-09-28 22:35:57 -05001package containers
2
3import (
4 "fmt"
Jon Perritt8c93a302014-09-28 22:35:57 -05005 "strings"
Jon Perritt8314f4e2014-12-01 10:58:40 -07006 "time"
Jon Perritt5db08922014-09-30 21:32:48 -05007
Ash Wilsonaf262872014-10-20 09:32:29 -04008 "github.com/rackspace/gophercloud"
Jon Perritt5db08922014-09-30 21:32:48 -05009 "github.com/rackspace/gophercloud/pagination"
Jon Perrittb3461402014-10-09 21:36:17 -050010
11 "github.com/mitchellh/mapstructure"
Jon Perritt8c93a302014-09-28 22:35:57 -050012)
13
Jamie Hannaford4ff79962014-10-06 16:28:23 +020014// Container represents a container resource.
Jon Perritt8aa40262014-09-29 15:41:32 -050015type Container struct {
Jamie Hannaford4ff79962014-10-06 16:28:23 +020016 // The total number of bytes stored in the container.
17 Bytes int `json:"bytes" mapstructure:"bytes"`
18
19 // The total number of objects stored in the container.
20 Count int `json:"count" mapstructure:"count"`
21
22 // The name of the container.
23 Name string `json:"name" mapstructure:"name"`
Jon Perritt8aa40262014-09-29 15:41:32 -050024}
Jon Perritt8c93a302014-09-28 22:35:57 -050025
Jamie Hannaford4ff79962014-10-06 16:28:23 +020026// ContainerPage is the page returned by a pager when traversing over a
27// collection of containers.
Jon Perritt8c93a302014-09-28 22:35:57 -050028type ContainerPage struct {
29 pagination.MarkerPageBase
30}
31
32// IsEmpty returns true if a ListResult contains no container names.
33func (r ContainerPage) IsEmpty() (bool, error) {
34 names, err := ExtractNames(r)
35 if err != nil {
36 return true, err
37 }
38 return len(names) == 0, nil
39}
40
41// LastMarker returns the last container name in a ListResult.
42func (r ContainerPage) LastMarker() (string, error) {
43 names, err := ExtractNames(r)
44 if err != nil {
45 return "", err
46 }
47 if len(names) == 0 {
48 return "", nil
49 }
50 return names[len(names)-1], nil
51}
52
53// ExtractInfo is a function that takes a ListResult and returns the containers' information.
54func ExtractInfo(page pagination.Page) ([]Container, error) {
55 untyped := page.(ContainerPage).Body.([]interface{})
56 results := make([]Container, len(untyped))
57 for index, each := range untyped {
Jon Perritt8aa40262014-09-29 15:41:32 -050058 container := each.(map[string]interface{})
Jon Perrittfdac6e52014-09-29 19:43:45 -050059 err := mapstructure.Decode(container, &results[index])
Jon Perritt8aa40262014-09-29 15:41:32 -050060 if err != nil {
61 return results, err
62 }
Jon Perritt8c93a302014-09-28 22:35:57 -050063 }
64 return results, nil
65}
66
67// ExtractNames is a function that takes a ListResult and returns the containers' names.
68func ExtractNames(page pagination.Page) ([]string, error) {
69 casted := page.(ContainerPage)
Ash Wilson72e4d2c2014-10-20 10:27:30 -040070 ct := casted.Header.Get("Content-Type")
Jon Perritt8c93a302014-09-28 22:35:57 -050071
72 switch {
73 case strings.HasPrefix(ct, "application/json"):
74 parsed, err := ExtractInfo(page)
75 if err != nil {
76 return nil, err
77 }
78
79 names := make([]string, 0, len(parsed))
80 for _, container := range parsed {
Jon Perritt8aa40262014-09-29 15:41:32 -050081 names = append(names, container.Name)
Jon Perritt8c93a302014-09-28 22:35:57 -050082 }
83 return names, nil
84 case strings.HasPrefix(ct, "text/plain"):
85 names := make([]string, 0, 50)
86
87 body := string(page.(ContainerPage).Body.([]uint8))
88 for _, name := range strings.Split(body, "\n") {
89 if len(name) > 0 {
90 names = append(names, name)
91 }
92 }
93
94 return names, nil
95 default:
96 return nil, fmt.Errorf("Cannot extract names from response with content-type: [%s]", ct)
97 }
98}
99
Jon Perritt8314f4e2014-12-01 10:58:40 -0700100// GetHeader represents the headers returned in the response from a Get request.
101type GetHeader struct {
Jon Perritt8c31b2a2014-12-03 10:21:11 -0700102 BytesUsed int64 `mapstructure:"X-Account-Bytes-Used"`
103 ContentLength int64 `mapstructure:"Content-Length"`
104 ContentType string `mapstructure:"Content-Type"`
105 Date time.Time `mapstructure:"-"`
106 ObjectCount int64 `mapstructure:"X-Account-Object-Count"`
107 TransID string `mapstructure:"X-Trans-Id"`
Jon Perritt8314f4e2014-12-01 10:58:40 -0700108}
109
Jon Perritt5db08922014-09-30 21:32:48 -0500110// GetResult represents the result of a get operation.
111type GetResult struct {
Jon Perrittd50f93e2014-10-27 14:19:27 -0500112 gophercloud.HeaderResult
Jon Perritt5db08922014-09-30 21:32:48 -0500113}
114
Jon Perritt8314f4e2014-12-01 10:58:40 -0700115// Extract will return a struct of headers returned from a call to Get. To obtain
116// a map of headers, call the ExtractHeader method on the GetResult.
117func (gr GetResult) Extract() (GetHeader, error) {
118 var gh GetHeader
Jon Perritt8c31b2a2014-12-03 10:21:11 -0700119 if gr.Err != nil {
120 return gh, gr.Err
121 }
Jon Perritt8314f4e2014-12-01 10:58:40 -0700122
123 if err := mapstructure.Decode(gr.Header, &gh); err != nil {
124 return gh, err
125 }
126
127 if date, ok := gr.Header["Date"]; ok && len(date) > 0 {
128 t, err := time.Parse(time.RFC1123, gr.Header["Date"][0])
129 if err != nil {
130 return gh, err
131 }
132 gh.Date = t
133 }
134
135 return gh, nil
136}
137
Jon Perritt8c93a302014-09-28 22:35:57 -0500138// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
139// and returns the custom metadata associated with the container.
140func (gr GetResult) ExtractMetadata() (map[string]string, error) {
141 if gr.Err != nil {
142 return nil, gr.Err
143 }
144 metadata := make(map[string]string)
Ash Wilson72e4d2c2014-10-20 10:27:30 -0400145 for k, v := range gr.Header {
Jon Perritt8c93a302014-09-28 22:35:57 -0500146 if strings.HasPrefix(k, "X-Container-Meta-") {
147 key := strings.TrimPrefix(k, "X-Container-Meta-")
148 metadata[key] = v[0]
149 }
150 }
151 return metadata, nil
152}
Jon Perritt5db08922014-09-30 21:32:48 -0500153
Jon Perritt8314f4e2014-12-01 10:58:40 -0700154// CreateHeader represents the headers returned in the response from a Create request.
155type CreateHeader struct {
Jon Perritt8c31b2a2014-12-03 10:21:11 -0700156 ContentLength int64 `mapstructure:"Content-Length"`
157 ContentType string `mapstructure:"Content-Type"`
158 Date time.Time `mapstructure:"-"`
159 TransID string `mapstructure:"X-Trans-Id"`
Jon Perritt8314f4e2014-12-01 10:58:40 -0700160}
161
Jamie Hannaford22ec4792014-10-07 10:07:41 +0200162// CreateResult represents the result of a create operation. To extract the
Jon Perritt9856a342014-10-27 13:44:06 -0500163// the headers from the HTTP response, you can invoke the 'ExtractHeader'
Jamie Hannaford22ec4792014-10-07 10:07:41 +0200164// method on the result struct.
Jon Perritt5db08922014-09-30 21:32:48 -0500165type CreateResult struct {
Jon Perrittd50f93e2014-10-27 14:19:27 -0500166 gophercloud.HeaderResult
Jon Perritt5db08922014-09-30 21:32:48 -0500167}
168
Jon Perritt8314f4e2014-12-01 10:58:40 -0700169// Extract will return a struct of headers returned from a call to Create. To obtain
170// a map of headers, call the ExtractHeader method on the CreateResult.
171func (cr CreateResult) Extract() (CreateHeader, error) {
172 var ch CreateHeader
Jon Perritt8c31b2a2014-12-03 10:21:11 -0700173 if cr.Err != nil {
174 return ch, cr.Err
175 }
Jon Perritt8314f4e2014-12-01 10:58:40 -0700176
177 if err := mapstructure.Decode(cr.Header, &ch); err != nil {
178 return ch, err
179 }
180
181 if date, ok := cr.Header["Date"]; ok && len(date) > 0 {
182 t, err := time.Parse(time.RFC1123, cr.Header["Date"][0])
183 if err != nil {
184 return ch, err
185 }
186 ch.Date = t
187 }
188
189 return ch, nil
190}
191
192// UpdateHeader represents the headers returned in the response from a Update request.
193type UpdateHeader struct {
Jon Perritt8c31b2a2014-12-03 10:21:11 -0700194 ContentLength int64 `mapstructure:"Content-Length"`
195 ContentType string `mapstructure:"Content-Type"`
196 Date time.Time `mapstructure:"-"`
197 TransID string `mapstructure:"X-Trans-Id"`
Jon Perritt8314f4e2014-12-01 10:58:40 -0700198}
199
Jamie Hannaford22ec4792014-10-07 10:07:41 +0200200// UpdateResult represents the result of an update operation. To extract the
Jon Perritt9856a342014-10-27 13:44:06 -0500201// the headers from the HTTP response, you can invoke the 'ExtractHeader'
Jamie Hannaford22ec4792014-10-07 10:07:41 +0200202// method on the result struct.
Jon Perritt5db08922014-09-30 21:32:48 -0500203type UpdateResult struct {
Jon Perrittd50f93e2014-10-27 14:19:27 -0500204 gophercloud.HeaderResult
Jon Perritt5db08922014-09-30 21:32:48 -0500205}
206
Jon Perritt8314f4e2014-12-01 10:58:40 -0700207// Extract will return a struct of headers returned from a call to Update. To obtain
208// a map of headers, call the ExtractHeader method on the UpdateResult.
209func (ur UpdateResult) Extract() (UpdateHeader, error) {
210 var uh UpdateHeader
Jon Perritt8c31b2a2014-12-03 10:21:11 -0700211 if ur.Err != nil {
212 return uh, ur.Err
213 }
Jon Perritt8314f4e2014-12-01 10:58:40 -0700214
215 if err := mapstructure.Decode(ur.Header, &uh); err != nil {
216 return uh, err
217 }
218
219 if date, ok := ur.Header["Date"]; ok && len(date) > 0 {
220 t, err := time.Parse(time.RFC1123, ur.Header["Date"][0])
221 if err != nil {
222 return uh, err
223 }
224 uh.Date = t
225 }
226
227 return uh, nil
228}
229
230// DeleteHeader represents the headers returned in the response from a Delete request.
231type DeleteHeader struct {
Jon Perritt8c31b2a2014-12-03 10:21:11 -0700232 ContentLength int64 `mapstructure:"Content-Length"`
233 ContentType string `mapstructure:"Content-Type"`
234 Date time.Time `mapstructure:"-"`
235 TransID string `mapstructure:"X-Trans-Id"`
Jon Perritt8314f4e2014-12-01 10:58:40 -0700236}
237
Jamie Hannaford22ec4792014-10-07 10:07:41 +0200238// DeleteResult represents the result of a delete operation. To extract the
Jon Perritt9856a342014-10-27 13:44:06 -0500239// the headers from the HTTP response, you can invoke the 'ExtractHeader'
Jamie Hannaford22ec4792014-10-07 10:07:41 +0200240// method on the result struct.
Jon Perritt5db08922014-09-30 21:32:48 -0500241type DeleteResult struct {
Jon Perrittd50f93e2014-10-27 14:19:27 -0500242 gophercloud.HeaderResult
Jon Perritt5db08922014-09-30 21:32:48 -0500243}
Jon Perritt8314f4e2014-12-01 10:58:40 -0700244
245// Extract will return a struct of headers returned from a call to Delete. To obtain
246// a map of headers, call the ExtractHeader method on the DeleteResult.
247func (dr DeleteResult) Extract() (DeleteHeader, error) {
248 var dh DeleteHeader
Jon Perritt8c31b2a2014-12-03 10:21:11 -0700249 if dr.Err != nil {
250 return dh, dr.Err
251 }
Jon Perritt8314f4e2014-12-01 10:58:40 -0700252
253 if err := mapstructure.Decode(dr.Header, &dh); err != nil {
254 return dh, err
255 }
256
257 if date, ok := dr.Header["Date"]; ok && len(date) > 0 {
258 t, err := time.Parse(time.RFC1123, dr.Header["Date"][0])
259 if err != nil {
260 return dh, err
261 }
262 dh.Date = t
263 }
264
265 return dh, nil
266}