blob: d63d7c887aa7db8961b1d9ad8c7c103fb7cb0921 [file] [log] [blame]
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -08001package servers
2
3import (
Jon Perritt9a0980e2015-01-14 21:29:44 -07004 "reflect"
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 Wilson397c78b2014-09-25 15:19:14 -040011type serverResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -040012 gophercloud.Result
Ash Wilson397c78b2014-09-25 15:19:14 -040013}
14
15// Extract interprets any serverResult as a Server, if possible.
16func (r serverResult) Extract() (*Server, error) {
17 if r.Err != nil {
18 return nil, r.Err
19 }
20
21 var response struct {
22 Server Server `mapstructure:"server"`
23 }
24
Jon Perritt9a0980e2015-01-14 21:29:44 -070025 config := &mapstructure.DecoderConfig{
Jon Perritt0e19f602015-01-15 09:35:46 -070026 DecodeHook: toMapFromString,
27 Result: &response,
Jon Perritt9a0980e2015-01-14 21:29:44 -070028 }
29 decoder, err := mapstructure.NewDecoder(config)
30 if err != nil {
31 return nil, err
32 }
33
34 err = decoder.Decode(r.Body)
35 if err != nil {
36 return nil, err
37 }
38
39 return &response.Server, nil
Ash Wilson397c78b2014-09-25 15:19:14 -040040}
41
42// CreateResult temporarily contains the response from a Create call.
43type CreateResult struct {
44 serverResult
45}
46
47// GetResult temporarily contains the response from a Get call.
48type GetResult struct {
49 serverResult
50}
51
52// UpdateResult temporarily contains the response from an Update call.
53type UpdateResult struct {
54 serverResult
55}
56
Jon Perrittcc77da62014-11-16 13:14:21 -070057// DeleteResult temporarily contains the response from a Delete call.
Jamie Hannaford34732fe2014-10-27 11:29:36 +010058type DeleteResult struct {
Jon Perrittba2395e2014-10-27 15:23:21 -050059 gophercloud.ErrResult
Jamie Hannaford34732fe2014-10-27 11:29:36 +010060}
61
Ash Wilson397c78b2014-09-25 15:19:14 -040062// RebuildResult temporarily contains the response from a Rebuild call.
Jamie Hannaford01c1efe2014-10-16 15:08:59 +020063type RebuildResult struct {
Ash Wilson397c78b2014-09-25 15:19:14 -040064 serverResult
65}
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080066
Jamie Hannaford01c1efe2014-10-16 15:08:59 +020067// ActionResult represents the result of server action operations, like reboot
68type ActionResult struct {
Jon Perrittba2395e2014-10-27 15:23:21 -050069 gophercloud.ErrResult
Jamie Hannaford01c1efe2014-10-16 15:08:59 +020070}
71
Alex Gaynor587e3e32014-11-13 10:39:09 -080072// RescueResult represents the result of a server rescue operation
Alex Gaynorfbe61bb2014-11-12 13:35:03 -080073type RescueResult struct {
74 ActionResult
Alex Gaynor7f3b06e2014-11-13 09:54:03 -080075}
76
Jon Perrittcc77da62014-11-16 13:14:21 -070077// Extract interprets any RescueResult as an AdminPass, if possible.
Alex Gaynor7f3b06e2014-11-13 09:54:03 -080078func (r RescueResult) Extract() (string, error) {
79 if r.Err != nil {
Alex Gaynor94a28aa2014-11-13 10:09:56 -080080 return "", r.Err
Alex Gaynor7f3b06e2014-11-13 09:54:03 -080081 }
82
83 var response struct {
84 AdminPass string `mapstructure:"adminPass"`
85 }
86
87 err := mapstructure.Decode(r.Body, &response)
Alex Gaynor94a28aa2014-11-13 10:09:56 -080088 return response.AdminPass, err
Alex Gaynorfbe61bb2014-11-12 13:35:03 -080089}
90
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080091// 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 -080092type Server struct {
Ash Wilson01626a32014-09-17 10:38:07 -040093 // ID uniquely identifies this server amongst all other servers, including those not accessible to the current tenant.
94 ID string
95
96 // TenantID identifies the tenant owning this server resource.
97 TenantID string `mapstructure:"tenant_id"`
98
99 // UserID uniquely identifies the user account owning the tenant.
100 UserID string `mapstructure:"user_id"`
101
102 // Name contains the human-readable name for the server.
103 Name string
104
105 // Updated and Created contain ISO-8601 timestamps of when the state of the server last changed, and when it was created.
106 Updated string
107 Created string
108
109 HostID string
110
111 // Status contains the current operational status of the server, such as IN_PROGRESS or ACTIVE.
112 Status string
113
114 // Progress ranges from 0..100.
115 // A request made against the server completes only once Progress reaches 100.
116 Progress int
117
118 // AccessIPv4 and AccessIPv6 contain the IP addresses of the server, suitable for remote access for administration.
Jamie Hannaford908e1e92014-10-27 14:41:17 +0100119 AccessIPv4, AccessIPv6 string
Ash Wilson01626a32014-09-17 10:38:07 -0400120
121 // Image refers to a JSON object, which itself indicates the OS image used to deploy the server.
122 Image map[string]interface{}
123
124 // Flavor refers to a JSON object, which itself indicates the hardware configuration of the deployed server.
125 Flavor map[string]interface{}
126
127 // Addresses includes a list of all IP addresses assigned to the server, keyed by pool.
Ash Wilson01626a32014-09-17 10:38:07 -0400128 Addresses map[string]interface{}
129
130 // Metadata includes a list of all user-specified key-value pairs attached to the server.
131 Metadata map[string]interface{}
132
133 // Links includes HTTP references to the itself, useful for passing along to other APIs that might want a server reference.
134 Links []interface{}
135
Ash Wilsonad21c712014-09-25 10:15:22 -0400136 // KeyName indicates which public key was injected into the server on launch.
Ash Wilson69b144f2014-10-21 14:03:52 -0400137 KeyName string `json:"key_name" mapstructure:"key_name"`
Ash Wilsonad21c712014-09-25 10:15:22 -0400138
Ash Wilson01626a32014-09-17 10:38:07 -0400139 // 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.
140 // Note that this is the ONLY time this field will be valid.
Ash Wilson69b144f2014-10-21 14:03:52 -0400141 AdminPass string `json:"adminPass" mapstructure:"adminPass"`
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -0800142}
143
Ash Wilson397c78b2014-09-25 15:19:14 -0400144// ServerPage abstracts the raw results of making a List() request against the API.
145// As OpenStack extensions may freely alter the response bodies of structures returned to the client, you may only safely access the
146// data provided through the ExtractServers call.
147type ServerPage struct {
148 pagination.LinkedPageBase
Ash Wilsonf57381e2014-09-25 13:21:34 -0400149}
150
Ash Wilson397c78b2014-09-25 15:19:14 -0400151// IsEmpty returns true if a page contains no Server results.
152func (page ServerPage) IsEmpty() (bool, error) {
153 servers, err := ExtractServers(page)
154 if err != nil {
155 return true, err
156 }
157 return len(servers) == 0, nil
158}
159
160// NextPageURL uses the response's embedded link reference to navigate to the next page of results.
161func (page ServerPage) NextPageURL() (string, error) {
Ash Wilson397c78b2014-09-25 15:19:14 -0400162 type resp struct {
Jamie Hannaforda581acd2014-10-08 17:14:13 +0200163 Links []gophercloud.Link `mapstructure:"servers_links"`
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400164 }
165
Ash Wilson397c78b2014-09-25 15:19:14 -0400166 var r resp
167 err := mapstructure.Decode(page.Body, &r)
168 if err != nil {
169 return "", err
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400170 }
171
Jamie Hannaforda581acd2014-10-08 17:14:13 +0200172 return gophercloud.ExtractNextURL(r.Links)
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400173}
174
Ash Wilson01626a32014-09-17 10:38:07 -0400175// ExtractServers interprets the results of a single page from a List() call, producing a slice of Server entities.
176func ExtractServers(page pagination.Page) ([]Server, error) {
Ash Wilson397c78b2014-09-25 15:19:14 -0400177 casted := page.(ServerPage).Body
Ash Wilson12259392014-09-17 10:50:02 -0400178
179 var response struct {
180 Servers []Server `mapstructure:"servers"`
181 }
Jon Perritt9a0980e2015-01-14 21:29:44 -0700182
183 config := &mapstructure.DecoderConfig{
Jon Perritt0e19f602015-01-15 09:35:46 -0700184 DecodeHook: toMapFromString,
185 Result: &response,
Jon Perritt9a0980e2015-01-14 21:29:44 -0700186 }
187 decoder, err := mapstructure.NewDecoder(config)
188 if err != nil {
189 return nil, err
190 }
191
192 err = decoder.Decode(casted)
193
194 //err := mapstructure.Decode(casted, &response)
Ash Wilson12259392014-09-17 10:50:02 -0400195 return response.Servers, err
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -0800196}
Jon Perrittcc77da62014-11-16 13:14:21 -0700197
Jon Perritt78c57ce2014-11-20 11:07:18 -0700198// MetadataResult contains the result of a call for (potentially) multiple key-value pairs.
Jon Perrittcc77da62014-11-16 13:14:21 -0700199type MetadataResult struct {
200 gophercloud.Result
201}
202
203// GetMetadataResult temporarily contains the response from a metadata Get call.
204type GetMetadataResult struct {
205 MetadataResult
206}
207
Jon Perritt789f8322014-11-21 08:20:04 -0700208// ResetMetadataResult temporarily contains the response from a metadata Reset call.
209type ResetMetadataResult struct {
Jon Perrittcc77da62014-11-16 13:14:21 -0700210 MetadataResult
211}
212
Jon Perritt78c57ce2014-11-20 11:07:18 -0700213// UpdateMetadataResult temporarily contains the response from a metadata Update call.
214type UpdateMetadataResult struct {
215 MetadataResult
Jon Perrittcc77da62014-11-16 13:14:21 -0700216}
217
Jon Perritt78c57ce2014-11-20 11:07:18 -0700218// MetadatumResult contains the result of a call for individual a single key-value pair.
219type MetadatumResult struct {
220 gophercloud.Result
221}
Jon Perrittcc77da62014-11-16 13:14:21 -0700222
Jon Perritt78c57ce2014-11-20 11:07:18 -0700223// GetMetadatumResult temporarily contains the response from a metadatum Get call.
224type GetMetadatumResult struct {
225 MetadatumResult
226}
Jon Perrittcc77da62014-11-16 13:14:21 -0700227
Jon Perritt78c57ce2014-11-20 11:07:18 -0700228// CreateMetadatumResult temporarily contains the response from a metadatum Create call.
229type CreateMetadatumResult struct {
230 MetadatumResult
231}
232
233// DeleteMetadatumResult temporarily contains the response from a metadatum Delete call.
234type DeleteMetadatumResult struct {
235 gophercloud.ErrResult
Jon Perrittcc77da62014-11-16 13:14:21 -0700236}
237
238// Extract interprets any MetadataResult as a Metadata, if possible.
239func (r MetadataResult) Extract() (map[string]string, error) {
240 if r.Err != nil {
241 return nil, r.Err
242 }
243
244 var response struct {
Jon Perritt78c57ce2014-11-20 11:07:18 -0700245 Metadata map[string]string `mapstructure:"metadata"`
Jon Perrittcc77da62014-11-16 13:14:21 -0700246 }
247
248 err := mapstructure.Decode(r.Body, &response)
249 return response.Metadata, err
250}
Jon Perritt78c57ce2014-11-20 11:07:18 -0700251
252// Extract interprets any MetadatumResult as a Metadatum, if possible.
253func (r MetadatumResult) Extract() (map[string]string, error) {
254 if r.Err != nil {
255 return nil, r.Err
256 }
257
258 var response struct {
259 Metadatum map[string]string `mapstructure:"meta"`
260 }
261
262 err := mapstructure.Decode(r.Body, &response)
263 return response.Metadatum, err
264}
Jon Perritt0e19f602015-01-15 09:35:46 -0700265
266func toMapFromString(from reflect.Kind, to reflect.Kind, data interface{}) (interface{}, error) {
267 if (from == reflect.String) && (to == reflect.Map) {
268 return map[string]interface{}{}, nil
269 }
270 return data, nil
271}