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