1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
package containers
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/pagination"
)
// Container represents a container resource.
type Container struct {
// The total number of bytes stored in the container.
Bytes int64 `json:"bytes"`
// The total number of objects stored in the container.
Count int64 `json:"count"`
// The name of the container.
Name string `json:"name"`
}
// ContainerPage is the page returned by a pager when traversing over a
// collection of containers.
type ContainerPage struct {
pagination.MarkerPageBase
}
// IsEmpty returns true if a ListResult contains no container names.
func (r ContainerPage) IsEmpty() (bool, error) {
if r.StatusCode == 204 {
return true, nil
}
names, err := ExtractNames(r)
return len(names) == 0, err
}
// LastMarker returns the last container name in a ListResult.
func (r ContainerPage) LastMarker() (string, error) {
names, err := ExtractNames(r)
if err != nil {
return "", err
}
if len(names) == 0 {
return "", nil
}
return names[len(names)-1], nil
}
// ExtractInfo is a function that takes a ListResult and returns the
// containers' information.
func ExtractInfo(r pagination.Page) ([]Container, error) {
var s []Container
err := (r.(ContainerPage)).ExtractInto(&s)
return s, err
}
// ExtractNames is a function that takes a ListResult and returns the
// containers' names.
func ExtractNames(page pagination.Page) ([]string, error) {
casted := page.(ContainerPage)
ct := casted.Header.Get("Content-Type")
switch {
case strings.HasPrefix(ct, "application/json"):
parsed, err := ExtractInfo(page)
if err != nil {
return nil, err
}
names := make([]string, 0, len(parsed))
for _, container := range parsed {
names = append(names, container.Name)
}
return names, nil
case strings.HasPrefix(ct, "text/plain") || ct == "":
names := make([]string, 0, 50)
body := string(page.(ContainerPage).Body.([]uint8))
for _, name := range strings.Split(body, "\n") {
if len(name) > 0 {
names = append(names, name)
}
}
return names, nil
default:
return nil, fmt.Errorf("Cannot extract names from response with content-type: [%s]", ct)
}
}
// GetHeader represents the headers returned in the response from a Get request.
type GetHeader struct {
AcceptRanges string `json:"Accept-Ranges"`
BytesUsed int64 `json:"X-Container-Bytes-Used,string"`
ContentLength int64 `json:"Content-Length,string"`
ContentType string `json:"Content-Type"`
Date time.Time `json:"-"`
ObjectCount int64 `json:"X-Container-Object-Count,string"`
Read []string `json:"-"`
TransID string `json:"X-Trans-Id"`
VersionsLocation string `json:"X-Versions-Location"`
HistoryLocation string `json:"X-History-Location"`
Write []string `json:"-"`
StoragePolicy string `json:"X-Storage-Policy"`
TempURLKey string `json:"X-Container-Meta-Temp-URL-Key"`
TempURLKey2 string `json:"X-Container-Meta-Temp-URL-Key-2"`
Timestamp float64 `json:"X-Timestamp,string"`
VersionsEnabled bool `json:"-"`
}
func (r *GetHeader) UnmarshalJSON(b []byte) error {
type tmp GetHeader
var s struct {
tmp
Write string `json:"X-Container-Write"`
Read string `json:"X-Container-Read"`
Date gophercloud.JSONRFC1123 `json:"Date"`
VersionsEnabled string `json:"X-Versions-Enabled"`
}
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
*r = GetHeader(s.tmp)
r.Read = strings.Split(s.Read, ",")
r.Write = strings.Split(s.Write, ",")
r.Date = time.Time(s.Date)
if s.VersionsEnabled != "" {
// custom unmarshaller here is required to handle boolean value
// that starts with a capital letter
r.VersionsEnabled, err = strconv.ParseBool(s.VersionsEnabled)
}
return err
}
// GetResult represents the result of a get operation.
type GetResult struct {
gophercloud.HeaderResult
}
// Extract will return a struct of headers returned from a call to Get.
func (r GetResult) Extract() (*GetHeader, error) {
var s GetHeader
err := r.ExtractInto(&s)
return &s, err
}
// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
// and returns the custom metadata associated with the container.
func (r GetResult) ExtractMetadata() (map[string]string, error) {
if r.Err != nil {
return nil, r.Err
}
metadata := make(map[string]string)
for k, v := range r.Header {
if strings.HasPrefix(k, "X-Container-Meta-") {
key := strings.TrimPrefix(k, "X-Container-Meta-")
metadata[key] = v[0]
}
}
return metadata, nil
}
// CreateHeader represents the headers returned in the response from a Create
// request.
type CreateHeader struct {
ContentLength int64 `json:"Content-Length,string"`
ContentType string `json:"Content-Type"`
Date time.Time `json:"-"`
TransID string `json:"X-Trans-Id"`
}
func (r *CreateHeader) UnmarshalJSON(b []byte) error {
type tmp CreateHeader
var s struct {
tmp
Date gophercloud.JSONRFC1123 `json:"Date"`
}
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
*r = CreateHeader(s.tmp)
r.Date = time.Time(s.Date)
return err
}
// CreateResult represents the result of a create operation. To extract the
// the headers from the HTTP response, call its Extract method.
type CreateResult struct {
gophercloud.HeaderResult
}
// Extract will return a struct of headers returned from a call to Create.
// To extract the headers from the HTTP response, call its Extract method.
func (r CreateResult) Extract() (*CreateHeader, error) {
var s CreateHeader
err := r.ExtractInto(&s)
return &s, err
}
// UpdateHeader represents the headers returned in the response from a Update
// request.
type UpdateHeader struct {
ContentLength int64 `json:"Content-Length,string"`
ContentType string `json:"Content-Type"`
Date time.Time `json:"-"`
TransID string `json:"X-Trans-Id"`
}
func (r *UpdateHeader) UnmarshalJSON(b []byte) error {
type tmp UpdateHeader
var s struct {
tmp
Date gophercloud.JSONRFC1123 `json:"Date"`
}
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
*r = UpdateHeader(s.tmp)
r.Date = time.Time(s.Date)
return err
}
// UpdateResult represents the result of an update operation. To extract the
// the headers from the HTTP response, call its Extract method.
type UpdateResult struct {
gophercloud.HeaderResult
}
// Extract will return a struct of headers returned from a call to Update.
func (r UpdateResult) Extract() (*UpdateHeader, error) {
var s UpdateHeader
err := r.ExtractInto(&s)
return &s, err
}
// DeleteHeader represents the headers returned in the response from a Delete
// request.
type DeleteHeader struct {
ContentLength int64 `json:"Content-Length,string"`
ContentType string `json:"Content-Type"`
Date time.Time `json:"-"`
TransID string `json:"X-Trans-Id"`
}
func (r *DeleteHeader) UnmarshalJSON(b []byte) error {
type tmp DeleteHeader
var s struct {
tmp
Date gophercloud.JSONRFC1123 `json:"Date"`
}
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
*r = DeleteHeader(s.tmp)
r.Date = time.Time(s.Date)
return err
}
// DeleteResult represents the result of a delete operation. To extract the
// headers from the HTTP response, call its Extract method.
type DeleteResult struct {
gophercloud.HeaderResult
}
// Extract will return a struct of headers returned from a call to Delete.
func (r DeleteResult) Extract() (*DeleteHeader, error) {
var s DeleteHeader
err := r.ExtractInto(&s)
return &s, err
}
type BulkDeleteResponse struct {
ResponseStatus string `json:"Response Status"`
ResponseBody string `json:"Response Body"`
Errors [][]string `json:"Errors"`
NumberDeleted int `json:"Number Deleted"`
NumberNotFound int `json:"Number Not Found"`
}
// BulkDeleteResult represents the result of a bulk delete operation. To extract
// the response object from the HTTP response, call its Extract method.
type BulkDeleteResult struct {
gophercloud.Result
}
// Extract will return a BulkDeleteResponse struct returned from a BulkDelete
// call.
func (r BulkDeleteResult) Extract() (*BulkDeleteResponse, error) {
var s BulkDeleteResponse
err := r.ExtractInto(&s)
return &s, err
}
|