blob: 8e967f03b8fd6c6d809f6598edbca0a592d7c01d [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 Wilsond27e0ff2014-09-25 11:50:31 -040068// extractServer interprets the result of a call expected to return data on a single server.
69func extractServer(r gophercloud.CommonResult) (*Server, error) {
70 if r.Err != nil {
71 return nil, r.Err
72 }
73
74 var response struct {
75 Server Server `mapstructure:"server"`
76 }
77
78 err := mapstructure.Decode(r.Resp, &response)
79 return &response.Server, err
80}
81
82// CreateResult temporarily contains the response from a Create call.
83type CreateResult struct {
84 gophercloud.CommonResult
85}
86
87// Extract interprets a CreateResult as a Server, if possible.
88func (r CreateResult) Extract() (*Server, error) {
89 return extractServer(r.CommonResult)
90}
91
92// GetResult temporarily contains the response from a Get call.
93type GetResult struct {
94 gophercloud.CommonResult
95}
96
97// Extract interprets a GetResult as a Server, if possible.
98func (r GetResult) Extract() (*Server, error) {
99 return extractServer(r.CommonResult)
100}
101
102// UpdateResult temporarily contains the response from an Update call.
103type UpdateResult struct {
104 gophercloud.CommonResult
105}
106
107// Extract interprets an UpdateResult as a Server, if possible.
108func (r UpdateResult) Extract() (*Server, error) {
109 return extractServer(r.CommonResult)
110}
111
112// RebuildResult temporarily contains the response from a Rebuild call.
113type RebuildResult struct {
114 gophercloud.CommonResult
115}
116
117// Extract interprets a RebuildResult as a Server, if possible.
118func (r RebuildResult) Extract() (*Server, error) {
119 return extractServer(r.CommonResult)
120}
121
Ash Wilson01626a32014-09-17 10:38:07 -0400122// ExtractServers interprets the results of a single page from a List() call, producing a slice of Server entities.
123func ExtractServers(page pagination.Page) ([]Server, error) {
Ash Wilsonfd043792014-09-17 10:40:17 -0400124 casted := page.(ListPage).Body
Ash Wilson12259392014-09-17 10:50:02 -0400125
126 var response struct {
127 Servers []Server `mapstructure:"servers"`
128 }
129 err := mapstructure.Decode(casted, &response)
130 return response.Servers, err
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -0800131}