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