blob: c00a4bcd3c7e0e292a37032ba8a4c4e904e189fa [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 Perritt5db08922014-09-30 21:32:48 -05006
Ash Wilsonaf262872014-10-20 09:32:29 -04007 "github.com/rackspace/gophercloud"
Jon Perritt5db08922014-09-30 21:32:48 -05008 "github.com/rackspace/gophercloud/pagination"
Jon Perrittb3461402014-10-09 21:36:17 -05009
10 "github.com/mitchellh/mapstructure"
Jon Perritt8c93a302014-09-28 22:35:57 -050011)
12
Jamie Hannaford4ff79962014-10-06 16:28:23 +020013// Container represents a container resource.
Jon Perritt8aa40262014-09-29 15:41:32 -050014type Container struct {
Jamie Hannaford4ff79962014-10-06 16:28:23 +020015 // The total number of bytes stored in the container.
16 Bytes int `json:"bytes" mapstructure:"bytes"`
17
18 // The total number of objects stored in the container.
19 Count int `json:"count" mapstructure:"count"`
20
21 // The name of the container.
22 Name string `json:"name" mapstructure:"name"`
Jon Perritt8aa40262014-09-29 15:41:32 -050023}
Jon Perritt8c93a302014-09-28 22:35:57 -050024
Jamie Hannaford4ff79962014-10-06 16:28:23 +020025// ContainerPage is the page returned by a pager when traversing over a
26// collection of containers.
Jon Perritt8c93a302014-09-28 22:35:57 -050027type ContainerPage struct {
28 pagination.MarkerPageBase
29}
30
31// IsEmpty returns true if a ListResult contains no container names.
32func (r ContainerPage) IsEmpty() (bool, error) {
33 names, err := ExtractNames(r)
34 if err != nil {
35 return true, err
36 }
37 return len(names) == 0, nil
38}
39
40// LastMarker returns the last container name in a ListResult.
41func (r ContainerPage) LastMarker() (string, error) {
42 names, err := ExtractNames(r)
43 if err != nil {
44 return "", err
45 }
46 if len(names) == 0 {
47 return "", nil
48 }
49 return names[len(names)-1], nil
50}
51
52// ExtractInfo is a function that takes a ListResult and returns the containers' information.
53func ExtractInfo(page pagination.Page) ([]Container, error) {
54 untyped := page.(ContainerPage).Body.([]interface{})
55 results := make([]Container, len(untyped))
56 for index, each := range untyped {
Jon Perritt8aa40262014-09-29 15:41:32 -050057 container := each.(map[string]interface{})
Jon Perrittfdac6e52014-09-29 19:43:45 -050058 err := mapstructure.Decode(container, &results[index])
Jon Perritt8aa40262014-09-29 15:41:32 -050059 if err != nil {
60 return results, err
61 }
Jon Perritt8c93a302014-09-28 22:35:57 -050062 }
63 return results, nil
64}
65
66// ExtractNames is a function that takes a ListResult and returns the containers' names.
67func ExtractNames(page pagination.Page) ([]string, error) {
68 casted := page.(ContainerPage)
Ash Wilson72e4d2c2014-10-20 10:27:30 -040069 ct := casted.Header.Get("Content-Type")
Jon Perritt8c93a302014-09-28 22:35:57 -050070
71 switch {
72 case strings.HasPrefix(ct, "application/json"):
73 parsed, err := ExtractInfo(page)
74 if err != nil {
75 return nil, err
76 }
77
78 names := make([]string, 0, len(parsed))
79 for _, container := range parsed {
Jon Perritt8aa40262014-09-29 15:41:32 -050080 names = append(names, container.Name)
Jon Perritt8c93a302014-09-28 22:35:57 -050081 }
82 return names, nil
83 case strings.HasPrefix(ct, "text/plain"):
84 names := make([]string, 0, 50)
85
86 body := string(page.(ContainerPage).Body.([]uint8))
87 for _, name := range strings.Split(body, "\n") {
88 if len(name) > 0 {
89 names = append(names, name)
90 }
91 }
92
93 return names, nil
94 default:
95 return nil, fmt.Errorf("Cannot extract names from response with content-type: [%s]", ct)
96 }
97}
98
Jon Perritt2b36fa32014-10-24 15:44:23 -050099type commonResult struct {
100 gophercloud.Result
101}
102
Jon Perritt5db08922014-09-30 21:32:48 -0500103// GetResult represents the result of a get operation.
104type GetResult struct {
Jon Perritt2b36fa32014-10-24 15:44:23 -0500105 commonResult
Jon Perritt5db08922014-09-30 21:32:48 -0500106}
107
Jon Perritt8c93a302014-09-28 22:35:57 -0500108// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
109// and returns the custom metadata associated with the container.
110func (gr GetResult) ExtractMetadata() (map[string]string, error) {
111 if gr.Err != nil {
112 return nil, gr.Err
113 }
114 metadata := make(map[string]string)
Ash Wilson72e4d2c2014-10-20 10:27:30 -0400115 for k, v := range gr.Header {
Jon Perritt8c93a302014-09-28 22:35:57 -0500116 if strings.HasPrefix(k, "X-Container-Meta-") {
117 key := strings.TrimPrefix(k, "X-Container-Meta-")
118 metadata[key] = v[0]
119 }
120 }
121 return metadata, nil
122}
Jon Perritt5db08922014-09-30 21:32:48 -0500123
Jamie Hannaford22ec4792014-10-07 10:07:41 +0200124// CreateResult represents the result of a create operation. To extract the
125// the headers from the HTTP response, you can invoke the 'ExtractHeaders'
126// method on the result struct.
Jon Perritt5db08922014-09-30 21:32:48 -0500127type CreateResult struct {
Jon Perritt2b36fa32014-10-24 15:44:23 -0500128 commonResult
Jon Perritt5db08922014-09-30 21:32:48 -0500129}
130
Jamie Hannaford22ec4792014-10-07 10:07:41 +0200131// UpdateResult represents the result of an update operation. To extract the
132// the headers from the HTTP response, you can invoke the 'ExtractHeaders'
133// method on the result struct.
Jon Perritt5db08922014-09-30 21:32:48 -0500134type UpdateResult struct {
Jon Perritt2b36fa32014-10-24 15:44:23 -0500135 commonResult
Jon Perritt5db08922014-09-30 21:32:48 -0500136}
137
Jamie Hannaford22ec4792014-10-07 10:07:41 +0200138// DeleteResult represents the result of a delete operation. To extract the
139// the headers from the HTTP response, you can invoke the 'ExtractHeaders'
140// method on the result struct.
Jon Perritt5db08922014-09-30 21:32:48 -0500141type DeleteResult struct {
Jon Perritt2b36fa32014-10-24 15:44:23 -0500142 commonResult
Jon Perritt5db08922014-09-30 21:32:48 -0500143}