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