Jamie Hannaford | 93209fe | 2014-10-10 11:54:19 +0200 | [diff] [blame] | 1 | package accounts |
| 2 | |
| 3 | import ( |
Jamie Hannaford | 93209fe | 2014-10-10 11:54:19 +0200 | [diff] [blame] | 4 | "strings" |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 5 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 6 | "github.com/gophercloud/gophercloud" |
Jamie Hannaford | 93209fe | 2014-10-10 11:54:19 +0200 | [diff] [blame] | 7 | ) |
| 8 | |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 9 | // UpdateResult is returned from a call to the Update function. |
| 10 | type UpdateResult struct { |
| 11 | gophercloud.HeaderResult |
| 12 | } |
| 13 | |
| 14 | // UpdateHeader represents the headers returned in the response from an Update request. |
| 15 | type UpdateHeader struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 16 | ContentLength string `json:"Content-Length"` |
| 17 | ContentType string `json:"Content-Type"` |
| 18 | Date gophercloud.JSONRFC1123 `json:"Date"` |
| 19 | TransID string `json:"X-Trans-Id"` |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | // Extract will return a struct of headers returned from a call to Get. To obtain |
| 23 | // a map of headers, call the ExtractHeader method on the GetResult. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 24 | func (ur UpdateResult) Extract() (*UpdateHeader, error) { |
| 25 | var uh *UpdateHeader |
| 26 | err := ur.ExtractInto(&uh) |
| 27 | return uh, err |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | // GetHeader represents the headers returned in the response from a Get request. |
| 31 | type GetHeader struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 32 | BytesUsed string `json:"X-Account-Bytes-Used"` |
| 33 | ContainerCount string `json:"X-Account-Container-Count"` |
| 34 | ContentLength string `json:"Content-Length"` |
| 35 | ContentType string `json:"Content-Type"` |
| 36 | Date gophercloud.JSONRFC1123 `json:"Date"` |
| 37 | ObjectCount string `json:"X-Account-Object-Count"` |
| 38 | TransID string `json:"X-Trans-Id"` |
| 39 | TempURLKey string `json:"X-Account-Meta-Temp-URL-Key"` |
| 40 | TempURLKey2 string `json:"X-Account-Meta-Temp-URL-Key-2"` |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 41 | } |
| 42 | |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 43 | // GetResult is returned from a call to the Get function. |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 44 | type GetResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 45 | gophercloud.HeaderResult |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 46 | } |
Jamie Hannaford | 93209fe | 2014-10-10 11:54:19 +0200 | [diff] [blame] | 47 | |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 48 | // Extract will return a struct of headers returned from a call to Get. To obtain |
| 49 | // a map of headers, call the ExtractHeader method on the GetResult. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 50 | func (r GetResult) Extract() (*GetHeader, error) { |
| 51 | var s *GetHeader |
| 52 | err := r.ExtractInto(&s) |
| 53 | return s, err |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 54 | } |
| 55 | |
Jamie Hannaford | 93209fe | 2014-10-10 11:54:19 +0200 | [diff] [blame] | 56 | // ExtractMetadata is a function that takes a GetResult (of type *http.Response) |
| 57 | // and returns the custom metatdata associated with the account. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 58 | func (r GetResult) ExtractMetadata() (map[string]string, error) { |
| 59 | if r.Err != nil { |
| 60 | return nil, r.Err |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame] | 61 | } |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 62 | |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame] | 63 | metadata := make(map[string]string) |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 64 | for k, v := range r.Header { |
Jamie Hannaford | 93209fe | 2014-10-10 11:54:19 +0200 | [diff] [blame] | 65 | if strings.HasPrefix(k, "X-Account-Meta-") { |
| 66 | key := strings.TrimPrefix(k, "X-Account-Meta-") |
| 67 | metadata[key] = v[0] |
| 68 | } |
| 69 | } |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame] | 70 | return metadata, nil |
| 71 | } |