blob: 023d0dd168998cd6a529adb40b89d7941111ff61 [file] [log] [blame]
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -08001package servers
2
3import (
Rickard von Essen5b8bbff2016-02-16 07:48:20 +01004 "crypto/rsa"
5 "encoding/base64"
einarf4e5fdaf2015-04-16 23:14:59 +00006 "fmt"
einarf4e5fdaf2015-04-16 23:14:59 +00007 "net/url"
Jon Perritt12395212016-02-24 10:41:17 -06008 "path"
Jon Perritt9a0980e2015-01-14 21:29:44 -07009
Jon Perritt27249f42016-02-18 10:35:59 -060010 "github.com/gophercloud/gophercloud"
11 "github.com/gophercloud/gophercloud/pagination"
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -080012 "github.com/mitchellh/mapstructure"
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080013)
14
Ash Wilson397c78b2014-09-25 15:19:14 -040015type serverResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -040016 gophercloud.Result
Ash Wilson397c78b2014-09-25 15:19:14 -040017}
18
19// Extract interprets any serverResult as a Server, if possible.
20func (r serverResult) Extract() (*Server, error) {
Jon Perritt12395212016-02-24 10:41:17 -060021 var s struct {
22 Server *Server `json:"server"`
Ash Wilson397c78b2014-09-25 15:19:14 -040023 }
Jon Perritt12395212016-02-24 10:41:17 -060024 err := r.ExtractInto(&s)
25 return s.Server, err
Ash Wilson397c78b2014-09-25 15:19:14 -040026}
27
28// CreateResult temporarily contains the response from a Create call.
29type CreateResult struct {
30 serverResult
31}
32
33// GetResult temporarily contains the response from a Get call.
34type GetResult struct {
35 serverResult
36}
37
38// UpdateResult temporarily contains the response from an Update call.
39type UpdateResult struct {
40 serverResult
41}
42
Jon Perrittcc77da62014-11-16 13:14:21 -070043// DeleteResult temporarily contains the response from a Delete call.
Jamie Hannaford34732fe2014-10-27 11:29:36 +010044type DeleteResult struct {
Jon Perrittba2395e2014-10-27 15:23:21 -050045 gophercloud.ErrResult
Jamie Hannaford34732fe2014-10-27 11:29:36 +010046}
47
Ash Wilson397c78b2014-09-25 15:19:14 -040048// RebuildResult temporarily contains the response from a Rebuild call.
Jamie Hannaford01c1efe2014-10-16 15:08:59 +020049type RebuildResult struct {
Ash Wilson397c78b2014-09-25 15:19:14 -040050 serverResult
51}
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080052
Jamie Hannaford01c1efe2014-10-16 15:08:59 +020053// ActionResult represents the result of server action operations, like reboot
54type ActionResult struct {
Jon Perrittba2395e2014-10-27 15:23:21 -050055 gophercloud.ErrResult
Jamie Hannaford01c1efe2014-10-16 15:08:59 +020056}
57
Alex Gaynor587e3e32014-11-13 10:39:09 -080058// RescueResult represents the result of a server rescue operation
Alex Gaynorfbe61bb2014-11-12 13:35:03 -080059type RescueResult struct {
60 ActionResult
Alex Gaynor7f3b06e2014-11-13 09:54:03 -080061}
62
einarf4e5fdaf2015-04-16 23:14:59 +000063// CreateImageResult represents the result of an image creation operation
64type CreateImageResult struct {
einarf2fc665e2015-04-16 20:16:21 +000065 gophercloud.Result
66}
67
Rickard von Essen5b8bbff2016-02-16 07:48:20 +010068// GetPasswordResult represent the result of a get os-server-password operation.
69type 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>
77func (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
94func 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
einarf4e5fdaf2015-04-16 23:14:59 +0000109// ExtractImageID gets the ID of the newly created server image from the header
110func (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 Perritt13808262016-03-09 00:50:12 -0600117 return "", err
einarf4e5fdaf2015-04-16 23:14:59 +0000118 }
Jon Perritt12395212016-02-24 10:41:17 -0600119 imageID := path.Base(u.Path)
120 if imageID == "." || imageID == "/" {
einarf4e5fdaf2015-04-16 23:14:59 +0000121 return "", fmt.Errorf("Failed to parse the ID of newly created image: %s", u)
122 }
Jon Perritt12395212016-02-24 10:41:17 -0600123 return imageID, nil
einarf4e5fdaf2015-04-16 23:14:59 +0000124}
125
Jon Perrittcc77da62014-11-16 13:14:21 -0700126// Extract interprets any RescueResult as an AdminPass, if possible.
Alex Gaynor7f3b06e2014-11-13 09:54:03 -0800127func (r RescueResult) Extract() (string, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600128 var s struct {
129 AdminPass string `json:"adminPass"`
Alex Gaynor7f3b06e2014-11-13 09:54:03 -0800130 }
Jon Perritt12395212016-02-24 10:41:17 -0600131 err := r.ExtractInto(&s)
132 return s.AdminPass, err
Alex Gaynorfbe61bb2014-11-12 13:35:03 -0800133}
134
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -0800135// Server exposes only the standard OpenStack fields corresponding to a given server on the user's account.
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -0800136type Server struct {
Ash Wilson01626a32014-09-17 10:38:07 -0400137 // ID uniquely identifies this server amongst all other servers, including those not accessible to the current tenant.
Jon Perritt12395212016-02-24 10:41:17 -0600138 ID string `json:"id"`
Ash Wilson01626a32014-09-17 10:38:07 -0400139
140 // TenantID identifies the tenant owning this server resource.
Jon Perritt12395212016-02-24 10:41:17 -0600141 TenantID string `json:"tenant_id"`
Ash Wilson01626a32014-09-17 10:38:07 -0400142
143 // UserID uniquely identifies the user account owning the tenant.
Jon Perritt12395212016-02-24 10:41:17 -0600144 UserID string `json:"user_id"`
Ash Wilson01626a32014-09-17 10:38:07 -0400145
146 // Name contains the human-readable name for the server.
Jon Perritt12395212016-02-24 10:41:17 -0600147 Name string `json:"name"`
Ash Wilson01626a32014-09-17 10:38:07 -0400148
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 Hannaford908e1e92014-10-27 14:41:17 +0100163 AccessIPv4, AccessIPv6 string
Ash Wilson01626a32014-09-17 10:38:07 -0400164
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 Wilson01626a32014-09-17 10:38:07 -0400172 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 Wilsonad21c712014-09-25 10:15:22 -0400180 // KeyName indicates which public key was injected into the server on launch.
Jon Perritt12395212016-02-24 10:41:17 -0600181 KeyName string `json:"key_name"`
Ash Wilsonad21c712014-09-25 10:15:22 -0400182
Ash Wilson01626a32014-09-17 10:38:07 -0400183 // 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 Perritt12395212016-02-24 10:41:17 -0600185 AdminPass string `json:"adminPass"`
Joe Topjian978bb502015-02-12 20:55:31 +0000186
187 // SecurityGroups includes the security groups that this instance has applied to it
Jon Perritt12395212016-02-24 10:41:17 -0600188 SecurityGroups []map[string]interface{} `json:"security_groups"`
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -0800189}
190
Ash Wilson397c78b2014-09-25 15:19:14 -0400191// 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.
194type ServerPage struct {
195 pagination.LinkedPageBase
Ash Wilsonf57381e2014-09-25 13:21:34 -0400196}
197
Ash Wilson397c78b2014-09-25 15:19:14 -0400198// IsEmpty returns true if a page contains no Server results.
199func (page ServerPage) IsEmpty() (bool, error) {
200 servers, err := ExtractServers(page)
Jon Perritt12395212016-02-24 10:41:17 -0600201 return len(servers) == 0, err
Ash Wilson397c78b2014-09-25 15:19:14 -0400202}
203
204// NextPageURL uses the response's embedded link reference to navigate to the next page of results.
205func (page ServerPage) NextPageURL() (string, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600206 var s struct {
207 Links []gophercloud.Link `json:"servers_links"`
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400208 }
Jon Perritt12395212016-02-24 10:41:17 -0600209 err := page.ExtractInto(&s)
Ash Wilson397c78b2014-09-25 15:19:14 -0400210 if err != nil {
211 return "", err
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400212 }
Jon Perritt12395212016-02-24 10:41:17 -0600213 return gophercloud.ExtractNextURL(s.Links)
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400214}
215
Ash Wilson01626a32014-09-17 10:38:07 -0400216// ExtractServers interprets the results of a single page from a List() call, producing a slice of Server entities.
Jon Perritt31b66462016-02-25 22:25:30 -0600217func ExtractServers(r pagination.Page) ([]Server, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600218 var s struct {
219 Servers []Server `json:"servers"`
Ash Wilson12259392014-09-17 10:50:02 -0400220 }
Jon Perritt31b66462016-02-25 22:25:30 -0600221 err := (r.(ServerPage)).ExtractInto(&s)
Jon Perritt12395212016-02-24 10:41:17 -0600222 return s.Servers, err
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -0800223}
Jon Perrittcc77da62014-11-16 13:14:21 -0700224
Jon Perritt78c57ce2014-11-20 11:07:18 -0700225// MetadataResult contains the result of a call for (potentially) multiple key-value pairs.
Jon Perrittcc77da62014-11-16 13:14:21 -0700226type MetadataResult struct {
227 gophercloud.Result
228}
229
230// GetMetadataResult temporarily contains the response from a metadata Get call.
231type GetMetadataResult struct {
232 MetadataResult
233}
234
Jon Perritt789f8322014-11-21 08:20:04 -0700235// ResetMetadataResult temporarily contains the response from a metadata Reset call.
236type ResetMetadataResult struct {
Jon Perrittcc77da62014-11-16 13:14:21 -0700237 MetadataResult
238}
239
Jon Perritt78c57ce2014-11-20 11:07:18 -0700240// UpdateMetadataResult temporarily contains the response from a metadata Update call.
241type UpdateMetadataResult struct {
242 MetadataResult
Jon Perrittcc77da62014-11-16 13:14:21 -0700243}
244
Jon Perritt78c57ce2014-11-20 11:07:18 -0700245// MetadatumResult contains the result of a call for individual a single key-value pair.
246type MetadatumResult struct {
247 gophercloud.Result
248}
Jon Perrittcc77da62014-11-16 13:14:21 -0700249
Jon Perritt78c57ce2014-11-20 11:07:18 -0700250// GetMetadatumResult temporarily contains the response from a metadatum Get call.
251type GetMetadatumResult struct {
252 MetadatumResult
253}
Jon Perrittcc77da62014-11-16 13:14:21 -0700254
Jon Perritt78c57ce2014-11-20 11:07:18 -0700255// CreateMetadatumResult temporarily contains the response from a metadatum Create call.
256type CreateMetadatumResult struct {
257 MetadatumResult
258}
259
260// DeleteMetadatumResult temporarily contains the response from a metadatum Delete call.
261type DeleteMetadatumResult struct {
262 gophercloud.ErrResult
Jon Perrittcc77da62014-11-16 13:14:21 -0700263}
264
265// Extract interprets any MetadataResult as a Metadata, if possible.
266func (r MetadataResult) Extract() (map[string]string, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600267 var s struct {
268 Metadata map[string]string `json:"metadata"`
Jon Perrittcc77da62014-11-16 13:14:21 -0700269 }
Jon Perritt12395212016-02-24 10:41:17 -0600270 err := r.ExtractInto(&s)
271 return s.Metadata, err
Jon Perrittcc77da62014-11-16 13:14:21 -0700272}
Jon Perritt78c57ce2014-11-20 11:07:18 -0700273
274// Extract interprets any MetadatumResult as a Metadatum, if possible.
275func (r MetadatumResult) Extract() (map[string]string, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600276 var s struct {
277 Metadatum map[string]string `json:"meta"`
Jon Perritt78c57ce2014-11-20 11:07:18 -0700278 }
Jon Perritt12395212016-02-24 10:41:17 -0600279 err := r.ExtractInto(&s)
280 return s.Metadatum, err
Jon Perritt0e19f602015-01-15 09:35:46 -0700281}
Jon Perritt5cb49482015-02-19 12:19:58 -0700282
283// Address represents an IP address.
284type Address struct {
Jon Perritt12395212016-02-24 10:41:17 -0600285 Version int `json:"version"`
286 Address string `json:"addr"`
Jon Perritt5cb49482015-02-19 12:19:58 -0700287}
288
Jon Perritt04d073c2015-02-19 21:46:23 -0700289// AddressPage abstracts the raw results of making a ListAddresses() request against the API.
Jon Perritt5cb49482015-02-19 12:19:58 -0700290// 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.
292type AddressPage struct {
293 pagination.SinglePageBase
294}
295
Jon Perritt04d073c2015-02-19 21:46:23 -0700296// IsEmpty returns true if an AddressPage contains no networks.
Jon Perritt5cb49482015-02-19 12:19:58 -0700297func (r AddressPage) IsEmpty() (bool, error) {
298 addresses, err := ExtractAddresses(r)
Jon Perritt12395212016-02-24 10:41:17 -0600299 return len(addresses) == 0, err
Jon Perritt5cb49482015-02-19 12:19:58 -0700300}
301
Jon Perritt04d073c2015-02-19 21:46:23 -0700302// ExtractAddresses interprets the results of a single page from a ListAddresses() call,
Jon Perritt5cb49482015-02-19 12:19:58 -0700303// producing a map of addresses.
Jon Perritt31b66462016-02-25 22:25:30 -0600304func ExtractAddresses(r pagination.Page) (map[string][]Address, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600305 var s struct {
306 Addresses map[string][]Address `json:"addresses"`
Jon Perritt5cb49482015-02-19 12:19:58 -0700307 }
Jon Perritt31b66462016-02-25 22:25:30 -0600308 err := (r.(AddressPage)).ExtractInto(&s)
Jon Perritt12395212016-02-24 10:41:17 -0600309 return s.Addresses, err
Jon Perritt5cb49482015-02-19 12:19:58 -0700310}
Jon Perritt04d073c2015-02-19 21:46:23 -0700311
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.
315type NetworkAddressPage struct {
316 pagination.SinglePageBase
317}
318
319// IsEmpty returns true if a NetworkAddressPage contains no addresses.
320func (r NetworkAddressPage) IsEmpty() (bool, error) {
321 addresses, err := ExtractNetworkAddresses(r)
Jon Perritt12395212016-02-24 10:41:17 -0600322 return len(addresses) == 0, err
Jon Perritt04d073c2015-02-19 21:46:23 -0700323}
324
325// ExtractNetworkAddresses interprets the results of a single page from a ListAddressesByNetwork() call,
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700326// producing a slice of addresses.
Jon Perritt31b66462016-02-25 22:25:30 -0600327func ExtractNetworkAddresses(r pagination.Page) ([]Address, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600328 var s map[string][]Address
Jon Perritt31b66462016-02-25 22:25:30 -0600329 err := (r.(NetworkAddressPage)).ExtractInto(&s)
Jon Perritt04d073c2015-02-19 21:46:23 -0700330 if err != nil {
331 return nil, err
332 }
333
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700334 var key string
Jon Perritt12395212016-02-24 10:41:17 -0600335 for k := range s {
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700336 key = k
337 }
338
Jon Perritt12395212016-02-24 10:41:17 -0600339 return s[key], err
Jon Perritt04d073c2015-02-19 21:46:23 -0700340}