blob: 9eec3f4236c0f87ffb920f0e6f98b91ddf362857 [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
Jon Perritt27249f42016-02-18 10:35:59 -06007 "github.com/gophercloud/gophercloud"
8 "github.com/gophercloud/gophercloud/pagination"
Jon Perritt8c93a302014-09-28 22:35:57 -05009)
10
Jamie Hannaford4ff79962014-10-06 16:28:23 +020011// Container represents a container resource.
Jon Perritt8aa40262014-09-29 15:41:32 -050012type Container struct {
Jamie Hannaford4ff79962014-10-06 16:28:23 +020013 // The total number of bytes stored in the container.
Jon Perritt3c166472016-02-25 03:07:41 -060014 Bytes int `json:"bytes"`
Jamie Hannaford4ff79962014-10-06 16:28:23 +020015
16 // The total number of objects stored in the container.
Jon Perritt3c166472016-02-25 03:07:41 -060017 Count int `json:"count"`
Jamie Hannaford4ff79962014-10-06 16:28:23 +020018
19 // The name of the container.
Jon Perritt3c166472016-02-25 03:07:41 -060020 Name string `json:"name"`
Jon Perritt8aa40262014-09-29 15:41:32 -050021}
Jon Perritt8c93a302014-09-28 22:35:57 -050022
Jamie Hannaford4ff79962014-10-06 16:28:23 +020023// ContainerPage is the page returned by a pager when traversing over a
24// collection of containers.
Jon Perritt8c93a302014-09-28 22:35:57 -050025type ContainerPage struct {
26 pagination.MarkerPageBase
27}
28
Jon Perritt3c166472016-02-25 03:07:41 -060029//IsEmpty returns true if a ListResult contains no container names.
Jon Perritt8c93a302014-09-28 22:35:57 -050030func (r ContainerPage) IsEmpty() (bool, error) {
31 names, err := ExtractNames(r)
Jon Perritt3c166472016-02-25 03:07:41 -060032 return len(names) == 0, err
Jon Perritt8c93a302014-09-28 22:35:57 -050033}
34
35// LastMarker returns the last container name in a ListResult.
36func (r ContainerPage) LastMarker() (string, error) {
37 names, err := ExtractNames(r)
38 if err != nil {
39 return "", err
40 }
41 if len(names) == 0 {
42 return "", nil
43 }
44 return names[len(names)-1], nil
45}
46
47// ExtractInfo is a function that takes a ListResult and returns the containers' information.
Jon Perritt3c166472016-02-25 03:07:41 -060048func ExtractInfo(r pagination.Page) ([]Container, error) {
49 var s []Container
50 err := (r.(ContainerPage)).ExtractInto(&s)
51 return s, err
Jon Perritt8c93a302014-09-28 22:35:57 -050052}
53
54// ExtractNames is a function that takes a ListResult and returns the containers' names.
55func ExtractNames(page pagination.Page) ([]string, error) {
56 casted := page.(ContainerPage)
Ash Wilson72e4d2c2014-10-20 10:27:30 -040057 ct := casted.Header.Get("Content-Type")
Jon Perritt8c93a302014-09-28 22:35:57 -050058
59 switch {
60 case strings.HasPrefix(ct, "application/json"):
61 parsed, err := ExtractInfo(page)
62 if err != nil {
63 return nil, err
64 }
65
66 names := make([]string, 0, len(parsed))
67 for _, container := range parsed {
Jon Perritt8aa40262014-09-29 15:41:32 -050068 names = append(names, container.Name)
Jon Perritt8c93a302014-09-28 22:35:57 -050069 }
70 return names, nil
71 case strings.HasPrefix(ct, "text/plain"):
72 names := make([]string, 0, 50)
73
74 body := string(page.(ContainerPage).Body.([]uint8))
75 for _, name := range strings.Split(body, "\n") {
76 if len(name) > 0 {
77 names = append(names, name)
78 }
79 }
80
81 return names, nil
82 default:
83 return nil, fmt.Errorf("Cannot extract names from response with content-type: [%s]", ct)
84 }
85}
86
Jon Perritt8314f4e2014-12-01 10:58:40 -070087// GetHeader represents the headers returned in the response from a Get request.
88type GetHeader struct {
Jon Perritt3c166472016-02-25 03:07:41 -060089 AcceptRanges string `json:"Accept-Ranges"`
90 BytesUsed string `json:"X-Account-Bytes-Used"`
91 ContentLength string `json:"Content-Length"`
92 ContentType string `json:"Content-Type"`
93 Date gophercloud.JSONRFC1123 `json:"Date"`
94 ObjectCount string `json:"X-Container-Object-Count"`
95 Read string `json:"X-Container-Read"`
96 TransID string `json:"X-Trans-Id"`
97 VersionsLocation string `json:"X-Versions-Location"`
98 Write string `json:"X-Container-Write"`
Jon Perritt8314f4e2014-12-01 10:58:40 -070099}
100
Jon Perritt5db08922014-09-30 21:32:48 -0500101// GetResult represents the result of a get operation.
102type GetResult struct {
Jon Perrittd50f93e2014-10-27 14:19:27 -0500103 gophercloud.HeaderResult
Jon Perritt5db08922014-09-30 21:32:48 -0500104}
105
Jon Perritt8314f4e2014-12-01 10:58:40 -0700106// Extract will return a struct of headers returned from a call to Get. To obtain
107// a map of headers, call the ExtractHeader method on the GetResult.
Jon Perritt3c166472016-02-25 03:07:41 -0600108func (r GetResult) Extract() (*GetHeader, error) {
109 var s *GetHeader
110 err := r.ExtractInto(&s)
111 return s, err
Jon Perritt8314f4e2014-12-01 10:58:40 -0700112}
113
Jon Perritt8c93a302014-09-28 22:35:57 -0500114// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
115// and returns the custom metadata associated with the container.
Jon Perritt3c166472016-02-25 03:07:41 -0600116func (r GetResult) ExtractMetadata() (map[string]string, error) {
117 if r.Err != nil {
118 return nil, r.Err
Jon Perritt8c93a302014-09-28 22:35:57 -0500119 }
120 metadata := make(map[string]string)
Jon Perritt3c166472016-02-25 03:07:41 -0600121 for k, v := range r.Header {
Jon Perritt8c93a302014-09-28 22:35:57 -0500122 if strings.HasPrefix(k, "X-Container-Meta-") {
123 key := strings.TrimPrefix(k, "X-Container-Meta-")
124 metadata[key] = v[0]
125 }
126 }
127 return metadata, nil
128}
Jon Perritt5db08922014-09-30 21:32:48 -0500129
Jon Perritt8314f4e2014-12-01 10:58:40 -0700130// CreateHeader represents the headers returned in the response from a Create request.
131type CreateHeader struct {
Jon Perritt3c166472016-02-25 03:07:41 -0600132 ContentLength string `json:"Content-Length"`
133 ContentType string `json:"Content-Type"`
134 Date gophercloud.JSONRFC1123 `json:"Date"`
135 TransID string `json:"X-Trans-Id"`
Jon Perritt8314f4e2014-12-01 10:58:40 -0700136}
137
Jamie Hannaford22ec4792014-10-07 10:07:41 +0200138// CreateResult represents the result of a create operation. To extract the
Jon Perritt9856a342014-10-27 13:44:06 -0500139// the headers from the HTTP response, you can invoke the 'ExtractHeader'
Jamie Hannaford22ec4792014-10-07 10:07:41 +0200140// method on the result struct.
Jon Perritt5db08922014-09-30 21:32:48 -0500141type CreateResult struct {
Jon Perrittd50f93e2014-10-27 14:19:27 -0500142 gophercloud.HeaderResult
Jon Perritt5db08922014-09-30 21:32:48 -0500143}
144
Jon Perritt8314f4e2014-12-01 10:58:40 -0700145// Extract will return a struct of headers returned from a call to Create. To obtain
146// a map of headers, call the ExtractHeader method on the CreateResult.
Jon Perritt3c166472016-02-25 03:07:41 -0600147func (r CreateResult) Extract() (*CreateHeader, error) {
148 var s *CreateHeader
149 err := r.ExtractInto(&s)
150 return s, err
Jon Perritt8314f4e2014-12-01 10:58:40 -0700151}
152
153// UpdateHeader represents the headers returned in the response from a Update request.
154type UpdateHeader struct {
Jon Perritt3c166472016-02-25 03:07:41 -0600155 ContentLength string `json:"Content-Length"`
156 ContentType string `json:"Content-Type"`
157 Date gophercloud.JSONRFC1123 `json:"Date"`
158 TransID string `json:"X-Trans-Id"`
Jon Perritt8314f4e2014-12-01 10:58:40 -0700159}
160
Jamie Hannaford22ec4792014-10-07 10:07:41 +0200161// UpdateResult represents the result of an update operation. To extract the
Jon Perritt9856a342014-10-27 13:44:06 -0500162// the headers from the HTTP response, you can invoke the 'ExtractHeader'
Jamie Hannaford22ec4792014-10-07 10:07:41 +0200163// method on the result struct.
Jon Perritt5db08922014-09-30 21:32:48 -0500164type UpdateResult struct {
Jon Perrittd50f93e2014-10-27 14:19:27 -0500165 gophercloud.HeaderResult
Jon Perritt5db08922014-09-30 21:32:48 -0500166}
167
Jon Perritt8314f4e2014-12-01 10:58:40 -0700168// Extract will return a struct of headers returned from a call to Update. To obtain
169// a map of headers, call the ExtractHeader method on the UpdateResult.
Jon Perritt3c166472016-02-25 03:07:41 -0600170func (r UpdateResult) Extract() (*UpdateHeader, error) {
171 var s *UpdateHeader
172 err := r.ExtractInto(&s)
173 return s, err
Jon Perritt8314f4e2014-12-01 10:58:40 -0700174}
175
176// DeleteHeader represents the headers returned in the response from a Delete request.
177type DeleteHeader struct {
Jon Perritt3c166472016-02-25 03:07:41 -0600178 ContentLength string `json:"Content-Length"`
179 ContentType string `json:"Content-Type"`
180 Date gophercloud.JSONRFC1123 `json:"Date"`
181 TransID string `json:"X-Trans-Id"`
Jon Perritt8314f4e2014-12-01 10:58:40 -0700182}
183
Jamie Hannaford22ec4792014-10-07 10:07:41 +0200184// DeleteResult represents the result of a delete operation. To extract the
Jon Perritt9856a342014-10-27 13:44:06 -0500185// the headers from the HTTP response, you can invoke the 'ExtractHeader'
Jamie Hannaford22ec4792014-10-07 10:07:41 +0200186// method on the result struct.
Jon Perritt5db08922014-09-30 21:32:48 -0500187type DeleteResult struct {
Jon Perrittd50f93e2014-10-27 14:19:27 -0500188 gophercloud.HeaderResult
Jon Perritt5db08922014-09-30 21:32:48 -0500189}
Jon Perritt8314f4e2014-12-01 10:58:40 -0700190
191// Extract will return a struct of headers returned from a call to Delete. To obtain
192// a map of headers, call the ExtractHeader method on the DeleteResult.
Jon Perritt3c166472016-02-25 03:07:41 -0600193func (r DeleteResult) Extract() (*DeleteHeader, error) {
194 var s *DeleteHeader
195 err := r.ExtractInto(&s)
196 return s, err
Jon Perritt8314f4e2014-12-01 10:58:40 -0700197}