Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 1 | package objects |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 5 | "io" |
| 6 | "io/ioutil" |
Jon Perritt | 4f93f8e | 2015-02-02 11:19:41 -0700 | [diff] [blame] | 7 | "strconv" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 8 | "strings" |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 9 | "time" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 10 | |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 11 | "github.com/rackspace/gophercloud" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 12 | "github.com/rackspace/gophercloud/pagination" |
Jon Perritt | ea4e301 | 2014-10-09 22:03:19 -0500 | [diff] [blame] | 13 | |
| 14 | "github.com/mitchellh/mapstructure" |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 15 | ) |
| 16 | |
| 17 | // Object is a structure that holds information related to a storage object. |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 18 | type Object struct { |
Jon Perritt | 9415ca7 | 2014-11-03 11:58:48 -0600 | [diff] [blame] | 19 | // Bytes is the total number of bytes that comprise the object. |
| 20 | Bytes int64 `json:"bytes" mapstructure:"bytes"` |
| 21 | |
| 22 | // ContentType is the content type of the object. |
| 23 | ContentType string `json:"content_type" mapstructure:"content_type"` |
| 24 | |
| 25 | // Hash represents the MD5 checksum value of the object's content. |
| 26 | Hash string `json:"hash" mapstructure:"hash"` |
| 27 | |
| 28 | // LastModified is the RFC3339Milli time the object was last modified, represented |
| 29 | // as a string. For any given object (obj), this value may be parsed to a time.Time: |
| 30 | // lastModified, err := time.Parse(gophercloud.RFC3339Milli, obj.LastModified) |
Jon Perritt | f3171c1 | 2014-09-30 17:39:31 -0500 | [diff] [blame] | 31 | LastModified string `json:"last_modified" mapstructure:"last_modified"` |
Jon Perritt | 9415ca7 | 2014-11-03 11:58:48 -0600 | [diff] [blame] | 32 | |
| 33 | // Name is the unique name for the object. |
| 34 | Name string `json:"name" mapstructure:"name"` |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 35 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 36 | |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 37 | // ObjectPage is a single page of objects that is returned from a call to the |
| 38 | // List function. |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 39 | type ObjectPage struct { |
| 40 | pagination.MarkerPageBase |
| 41 | } |
| 42 | |
| 43 | // IsEmpty returns true if a ListResult contains no object names. |
| 44 | func (r ObjectPage) IsEmpty() (bool, error) { |
| 45 | names, err := ExtractNames(r) |
| 46 | if err != nil { |
| 47 | return true, err |
| 48 | } |
| 49 | return len(names) == 0, nil |
| 50 | } |
| 51 | |
| 52 | // LastMarker returns the last object name in a ListResult. |
| 53 | func (r ObjectPage) LastMarker() (string, error) { |
| 54 | names, err := ExtractNames(r) |
| 55 | if err != nil { |
| 56 | return "", err |
| 57 | } |
| 58 | if len(names) == 0 { |
| 59 | return "", nil |
| 60 | } |
| 61 | return names[len(names)-1], nil |
| 62 | } |
| 63 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 64 | // ExtractInfo is a function that takes a page of objects and returns their full information. |
| 65 | func ExtractInfo(page pagination.Page) ([]Object, error) { |
| 66 | untyped := page.(ObjectPage).Body.([]interface{}) |
| 67 | results := make([]Object, len(untyped)) |
| 68 | for index, each := range untyped { |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 69 | object := each.(map[string]interface{}) |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame] | 70 | err := mapstructure.Decode(object, &results[index]) |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 71 | if err != nil { |
| 72 | return results, err |
| 73 | } |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 74 | } |
| 75 | return results, nil |
| 76 | } |
| 77 | |
| 78 | // ExtractNames is a function that takes a page of objects and returns only their names. |
| 79 | func ExtractNames(page pagination.Page) ([]string, error) { |
| 80 | casted := page.(ObjectPage) |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 81 | ct := casted.Header.Get("Content-Type") |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 82 | switch { |
| 83 | case strings.HasPrefix(ct, "application/json"): |
| 84 | parsed, err := ExtractInfo(page) |
| 85 | if err != nil { |
| 86 | return nil, err |
| 87 | } |
| 88 | |
| 89 | names := make([]string, 0, len(parsed)) |
| 90 | for _, object := range parsed { |
Jon Perritt | 8aa4026 | 2014-09-29 15:41:32 -0500 | [diff] [blame] | 91 | names = append(names, object.Name) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 92 | } |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame] | 93 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 94 | return names, nil |
| 95 | case strings.HasPrefix(ct, "text/plain"): |
| 96 | names := make([]string, 0, 50) |
| 97 | |
| 98 | body := string(page.(ObjectPage).Body.([]uint8)) |
| 99 | for _, name := range strings.Split(body, "\n") { |
| 100 | if len(name) > 0 { |
| 101 | names = append(names, name) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return names, nil |
Jon Perritt | fdac6e5 | 2014-09-29 19:43:45 -0500 | [diff] [blame] | 106 | case strings.HasPrefix(ct, "text/html"): |
| 107 | return []string{}, nil |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 108 | default: |
| 109 | return nil, fmt.Errorf("Cannot extract names from response with content-type: [%s]", ct) |
| 110 | } |
| 111 | } |
| 112 | |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 113 | // DownloadHeader represents the headers returned in the response from a Download request. |
| 114 | type DownloadHeader struct { |
| 115 | AcceptRanges string `mapstructure:"Accept-Ranges"` |
| 116 | ContentDisposition string `mapstructure:"Content-Disposition"` |
| 117 | ContentEncoding string `mapstructure:"Content-Encoding"` |
| 118 | ContentLength int64 `mapstructure:"Content-Length"` |
| 119 | ContentType string `mapstructure:"Content-Type"` |
| 120 | Date time.Time `mapstructure:"-"` |
Jon Perritt | 4f93f8e | 2015-02-02 11:19:41 -0700 | [diff] [blame] | 121 | DeleteAt time.Time `mapstructure:"-"` |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 122 | ETag string `mapstructure:"Etag"` |
| 123 | LastModified time.Time `mapstructure:"-"` |
| 124 | ObjectManifest string `mapstructure:"X-Object-Manifest"` |
| 125 | StaticLargeObject bool `mapstructure:"X-Static-Large-Object"` |
| 126 | TransID string `mapstructure:"X-Trans-Id"` |
| 127 | } |
| 128 | |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 129 | // DownloadResult is a *http.Response that is returned from a call to the Download function. |
| 130 | type DownloadResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 131 | gophercloud.HeaderResult |
Jamie Hannaford | ee11555 | 2014-10-27 16:11:05 +0100 | [diff] [blame] | 132 | Body io.ReadCloser |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 133 | } |
| 134 | |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 135 | // Extract will return a struct of headers returned from a call to Download. To obtain |
| 136 | // a map of headers, call the ExtractHeader method on the DownloadResult. |
| 137 | func (dr DownloadResult) Extract() (DownloadHeader, error) { |
| 138 | var dh DownloadHeader |
| 139 | if dr.Err != nil { |
| 140 | return dh, dr.Err |
| 141 | } |
| 142 | |
Jon Perritt | 63e7a48 | 2014-12-04 09:47:23 -0700 | [diff] [blame] | 143 | if err := gophercloud.DecodeHeader(dr.Header, &dh); err != nil { |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 144 | return dh, err |
| 145 | } |
| 146 | |
| 147 | if date, ok := dr.Header["Date"]; ok && len(date) > 0 { |
Jon Perritt | 4f93f8e | 2015-02-02 11:19:41 -0700 | [diff] [blame] | 148 | t, err := time.Parse(time.RFC1123, date[0]) |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 149 | if err != nil { |
| 150 | return dh, err |
| 151 | } |
| 152 | dh.Date = t |
| 153 | } |
| 154 | |
| 155 | if date, ok := dr.Header["Last-Modified"]; ok && len(date) > 0 { |
Jon Perritt | 4f93f8e | 2015-02-02 11:19:41 -0700 | [diff] [blame] | 156 | t, err := time.Parse(time.RFC1123, date[0]) |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 157 | if err != nil { |
| 158 | return dh, err |
| 159 | } |
| 160 | dh.LastModified = t |
| 161 | } |
| 162 | |
Jon Perritt | 4f93f8e | 2015-02-02 11:19:41 -0700 | [diff] [blame] | 163 | if date, ok := dr.Header["X-Delete-At"]; ok && len(date) > 0 { |
| 164 | unix, err := strconv.ParseInt(date[0], 10, 64) |
| 165 | if err != nil { |
| 166 | return dh, err |
| 167 | } |
| 168 | dh.DeleteAt = time.Unix(unix, 0) |
| 169 | } |
| 170 | |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 171 | return dh, nil |
| 172 | } |
| 173 | |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 174 | // ExtractContent is a function that takes a DownloadResult's io.Reader body |
| 175 | // and reads all available data into a slice of bytes. Please be aware that due |
| 176 | // the nature of io.Reader is forward-only - meaning that it can only be read |
| 177 | // once and not rewound. You can recreate a reader from the output of this |
| 178 | // function by using bytes.NewReader(downloadBytes) |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 179 | func (dr DownloadResult) ExtractContent() ([]byte, error) { |
| 180 | if dr.Err != nil { |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 181 | return nil, dr.Err |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 182 | } |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 183 | body, err := ioutil.ReadAll(dr.Body) |
| 184 | if err != nil { |
| 185 | return nil, err |
| 186 | } |
Jamie Hannaford | ee11555 | 2014-10-27 16:11:05 +0100 | [diff] [blame] | 187 | dr.Body.Close() |
Jamie Hannaford | 2e78486 | 2014-10-27 10:40:27 +0100 | [diff] [blame] | 188 | return body, nil |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 189 | } |
| 190 | |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 191 | // GetHeader represents the headers returned in the response from a Get request. |
| 192 | type GetHeader struct { |
| 193 | ContentDisposition string `mapstructure:"Content-Disposition"` |
| 194 | ContentEncoding string `mapstructure:"Content-Encoding"` |
| 195 | ContentLength int64 `mapstructure:"Content-Length"` |
| 196 | ContentType string `mapstructure:"Content-Type"` |
| 197 | Date time.Time `mapstructure:"-"` |
Jon Perritt | 4f93f8e | 2015-02-02 11:19:41 -0700 | [diff] [blame] | 198 | DeleteAt time.Time `mapstructure:"-"` |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 199 | ETag string `mapstructure:"Etag"` |
| 200 | LastModified time.Time `mapstructure:"-"` |
| 201 | ObjectManifest string `mapstructure:"X-Object-Manifest"` |
| 202 | StaticLargeObject bool `mapstructure:"X-Static-Large-Object"` |
| 203 | TransID string `mapstructure:"X-Trans-Id"` |
| 204 | } |
| 205 | |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 206 | // GetResult is a *http.Response that is returned from a call to the Get function. |
| 207 | type GetResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 208 | gophercloud.HeaderResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 209 | } |
| 210 | |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 211 | // Extract will return a struct of headers returned from a call to Get. To obtain |
| 212 | // a map of headers, call the ExtractHeader method on the GetResult. |
| 213 | func (gr GetResult) Extract() (GetHeader, error) { |
| 214 | var gh GetHeader |
| 215 | if gr.Err != nil { |
| 216 | return gh, gr.Err |
| 217 | } |
| 218 | |
Jon Perritt | 63e7a48 | 2014-12-04 09:47:23 -0700 | [diff] [blame] | 219 | if err := gophercloud.DecodeHeader(gr.Header, &gh); err != nil { |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 220 | return gh, err |
| 221 | } |
| 222 | |
| 223 | if date, ok := gr.Header["Date"]; ok && len(date) > 0 { |
| 224 | t, err := time.Parse(time.RFC1123, gr.Header["Date"][0]) |
| 225 | if err != nil { |
| 226 | return gh, err |
| 227 | } |
| 228 | gh.Date = t |
| 229 | } |
| 230 | |
| 231 | if date, ok := gr.Header["Last-Modified"]; ok && len(date) > 0 { |
| 232 | t, err := time.Parse(time.RFC1123, gr.Header["Last-Modified"][0]) |
| 233 | if err != nil { |
| 234 | return gh, err |
| 235 | } |
| 236 | gh.LastModified = t |
| 237 | } |
| 238 | |
Jon Perritt | 4f93f8e | 2015-02-02 11:19:41 -0700 | [diff] [blame] | 239 | if date, ok := gr.Header["X-Delete-At"]; ok && len(date) > 0 { |
| 240 | unix, err := strconv.ParseInt(date[0], 10, 64) |
| 241 | if err != nil { |
| 242 | return gh, err |
| 243 | } |
| 244 | gh.DeleteAt = time.Unix(unix, 0) |
| 245 | } |
| 246 | |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 247 | return gh, nil |
| 248 | } |
| 249 | |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 250 | // ExtractMetadata is a function that takes a GetResult (of type *http.Response) |
| 251 | // and returns the custom metadata associated with the object. |
| 252 | func (gr GetResult) ExtractMetadata() (map[string]string, error) { |
| 253 | if gr.Err != nil { |
| 254 | return nil, gr.Err |
| 255 | } |
| 256 | metadata := make(map[string]string) |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 257 | for k, v := range gr.Header { |
Jon Perritt | 8c93a30 | 2014-09-28 22:35:57 -0500 | [diff] [blame] | 258 | if strings.HasPrefix(k, "X-Object-Meta-") { |
| 259 | key := strings.TrimPrefix(k, "X-Object-Meta-") |
| 260 | metadata[key] = v[0] |
| 261 | } |
| 262 | } |
| 263 | return metadata, nil |
| 264 | } |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 265 | |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 266 | // CreateHeader represents the headers returned in the response from a Create request. |
| 267 | type CreateHeader struct { |
| 268 | ContentLength int64 `mapstructure:"Content-Length"` |
| 269 | ContentType string `mapstructure:"Content-Type"` |
| 270 | Date time.Time `mapstructure:"-"` |
| 271 | ETag string `mapstructure:"Etag"` |
| 272 | LastModified time.Time `mapstructure:"-"` |
| 273 | TransID string `mapstructure:"X-Trans-Id"` |
| 274 | } |
| 275 | |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 276 | // CreateResult represents the result of a create operation. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 277 | type CreateResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 278 | gophercloud.HeaderResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 279 | } |
| 280 | |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 281 | // Extract will return a struct of headers returned from a call to Create. To obtain |
| 282 | // a map of headers, call the ExtractHeader method on the CreateResult. |
| 283 | func (cr CreateResult) Extract() (CreateHeader, error) { |
| 284 | var ch CreateHeader |
| 285 | if cr.Err != nil { |
| 286 | return ch, cr.Err |
| 287 | } |
| 288 | |
Jon Perritt | 63e7a48 | 2014-12-04 09:47:23 -0700 | [diff] [blame] | 289 | if err := gophercloud.DecodeHeader(cr.Header, &ch); err != nil { |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 290 | return ch, err |
| 291 | } |
| 292 | |
| 293 | if date, ok := cr.Header["Date"]; ok && len(date) > 0 { |
| 294 | t, err := time.Parse(time.RFC1123, cr.Header["Date"][0]) |
| 295 | if err != nil { |
| 296 | return ch, err |
| 297 | } |
| 298 | ch.Date = t |
| 299 | } |
| 300 | |
| 301 | if date, ok := cr.Header["Last-Modified"]; ok && len(date) > 0 { |
| 302 | t, err := time.Parse(time.RFC1123, cr.Header["Last-Modified"][0]) |
| 303 | if err != nil { |
| 304 | return ch, err |
| 305 | } |
| 306 | ch.LastModified = t |
| 307 | } |
| 308 | |
| 309 | return ch, nil |
| 310 | } |
| 311 | |
| 312 | // UpdateHeader represents the headers returned in the response from a Update request. |
| 313 | type UpdateHeader struct { |
| 314 | ContentLength int64 `mapstructure:"Content-Length"` |
| 315 | ContentType string `mapstructure:"Content-Type"` |
| 316 | Date time.Time `mapstructure:"-"` |
| 317 | TransID string `mapstructure:"X-Trans-Id"` |
| 318 | } |
| 319 | |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 320 | // UpdateResult represents the result of an update operation. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 321 | type UpdateResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 322 | gophercloud.HeaderResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 323 | } |
| 324 | |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 325 | // Extract will return a struct of headers returned from a call to Update. To obtain |
| 326 | // a map of headers, call the ExtractHeader method on the UpdateResult. |
| 327 | func (ur UpdateResult) Extract() (UpdateHeader, error) { |
| 328 | var uh UpdateHeader |
| 329 | if ur.Err != nil { |
| 330 | return uh, ur.Err |
| 331 | } |
| 332 | |
Jon Perritt | 63e7a48 | 2014-12-04 09:47:23 -0700 | [diff] [blame] | 333 | if err := gophercloud.DecodeHeader(ur.Header, &uh); err != nil { |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 334 | return uh, err |
| 335 | } |
| 336 | |
| 337 | if date, ok := ur.Header["Date"]; ok && len(date) > 0 { |
| 338 | t, err := time.Parse(time.RFC1123, ur.Header["Date"][0]) |
| 339 | if err != nil { |
| 340 | return uh, err |
| 341 | } |
| 342 | uh.Date = t |
| 343 | } |
| 344 | |
| 345 | return uh, nil |
| 346 | } |
| 347 | |
| 348 | // DeleteHeader represents the headers returned in the response from a Delete request. |
| 349 | type DeleteHeader struct { |
| 350 | ContentLength int64 `mapstructure:"Content-Length"` |
| 351 | ContentType string `mapstructure:"Content-Type"` |
| 352 | Date time.Time `mapstructure:"-"` |
| 353 | TransID string `mapstructure:"X-Trans-Id"` |
| 354 | } |
| 355 | |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 356 | // DeleteResult represents the result of a delete operation. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 357 | type DeleteResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 358 | gophercloud.HeaderResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 359 | } |
| 360 | |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 361 | // Extract will return a struct of headers returned from a call to Delete. To obtain |
| 362 | // a map of headers, call the ExtractHeader method on the DeleteResult. |
| 363 | func (dr DeleteResult) Extract() (DeleteHeader, error) { |
| 364 | var dh DeleteHeader |
| 365 | if dr.Err != nil { |
| 366 | return dh, dr.Err |
| 367 | } |
| 368 | |
Jon Perritt | 63e7a48 | 2014-12-04 09:47:23 -0700 | [diff] [blame] | 369 | if err := gophercloud.DecodeHeader(dr.Header, &dh); err != nil { |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 370 | return dh, err |
| 371 | } |
| 372 | |
| 373 | if date, ok := dr.Header["Date"]; ok && len(date) > 0 { |
| 374 | t, err := time.Parse(time.RFC1123, dr.Header["Date"][0]) |
| 375 | if err != nil { |
| 376 | return dh, err |
| 377 | } |
| 378 | dh.Date = t |
| 379 | } |
| 380 | |
| 381 | return dh, nil |
| 382 | } |
| 383 | |
| 384 | // CopyHeader represents the headers returned in the response from a Copy request. |
| 385 | type CopyHeader struct { |
| 386 | ContentLength int64 `mapstructure:"Content-Length"` |
| 387 | ContentType string `mapstructure:"Content-Type"` |
| 388 | CopiedFrom string `mapstructure:"X-Copied-From"` |
| 389 | CopiedFromLastModified time.Time `mapstructure:"-"` |
| 390 | Date time.Time `mapstructure:"-"` |
| 391 | ETag string `mapstructure:"Etag"` |
| 392 | LastModified time.Time `mapstructure:"-"` |
| 393 | TransID string `mapstructure:"X-Trans-Id"` |
| 394 | } |
| 395 | |
Jamie Hannaford | c9cdc8f | 2014-10-06 16:32:56 +0200 | [diff] [blame] | 396 | // CopyResult represents the result of a copy operation. |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 397 | type CopyResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 398 | gophercloud.HeaderResult |
Jon Perritt | 5db0892 | 2014-09-30 21:32:48 -0500 | [diff] [blame] | 399 | } |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 400 | |
| 401 | // Extract will return a struct of headers returned from a call to Copy. To obtain |
| 402 | // a map of headers, call the ExtractHeader method on the CopyResult. |
| 403 | func (cr CopyResult) Extract() (CopyHeader, error) { |
| 404 | var ch CopyHeader |
| 405 | if cr.Err != nil { |
| 406 | return ch, cr.Err |
| 407 | } |
| 408 | |
Jon Perritt | 63e7a48 | 2014-12-04 09:47:23 -0700 | [diff] [blame] | 409 | if err := gophercloud.DecodeHeader(cr.Header, &ch); err != nil { |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 410 | return ch, err |
| 411 | } |
| 412 | |
| 413 | if date, ok := cr.Header["Date"]; ok && len(date) > 0 { |
| 414 | t, err := time.Parse(time.RFC1123, cr.Header["Date"][0]) |
| 415 | if err != nil { |
| 416 | return ch, err |
| 417 | } |
| 418 | ch.Date = t |
| 419 | } |
| 420 | |
| 421 | if date, ok := cr.Header["Last-Modified"]; ok && len(date) > 0 { |
| 422 | t, err := time.Parse(time.RFC1123, cr.Header["Last-Modified"][0]) |
| 423 | if err != nil { |
| 424 | return ch, err |
| 425 | } |
| 426 | ch.LastModified = t |
| 427 | } |
| 428 | |
| 429 | if date, ok := cr.Header["X-Copied-From-Last-Modified"]; ok && len(date) > 0 { |
| 430 | t, err := time.Parse(time.RFC1123, cr.Header["X-Copied-From-Last-Modified"][0]) |
| 431 | if err != nil { |
| 432 | return ch, err |
| 433 | } |
| 434 | ch.CopiedFromLastModified = t |
| 435 | } |
| 436 | |
| 437 | return ch, nil |
| 438 | } |