blob: 74f3286046568acb7d85ab34393f8ae1412dfc38 [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 Perritt5db08922014-09-30 21:32:48 -050099// GetResult represents the result of a get operation.
100type GetResult struct {
Jon Perrittd50f93e2014-10-27 14:19:27 -0500101 gophercloud.HeaderResult
Jon Perritt5db08922014-09-30 21:32:48 -0500102}
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)
Ash Wilson72e4d2c2014-10-20 10:27:30 -0400111 for k, v := range gr.Header {
Jon Perritt8c93a302014-09-28 22:35:57 -0500112 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
Jamie Hannaford22ec4792014-10-07 10:07:41 +0200120// CreateResult represents the result of a create operation. To extract the
Jon Perritt9856a342014-10-27 13:44:06 -0500121// the headers from the HTTP response, you can invoke the 'ExtractHeader'
Jamie Hannaford22ec4792014-10-07 10:07:41 +0200122// method on the result struct.
Jon Perritt5db08922014-09-30 21:32:48 -0500123type CreateResult struct {
Jon Perrittd50f93e2014-10-27 14:19:27 -0500124 gophercloud.HeaderResult
Jon Perritt5db08922014-09-30 21:32:48 -0500125}
126
Jamie Hannaford22ec4792014-10-07 10:07:41 +0200127// UpdateResult represents the result of an update operation. To extract the
Jon Perritt9856a342014-10-27 13:44:06 -0500128// the headers from the HTTP response, you can invoke the 'ExtractHeader'
Jamie Hannaford22ec4792014-10-07 10:07:41 +0200129// method on the result struct.
Jon Perritt5db08922014-09-30 21:32:48 -0500130type UpdateResult struct {
Jon Perrittd50f93e2014-10-27 14:19:27 -0500131 gophercloud.HeaderResult
Jon Perritt5db08922014-09-30 21:32:48 -0500132}
133
Jamie Hannaford22ec4792014-10-07 10:07:41 +0200134// DeleteResult represents the result of a delete operation. To extract the
Jon Perritt9856a342014-10-27 13:44:06 -0500135// the headers from the HTTP response, you can invoke the 'ExtractHeader'
Jamie Hannaford22ec4792014-10-07 10:07:41 +0200136// method on the result struct.
Jon Perritt5db08922014-09-30 21:32:48 -0500137type DeleteResult struct {
Jon Perrittd50f93e2014-10-27 14:19:27 -0500138 gophercloud.HeaderResult
Jon Perritt5db08922014-09-30 21:32:48 -0500139}