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