blob: c121a6be7d6d95c4fdc098469cdfa0520f444483 [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"
jrperritt1fa92502016-07-21 19:22:59 -05006 "encoding/json"
einarf4e5fdaf2015-04-16 23:14:59 +00007 "fmt"
einarf4e5fdaf2015-04-16 23:14:59 +00008 "net/url"
Jon Perritt12395212016-02-24 10:41:17 -06009 "path"
jrperritt98d01622017-01-12 14:24:42 -060010 "time"
Jon Perritt9a0980e2015-01-14 21:29:44 -070011
Jon Perritt27249f42016-02-18 10:35:59 -060012 "github.com/gophercloud/gophercloud"
13 "github.com/gophercloud/gophercloud/pagination"
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080014)
15
Ash Wilson397c78b2014-09-25 15:19:14 -040016type serverResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -040017 gophercloud.Result
Ash Wilson397c78b2014-09-25 15:19:14 -040018}
19
20// Extract interprets any serverResult as a Server, if possible.
21func (r serverResult) Extract() (*Server, error) {
Jon Perritt12395212016-02-24 10:41:17 -060022 var s struct {
23 Server *Server `json:"server"`
Ash Wilson397c78b2014-09-25 15:19:14 -040024 }
Jon Perritt12395212016-02-24 10:41:17 -060025 err := r.ExtractInto(&s)
26 return s.Server, err
Ash Wilson397c78b2014-09-25 15:19:14 -040027}
28
29// CreateResult temporarily contains the response from a Create call.
30type CreateResult struct {
31 serverResult
32}
33
34// GetResult temporarily contains the response from a Get call.
35type GetResult struct {
36 serverResult
37}
38
39// UpdateResult temporarily contains the response from an Update call.
40type UpdateResult struct {
41 serverResult
42}
43
Jon Perrittcc77da62014-11-16 13:14:21 -070044// DeleteResult temporarily contains the response from a Delete call.
Jamie Hannaford34732fe2014-10-27 11:29:36 +010045type DeleteResult struct {
Jon Perrittba2395e2014-10-27 15:23:21 -050046 gophercloud.ErrResult
Jamie Hannaford34732fe2014-10-27 11:29:36 +010047}
48
Ash Wilson397c78b2014-09-25 15:19:14 -040049// RebuildResult temporarily contains the response from a Rebuild call.
Jamie Hannaford01c1efe2014-10-16 15:08:59 +020050type RebuildResult struct {
Ash Wilson397c78b2014-09-25 15:19:14 -040051 serverResult
52}
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080053
Jamie Hannaford01c1efe2014-10-16 15:08:59 +020054// ActionResult represents the result of server action operations, like reboot
55type ActionResult struct {
Jon Perrittba2395e2014-10-27 15:23:21 -050056 gophercloud.ErrResult
Jamie Hannaford01c1efe2014-10-16 15:08:59 +020057}
58
Alex Gaynor587e3e32014-11-13 10:39:09 -080059// RescueResult represents the result of a server rescue operation
Alex Gaynorfbe61bb2014-11-12 13:35:03 -080060type RescueResult struct {
61 ActionResult
Alex Gaynor7f3b06e2014-11-13 09:54:03 -080062}
63
einarf4e5fdaf2015-04-16 23:14:59 +000064// CreateImageResult represents the result of an image creation operation
65type CreateImageResult struct {
einarf2fc665e2015-04-16 20:16:21 +000066 gophercloud.Result
67}
68
Rickard von Essen5b8bbff2016-02-16 07:48:20 +010069// GetPasswordResult represent the result of a get os-server-password operation.
70type GetPasswordResult struct {
71 gophercloud.Result
72}
73
74// ExtractPassword gets the encrypted password.
75// If privateKey != nil the password is decrypted with the private key.
76// If privateKey == nil the encrypted password is returned and can be decrypted with:
77// echo '<pwd>' | base64 -D | openssl rsautl -decrypt -inkey <private_key>
78func (r GetPasswordResult) ExtractPassword(privateKey *rsa.PrivateKey) (string, error) {
jrperritt6a4dcc72016-07-21 18:59:43 -050079 var s struct {
80 Password string `json:"password"`
Rickard von Essen5b8bbff2016-02-16 07:48:20 +010081 }
jrperritt6a4dcc72016-07-21 18:59:43 -050082 err := r.ExtractInto(&s)
83 if err == nil && privateKey != nil && s.Password != "" {
84 return decryptPassword(s.Password, privateKey)
Rickard von Essen5b8bbff2016-02-16 07:48:20 +010085 }
jrperritt6a4dcc72016-07-21 18:59:43 -050086 return s.Password, err
Rickard von Essen5b8bbff2016-02-16 07:48:20 +010087}
88
89func decryptPassword(encryptedPassword string, privateKey *rsa.PrivateKey) (string, error) {
90 b64EncryptedPassword := make([]byte, base64.StdEncoding.DecodedLen(len(encryptedPassword)))
91
92 n, err := base64.StdEncoding.Decode(b64EncryptedPassword, []byte(encryptedPassword))
93 if err != nil {
94 return "", fmt.Errorf("Failed to base64 decode encrypted password: %s", err)
95 }
96 password, err := rsa.DecryptPKCS1v15(nil, privateKey, b64EncryptedPassword[0:n])
97 if err != nil {
98 return "", fmt.Errorf("Failed to decrypt password: %s", err)
99 }
100
101 return string(password), nil
102}
103
einarf4e5fdaf2015-04-16 23:14:59 +0000104// ExtractImageID gets the ID of the newly created server image from the header
jrperritt98d01622017-01-12 14:24:42 -0600105func (r CreateImageResult) ExtractImageID() (string, error) {
106 if r.Err != nil {
107 return "", r.Err
einarf4e5fdaf2015-04-16 23:14:59 +0000108 }
109 // Get the image id from the header
jrperritt98d01622017-01-12 14:24:42 -0600110 u, err := url.ParseRequestURI(r.Header.Get("Location"))
einarf4e5fdaf2015-04-16 23:14:59 +0000111 if err != nil {
Jon Perritt13808262016-03-09 00:50:12 -0600112 return "", err
einarf4e5fdaf2015-04-16 23:14:59 +0000113 }
Jon Perritt12395212016-02-24 10:41:17 -0600114 imageID := path.Base(u.Path)
115 if imageID == "." || imageID == "/" {
einarf4e5fdaf2015-04-16 23:14:59 +0000116 return "", fmt.Errorf("Failed to parse the ID of newly created image: %s", u)
117 }
Jon Perritt12395212016-02-24 10:41:17 -0600118 return imageID, nil
einarf4e5fdaf2015-04-16 23:14:59 +0000119}
120
Jon Perrittcc77da62014-11-16 13:14:21 -0700121// Extract interprets any RescueResult as an AdminPass, if possible.
Alex Gaynor7f3b06e2014-11-13 09:54:03 -0800122func (r RescueResult) Extract() (string, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600123 var s struct {
124 AdminPass string `json:"adminPass"`
Alex Gaynor7f3b06e2014-11-13 09:54:03 -0800125 }
Jon Perritt12395212016-02-24 10:41:17 -0600126 err := r.ExtractInto(&s)
127 return s.AdminPass, err
Alex Gaynorfbe61bb2014-11-12 13:35:03 -0800128}
129
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -0800130// 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 -0800131type Server struct {
Ash Wilson01626a32014-09-17 10:38:07 -0400132 // ID uniquely identifies this server amongst all other servers, including those not accessible to the current tenant.
Jon Perritt12395212016-02-24 10:41:17 -0600133 ID string `json:"id"`
Ash Wilson01626a32014-09-17 10:38:07 -0400134 // TenantID identifies the tenant owning this server resource.
Jon Perritt12395212016-02-24 10:41:17 -0600135 TenantID string `json:"tenant_id"`
Ash Wilson01626a32014-09-17 10:38:07 -0400136 // UserID uniquely identifies the user account owning the tenant.
Jon Perritt12395212016-02-24 10:41:17 -0600137 UserID string `json:"user_id"`
Ash Wilson01626a32014-09-17 10:38:07 -0400138 // Name contains the human-readable name for the server.
Jon Perritt12395212016-02-24 10:41:17 -0600139 Name string `json:"name"`
Ash Wilson01626a32014-09-17 10:38:07 -0400140 // Updated and Created contain ISO-8601 timestamps of when the state of the server last changed, and when it was created.
jrperritt98d01622017-01-12 14:24:42 -0600141 Updated time.Time `json:"updated"`
142 Created time.Time `json:"created"`
143 HostID string `json:"hostid"`
Ash Wilson01626a32014-09-17 10:38:07 -0400144 // Status contains the current operational status of the server, such as IN_PROGRESS or ACTIVE.
jrperritt98d01622017-01-12 14:24:42 -0600145 Status string `json:"status"`
Ash Wilson01626a32014-09-17 10:38:07 -0400146 // Progress ranges from 0..100.
147 // A request made against the server completes only once Progress reaches 100.
jrperritt98d01622017-01-12 14:24:42 -0600148 Progress int `json:"progress"`
Ash Wilson01626a32014-09-17 10:38:07 -0400149 // AccessIPv4 and AccessIPv6 contain the IP addresses of the server, suitable for remote access for administration.
jrperritt98d01622017-01-12 14:24:42 -0600150 AccessIPv4 string `json:"accessIPv4"`
151 AccessIPv6 string `json:"accessIPv6"`
Ash Wilson01626a32014-09-17 10:38:07 -0400152 // Image refers to a JSON object, which itself indicates the OS image used to deploy the server.
jrperritt98d01622017-01-12 14:24:42 -0600153 Image map[string]interface{} `json:"-"`
Ash Wilson01626a32014-09-17 10:38:07 -0400154 // Flavor refers to a JSON object, which itself indicates the hardware configuration of the deployed server.
jrperritt98d01622017-01-12 14:24:42 -0600155 Flavor map[string]interface{} `json:"flavor"`
Ash Wilson01626a32014-09-17 10:38:07 -0400156 // Addresses includes a list of all IP addresses assigned to the server, keyed by pool.
jrperritt98d01622017-01-12 14:24:42 -0600157 Addresses map[string]interface{} `json:"addresses"`
Ash Wilson01626a32014-09-17 10:38:07 -0400158 // Metadata includes a list of all user-specified key-value pairs attached to the server.
jrperritt98d01622017-01-12 14:24:42 -0600159 Metadata map[string]string `json:"metadata"`
Ash Wilson01626a32014-09-17 10:38:07 -0400160 // Links includes HTTP references to the itself, useful for passing along to other APIs that might want a server reference.
jrperritt98d01622017-01-12 14:24:42 -0600161 Links []interface{} `json:"links"`
Ash Wilsonad21c712014-09-25 10:15:22 -0400162 // KeyName indicates which public key was injected into the server on launch.
Jon Perritt12395212016-02-24 10:41:17 -0600163 KeyName string `json:"key_name"`
Ash Wilson01626a32014-09-17 10:38:07 -0400164 // 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.
165 // Note that this is the ONLY time this field will be valid.
Jon Perritt12395212016-02-24 10:41:17 -0600166 AdminPass string `json:"adminPass"`
Joe Topjian978bb502015-02-12 20:55:31 +0000167 // SecurityGroups includes the security groups that this instance has applied to it
Jon Perritt12395212016-02-24 10:41:17 -0600168 SecurityGroups []map[string]interface{} `json:"security_groups"`
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -0800169}
170
jrperritt98d01622017-01-12 14:24:42 -0600171func (r *Server) UnmarshalJSON(b []byte) error {
jrperritt1fa92502016-07-21 19:22:59 -0500172 type tmp Server
jrperritt98d01622017-01-12 14:24:42 -0600173 var s struct {
jrperritt1fa92502016-07-21 19:22:59 -0500174 tmp
jrperritt98d01622017-01-12 14:24:42 -0600175 Image interface{} `json:"image"`
jrperritt1fa92502016-07-21 19:22:59 -0500176 }
jrperritt98d01622017-01-12 14:24:42 -0600177 err := json.Unmarshal(b, &s)
jrperritt1fa92502016-07-21 19:22:59 -0500178 if err != nil {
179 return err
180 }
181
jrperritt98d01622017-01-12 14:24:42 -0600182 *r = Server(s.tmp)
jrperritt1fa92502016-07-21 19:22:59 -0500183
jrperritt98d01622017-01-12 14:24:42 -0600184 switch t := s.Image.(type) {
jrperritt1fa92502016-07-21 19:22:59 -0500185 case map[string]interface{}:
jrperritt98d01622017-01-12 14:24:42 -0600186 r.Image = t
jrperritt1fa92502016-07-21 19:22:59 -0500187 case string:
188 switch t {
189 case "":
jrperritt98d01622017-01-12 14:24:42 -0600190 r.Image = nil
jrperritt1fa92502016-07-21 19:22:59 -0500191 }
192 }
193
jrperritt98d01622017-01-12 14:24:42 -0600194 return err
jrperritt1fa92502016-07-21 19:22:59 -0500195}
196
Ash Wilson397c78b2014-09-25 15:19:14 -0400197// ServerPage abstracts the raw results of making a List() request against the API.
198// As OpenStack extensions may freely alter the response bodies of structures returned to the client, you may only safely access the
199// data provided through the ExtractServers call.
200type ServerPage struct {
201 pagination.LinkedPageBase
Ash Wilsonf57381e2014-09-25 13:21:34 -0400202}
203
Ash Wilson397c78b2014-09-25 15:19:14 -0400204// IsEmpty returns true if a page contains no Server results.
jrperritt98d01622017-01-12 14:24:42 -0600205func (r ServerPage) IsEmpty() (bool, error) {
206 s, err := ExtractServers(r)
207 return len(s) == 0, err
Ash Wilson397c78b2014-09-25 15:19:14 -0400208}
209
210// NextPageURL uses the response's embedded link reference to navigate to the next page of results.
jrperritt98d01622017-01-12 14:24:42 -0600211func (r ServerPage) NextPageURL() (string, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600212 var s struct {
213 Links []gophercloud.Link `json:"servers_links"`
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400214 }
jrperritt98d01622017-01-12 14:24:42 -0600215 err := r.ExtractInto(&s)
Ash Wilson397c78b2014-09-25 15:19:14 -0400216 if err != nil {
217 return "", err
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400218 }
Jon Perritt12395212016-02-24 10:41:17 -0600219 return gophercloud.ExtractNextURL(s.Links)
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400220}
221
Ash Wilson01626a32014-09-17 10:38:07 -0400222// 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 -0600223func ExtractServers(r pagination.Page) ([]Server, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600224 var s struct {
225 Servers []Server `json:"servers"`
Ash Wilson12259392014-09-17 10:50:02 -0400226 }
Jon Perritt31b66462016-02-25 22:25:30 -0600227 err := (r.(ServerPage)).ExtractInto(&s)
Jon Perritt12395212016-02-24 10:41:17 -0600228 return s.Servers, err
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -0800229}
Jon Perrittcc77da62014-11-16 13:14:21 -0700230
Jon Perritt78c57ce2014-11-20 11:07:18 -0700231// MetadataResult contains the result of a call for (potentially) multiple key-value pairs.
Jon Perrittcc77da62014-11-16 13:14:21 -0700232type MetadataResult struct {
233 gophercloud.Result
234}
235
236// GetMetadataResult temporarily contains the response from a metadata Get call.
237type GetMetadataResult struct {
238 MetadataResult
239}
240
Jon Perritt789f8322014-11-21 08:20:04 -0700241// ResetMetadataResult temporarily contains the response from a metadata Reset call.
242type ResetMetadataResult struct {
Jon Perrittcc77da62014-11-16 13:14:21 -0700243 MetadataResult
244}
245
Jon Perritt78c57ce2014-11-20 11:07:18 -0700246// UpdateMetadataResult temporarily contains the response from a metadata Update call.
247type UpdateMetadataResult struct {
248 MetadataResult
Jon Perrittcc77da62014-11-16 13:14:21 -0700249}
250
Jon Perritt78c57ce2014-11-20 11:07:18 -0700251// MetadatumResult contains the result of a call for individual a single key-value pair.
252type MetadatumResult struct {
253 gophercloud.Result
254}
Jon Perrittcc77da62014-11-16 13:14:21 -0700255
Jon Perritt78c57ce2014-11-20 11:07:18 -0700256// GetMetadatumResult temporarily contains the response from a metadatum Get call.
257type GetMetadatumResult struct {
258 MetadatumResult
259}
Jon Perrittcc77da62014-11-16 13:14:21 -0700260
Jon Perritt78c57ce2014-11-20 11:07:18 -0700261// CreateMetadatumResult temporarily contains the response from a metadatum Create call.
262type CreateMetadatumResult struct {
263 MetadatumResult
264}
265
266// DeleteMetadatumResult temporarily contains the response from a metadatum Delete call.
267type DeleteMetadatumResult struct {
268 gophercloud.ErrResult
Jon Perrittcc77da62014-11-16 13:14:21 -0700269}
270
271// Extract interprets any MetadataResult as a Metadata, if possible.
272func (r MetadataResult) Extract() (map[string]string, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600273 var s struct {
274 Metadata map[string]string `json:"metadata"`
Jon Perrittcc77da62014-11-16 13:14:21 -0700275 }
Jon Perritt12395212016-02-24 10:41:17 -0600276 err := r.ExtractInto(&s)
277 return s.Metadata, err
Jon Perrittcc77da62014-11-16 13:14:21 -0700278}
Jon Perritt78c57ce2014-11-20 11:07:18 -0700279
280// Extract interprets any MetadatumResult as a Metadatum, if possible.
281func (r MetadatumResult) Extract() (map[string]string, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600282 var s struct {
283 Metadatum map[string]string `json:"meta"`
Jon Perritt78c57ce2014-11-20 11:07:18 -0700284 }
Jon Perritt12395212016-02-24 10:41:17 -0600285 err := r.ExtractInto(&s)
286 return s.Metadatum, err
Jon Perritt0e19f602015-01-15 09:35:46 -0700287}
Jon Perritt5cb49482015-02-19 12:19:58 -0700288
289// Address represents an IP address.
290type Address struct {
Jon Perritt12395212016-02-24 10:41:17 -0600291 Version int `json:"version"`
292 Address string `json:"addr"`
Jon Perritt5cb49482015-02-19 12:19:58 -0700293}
294
Jon Perritt04d073c2015-02-19 21:46:23 -0700295// AddressPage abstracts the raw results of making a ListAddresses() request against the API.
Jon Perritt5cb49482015-02-19 12:19:58 -0700296// As OpenStack extensions may freely alter the response bodies of structures returned
297// to the client, you may only safely access the data provided through the ExtractAddresses call.
298type AddressPage struct {
299 pagination.SinglePageBase
300}
301
Jon Perritt04d073c2015-02-19 21:46:23 -0700302// IsEmpty returns true if an AddressPage contains no networks.
Jon Perritt5cb49482015-02-19 12:19:58 -0700303func (r AddressPage) IsEmpty() (bool, error) {
304 addresses, err := ExtractAddresses(r)
Jon Perritt12395212016-02-24 10:41:17 -0600305 return len(addresses) == 0, err
Jon Perritt5cb49482015-02-19 12:19:58 -0700306}
307
Jon Perritt04d073c2015-02-19 21:46:23 -0700308// ExtractAddresses interprets the results of a single page from a ListAddresses() call,
Jon Perritt5cb49482015-02-19 12:19:58 -0700309// producing a map of addresses.
Jon Perritt31b66462016-02-25 22:25:30 -0600310func ExtractAddresses(r pagination.Page) (map[string][]Address, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600311 var s struct {
312 Addresses map[string][]Address `json:"addresses"`
Jon Perritt5cb49482015-02-19 12:19:58 -0700313 }
Jon Perritt31b66462016-02-25 22:25:30 -0600314 err := (r.(AddressPage)).ExtractInto(&s)
Jon Perritt12395212016-02-24 10:41:17 -0600315 return s.Addresses, err
Jon Perritt5cb49482015-02-19 12:19:58 -0700316}
Jon Perritt04d073c2015-02-19 21:46:23 -0700317
318// NetworkAddressPage abstracts the raw results of making a ListAddressesByNetwork() request against the API.
319// As OpenStack extensions may freely alter the response bodies of structures returned
320// to the client, you may only safely access the data provided through the ExtractAddresses call.
321type NetworkAddressPage struct {
322 pagination.SinglePageBase
323}
324
325// IsEmpty returns true if a NetworkAddressPage contains no addresses.
326func (r NetworkAddressPage) IsEmpty() (bool, error) {
327 addresses, err := ExtractNetworkAddresses(r)
Jon Perritt12395212016-02-24 10:41:17 -0600328 return len(addresses) == 0, err
Jon Perritt04d073c2015-02-19 21:46:23 -0700329}
330
331// ExtractNetworkAddresses interprets the results of a single page from a ListAddressesByNetwork() call,
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700332// producing a slice of addresses.
Jon Perritt31b66462016-02-25 22:25:30 -0600333func ExtractNetworkAddresses(r pagination.Page) ([]Address, error) {
Jon Perritt12395212016-02-24 10:41:17 -0600334 var s map[string][]Address
Jon Perritt31b66462016-02-25 22:25:30 -0600335 err := (r.(NetworkAddressPage)).ExtractInto(&s)
Jon Perritt04d073c2015-02-19 21:46:23 -0700336 if err != nil {
337 return nil, err
338 }
339
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700340 var key string
Jon Perritt12395212016-02-24 10:41:17 -0600341 for k := range s {
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700342 key = k
343 }
344
Jon Perritt12395212016-02-24 10:41:17 -0600345 return s[key], err
Jon Perritt04d073c2015-02-19 21:46:23 -0700346}