blob: be96fca2cc3c2975b0687b0deace3c07a84fbbb9 [file] [log] [blame]
Jon Perritt8c93a302014-09-28 22:35:57 -05001package containers
2
3import (
4 "fmt"
Jon Perritt8c93a302014-09-28 22:35:57 -05005 "net/http"
6 "strings"
Jon Perritt5db08922014-09-30 21:32:48 -05007
8 "github.com/mitchellh/mapstructure"
9 "github.com/rackspace/gophercloud/pagination"
Jon Perritt8c93a302014-09-28 22:35:57 -050010)
11
Jamie Hannaford4ff79962014-10-06 16:28:23 +020012// Container represents a container resource.
Jon Perritt8aa40262014-09-29 15:41:32 -050013type Container struct {
Jamie Hannaford4ff79962014-10-06 16:28:23 +020014 // The total number of bytes stored in the container.
15 Bytes int `json:"bytes" mapstructure:"bytes"`
16
17 // The total number of objects stored in the container.
18 Count int `json:"count" mapstructure:"count"`
19
20 // The name of the container.
21 Name string `json:"name" mapstructure:"name"`
Jon Perritt8aa40262014-09-29 15:41:32 -050022}
Jon Perritt8c93a302014-09-28 22:35:57 -050023
Jamie Hannaford4ff79962014-10-06 16:28:23 +020024// ContainerPage is the page returned by a pager when traversing over a
25// collection of containers.
Jon Perritt8c93a302014-09-28 22:35:57 -050026type ContainerPage struct {
27 pagination.MarkerPageBase
28}
29
30// IsEmpty returns true if a ListResult contains no container names.
31func (r ContainerPage) IsEmpty() (bool, error) {
32 names, err := ExtractNames(r)
33 if err != nil {
34 return true, err
35 }
36 return len(names) == 0, nil
37}
38
39// LastMarker returns the last container name in a ListResult.
40func (r ContainerPage) LastMarker() (string, error) {
41 names, err := ExtractNames(r)
42 if err != nil {
43 return "", err
44 }
45 if len(names) == 0 {
46 return "", nil
47 }
48 return names[len(names)-1], nil
49}
50
51// ExtractInfo is a function that takes a ListResult and returns the containers' information.
52func ExtractInfo(page pagination.Page) ([]Container, error) {
53 untyped := page.(ContainerPage).Body.([]interface{})
54 results := make([]Container, len(untyped))
55 for index, each := range untyped {
Jon Perritt8aa40262014-09-29 15:41:32 -050056 container := each.(map[string]interface{})
Jon Perrittfdac6e52014-09-29 19:43:45 -050057 err := mapstructure.Decode(container, &results[index])
Jon Perritt8aa40262014-09-29 15:41:32 -050058 if err != nil {
59 return results, err
60 }
Jon Perritt8c93a302014-09-28 22:35:57 -050061 }
62 return results, nil
63}
64
65// ExtractNames is a function that takes a ListResult and returns the containers' names.
66func ExtractNames(page pagination.Page) ([]string, error) {
67 casted := page.(ContainerPage)
68 ct := casted.Header.Get("Content-Type")
69
70 switch {
71 case strings.HasPrefix(ct, "application/json"):
72 parsed, err := ExtractInfo(page)
73 if err != nil {
74 return nil, err
75 }
76
77 names := make([]string, 0, len(parsed))
78 for _, container := range parsed {
Jon Perritt8aa40262014-09-29 15:41:32 -050079 names = append(names, container.Name)
Jon Perritt8c93a302014-09-28 22:35:57 -050080 }
81 return names, nil
82 case strings.HasPrefix(ct, "text/plain"):
83 names := make([]string, 0, 50)
84
85 body := string(page.(ContainerPage).Body.([]uint8))
86 for _, name := range strings.Split(body, "\n") {
87 if len(name) > 0 {
88 names = append(names, name)
89 }
90 }
91
92 return names, nil
93 default:
94 return nil, fmt.Errorf("Cannot extract names from response with content-type: [%s]", ct)
95 }
96}
97
Jon Perritt5db08922014-09-30 21:32:48 -050098// GetResult represents the result of a get operation.
99type GetResult struct {
100 Resp *http.Response
101 Err error
102}
103
Jon Perritt8c93a302014-09-28 22:35:57 -0500104// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
105// and returns the custom metadata associated with the container.
106func (gr GetResult) ExtractMetadata() (map[string]string, error) {
107 if gr.Err != nil {
108 return nil, gr.Err
109 }
110 metadata := make(map[string]string)
111 for k, v := range gr.Resp.Header {
112 if strings.HasPrefix(k, "X-Container-Meta-") {
113 key := strings.TrimPrefix(k, "X-Container-Meta-")
114 metadata[key] = v[0]
115 }
116 }
117 return metadata, nil
118}
Jon Perritt5db08922014-09-30 21:32:48 -0500119
120type commonResult struct {
121 Resp *http.Response
122 Err error
123}
124
125func (cr commonResult) ExtractHeaders() (http.Header, error) {
126 var headers http.Header
127 if cr.Err != nil {
128 return headers, cr.Err
129 }
130
131 return cr.Resp.Header, nil
132}
133
Jamie Hannaford22ec4792014-10-07 10:07:41 +0200134// CreateResult represents the result of a create operation. To extract the
135// the headers from the HTTP response, you can invoke the 'ExtractHeaders'
136// method on the result struct.
Jon Perritt5db08922014-09-30 21:32:48 -0500137type CreateResult struct {
138 commonResult
139}
140
Jamie Hannaford22ec4792014-10-07 10:07:41 +0200141// UpdateResult represents the result of an update operation. To extract the
142// the headers from the HTTP response, you can invoke the 'ExtractHeaders'
143// method on the result struct.
Jon Perritt5db08922014-09-30 21:32:48 -0500144type UpdateResult struct {
145 commonResult
146}
147
Jamie Hannaford22ec4792014-10-07 10:07:41 +0200148// DeleteResult represents the result of a delete operation. To extract the
149// the headers from the HTTP response, you can invoke the 'ExtractHeaders'
150// method on the result struct.
Jon Perritt5db08922014-09-30 21:32:48 -0500151type DeleteResult struct {
152 commonResult
153}