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" |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 5 | "time" |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 6 | |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | 93209fe | 2014-10-10 11:54:19 +0200 | [diff] [blame] | 8 | ) |
| 9 | |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 10 | // UpdateResult is returned from a call to the Update function. |
| 11 | type UpdateResult struct { |
| 12 | gophercloud.HeaderResult |
| 13 | } |
| 14 | |
| 15 | // UpdateHeader represents the headers returned in the response from an Update request. |
| 16 | type UpdateHeader struct { |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 17 | ContentLength string `mapstructure:"Content-Length"` |
| 18 | ContentType string `mapstructure:"Content-Type"` |
| 19 | Date time.Time `mapstructure:"-"` |
Jon Perritt | a052f35 | 2014-12-15 14:50:44 -0700 | [diff] [blame] | 20 | TransID string `mapstructure:"X-Trans-ID"` |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | // Extract will return a struct of headers returned from a call to Get. To obtain |
| 24 | // a map of headers, call the ExtractHeader method on the GetResult. |
| 25 | func (ur UpdateResult) Extract() (UpdateHeader, error) { |
| 26 | var uh UpdateHeader |
| 27 | if ur.Err != nil { |
| 28 | return uh, ur.Err |
| 29 | } |
| 30 | |
Jon Perritt | 63e7a48 | 2014-12-04 09:47:23 -0700 | [diff] [blame] | 31 | if err := gophercloud.DecodeHeader(ur.Header, &uh); err != nil { |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 32 | return uh, err |
| 33 | } |
| 34 | |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 35 | if date, ok := ur.Header["Date"]; ok && len(date) > 0 { |
| 36 | t, err := time.Parse(time.RFC1123, ur.Header["Date"][0]) |
| 37 | if err != nil { |
| 38 | return uh, err |
| 39 | } |
| 40 | uh.Date = t |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 41 | } |
| 42 | |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 43 | return uh, nil |
| 44 | } |
| 45 | |
| 46 | // GetHeader represents the headers returned in the response from a Get request. |
| 47 | type GetHeader struct { |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 48 | BytesUsed int64 `mapstructure:"X-Account-Bytes-Used"` |
| 49 | ContainerCount int `mapstructure:"X-Account-Container-Count"` |
| 50 | ContentLength int64 `mapstructure:"Content-Length"` |
| 51 | ContentType string `mapstructure:"Content-Type"` |
| 52 | Date time.Time `mapstructure:"-"` |
| 53 | ObjectCount int64 `mapstructure:"X-Account-Object-Count"` |
| 54 | TransID string `mapstructure:"X-Trans-Id"` |
Jon Perritt | 3828d5a | 2015-02-01 17:01:59 -0700 | [diff] [blame^] | 55 | TempURLKey string `mapstructure:"X-Account-Meta-Temp-URL-Key"` |
| 56 | TempURLKey2 string `mapstructure:"X-Account-Meta-Temp-URL-Key-2"` |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 57 | } |
| 58 | |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 59 | // GetResult is returned from a call to the Get function. |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 60 | type GetResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 61 | gophercloud.HeaderResult |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 62 | } |
Jamie Hannaford | 93209fe | 2014-10-10 11:54:19 +0200 | [diff] [blame] | 63 | |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 64 | // Extract will return a struct of headers returned from a call to Get. To obtain |
| 65 | // a map of headers, call the ExtractHeader method on the GetResult. |
| 66 | func (gr GetResult) Extract() (GetHeader, error) { |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 67 | var gh GetHeader |
Jon Perritt | 8c31b2a | 2014-12-03 10:21:11 -0700 | [diff] [blame] | 68 | if gr.Err != nil { |
| 69 | return gh, gr.Err |
| 70 | } |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 71 | |
Jon Perritt | 63e7a48 | 2014-12-04 09:47:23 -0700 | [diff] [blame] | 72 | if err := gophercloud.DecodeHeader(gr.Header, &gh); err != nil { |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 73 | return gh, err |
| 74 | } |
| 75 | |
Jon Perritt | 8314f4e | 2014-12-01 10:58:40 -0700 | [diff] [blame] | 76 | if date, ok := gr.Header["Date"]; ok && len(date) > 0 { |
| 77 | t, err := time.Parse(time.RFC1123, gr.Header["Date"][0]) |
| 78 | if err != nil { |
| 79 | return gh, err |
| 80 | } |
| 81 | gh.Date = t |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 82 | } |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 83 | |
| 84 | return gh, nil |
| 85 | } |
| 86 | |
Jamie Hannaford | 93209fe | 2014-10-10 11:54:19 +0200 | [diff] [blame] | 87 | // ExtractMetadata is a function that takes a GetResult (of type *http.Response) |
| 88 | // and returns the custom metatdata associated with the account. |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame] | 89 | func (gr GetResult) ExtractMetadata() (map[string]string, error) { |
| 90 | if gr.Err != nil { |
| 91 | return nil, gr.Err |
| 92 | } |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 93 | |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame] | 94 | metadata := make(map[string]string) |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 95 | for k, v := range gr.Header { |
Jamie Hannaford | 93209fe | 2014-10-10 11:54:19 +0200 | [diff] [blame] | 96 | if strings.HasPrefix(k, "X-Account-Meta-") { |
| 97 | key := strings.TrimPrefix(k, "X-Account-Meta-") |
| 98 | metadata[key] = v[0] |
| 99 | } |
| 100 | } |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame] | 101 | return metadata, nil |
| 102 | } |