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 | 63e7a48 | 2014-12-04 09:47:23 -0700 | [diff] [blame] | 8 | "reflect" |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame^] | 9 | "time" |
Jon Perritt | 63e7a48 | 2014-12-04 09:47:23 -0700 | [diff] [blame] | 10 | |
| 11 | "github.com/mitchellh/mapstructure" |
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 | } |
| 53 | jsonDecoder := json.NewDecoder(reader) |
| 54 | return jsonDecoder.Decode(to) |
| 55 | } |
| 56 | |
| 57 | b, err := json.Marshal(r.Body) |
| 58 | if err != nil { |
| 59 | return err |
| 60 | } |
| 61 | err = json.Unmarshal(b, to) |
| 62 | |
| 63 | return err |
| 64 | } |
| 65 | |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 66 | // PrettyPrintJSON creates a string containing the full response body as |
| 67 | // pretty-printed JSON. It's useful for capturing test fixtures and for |
Ash Wilson | 0fe6c96 | 2014-10-31 15:34:24 -0400 | [diff] [blame] | 68 | // debugging extraction bugs. If you include its output in an issue related to |
| 69 | // a buggy extraction function, we will all love you forever. |
Ash Wilson | e8192ac | 2014-10-21 09:02:01 -0400 | [diff] [blame] | 70 | func (r Result) PrettyPrintJSON() string { |
| 71 | pretty, err := json.MarshalIndent(r.Body, "", " ") |
| 72 | if err != nil { |
| 73 | panic(err.Error()) |
| 74 | } |
| 75 | return string(pretty) |
| 76 | } |
| 77 | |
Ash Wilson | 64ba49f | 2014-10-31 15:31:46 -0400 | [diff] [blame] | 78 | // ErrResult is an internal type to be used by individual resource packages, but |
| 79 | // its methods will be available on a wide variety of user-facing embedding |
| 80 | // types. |
| 81 | // |
| 82 | // It represents results that only contain a potential error and |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 83 | // nothing else. Usually, if the operation executed successfully, the Err field |
| 84 | // 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] | 85 | // ExtractErr method |
| 86 | // to cleanly pull it out. |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 87 | type ErrResult struct { |
Jon Perritt | 0c2b037 | 2014-10-27 15:57:29 -0500 | [diff] [blame] | 88 | Result |
Jamie Hannaford | 021b35c | 2014-10-27 14:01:53 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 91 | // 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] | 92 | func (r ErrResult) ExtractErr() error { |
Jamie Hannaford | 021b35c | 2014-10-27 14:01:53 +0100 | [diff] [blame] | 93 | return r.Err |
| 94 | } |
| 95 | |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 96 | /* |
Ash Wilson | 64ba49f | 2014-10-31 15:31:46 -0400 | [diff] [blame] | 97 | HeaderResult is an internal type to be used by individual resource packages, but |
| 98 | 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] | 99 | |
| 100 | It represents a result that only contains an error (possibly nil) and an |
| 101 | http.Header. This is used, for example, by the objectstorage packages in |
| 102 | openstack, because most of the operations don't return response bodies, but do |
| 103 | have relevant information in headers. |
| 104 | */ |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 105 | type HeaderResult struct { |
Jon Perritt | 0c2b037 | 2014-10-27 15:57:29 -0500 | [diff] [blame] | 106 | Result |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | // ExtractHeader will return the http.Header and error from the HeaderResult. |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 110 | // |
| 111 | // header, err := objects.Create(client, "my_container", objects.CreateOpts{}).ExtractHeader() |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 112 | func (hr HeaderResult) ExtractHeader() (http.Header, error) { |
| 113 | return hr.Header, hr.Err |
| 114 | } |
| 115 | |
Jon Perritt | 63e7a48 | 2014-12-04 09:47:23 -0700 | [diff] [blame] | 116 | // DecodeHeader is a function that decodes a header (usually of type map[string]interface{}) to |
| 117 | // another type (usually a struct). This function is used by the objectstorage package to give |
| 118 | // users access to response headers without having to query a map. A DecodeHookFunction is used, |
| 119 | // because OpenStack-based clients return header values as arrays (Go slices). |
| 120 | func DecodeHeader(from, to interface{}) error { |
| 121 | config := &mapstructure.DecoderConfig{ |
| 122 | DecodeHook: func(from, to reflect.Kind, data interface{}) (interface{}, error) { |
| 123 | if from == reflect.Slice { |
| 124 | return data.([]string)[0], nil |
| 125 | } |
| 126 | return data, nil |
| 127 | }, |
| 128 | Result: to, |
| 129 | WeaklyTypedInput: true, |
| 130 | } |
| 131 | decoder, err := mapstructure.NewDecoder(config) |
| 132 | if err != nil { |
| 133 | return err |
| 134 | } |
| 135 | if err := decoder.Decode(from); err != nil { |
| 136 | return err |
| 137 | } |
| 138 | return nil |
| 139 | } |
| 140 | |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 141 | // RFC3339Milli describes a common time format used by some API responses. |
Ash Wilson | a6b0831 | 2014-10-02 15:27:45 -0400 | [diff] [blame] | 142 | const RFC3339Milli = "2006-01-02T15:04:05.999999Z" |
Jamie Hannaford | 369c9c6 | 2014-10-08 15:14:43 +0200 | [diff] [blame] | 143 | |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame^] | 144 | const RFC3339MilliNoZ = "2006-01-02T03:04:05.999999" |
| 145 | |
| 146 | type JSONRFC3339Milli time.Time |
| 147 | |
| 148 | func (jt *JSONRFC3339Milli) UnmarshalJSON(data []byte) error { |
| 149 | b := bytes.NewBuffer(data) |
| 150 | dec := json.NewDecoder(b) |
| 151 | var s string |
| 152 | if err := dec.Decode(&s); err != nil { |
| 153 | return err |
| 154 | } |
| 155 | t, err := time.Parse(RFC3339Milli, s) |
| 156 | if err != nil { |
| 157 | return err |
| 158 | } |
| 159 | *jt = JSONRFC3339Milli(t) |
| 160 | return nil |
| 161 | } |
| 162 | |
| 163 | type JSONRFC3339MilliNoZ time.Time |
| 164 | |
| 165 | func (jt *JSONRFC3339MilliNoZ) UnmarshalJSON(data []byte) error { |
| 166 | b := bytes.NewBuffer(data) |
| 167 | dec := json.NewDecoder(b) |
| 168 | var s string |
| 169 | if err := dec.Decode(&s); err != nil { |
| 170 | return err |
| 171 | } |
| 172 | t, err := time.Parse(RFC3339MilliNoZ, s) |
| 173 | if err != nil { |
| 174 | return err |
| 175 | } |
| 176 | *jt = JSONRFC3339MilliNoZ(t) |
| 177 | return nil |
| 178 | } |
| 179 | |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 180 | // StackFmtTime is the time format used in Heat (Orchestration). |
| 181 | const StackFmtTime = "2006-01-02T15:04:05" |
Pratik Mallya | e1b6cbb | 2015-09-09 14:24:14 -0500 | [diff] [blame] | 182 | |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 183 | /* |
| 184 | Link is an internal type to be used in packages of collection resources that are |
| 185 | paginated in a certain way. |
| 186 | |
| 187 | It's a response substructure common to many paginated collection results that is |
| 188 | used to point to related pages. Usually, the one we care about is the one with |
| 189 | Rel field set to "next". |
| 190 | */ |
Jamie Hannaford | 369c9c6 | 2014-10-08 15:14:43 +0200 | [diff] [blame] | 191 | type Link struct { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame^] | 192 | Href string `json:"href"` |
| 193 | Rel string `json:"rel"` |
Jamie Hannaford | 369c9c6 | 2014-10-08 15:14:43 +0200 | [diff] [blame] | 194 | } |
| 195 | |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 196 | /* |
| 197 | ExtractNextURL is an internal function useful for packages of collection |
| 198 | resources that are paginated in a certain way. |
| 199 | |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 200 | 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] | 201 | "" if no such URL is present. |
| 202 | */ |
Jamie Hannaford | 369c9c6 | 2014-10-08 15:14:43 +0200 | [diff] [blame] | 203 | func ExtractNextURL(links []Link) (string, error) { |
| 204 | var url string |
| 205 | |
| 206 | for _, l := range links { |
| 207 | if l.Rel == "next" { |
| 208 | url = l.Href |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | if url == "" { |
| 213 | return "", nil |
| 214 | } |
| 215 | |
| 216 | return url, nil |
| 217 | } |