Jamie Hannaford | 93209fe | 2014-10-10 11:54:19 +0200 | [diff] [blame] | 1 | package accounts |
| 2 | |
| 3 | import ( |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 4 | "encoding/json" |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 5 | "strconv" |
Jamie Hannaford | 93209fe | 2014-10-10 11:54:19 +0200 | [diff] [blame] | 6 | "strings" |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 7 | "time" |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 8 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 9 | "github.com/gophercloud/gophercloud" |
Jamie Hannaford | 93209fe | 2014-10-10 11:54:19 +0200 | [diff] [blame] | 10 | ) |
| 11 | |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 12 | // UpdateResult is returned from a call to the Update function. |
| 13 | type UpdateResult struct { |
| 14 | gophercloud.HeaderResult |
| 15 | } |
| 16 | |
| 17 | // UpdateHeader represents the headers returned in the response from an Update request. |
| 18 | type UpdateHeader struct { |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 19 | ContentLength int64 `json:"-"` |
| 20 | ContentType string `json:"Content-Type"` |
| 21 | TransID string `json:"X-Trans-Id"` |
| 22 | Date time.Time `json:"-"` |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 23 | } |
| 24 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 25 | func (r *UpdateHeader) UnmarshalJSON(b []byte) error { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 26 | type tmp UpdateHeader |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 27 | var s struct { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 28 | tmp |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 29 | ContentLength string `json:"Content-Length"` |
| 30 | Date gophercloud.JSONRFC1123 `json:"Date"` |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 31 | } |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 32 | err := json.Unmarshal(b, &s) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 33 | if err != nil { |
| 34 | return err |
| 35 | } |
| 36 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 37 | *r = UpdateHeader(s.tmp) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 38 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 39 | switch s.ContentLength { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 40 | case "": |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 41 | r.ContentLength = 0 |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 42 | default: |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 43 | r.ContentLength, err = strconv.ParseInt(s.ContentLength, 10, 64) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 44 | if err != nil { |
| 45 | return err |
| 46 | } |
| 47 | } |
| 48 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 49 | r.Date = time.Time(s.Date) |
| 50 | |
| 51 | return err |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | // Extract will return a struct of headers returned from a call to Get. To obtain |
| 55 | // a map of headers, call the ExtractHeader method on the GetResult. |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 56 | func (r UpdateResult) Extract() (*UpdateHeader, error) { |
| 57 | var s *UpdateHeader |
| 58 | err := r.ExtractInto(&s) |
| 59 | return s, err |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | // GetHeader represents the headers returned in the response from a Get request. |
| 63 | type GetHeader struct { |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 64 | BytesUsed int64 `json:"-"` |
| 65 | ContainerCount int64 `json:"-"` |
| 66 | ContentLength int64 `json:"-"` |
| 67 | ObjectCount int64 `json:"-"` |
| 68 | ContentType string `json:"Content-Type"` |
| 69 | TransID string `json:"X-Trans-Id"` |
| 70 | TempURLKey string `json:"X-Account-Meta-Temp-URL-Key"` |
| 71 | TempURLKey2 string `json:"X-Account-Meta-Temp-URL-Key-2"` |
| 72 | Date time.Time `json:"-"` |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 73 | } |
| 74 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 75 | func (r *GetHeader) UnmarshalJSON(b []byte) error { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 76 | type tmp GetHeader |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 77 | var s struct { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 78 | tmp |
| 79 | BytesUsed string `json:"X-Account-Bytes-Used"` |
| 80 | ContentLength string `json:"Content-Length"` |
| 81 | ContainerCount string `json:"X-Account-Container-Count"` |
| 82 | ObjectCount string `json:"X-Account-Object-Count"` |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 83 | Date string `json:"Date"` |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 84 | } |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 85 | err := json.Unmarshal(b, &s) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 86 | if err != nil { |
| 87 | return err |
| 88 | } |
| 89 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 90 | *r = GetHeader(s.tmp) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 91 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 92 | switch s.BytesUsed { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 93 | case "": |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 94 | r.BytesUsed = 0 |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 95 | default: |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 96 | r.BytesUsed, err = strconv.ParseInt(s.BytesUsed, 10, 64) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 97 | if err != nil { |
| 98 | return err |
| 99 | } |
| 100 | } |
| 101 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 102 | switch s.ContentLength { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 103 | case "": |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 104 | r.ContentLength = 0 |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 105 | default: |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 106 | r.ContentLength, err = strconv.ParseInt(s.ContentLength, 10, 64) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 107 | if err != nil { |
| 108 | return err |
| 109 | } |
| 110 | } |
| 111 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 112 | switch s.ObjectCount { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 113 | case "": |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 114 | r.ObjectCount = 0 |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 115 | default: |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 116 | r.ObjectCount, err = strconv.ParseInt(s.ObjectCount, 10, 64) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 117 | if err != nil { |
| 118 | return err |
| 119 | } |
| 120 | } |
| 121 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 122 | switch s.ContainerCount { |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 123 | case "": |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 124 | r.ContainerCount = 0 |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 125 | default: |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 126 | r.ContainerCount, err = strconv.ParseInt(s.ContainerCount, 10, 64) |
jrperritt | 655245a | 2016-08-31 15:30:27 -0500 | [diff] [blame] | 127 | if err != nil { |
| 128 | return err |
| 129 | } |
| 130 | } |
| 131 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 132 | if s.Date != "" { |
| 133 | r.Date, err = time.Parse(time.RFC1123, s.Date) |
| 134 | } |
| 135 | |
| 136 | return err |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 137 | } |
| 138 | |
Ash Wilson | af26287 | 2014-10-20 09:32:29 -0400 | [diff] [blame] | 139 | // GetResult is returned from a call to the Get function. |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 140 | type GetResult struct { |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 141 | gophercloud.HeaderResult |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 142 | } |
Jamie Hannaford | 93209fe | 2014-10-10 11:54:19 +0200 | [diff] [blame] | 143 | |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 144 | // Extract will return a struct of headers returned from a call to Get. To obtain |
| 145 | // a map of headers, call the ExtractHeader method on the GetResult. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 146 | func (r GetResult) Extract() (*GetHeader, error) { |
| 147 | var s *GetHeader |
| 148 | err := r.ExtractInto(&s) |
| 149 | return s, err |
Jon Perritt | ae06ab7 | 2014-11-06 18:18:55 -0600 | [diff] [blame] | 150 | } |
| 151 | |
jrperritt | 98d0162 | 2017-01-12 14:24:42 -0600 | [diff] [blame] | 152 | // ExtractMetadata is a function that takes a GetResult (of type *htts.Response) |
Jamie Hannaford | 93209fe | 2014-10-10 11:54:19 +0200 | [diff] [blame] | 153 | // and returns the custom metatdata associated with the account. |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 154 | func (r GetResult) ExtractMetadata() (map[string]string, error) { |
| 155 | if r.Err != nil { |
| 156 | return nil, r.Err |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame] | 157 | } |
Jamie Hannaford | b2e129d | 2014-10-10 14:06:28 +0200 | [diff] [blame] | 158 | |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame] | 159 | metadata := make(map[string]string) |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 160 | for k, v := range r.Header { |
Jamie Hannaford | 93209fe | 2014-10-10 11:54:19 +0200 | [diff] [blame] | 161 | if strings.HasPrefix(k, "X-Account-Meta-") { |
| 162 | key := strings.TrimPrefix(k, "X-Account-Meta-") |
| 163 | metadata[key] = v[0] |
| 164 | } |
| 165 | } |
Jon Perritt | 4a59d23 | 2014-10-09 20:21:31 -0500 | [diff] [blame] | 166 | return metadata, nil |
| 167 | } |