Jamie Hannaford | 4baa123 | 2014-09-23 15:23:04 +0200 | [diff] [blame] | 1 | package gophercloud |
Jamie Hannaford | b3120f5 | 2014-09-23 15:17:57 +0200 | [diff] [blame] | 2 | |
Ash Wilson | e8192ac | 2014-10-21 09:02:01 -0400 | [diff] [blame] | 3 | import ( |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 4 | "bytes" |
Ash Wilson | e8192ac | 2014-10-21 09:02:01 -0400 | [diff] [blame] | 5 | "encoding/json" |
jrperritt | 613bea2 | 2016-11-08 16:58:10 -0600 | [diff] [blame] | 6 | "fmt" |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 7 | "io" |
Ash Wilson | e8192ac | 2014-10-21 09:02:01 -0400 | [diff] [blame] | 8 | "net/http" |
jrperritt | 613bea2 | 2016-11-08 16:58:10 -0600 | [diff] [blame] | 9 | "reflect" |
Jon Perritt | 6682282 | 2016-02-25 03:06:56 -0600 | [diff] [blame] | 10 | "strconv" |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 11 | "time" |
Ash Wilson | e8192ac | 2014-10-21 09:02:01 -0400 | [diff] [blame] | 12 | ) |
Ash Wilson | eab6a70 | 2014-10-20 08:18:30 -0400 | [diff] [blame] | 13 | |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 14 | /* |
Ash Wilson | 64ba49f | 2014-10-31 15:31:46 -0400 | [diff] [blame] | 15 | Result is an internal type to be used by individual resource packages, but its |
| 16 | methods will be available on a wide variety of user-facing embedding types. |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 17 | |
| 18 | It acts as a base struct that other Result types, returned from request |
| 19 | functions, can embed for convenience. All Results capture basic information |
| 20 | from the HTTP transaction that was performed, including the response body, |
| 21 | HTTP headers, and any errors that happened. |
| 22 | |
| 23 | Generally, each Result type will have an Extract method that can be used to |
| 24 | further interpret the result's payload in a specific context. Extensions or |
| 25 | providers can then provide additional extraction functions to pull out |
| 26 | provider- or extension-specific information as well. |
| 27 | */ |
Ash Wilson | eab6a70 | 2014-10-20 08:18:30 -0400 | [diff] [blame] | 28 | type Result struct { |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 29 | // Body is the payload of the HTTP response from the server. In most cases, |
| 30 | // this will be the deserialized JSON structure. |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 31 | Body interface{} |
Ash Wilson | eab6a70 | 2014-10-20 08:18:30 -0400 | [diff] [blame] | 32 | |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 33 | // Header contains the HTTP header structure from the original response. |
| 34 | Header http.Header |
Ash Wilson | eab6a70 | 2014-10-20 08:18:30 -0400 | [diff] [blame] | 35 | |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 36 | // Err is an error that occurred during the operation. It's deferred until |
| 37 | // extraction to make it easier to chain the Extract call. |
Ash Wilson | eab6a70 | 2014-10-20 08:18:30 -0400 | [diff] [blame] | 38 | Err error |
Jamie Hannaford | b3120f5 | 2014-09-23 15:17:57 +0200 | [diff] [blame] | 39 | } |
Ash Wilson | a6b0831 | 2014-10-02 15:27:45 -0400 | [diff] [blame] | 40 | |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 41 | // ExtractInto allows users to provide an object into which `Extract` will extract |
| 42 | // the `Result.Body`. This would be useful for OpenStack providers that have |
| 43 | // different fields in the response object than OpenStack proper. |
| 44 | func (r Result) ExtractInto(to interface{}) error { |
| 45 | if r.Err != nil { |
| 46 | return r.Err |
| 47 | } |
| 48 | |
| 49 | if reader, ok := r.Body.(io.Reader); ok { |
| 50 | if readCloser, ok := reader.(io.Closer); ok { |
| 51 | defer readCloser.Close() |
| 52 | } |
Jon Perritt | a33da23 | 2016-03-02 04:43:08 -0600 | [diff] [blame] | 53 | return json.NewDecoder(reader).Decode(to) |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | b, err := json.Marshal(r.Body) |
| 57 | if err != nil { |
| 58 | return err |
| 59 | } |
| 60 | err = json.Unmarshal(b, to) |
| 61 | |
| 62 | return err |
| 63 | } |
| 64 | |
jrperritt | 613bea2 | 2016-11-08 16:58:10 -0600 | [diff] [blame] | 65 | func (r Result) extractIntoPtr(to interface{}, label string) error { |
| 66 | if r.Err != nil { |
| 67 | return r.Err |
| 68 | } |
| 69 | |
jrperritt | 41a7078 | 2016-11-09 14:40:46 -0600 | [diff] [blame^] | 70 | if label == "" { |
| 71 | return r.ExtractInto(&to) |
jrperritt | 613bea2 | 2016-11-08 16:58:10 -0600 | [diff] [blame] | 72 | } |
| 73 | |
jrperritt | 41a7078 | 2016-11-09 14:40:46 -0600 | [diff] [blame^] | 74 | var m map[string]interface{} |
| 75 | err := r.ExtractInto(&m) |
| 76 | if err != nil { |
| 77 | return err |
jrperritt | 613bea2 | 2016-11-08 16:58:10 -0600 | [diff] [blame] | 78 | } |
jrperritt | 41a7078 | 2016-11-09 14:40:46 -0600 | [diff] [blame^] | 79 | |
| 80 | b, err := json.Marshal(m[label]) |
jrperritt | 613bea2 | 2016-11-08 16:58:10 -0600 | [diff] [blame] | 81 | if err != nil { |
| 82 | return err |
| 83 | } |
| 84 | |
| 85 | err = json.Unmarshal(b, &to) |
| 86 | return err |
| 87 | } |
| 88 | |
jrperritt | 41a7078 | 2016-11-09 14:40:46 -0600 | [diff] [blame^] | 89 | // ExtractIntoStructPtr will unmarshal the Result (r) into the provided |
| 90 | // interface{} (to). |
| 91 | // |
| 92 | // `to` must be a pointer to an underlying struct type |
| 93 | // |
| 94 | // If provided, `label` will be filtered out of the response |
| 95 | // body prior to `r` being unmarshalled into `to`. |
jrperritt | 613bea2 | 2016-11-08 16:58:10 -0600 | [diff] [blame] | 96 | func (r Result) ExtractIntoStructPtr(to interface{}, label string) error { |
jrperritt | 41a7078 | 2016-11-09 14:40:46 -0600 | [diff] [blame^] | 97 | t := reflect.TypeOf(to) |
| 98 | if k := t.Kind(); k != reflect.Ptr { |
| 99 | return fmt.Errorf("Expected pointer, got %v", k) |
| 100 | } |
| 101 | switch t.Elem().Kind() { |
| 102 | case reflect.Struct: |
| 103 | return r.extractIntoPtr(to, label) |
| 104 | default: |
| 105 | return fmt.Errorf("Expected pointer to struct, got: %v", t) |
| 106 | } |
jrperritt | 613bea2 | 2016-11-08 16:58:10 -0600 | [diff] [blame] | 107 | } |
| 108 | |
jrperritt | 41a7078 | 2016-11-09 14:40:46 -0600 | [diff] [blame^] | 109 | // ExtractIntoSlicePtr will unmarshal the Result (r) into the provided |
| 110 | // interface{} (to). |
| 111 | // |
| 112 | // `to` must be a pointer to an underlying slice type |
| 113 | // |
| 114 | // If provided, `label` will be filtered out of the response |
| 115 | // body prior to `r` being unmarshalled into `to`. |
jrperritt | 613bea2 | 2016-11-08 16:58:10 -0600 | [diff] [blame] | 116 | func (r Result) ExtractIntoSlicePtr(to interface{}, label string) error { |
jrperritt | 41a7078 | 2016-11-09 14:40:46 -0600 | [diff] [blame^] | 117 | t := reflect.TypeOf(to) |
| 118 | if k := t.Kind(); k != reflect.Ptr { |
| 119 | return fmt.Errorf("Expected pointer, got %v", k) |
| 120 | } |
| 121 | switch t.Elem().Kind() { |
| 122 | case reflect.Slice: |
| 123 | return r.extractIntoPtr(to, label) |
| 124 | default: |
| 125 | return fmt.Errorf("Expected pointer to slice, got: %v", t) |
| 126 | } |
jrperritt | 613bea2 | 2016-11-08 16:58:10 -0600 | [diff] [blame] | 127 | } |
| 128 | |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 129 | // PrettyPrintJSON creates a string containing the full response body as |
| 130 | // pretty-printed JSON. It's useful for capturing test fixtures and for |
Ash Wilson | 0fe6c96 | 2014-10-31 15:34:24 -0400 | [diff] [blame] | 131 | // debugging extraction bugs. If you include its output in an issue related to |
| 132 | // a buggy extraction function, we will all love you forever. |
Ash Wilson | e8192ac | 2014-10-21 09:02:01 -0400 | [diff] [blame] | 133 | func (r Result) PrettyPrintJSON() string { |
| 134 | pretty, err := json.MarshalIndent(r.Body, "", " ") |
| 135 | if err != nil { |
| 136 | panic(err.Error()) |
| 137 | } |
| 138 | return string(pretty) |
| 139 | } |
| 140 | |
Ash Wilson | 64ba49f | 2014-10-31 15:31:46 -0400 | [diff] [blame] | 141 | // ErrResult is an internal type to be used by individual resource packages, but |
| 142 | // its methods will be available on a wide variety of user-facing embedding |
| 143 | // types. |
| 144 | // |
| 145 | // It represents results that only contain a potential error and |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 146 | // nothing else. Usually, if the operation executed successfully, the Err field |
| 147 | // will be nil; otherwise it will be stocked with a relevant error. Use the |
Ash Wilson | 64ba49f | 2014-10-31 15:31:46 -0400 | [diff] [blame] | 148 | // ExtractErr method |
| 149 | // to cleanly pull it out. |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 150 | type ErrResult struct { |
Jon Perritt | 0c2b037 | 2014-10-27 15:57:29 -0500 | [diff] [blame] | 151 | Result |
Jamie Hannaford | 021b35c | 2014-10-27 14:01:53 +0100 | [diff] [blame] | 152 | } |
| 153 | |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 154 | // ExtractErr is a function that extracts error information, or nil, from a result. |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 155 | func (r ErrResult) ExtractErr() error { |
Jamie Hannaford | 021b35c | 2014-10-27 14:01:53 +0100 | [diff] [blame] | 156 | return r.Err |
| 157 | } |
| 158 | |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 159 | /* |
Ash Wilson | 64ba49f | 2014-10-31 15:31:46 -0400 | [diff] [blame] | 160 | HeaderResult is an internal type to be used by individual resource packages, but |
| 161 | its methods will be available on a wide variety of user-facing embedding types. |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 162 | |
| 163 | It represents a result that only contains an error (possibly nil) and an |
| 164 | http.Header. This is used, for example, by the objectstorage packages in |
| 165 | openstack, because most of the operations don't return response bodies, but do |
| 166 | have relevant information in headers. |
| 167 | */ |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 168 | type HeaderResult struct { |
Jon Perritt | 0c2b037 | 2014-10-27 15:57:29 -0500 | [diff] [blame] | 169 | Result |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // ExtractHeader will return the http.Header and error from the HeaderResult. |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 173 | // |
| 174 | // header, err := objects.Create(client, "my_container", objects.CreateOpts{}).ExtractHeader() |
Jon Perritt | 6682282 | 2016-02-25 03:06:56 -0600 | [diff] [blame] | 175 | func (r HeaderResult) ExtractInto(to interface{}) error { |
| 176 | if r.Err != nil { |
| 177 | return r.Err |
Jon Perritt | 63e7a48 | 2014-12-04 09:47:23 -0700 | [diff] [blame] | 178 | } |
Jon Perritt | 6682282 | 2016-02-25 03:06:56 -0600 | [diff] [blame] | 179 | |
| 180 | tmpHeaderMap := map[string]string{} |
| 181 | for k, v := range r.Header { |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 182 | if len(v) > 0 { |
| 183 | tmpHeaderMap[k] = v[0] |
| 184 | } |
Jon Perritt | 6682282 | 2016-02-25 03:06:56 -0600 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | b, err := json.Marshal(tmpHeaderMap) |
Jon Perritt | 63e7a48 | 2014-12-04 09:47:23 -0700 | [diff] [blame] | 188 | if err != nil { |
| 189 | return err |
| 190 | } |
Jon Perritt | 6682282 | 2016-02-25 03:06:56 -0600 | [diff] [blame] | 191 | err = json.Unmarshal(b, to) |
| 192 | |
| 193 | return err |
Jon Perritt | 63e7a48 | 2014-12-04 09:47:23 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 196 | // RFC3339Milli describes a common time format used by some API responses. |
Ash Wilson | a6b0831 | 2014-10-02 15:27:45 -0400 | [diff] [blame] | 197 | const RFC3339Milli = "2006-01-02T15:04:05.999999Z" |
Jamie Hannaford | 369c9c6 | 2014-10-08 15:14:43 +0200 | [diff] [blame] | 198 | |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 199 | type JSONRFC3339Milli time.Time |
| 200 | |
| 201 | func (jt *JSONRFC3339Milli) UnmarshalJSON(data []byte) error { |
| 202 | b := bytes.NewBuffer(data) |
| 203 | dec := json.NewDecoder(b) |
| 204 | var s string |
| 205 | if err := dec.Decode(&s); err != nil { |
| 206 | return err |
| 207 | } |
| 208 | t, err := time.Parse(RFC3339Milli, s) |
| 209 | if err != nil { |
| 210 | return err |
| 211 | } |
| 212 | *jt = JSONRFC3339Milli(t) |
| 213 | return nil |
| 214 | } |
| 215 | |
jrperritt | 9b7b9e6 | 2016-07-11 22:30:50 -0500 | [diff] [blame] | 216 | const RFC3339MilliNoZ = "2006-01-02T15:04:05.999999" |
Jon Perritt | 6682282 | 2016-02-25 03:06:56 -0600 | [diff] [blame] | 217 | |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 218 | type JSONRFC3339MilliNoZ time.Time |
| 219 | |
| 220 | func (jt *JSONRFC3339MilliNoZ) UnmarshalJSON(data []byte) error { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 221 | var s string |
Jon Perritt | 82583e7 | 2016-02-25 06:41:51 -0600 | [diff] [blame] | 222 | if err := json.Unmarshal(data, &s); err != nil { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 223 | return err |
| 224 | } |
Jon Perritt | 82583e7 | 2016-02-25 06:41:51 -0600 | [diff] [blame] | 225 | if s == "" { |
| 226 | return nil |
| 227 | } |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 228 | t, err := time.Parse(RFC3339MilliNoZ, s) |
| 229 | if err != nil { |
| 230 | return err |
| 231 | } |
| 232 | *jt = JSONRFC3339MilliNoZ(t) |
| 233 | return nil |
| 234 | } |
| 235 | |
Jon Perritt | 6682282 | 2016-02-25 03:06:56 -0600 | [diff] [blame] | 236 | type JSONRFC1123 time.Time |
| 237 | |
| 238 | func (jt *JSONRFC1123) UnmarshalJSON(data []byte) error { |
Jon Perritt | 6682282 | 2016-02-25 03:06:56 -0600 | [diff] [blame] | 239 | var s string |
Jon Perritt | 82583e7 | 2016-02-25 06:41:51 -0600 | [diff] [blame] | 240 | if err := json.Unmarshal(data, &s); err != nil { |
Jon Perritt | 6682282 | 2016-02-25 03:06:56 -0600 | [diff] [blame] | 241 | return err |
| 242 | } |
Jon Perritt | 82583e7 | 2016-02-25 06:41:51 -0600 | [diff] [blame] | 243 | if s == "" { |
| 244 | return nil |
| 245 | } |
Jon Perritt | 6682282 | 2016-02-25 03:06:56 -0600 | [diff] [blame] | 246 | t, err := time.Parse(time.RFC1123, s) |
| 247 | if err != nil { |
| 248 | return err |
| 249 | } |
| 250 | *jt = JSONRFC1123(t) |
| 251 | return nil |
| 252 | } |
| 253 | |
| 254 | type JSONUnix time.Time |
| 255 | |
| 256 | func (jt *JSONUnix) UnmarshalJSON(data []byte) error { |
Jon Perritt | 6682282 | 2016-02-25 03:06:56 -0600 | [diff] [blame] | 257 | var s string |
Jon Perritt | 82583e7 | 2016-02-25 06:41:51 -0600 | [diff] [blame] | 258 | if err := json.Unmarshal(data, &s); err != nil { |
Jon Perritt | 6682282 | 2016-02-25 03:06:56 -0600 | [diff] [blame] | 259 | return err |
| 260 | } |
Jon Perritt | 82583e7 | 2016-02-25 06:41:51 -0600 | [diff] [blame] | 261 | if s == "" { |
| 262 | return nil |
| 263 | } |
Jon Perritt | 6682282 | 2016-02-25 03:06:56 -0600 | [diff] [blame] | 264 | unix, err := strconv.ParseInt(s, 10, 64) |
| 265 | if err != nil { |
| 266 | return err |
| 267 | } |
| 268 | t = time.Unix(unix, 0) |
| 269 | *jt = JSONUnix(t) |
| 270 | return nil |
| 271 | } |
| 272 | |
Jon Perritt | 31b6646 | 2016-02-25 22:25:30 -0600 | [diff] [blame] | 273 | // RFC3339NoZ is the time format used in Heat (Orchestration). |
Jon Perritt | 6682282 | 2016-02-25 03:06:56 -0600 | [diff] [blame] | 274 | const RFC3339NoZ = "2006-01-02T15:04:05" |
| 275 | |
| 276 | type JSONRFC3339NoZ time.Time |
| 277 | |
| 278 | func (jt *JSONRFC3339NoZ) UnmarshalJSON(data []byte) error { |
Jon Perritt | 6682282 | 2016-02-25 03:06:56 -0600 | [diff] [blame] | 279 | var s string |
Jon Perritt | 82583e7 | 2016-02-25 06:41:51 -0600 | [diff] [blame] | 280 | if err := json.Unmarshal(data, &s); err != nil { |
Jon Perritt | 6682282 | 2016-02-25 03:06:56 -0600 | [diff] [blame] | 281 | return err |
| 282 | } |
| 283 | if s == "" { |
| 284 | return nil |
| 285 | } |
| 286 | t, err := time.Parse(RFC3339NoZ, s) |
| 287 | if err != nil { |
| 288 | return err |
| 289 | } |
| 290 | *jt = JSONRFC3339NoZ(t) |
| 291 | return nil |
| 292 | } |
Pratik Mallya | e1b6cbb | 2015-09-09 14:24:14 -0500 | [diff] [blame] | 293 | |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 294 | /* |
| 295 | Link is an internal type to be used in packages of collection resources that are |
| 296 | paginated in a certain way. |
| 297 | |
| 298 | It's a response substructure common to many paginated collection results that is |
| 299 | used to point to related pages. Usually, the one we care about is the one with |
| 300 | Rel field set to "next". |
| 301 | */ |
Jamie Hannaford | 369c9c6 | 2014-10-08 15:14:43 +0200 | [diff] [blame] | 302 | type Link struct { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 303 | Href string `json:"href"` |
| 304 | Rel string `json:"rel"` |
Jamie Hannaford | 369c9c6 | 2014-10-08 15:14:43 +0200 | [diff] [blame] | 305 | } |
| 306 | |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 307 | /* |
| 308 | ExtractNextURL is an internal function useful for packages of collection |
| 309 | resources that are paginated in a certain way. |
| 310 | |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 311 | It attempts to extract the "next" URL from slice of Link structs, or |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 312 | "" if no such URL is present. |
| 313 | */ |
Jamie Hannaford | 369c9c6 | 2014-10-08 15:14:43 +0200 | [diff] [blame] | 314 | func ExtractNextURL(links []Link) (string, error) { |
| 315 | var url string |
| 316 | |
| 317 | for _, l := range links { |
| 318 | if l.Rel == "next" { |
| 319 | url = l.Href |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | if url == "" { |
| 324 | return "", nil |
| 325 | } |
| 326 | |
| 327 | return url, nil |
| 328 | } |