Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 1 | package containers |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 5 | "strings" |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 6 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 7 | "github.com/gophercloud/gophercloud" |
| 8 | "github.com/gophercloud/gophercloud/pagination" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 9 | ) |
| 10 | |
Jamie Hannaford | 4ff7996 | 2014-10-06 16:28:23 +0200 | [diff] [blame] | 11 | // Container represents a container resource. |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 12 | type Container struct { |
Jamie Hannaford | 4ff7996 | 2014-10-06 16:28:23 +0200 | [diff] [blame] | 13 | // The total number of bytes stored in the container. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame^] | 14 | Bytes int `json:"bytes"` |
Jamie Hannaford | 4ff7996 | 2014-10-06 16:28:23 +0200 | [diff] [blame] | 15 | |
| 16 | // The total number of objects stored in the container. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame^] | 17 | Count int `json:"count"` |
Jamie Hannaford | 4ff7996 | 2014-10-06 16:28:23 +0200 | [diff] [blame] | 18 | |
| 19 | // The name of the container. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame^] | 20 | Name string `json:"name"` |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 21 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 22 | |
Jamie Hannaford | 4ff7996 | 2014-10-06 16:28:23 +0200 | [diff] [blame] | 23 | // ContainerPage is the page returned by a pager when traversing over a |
| 24 | // collection of containers. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 25 | type ContainerPage struct { |
| 26 | pagination.MarkerPageBase |
| 27 | } |
| 28 | |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame^] | 29 | //IsEmpty returns true if a ListResult contains no container names. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 30 | func (r ContainerPage) IsEmpty() (bool, error) { |
| 31 | names, err := ExtractNames(r) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame^] | 32 | return len(names) == 0, err |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | // LastMarker returns the last container name in a ListResult. |
| 36 | func (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 Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame^] | 48 | func ExtractInfo(r pagination.Page) ([]Container, error) { |
| 49 | var s []Container |
| 50 | err := (r.(ContainerPage)).ExtractInto(&s) |
| 51 | return s, err |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | // ExtractNames is a function that takes a ListResult and returns the containers' names. |
| 55 | func ExtractNames(page pagination.Page) ([]string, error) { |
| 56 | casted := page.(ContainerPage) |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 57 | ct := casted.Header.Get("Content-Type") |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 58 | |
| 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 Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 68 | names = append(names, container.Name) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 69 | } |
| 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 Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 87 | // GetHeader represents the headers returned in the response from a Get request. |
| 88 | type GetHeader struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame^] | 89 | 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 Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 101 | // GetResult represents the result of a get operation. |
| 102 | type GetResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 103 | gophercloud.HeaderResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 104 | } |
| 105 | |
Jon Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 106 | // 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 Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame^] | 108 | func (r GetResult) Extract() (*GetHeader, error) { |
| 109 | var s *GetHeader |
| 110 | err := r.ExtractInto(&s) |
| 111 | return s, err |
Jon Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 114 | // ExtractMetadata is a function that takes a GetResult (of type *http.Response) |
| 115 | // and returns the custom metadata associated with the container. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame^] | 116 | func (r GetResult) ExtractMetadata() (map[string]string, error) { |
| 117 | if r.Err != nil { |
| 118 | return nil, r.Err |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 119 | } |
| 120 | metadata := make(map[string]string) |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame^] | 121 | for k, v := range r.Header { |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 122 | 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 Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 129 | |
Jon Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 130 | // CreateHeader represents the headers returned in the response from a Create request. |
| 131 | type CreateHeader struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame^] | 132 | 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 Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Jamie Hannaford | 22ec479 | 2014-10-07 10:07:41 +0200 | [diff] [blame] | 138 | // CreateResult represents the result of a create operation. To extract the |
Jon Perritt | 9856a34 | 2014-10-27 13:44:06 -0500 | [diff] [blame] | 139 | // the headers from the HTTP response, you can invoke the 'ExtractHeader' |
Jamie Hannaford | 22ec479 | 2014-10-07 10:07:41 +0200 | [diff] [blame] | 140 | // method on the result struct. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 141 | type CreateResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 142 | gophercloud.HeaderResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 143 | } |
| 144 | |
Jon Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 145 | // 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 Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame^] | 147 | func (r CreateResult) Extract() (*CreateHeader, error) { |
| 148 | var s *CreateHeader |
| 149 | err := r.ExtractInto(&s) |
| 150 | return s, err |
Jon Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | // UpdateHeader represents the headers returned in the response from a Update request. |
| 154 | type UpdateHeader struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame^] | 155 | 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 Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Jamie Hannaford | 22ec479 | 2014-10-07 10:07:41 +0200 | [diff] [blame] | 161 | // UpdateResult represents the result of an update operation. To extract the |
Jon Perritt | 9856a34 | 2014-10-27 13:44:06 -0500 | [diff] [blame] | 162 | // the headers from the HTTP response, you can invoke the 'ExtractHeader' |
Jamie Hannaford | 22ec479 | 2014-10-07 10:07:41 +0200 | [diff] [blame] | 163 | // method on the result struct. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 164 | type UpdateResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 165 | gophercloud.HeaderResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 166 | } |
| 167 | |
Jon Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 168 | // 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 Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame^] | 170 | func (r UpdateResult) Extract() (*UpdateHeader, error) { |
| 171 | var s *UpdateHeader |
| 172 | err := r.ExtractInto(&s) |
| 173 | return s, err |
Jon Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // DeleteHeader represents the headers returned in the response from a Delete request. |
| 177 | type DeleteHeader struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame^] | 178 | 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 Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Jamie Hannaford | 22ec479 | 2014-10-07 10:07:41 +0200 | [diff] [blame] | 184 | // DeleteResult represents the result of a delete operation. To extract the |
Jon Perritt | 9856a34 | 2014-10-27 13:44:06 -0500 | [diff] [blame] | 185 | // the headers from the HTTP response, you can invoke the 'ExtractHeader' |
Jamie Hannaford | 22ec479 | 2014-10-07 10:07:41 +0200 | [diff] [blame] | 186 | // method on the result struct. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 187 | type DeleteResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 188 | gophercloud.HeaderResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 189 | } |
Jon Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 190 | |
| 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 Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame^] | 193 | func (r DeleteResult) Extract() (*DeleteHeader, error) { |
| 194 | var s *DeleteHeader |
| 195 | err := r.ExtractInto(&s) |
| 196 | return s, err |
Jon Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 197 | } |