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