blob: 29cbf13373bb8ee89eeb6ec9d5d8f699b0e5129c [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 Wilsond27e0ff2014-09-25 11:50:31 -04007 "github.com/rackspace/gophercloud"
Ash Wilson01626a32014-09-17 10:38:07 -04008 "github.com/rackspace/gophercloud/pagination"
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -08009)
10
Ash Wilson01626a32014-09-17 10:38:07 -040011// ErrCannotInterpret is returned by an Extract call if the response body doesn't have the expected structure.
12var ErrCannotInterpet = errors.New("Unable to interpret a response body.")
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080013
14// 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 -080015type Server struct {
Ash Wilson01626a32014-09-17 10:38:07 -040016 // ID uniquely identifies this server amongst all other servers, including those not accessible to the current tenant.
17 ID string
18
19 // TenantID identifies the tenant owning this server resource.
20 TenantID string `mapstructure:"tenant_id"`
21
22 // UserID uniquely identifies the user account owning the tenant.
23 UserID string `mapstructure:"user_id"`
24
25 // Name contains the human-readable name for the server.
26 Name string
27
28 // Updated and Created contain ISO-8601 timestamps of when the state of the server last changed, and when it was created.
29 Updated string
30 Created string
31
32 HostID string
33
34 // Status contains the current operational status of the server, such as IN_PROGRESS or ACTIVE.
35 Status string
36
37 // Progress ranges from 0..100.
38 // A request made against the server completes only once Progress reaches 100.
39 Progress int
40
41 // 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 -080042 AccessIPv4 string
43 AccessIPv6 string
Ash Wilson01626a32014-09-17 10:38:07 -040044
45 // Image refers to a JSON object, which itself indicates the OS image used to deploy the server.
46 Image map[string]interface{}
47
48 // Flavor refers to a JSON object, which itself indicates the hardware configuration of the deployed server.
49 Flavor map[string]interface{}
50
51 // Addresses includes a list of all IP addresses assigned to the server, keyed by pool.
Ash Wilson01626a32014-09-17 10:38:07 -040052 Addresses map[string]interface{}
53
54 // Metadata includes a list of all user-specified key-value pairs attached to the server.
55 Metadata map[string]interface{}
56
57 // Links includes HTTP references to the itself, useful for passing along to other APIs that might want a server reference.
58 Links []interface{}
59
Ash Wilsonad21c712014-09-25 10:15:22 -040060 // KeyName indicates which public key was injected into the server on launch.
61 KeyName string `mapstructure:"keyname"`
62
Ash Wilson01626a32014-09-17 10:38:07 -040063 // 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.
64 // Note that this is the ONLY time this field will be valid.
65 AdminPass string `mapstructure:"adminPass"`
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080066}
67
Ash Wilsonf57381e2014-09-25 13:21:34 -040068type serverResult struct {
69 gophercloud.CommonResult
70}
71
72// Extract interprets any serverResult as a Server, if possible.
73func (r serverResult) Extract() (*Server, error) {
Ash Wilsond27e0ff2014-09-25 11:50:31 -040074 if r.Err != nil {
75 return nil, r.Err
76 }
77
78 var response struct {
79 Server Server `mapstructure:"server"`
80 }
81
82 err := mapstructure.Decode(r.Resp, &response)
83 return &response.Server, err
84}
85
86// CreateResult temporarily contains the response from a Create call.
87type CreateResult struct {
Ash Wilsonf57381e2014-09-25 13:21:34 -040088 serverResult
Ash Wilsond27e0ff2014-09-25 11:50:31 -040089}
90
91// GetResult temporarily contains the response from a Get call.
92type GetResult struct {
Ash Wilsonf57381e2014-09-25 13:21:34 -040093 serverResult
Ash Wilsond27e0ff2014-09-25 11:50:31 -040094}
95
96// UpdateResult temporarily contains the response from an Update call.
97type UpdateResult struct {
Ash Wilsonf57381e2014-09-25 13:21:34 -040098 serverResult
Ash Wilsond27e0ff2014-09-25 11:50:31 -040099}
100
101// RebuildResult temporarily contains the response from a Rebuild call.
102type RebuildResult struct {
Ash Wilsonf57381e2014-09-25 13:21:34 -0400103 serverResult
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400104}
105
Ash Wilson01626a32014-09-17 10:38:07 -0400106// ExtractServers interprets the results of a single page from a List() call, producing a slice of Server entities.
107func ExtractServers(page pagination.Page) ([]Server, error) {
Ash Wilsonfd043792014-09-17 10:40:17 -0400108 casted := page.(ListPage).Body
Ash Wilson12259392014-09-17 10:50:02 -0400109
110 var response struct {
111 Servers []Server `mapstructure:"servers"`
112 }
113 err := mapstructure.Decode(casted, &response)
114 return response.Servers, err
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -0800115}