blob: fbf50c32668f9ad9468eee34f2e02db6bf74efe3 [file] [log] [blame]
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -08001package servers
2
3import (
Ash Wilson01626a32014-09-17 10:38:07 -04004 "errors"
5
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -08006 "github.com/mitchellh/mapstructure"
Ash Wilson01626a32014-09-17 10:38:07 -04007 "github.com/rackspace/gophercloud/pagination"
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -08008)
9
Ash Wilson01626a32014-09-17 10:38:07 -040010// ErrCannotInterpret is returned by an Extract call if the response body doesn't have the expected structure.
11var ErrCannotInterpet = errors.New("Unable to interpret a response body.")
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080012
13// 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 -080014type Server struct {
Ash Wilson01626a32014-09-17 10:38:07 -040015 // ID uniquely identifies this server amongst all other servers, including those not accessible to the current tenant.
16 ID string
17
18 // TenantID identifies the tenant owning this server resource.
19 TenantID string `mapstructure:"tenant_id"`
20
21 // UserID uniquely identifies the user account owning the tenant.
22 UserID string `mapstructure:"user_id"`
23
24 // Name contains the human-readable name for the server.
25 Name string
26
27 // Updated and Created contain ISO-8601 timestamps of when the state of the server last changed, and when it was created.
28 Updated string
29 Created string
30
31 HostID string
32
33 // Status contains the current operational status of the server, such as IN_PROGRESS or ACTIVE.
34 Status string
35
36 // Progress ranges from 0..100.
37 // A request made against the server completes only once Progress reaches 100.
38 Progress int
39
40 // AccessIPv4 and AccessIPv6 contain the IP addresses of the server, suitable for remote access for administration.
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080041 AccessIPv4 string
42 AccessIPv6 string
Ash Wilson01626a32014-09-17 10:38:07 -040043
44 // Image refers to a JSON object, which itself indicates the OS image used to deploy the server.
45 Image map[string]interface{}
46
47 // Flavor refers to a JSON object, which itself indicates the hardware configuration of the deployed server.
48 Flavor map[string]interface{}
49
50 // Addresses includes a list of all IP addresses assigned to the server, keyed by pool.
Ash Wilson01626a32014-09-17 10:38:07 -040051 Addresses map[string]interface{}
52
53 // Metadata includes a list of all user-specified key-value pairs attached to the server.
54 Metadata map[string]interface{}
55
56 // Links includes HTTP references to the itself, useful for passing along to other APIs that might want a server reference.
57 Links []interface{}
58
Ash Wilsonad21c712014-09-25 10:15:22 -040059 // KeyName indicates which public key was injected into the server on launch.
60 KeyName string `mapstructure:"keyname"`
61
Ash Wilson01626a32014-09-17 10:38:07 -040062 // 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.
63 // Note that this is the ONLY time this field will be valid.
64 AdminPass string `mapstructure:"adminPass"`
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080065}
66
Ash Wilson01626a32014-09-17 10:38:07 -040067// ExtractServers interprets the results of a single page from a List() call, producing a slice of Server entities.
68func ExtractServers(page pagination.Page) ([]Server, error) {
Ash Wilsonfd043792014-09-17 10:40:17 -040069 casted := page.(ListPage).Body
Ash Wilson12259392014-09-17 10:50:02 -040070
71 var response struct {
72 Servers []Server `mapstructure:"servers"`
73 }
74 err := mapstructure.Decode(casted, &response)
75 return response.Servers, err
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080076}
77
Ash Wilson01626a32014-09-17 10:38:07 -040078// ExtractServer interprets the result of a call expected to return data on a single server.
79func ExtractServer(sr ServerResult) (*Server, error) {
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080080 so, ok := sr["server"]
81 if !ok {
Ash Wilson01626a32014-09-17 10:38:07 -040082 return nil, ErrCannotInterpet
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080083 }
84 serverObj := so.(map[string]interface{})
85 s := new(Server)
86 err := mapstructure.Decode(serverObj, s)
87 return s, err
88}