blob: 15003d8d5e0e49cec873145a93f13365f9659301 [file] [log] [blame]
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -08001package servers
2
3import (
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -08004 "github.com/mitchellh/mapstructure"
Ash Wilsond27e0ff2014-09-25 11:50:31 -04005 "github.com/rackspace/gophercloud"
Ash Wilson01626a32014-09-17 10:38:07 -04006 "github.com/rackspace/gophercloud/pagination"
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -08007)
8
Ash Wilson397c78b2014-09-25 15:19:14 -04009type serverResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -040010 gophercloud.Result
Ash Wilson397c78b2014-09-25 15:19:14 -040011}
12
13// Extract interprets any serverResult as a Server, if possible.
14func (r serverResult) Extract() (*Server, error) {
15 if r.Err != nil {
16 return nil, r.Err
17 }
18
19 var response struct {
20 Server Server `mapstructure:"server"`
21 }
22
23 err := mapstructure.Decode(r.Resp, &response)
24 return &response.Server, err
25}
26
27// CreateResult temporarily contains the response from a Create call.
28type CreateResult struct {
29 serverResult
30}
31
32// GetResult temporarily contains the response from a Get call.
33type GetResult struct {
34 serverResult
35}
36
37// UpdateResult temporarily contains the response from an Update call.
38type UpdateResult struct {
39 serverResult
40}
41
42// RebuildResult temporarily contains the response from a Rebuild call.
43type RebuildResult struct {
44 serverResult
45}
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080046
47// 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 -080048type Server struct {
Ash Wilson01626a32014-09-17 10:38:07 -040049 // ID uniquely identifies this server amongst all other servers, including those not accessible to the current tenant.
50 ID string
51
52 // TenantID identifies the tenant owning this server resource.
53 TenantID string `mapstructure:"tenant_id"`
54
55 // UserID uniquely identifies the user account owning the tenant.
56 UserID string `mapstructure:"user_id"`
57
58 // Name contains the human-readable name for the server.
59 Name string
60
61 // Updated and Created contain ISO-8601 timestamps of when the state of the server last changed, and when it was created.
62 Updated string
63 Created string
64
65 HostID string
66
67 // Status contains the current operational status of the server, such as IN_PROGRESS or ACTIVE.
68 Status string
69
70 // Progress ranges from 0..100.
71 // A request made against the server completes only once Progress reaches 100.
72 Progress int
73
74 // 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 -080075 AccessIPv4 string
76 AccessIPv6 string
Ash Wilson01626a32014-09-17 10:38:07 -040077
78 // Image refers to a JSON object, which itself indicates the OS image used to deploy the server.
79 Image map[string]interface{}
80
81 // Flavor refers to a JSON object, which itself indicates the hardware configuration of the deployed server.
82 Flavor map[string]interface{}
83
84 // Addresses includes a list of all IP addresses assigned to the server, keyed by pool.
Ash Wilson01626a32014-09-17 10:38:07 -040085 Addresses map[string]interface{}
86
87 // Metadata includes a list of all user-specified key-value pairs attached to the server.
88 Metadata map[string]interface{}
89
90 // Links includes HTTP references to the itself, useful for passing along to other APIs that might want a server reference.
91 Links []interface{}
92
Ash Wilsonad21c712014-09-25 10:15:22 -040093 // KeyName indicates which public key was injected into the server on launch.
94 KeyName string `mapstructure:"keyname"`
95
Ash Wilson01626a32014-09-17 10:38:07 -040096 // 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.
97 // Note that this is the ONLY time this field will be valid.
98 AdminPass string `mapstructure:"adminPass"`
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080099}
100
Ash Wilson397c78b2014-09-25 15:19:14 -0400101// ServerPage abstracts the raw results of making a List() request against the API.
102// As OpenStack extensions may freely alter the response bodies of structures returned to the client, you may only safely access the
103// data provided through the ExtractServers call.
104type ServerPage struct {
105 pagination.LinkedPageBase
Ash Wilsonf57381e2014-09-25 13:21:34 -0400106}
107
Ash Wilson397c78b2014-09-25 15:19:14 -0400108// IsEmpty returns true if a page contains no Server results.
109func (page ServerPage) IsEmpty() (bool, error) {
110 servers, err := ExtractServers(page)
111 if err != nil {
112 return true, err
113 }
114 return len(servers) == 0, nil
115}
116
117// NextPageURL uses the response's embedded link reference to navigate to the next page of results.
118func (page ServerPage) NextPageURL() (string, error) {
Ash Wilson397c78b2014-09-25 15:19:14 -0400119 type resp struct {
Jamie Hannaforda581acd2014-10-08 17:14:13 +0200120 Links []gophercloud.Link `mapstructure:"servers_links"`
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400121 }
122
Ash Wilson397c78b2014-09-25 15:19:14 -0400123 var r resp
124 err := mapstructure.Decode(page.Body, &r)
125 if err != nil {
126 return "", err
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400127 }
128
Jamie Hannaforda581acd2014-10-08 17:14:13 +0200129 return gophercloud.ExtractNextURL(r.Links)
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400130}
131
Ash Wilson01626a32014-09-17 10:38:07 -0400132// ExtractServers interprets the results of a single page from a List() call, producing a slice of Server entities.
133func ExtractServers(page pagination.Page) ([]Server, error) {
Ash Wilson397c78b2014-09-25 15:19:14 -0400134 casted := page.(ServerPage).Body
Ash Wilson12259392014-09-17 10:50:02 -0400135
136 var response struct {
137 Servers []Server `mapstructure:"servers"`
138 }
139 err := mapstructure.Decode(casted, &response)
140 return response.Servers, err
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -0800141}