Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 1 | package servers |
| 2 | |
| 3 | import ( |
Rickard von Essen | 5b8bbff | 2016-02-16 07:48:20 +0100 | [diff] [blame] | 4 | "crypto/rsa" |
| 5 | "encoding/base64" |
einarf | 4e5fdaf | 2015-04-16 23:14:59 +0000 | [diff] [blame] | 6 | "fmt" |
einarf | 4e5fdaf | 2015-04-16 23:14:59 +0000 | [diff] [blame] | 7 | "net/url" |
Rickard von Essen | 5b8bbff | 2016-02-16 07:48:20 +0100 | [diff] [blame] | 8 | "path" |
| 9 | "reflect" |
Jon Perritt | 9a0980e | 2015-01-14 21:29:44 -0700 | [diff] [blame] | 10 | |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 11 | "github.com/mitchellh/mapstructure" |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 12 | "github.com/rackspace/gophercloud" |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 13 | "github.com/rackspace/gophercloud/pagination" |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 14 | ) |
| 15 | |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 16 | type serverResult struct { |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 17 | gophercloud.Result |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | // Extract interprets any serverResult as a Server, if possible. |
| 21 | func (r serverResult) Extract() (*Server, error) { |
| 22 | if r.Err != nil { |
| 23 | return nil, r.Err |
| 24 | } |
| 25 | |
| 26 | var response struct { |
| 27 | Server Server `mapstructure:"server"` |
| 28 | } |
| 29 | |
Jon Perritt | 9a0980e | 2015-01-14 21:29:44 -0700 | [diff] [blame] | 30 | config := &mapstructure.DecoderConfig{ |
Jon Perritt | 0e19f60 | 2015-01-15 09:35:46 -0700 | [diff] [blame] | 31 | DecodeHook: toMapFromString, |
| 32 | Result: &response, |
Jon Perritt | 9a0980e | 2015-01-14 21:29:44 -0700 | [diff] [blame] | 33 | } |
| 34 | decoder, err := mapstructure.NewDecoder(config) |
| 35 | if err != nil { |
| 36 | return nil, err |
| 37 | } |
| 38 | |
| 39 | err = decoder.Decode(r.Body) |
| 40 | if err != nil { |
| 41 | return nil, err |
| 42 | } |
| 43 | |
| 44 | return &response.Server, nil |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | // CreateResult temporarily contains the response from a Create call. |
| 48 | type CreateResult struct { |
| 49 | serverResult |
| 50 | } |
| 51 | |
| 52 | // GetResult temporarily contains the response from a Get call. |
| 53 | type GetResult struct { |
| 54 | serverResult |
| 55 | } |
| 56 | |
| 57 | // UpdateResult temporarily contains the response from an Update call. |
| 58 | type UpdateResult struct { |
| 59 | serverResult |
| 60 | } |
| 61 | |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 62 | // DeleteResult temporarily contains the response from a Delete call. |
Jamie Hannaford | 34732fe | 2014-10-27 11:29:36 +0100 | [diff] [blame] | 63 | type DeleteResult struct { |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 64 | gophercloud.ErrResult |
Jamie Hannaford | 34732fe | 2014-10-27 11:29:36 +0100 | [diff] [blame] | 65 | } |
| 66 | |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 67 | // RebuildResult temporarily contains the response from a Rebuild call. |
Jamie Hannaford | 01c1efe | 2014-10-16 15:08:59 +0200 | [diff] [blame] | 68 | type RebuildResult struct { |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 69 | serverResult |
| 70 | } |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 71 | |
Jamie Hannaford | 01c1efe | 2014-10-16 15:08:59 +0200 | [diff] [blame] | 72 | // ActionResult represents the result of server action operations, like reboot |
| 73 | type ActionResult struct { |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 74 | gophercloud.ErrResult |
Jamie Hannaford | 01c1efe | 2014-10-16 15:08:59 +0200 | [diff] [blame] | 75 | } |
| 76 | |
Alex Gaynor | 587e3e3 | 2014-11-13 10:39:09 -0800 | [diff] [blame] | 77 | // RescueResult represents the result of a server rescue operation |
Alex Gaynor | fbe61bb | 2014-11-12 13:35:03 -0800 | [diff] [blame] | 78 | type RescueResult struct { |
| 79 | ActionResult |
Alex Gaynor | 7f3b06e | 2014-11-13 09:54:03 -0800 | [diff] [blame] | 80 | } |
| 81 | |
einarf | 4e5fdaf | 2015-04-16 23:14:59 +0000 | [diff] [blame] | 82 | // CreateImageResult represents the result of an image creation operation |
| 83 | type CreateImageResult struct { |
einarf | 2fc665e | 2015-04-16 20:16:21 +0000 | [diff] [blame] | 84 | gophercloud.Result |
| 85 | } |
| 86 | |
Rickard von Essen | 5b8bbff | 2016-02-16 07:48:20 +0100 | [diff] [blame] | 87 | // GetPasswordResult represent the result of a get os-server-password operation. |
| 88 | type GetPasswordResult struct { |
| 89 | gophercloud.Result |
| 90 | } |
| 91 | |
| 92 | // ExtractPassword gets the encrypted password. |
| 93 | // If privateKey != nil the password is decrypted with the private key. |
| 94 | // If privateKey == nil the encrypted password is returned and can be decrypted with: |
| 95 | // echo '<pwd>' | base64 -D | openssl rsautl -decrypt -inkey <private_key> |
| 96 | func (r GetPasswordResult) ExtractPassword(privateKey *rsa.PrivateKey) (string, error) { |
| 97 | |
| 98 | if r.Err != nil { |
| 99 | return "", r.Err |
| 100 | } |
| 101 | |
| 102 | var response struct { |
| 103 | Password string `mapstructure:"password"` |
| 104 | } |
| 105 | |
| 106 | err := mapstructure.Decode(r.Body, &response) |
| 107 | if err == nil && privateKey != nil && response.Password != "" { |
| 108 | return decryptPassword(response.Password, privateKey) |
| 109 | } |
| 110 | return response.Password, err |
| 111 | } |
| 112 | |
| 113 | func decryptPassword(encryptedPassword string, privateKey *rsa.PrivateKey) (string, error) { |
| 114 | b64EncryptedPassword := make([]byte, base64.StdEncoding.DecodedLen(len(encryptedPassword))) |
| 115 | |
| 116 | n, err := base64.StdEncoding.Decode(b64EncryptedPassword, []byte(encryptedPassword)) |
| 117 | if err != nil { |
| 118 | return "", fmt.Errorf("Failed to base64 decode encrypted password: %s", err) |
| 119 | } |
| 120 | password, err := rsa.DecryptPKCS1v15(nil, privateKey, b64EncryptedPassword[0:n]) |
| 121 | if err != nil { |
| 122 | return "", fmt.Errorf("Failed to decrypt password: %s", err) |
| 123 | } |
| 124 | |
| 125 | return string(password), nil |
| 126 | } |
| 127 | |
einarf | 4e5fdaf | 2015-04-16 23:14:59 +0000 | [diff] [blame] | 128 | // ExtractImageID gets the ID of the newly created server image from the header |
| 129 | func (res CreateImageResult) ExtractImageID() (string, error) { |
| 130 | if res.Err != nil { |
| 131 | return "", res.Err |
| 132 | } |
| 133 | // Get the image id from the header |
| 134 | u, err := url.ParseRequestURI(res.Header.Get("Location")) |
| 135 | if err != nil { |
| 136 | return "", fmt.Errorf("Failed to parse the image id: %s", err.Error()) |
| 137 | } |
| 138 | imageId := path.Base(u.Path) |
| 139 | if imageId == "." || imageId == "/" { |
| 140 | return "", fmt.Errorf("Failed to parse the ID of newly created image: %s", u) |
| 141 | } |
| 142 | return imageId, nil |
| 143 | } |
| 144 | |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 145 | // Extract interprets any RescueResult as an AdminPass, if possible. |
Alex Gaynor | 7f3b06e | 2014-11-13 09:54:03 -0800 | [diff] [blame] | 146 | func (r RescueResult) Extract() (string, error) { |
| 147 | if r.Err != nil { |
Alex Gaynor | 94a28aa | 2014-11-13 10:09:56 -0800 | [diff] [blame] | 148 | return "", r.Err |
Alex Gaynor | 7f3b06e | 2014-11-13 09:54:03 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | var response struct { |
| 152 | AdminPass string `mapstructure:"adminPass"` |
| 153 | } |
| 154 | |
| 155 | err := mapstructure.Decode(r.Body, &response) |
Alex Gaynor | 94a28aa | 2014-11-13 10:09:56 -0800 | [diff] [blame] | 156 | return response.AdminPass, err |
Alex Gaynor | fbe61bb | 2014-11-12 13:35:03 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 159 | // Server exposes only the standard OpenStack fields corresponding to a given server on the user's account. |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 160 | type Server struct { |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 161 | // ID uniquely identifies this server amongst all other servers, including those not accessible to the current tenant. |
| 162 | ID string |
| 163 | |
| 164 | // TenantID identifies the tenant owning this server resource. |
| 165 | TenantID string `mapstructure:"tenant_id"` |
| 166 | |
| 167 | // UserID uniquely identifies the user account owning the tenant. |
| 168 | UserID string `mapstructure:"user_id"` |
| 169 | |
| 170 | // Name contains the human-readable name for the server. |
| 171 | Name string |
| 172 | |
| 173 | // Updated and Created contain ISO-8601 timestamps of when the state of the server last changed, and when it was created. |
| 174 | Updated string |
| 175 | Created string |
| 176 | |
| 177 | HostID string |
| 178 | |
| 179 | // Status contains the current operational status of the server, such as IN_PROGRESS or ACTIVE. |
| 180 | Status string |
| 181 | |
| 182 | // Progress ranges from 0..100. |
| 183 | // A request made against the server completes only once Progress reaches 100. |
| 184 | Progress int |
| 185 | |
| 186 | // AccessIPv4 and AccessIPv6 contain the IP addresses of the server, suitable for remote access for administration. |
Jamie Hannaford | 908e1e9 | 2014-10-27 14:41:17 +0100 | [diff] [blame] | 187 | AccessIPv4, AccessIPv6 string |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 188 | |
| 189 | // Image refers to a JSON object, which itself indicates the OS image used to deploy the server. |
| 190 | Image map[string]interface{} |
| 191 | |
| 192 | // Flavor refers to a JSON object, which itself indicates the hardware configuration of the deployed server. |
| 193 | Flavor map[string]interface{} |
| 194 | |
| 195 | // Addresses includes a list of all IP addresses assigned to the server, keyed by pool. |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 196 | Addresses map[string]interface{} |
| 197 | |
| 198 | // Metadata includes a list of all user-specified key-value pairs attached to the server. |
| 199 | Metadata map[string]interface{} |
| 200 | |
| 201 | // Links includes HTTP references to the itself, useful for passing along to other APIs that might want a server reference. |
| 202 | Links []interface{} |
| 203 | |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 204 | // KeyName indicates which public key was injected into the server on launch. |
Ash Wilson | 69b144f | 2014-10-21 14:03:52 -0400 | [diff] [blame] | 205 | KeyName string `json:"key_name" mapstructure:"key_name"` |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 206 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 207 | // AdminPass will generally be empty (""). However, it will contain the administrative password chosen when provisioning a new server without a set AdminPass setting in the first place. |
| 208 | // Note that this is the ONLY time this field will be valid. |
Ash Wilson | 69b144f | 2014-10-21 14:03:52 -0400 | [diff] [blame] | 209 | AdminPass string `json:"adminPass" mapstructure:"adminPass"` |
Joe Topjian | 978bb50 | 2015-02-12 20:55:31 +0000 | [diff] [blame] | 210 | |
| 211 | // SecurityGroups includes the security groups that this instance has applied to it |
| 212 | SecurityGroups []map[string]interface{} `json:"security_groups" mapstructure:"security_groups"` |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 215 | // ServerPage abstracts the raw results of making a List() request against the API. |
| 216 | // As OpenStack extensions may freely alter the response bodies of structures returned to the client, you may only safely access the |
| 217 | // data provided through the ExtractServers call. |
| 218 | type ServerPage struct { |
| 219 | pagination.LinkedPageBase |
Ash Wilson | f57381e | 2014-09-25 13:21:34 -0400 | [diff] [blame] | 220 | } |
| 221 | |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 222 | // IsEmpty returns true if a page contains no Server results. |
| 223 | func (page ServerPage) IsEmpty() (bool, error) { |
| 224 | servers, err := ExtractServers(page) |
| 225 | if err != nil { |
| 226 | return true, err |
| 227 | } |
| 228 | return len(servers) == 0, nil |
| 229 | } |
| 230 | |
| 231 | // NextPageURL uses the response's embedded link reference to navigate to the next page of results. |
| 232 | func (page ServerPage) NextPageURL() (string, error) { |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 233 | type resp struct { |
Jamie Hannaford | a581acd | 2014-10-08 17:14:13 +0200 | [diff] [blame] | 234 | Links []gophercloud.Link `mapstructure:"servers_links"` |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 235 | } |
| 236 | |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 237 | var r resp |
| 238 | err := mapstructure.Decode(page.Body, &r) |
| 239 | if err != nil { |
| 240 | return "", err |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 241 | } |
| 242 | |
Jamie Hannaford | a581acd | 2014-10-08 17:14:13 +0200 | [diff] [blame] | 243 | return gophercloud.ExtractNextURL(r.Links) |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 244 | } |
| 245 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 246 | // ExtractServers interprets the results of a single page from a List() call, producing a slice of Server entities. |
| 247 | func ExtractServers(page pagination.Page) ([]Server, error) { |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 248 | casted := page.(ServerPage).Body |
Ash Wilson | 1225939 | 2014-09-17 10:50:02 -0400 | [diff] [blame] | 249 | |
| 250 | var response struct { |
| 251 | Servers []Server `mapstructure:"servers"` |
| 252 | } |
Jon Perritt | 9a0980e | 2015-01-14 21:29:44 -0700 | [diff] [blame] | 253 | |
| 254 | config := &mapstructure.DecoderConfig{ |
Jon Perritt | 0e19f60 | 2015-01-15 09:35:46 -0700 | [diff] [blame] | 255 | DecodeHook: toMapFromString, |
| 256 | Result: &response, |
Jon Perritt | 9a0980e | 2015-01-14 21:29:44 -0700 | [diff] [blame] | 257 | } |
| 258 | decoder, err := mapstructure.NewDecoder(config) |
| 259 | if err != nil { |
| 260 | return nil, err |
| 261 | } |
| 262 | |
| 263 | err = decoder.Decode(casted) |
| 264 | |
Ash Wilson | 1225939 | 2014-09-17 10:50:02 -0400 | [diff] [blame] | 265 | return response.Servers, err |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 266 | } |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 267 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 268 | // MetadataResult contains the result of a call for (potentially) multiple key-value pairs. |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 269 | type MetadataResult struct { |
| 270 | gophercloud.Result |
| 271 | } |
| 272 | |
| 273 | // GetMetadataResult temporarily contains the response from a metadata Get call. |
| 274 | type GetMetadataResult struct { |
| 275 | MetadataResult |
| 276 | } |
| 277 | |
Jon Perritt | 789f832 | 2014-11-21 08:20:04 -0700 | [diff] [blame] | 278 | // ResetMetadataResult temporarily contains the response from a metadata Reset call. |
| 279 | type ResetMetadataResult struct { |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 280 | MetadataResult |
| 281 | } |
| 282 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 283 | // UpdateMetadataResult temporarily contains the response from a metadata Update call. |
| 284 | type UpdateMetadataResult struct { |
| 285 | MetadataResult |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 288 | // MetadatumResult contains the result of a call for individual a single key-value pair. |
| 289 | type MetadatumResult struct { |
| 290 | gophercloud.Result |
| 291 | } |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 292 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 293 | // GetMetadatumResult temporarily contains the response from a metadatum Get call. |
| 294 | type GetMetadatumResult struct { |
| 295 | MetadatumResult |
| 296 | } |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 297 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 298 | // CreateMetadatumResult temporarily contains the response from a metadatum Create call. |
| 299 | type CreateMetadatumResult struct { |
| 300 | MetadatumResult |
| 301 | } |
| 302 | |
| 303 | // DeleteMetadatumResult temporarily contains the response from a metadatum Delete call. |
| 304 | type DeleteMetadatumResult struct { |
| 305 | gophercloud.ErrResult |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | // Extract interprets any MetadataResult as a Metadata, if possible. |
| 309 | func (r MetadataResult) Extract() (map[string]string, error) { |
| 310 | if r.Err != nil { |
| 311 | return nil, r.Err |
| 312 | } |
| 313 | |
| 314 | var response struct { |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 315 | Metadata map[string]string `mapstructure:"metadata"` |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | err := mapstructure.Decode(r.Body, &response) |
| 319 | return response.Metadata, err |
| 320 | } |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 321 | |
| 322 | // Extract interprets any MetadatumResult as a Metadatum, if possible. |
| 323 | func (r MetadatumResult) Extract() (map[string]string, error) { |
| 324 | if r.Err != nil { |
| 325 | return nil, r.Err |
| 326 | } |
| 327 | |
| 328 | var response struct { |
| 329 | Metadatum map[string]string `mapstructure:"meta"` |
| 330 | } |
| 331 | |
| 332 | err := mapstructure.Decode(r.Body, &response) |
| 333 | return response.Metadatum, err |
| 334 | } |
Jon Perritt | 0e19f60 | 2015-01-15 09:35:46 -0700 | [diff] [blame] | 335 | |
| 336 | func toMapFromString(from reflect.Kind, to reflect.Kind, data interface{}) (interface{}, error) { |
| 337 | if (from == reflect.String) && (to == reflect.Map) { |
| 338 | return map[string]interface{}{}, nil |
| 339 | } |
| 340 | return data, nil |
| 341 | } |
Jon Perritt | 5cb4948 | 2015-02-19 12:19:58 -0700 | [diff] [blame] | 342 | |
| 343 | // Address represents an IP address. |
| 344 | type Address struct { |
| 345 | Version int `mapstructure:"version"` |
| 346 | Address string `mapstructure:"addr"` |
| 347 | } |
| 348 | |
Jon Perritt | 04d073c | 2015-02-19 21:46:23 -0700 | [diff] [blame] | 349 | // AddressPage abstracts the raw results of making a ListAddresses() request against the API. |
Jon Perritt | 5cb4948 | 2015-02-19 12:19:58 -0700 | [diff] [blame] | 350 | // As OpenStack extensions may freely alter the response bodies of structures returned |
| 351 | // to the client, you may only safely access the data provided through the ExtractAddresses call. |
| 352 | type AddressPage struct { |
| 353 | pagination.SinglePageBase |
| 354 | } |
| 355 | |
Jon Perritt | 04d073c | 2015-02-19 21:46:23 -0700 | [diff] [blame] | 356 | // IsEmpty returns true if an AddressPage contains no networks. |
Jon Perritt | 5cb4948 | 2015-02-19 12:19:58 -0700 | [diff] [blame] | 357 | func (r AddressPage) IsEmpty() (bool, error) { |
| 358 | addresses, err := ExtractAddresses(r) |
| 359 | if err != nil { |
| 360 | return true, err |
| 361 | } |
| 362 | return len(addresses) == 0, nil |
| 363 | } |
| 364 | |
Jon Perritt | 04d073c | 2015-02-19 21:46:23 -0700 | [diff] [blame] | 365 | // ExtractAddresses interprets the results of a single page from a ListAddresses() call, |
Jon Perritt | 5cb4948 | 2015-02-19 12:19:58 -0700 | [diff] [blame] | 366 | // producing a map of addresses. |
| 367 | func ExtractAddresses(page pagination.Page) (map[string][]Address, error) { |
| 368 | casted := page.(AddressPage).Body |
| 369 | |
| 370 | var response struct { |
| 371 | Addresses map[string][]Address `mapstructure:"addresses"` |
| 372 | } |
| 373 | |
| 374 | err := mapstructure.Decode(casted, &response) |
| 375 | if err != nil { |
| 376 | return nil, err |
| 377 | } |
| 378 | |
| 379 | return response.Addresses, err |
| 380 | } |
Jon Perritt | 04d073c | 2015-02-19 21:46:23 -0700 | [diff] [blame] | 381 | |
| 382 | // NetworkAddressPage abstracts the raw results of making a ListAddressesByNetwork() request against the API. |
| 383 | // As OpenStack extensions may freely alter the response bodies of structures returned |
| 384 | // to the client, you may only safely access the data provided through the ExtractAddresses call. |
| 385 | type NetworkAddressPage struct { |
| 386 | pagination.SinglePageBase |
| 387 | } |
| 388 | |
| 389 | // IsEmpty returns true if a NetworkAddressPage contains no addresses. |
| 390 | func (r NetworkAddressPage) IsEmpty() (bool, error) { |
| 391 | addresses, err := ExtractNetworkAddresses(r) |
| 392 | if err != nil { |
| 393 | return true, err |
| 394 | } |
| 395 | return len(addresses) == 0, nil |
| 396 | } |
| 397 | |
| 398 | // ExtractNetworkAddresses interprets the results of a single page from a ListAddressesByNetwork() call, |
Jon Perritt | b51ba9c | 2015-02-23 10:56:35 -0700 | [diff] [blame] | 399 | // producing a slice of addresses. |
| 400 | func ExtractNetworkAddresses(page pagination.Page) ([]Address, error) { |
Jon Perritt | 04d073c | 2015-02-19 21:46:23 -0700 | [diff] [blame] | 401 | casted := page.(NetworkAddressPage).Body |
| 402 | |
| 403 | var response map[string][]Address |
| 404 | err := mapstructure.Decode(casted, &response) |
| 405 | if err != nil { |
| 406 | return nil, err |
| 407 | } |
| 408 | |
Jon Perritt | b51ba9c | 2015-02-23 10:56:35 -0700 | [diff] [blame] | 409 | var key string |
| 410 | for k := range response { |
| 411 | key = k |
| 412 | } |
| 413 | |
| 414 | return response[key], err |
Jon Perritt | 04d073c | 2015-02-19 21:46:23 -0700 | [diff] [blame] | 415 | } |