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