blob: c473450349bb5410b6767a0955504f7d573629bc [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
Ash Wilsond3dc2542014-10-20 10:10:48 -040023 err := mapstructure.Decode(r.Body, &response)
Ash Wilson397c78b2014-09-25 15:19:14 -040024 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
Jamie Hannaford34732fe2014-10-27 11:29:36 +010042// DeleteResult temporarily contains the response from an Delete call.
43type DeleteResult struct {
Jon Perrittba2395e2014-10-27 15:23:21 -050044 gophercloud.ErrResult
Jamie Hannaford34732fe2014-10-27 11:29:36 +010045}
46
Ash Wilson397c78b2014-09-25 15:19:14 -040047// RebuildResult temporarily contains the response from a Rebuild call.
Jamie Hannaford01c1efe2014-10-16 15:08:59 +020048type RebuildResult struct {
Ash Wilson397c78b2014-09-25 15:19:14 -040049 serverResult
50}
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080051
Jamie Hannaford01c1efe2014-10-16 15:08:59 +020052// ActionResult represents the result of server action operations, like reboot
53type ActionResult struct {
Jon Perrittba2395e2014-10-27 15:23:21 -050054 gophercloud.ErrResult
Jamie Hannaford01c1efe2014-10-16 15:08:59 +020055}
56
Alex Gaynorfbe61bb2014-11-12 13:35:03 -080057// RescueResult represents teh result of a server rescue operation
58type RescueResult struct {
59 ActionResult
60 AdminPass string `mapstructure:"adminPass"`
61}
62
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080063// 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 -080064type Server struct {
Ash Wilson01626a32014-09-17 10:38:07 -040065 // ID uniquely identifies this server amongst all other servers, including those not accessible to the current tenant.
66 ID string
67
68 // TenantID identifies the tenant owning this server resource.
69 TenantID string `mapstructure:"tenant_id"`
70
71 // UserID uniquely identifies the user account owning the tenant.
72 UserID string `mapstructure:"user_id"`
73
74 // Name contains the human-readable name for the server.
75 Name string
76
77 // Updated and Created contain ISO-8601 timestamps of when the state of the server last changed, and when it was created.
78 Updated string
79 Created string
80
81 HostID string
82
83 // Status contains the current operational status of the server, such as IN_PROGRESS or ACTIVE.
84 Status string
85
86 // Progress ranges from 0..100.
87 // A request made against the server completes only once Progress reaches 100.
88 Progress int
89
90 // AccessIPv4 and AccessIPv6 contain the IP addresses of the server, suitable for remote access for administration.
Jamie Hannaford908e1e92014-10-27 14:41:17 +010091 AccessIPv4, AccessIPv6 string
Ash Wilson01626a32014-09-17 10:38:07 -040092
93 // Image refers to a JSON object, which itself indicates the OS image used to deploy the server.
94 Image map[string]interface{}
95
96 // Flavor refers to a JSON object, which itself indicates the hardware configuration of the deployed server.
97 Flavor map[string]interface{}
98
99 // Addresses includes a list of all IP addresses assigned to the server, keyed by pool.
Ash Wilson01626a32014-09-17 10:38:07 -0400100 Addresses map[string]interface{}
101
102 // Metadata includes a list of all user-specified key-value pairs attached to the server.
103 Metadata map[string]interface{}
104
105 // Links includes HTTP references to the itself, useful for passing along to other APIs that might want a server reference.
106 Links []interface{}
107
Ash Wilsonad21c712014-09-25 10:15:22 -0400108 // KeyName indicates which public key was injected into the server on launch.
Ash Wilson69b144f2014-10-21 14:03:52 -0400109 KeyName string `json:"key_name" mapstructure:"key_name"`
Ash Wilsonad21c712014-09-25 10:15:22 -0400110
Ash Wilson01626a32014-09-17 10:38:07 -0400111 // 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.
112 // Note that this is the ONLY time this field will be valid.
Ash Wilson69b144f2014-10-21 14:03:52 -0400113 AdminPass string `json:"adminPass" mapstructure:"adminPass"`
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -0800114}
115
Ash Wilson397c78b2014-09-25 15:19:14 -0400116// ServerPage abstracts the raw results of making a List() request against the API.
117// As OpenStack extensions may freely alter the response bodies of structures returned to the client, you may only safely access the
118// data provided through the ExtractServers call.
119type ServerPage struct {
120 pagination.LinkedPageBase
Ash Wilsonf57381e2014-09-25 13:21:34 -0400121}
122
Ash Wilson397c78b2014-09-25 15:19:14 -0400123// IsEmpty returns true if a page contains no Server results.
124func (page ServerPage) IsEmpty() (bool, error) {
125 servers, err := ExtractServers(page)
126 if err != nil {
127 return true, err
128 }
129 return len(servers) == 0, nil
130}
131
132// NextPageURL uses the response's embedded link reference to navigate to the next page of results.
133func (page ServerPage) NextPageURL() (string, error) {
Ash Wilson397c78b2014-09-25 15:19:14 -0400134 type resp struct {
Jamie Hannaforda581acd2014-10-08 17:14:13 +0200135 Links []gophercloud.Link `mapstructure:"servers_links"`
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400136 }
137
Ash Wilson397c78b2014-09-25 15:19:14 -0400138 var r resp
139 err := mapstructure.Decode(page.Body, &r)
140 if err != nil {
141 return "", err
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400142 }
143
Jamie Hannaforda581acd2014-10-08 17:14:13 +0200144 return gophercloud.ExtractNextURL(r.Links)
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400145}
146
Ash Wilson01626a32014-09-17 10:38:07 -0400147// ExtractServers interprets the results of a single page from a List() call, producing a slice of Server entities.
148func ExtractServers(page pagination.Page) ([]Server, error) {
Ash Wilson397c78b2014-09-25 15:19:14 -0400149 casted := page.(ServerPage).Body
Ash Wilson12259392014-09-17 10:50:02 -0400150
151 var response struct {
152 Servers []Server `mapstructure:"servers"`
153 }
154 err := mapstructure.Decode(casted, &response)
155 return response.Servers, err
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -0800156}