Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 1 | package servers |
| 2 | |
| 3 | import ( |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 4 | "github.com/mitchellh/mapstructure" |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 5 | "github.com/rackspace/gophercloud" |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud/pagination" |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 7 | ) |
| 8 | |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 9 | type serverResult struct { |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 10 | gophercloud.Result |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 11 | } |
| 12 | |
| 13 | // Extract interprets any serverResult as a Server, if possible. |
| 14 | func (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 Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 23 | err := mapstructure.Decode(r.Body, &response) |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 24 | return &response.Server, err |
| 25 | } |
| 26 | |
| 27 | // CreateResult temporarily contains the response from a Create call. |
| 28 | type CreateResult struct { |
| 29 | serverResult |
| 30 | } |
| 31 | |
| 32 | // GetResult temporarily contains the response from a Get call. |
| 33 | type GetResult struct { |
| 34 | serverResult |
| 35 | } |
| 36 | |
| 37 | // UpdateResult temporarily contains the response from an Update call. |
| 38 | type UpdateResult struct { |
| 39 | serverResult |
| 40 | } |
| 41 | |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame^] | 42 | // DeleteResult temporarily contains the response from a Delete call. |
Jamie Hannaford | 34732fe | 2014-10-27 11:29:36 +0100 | [diff] [blame] | 43 | type DeleteResult struct { |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 44 | gophercloud.ErrResult |
Jamie Hannaford | 34732fe | 2014-10-27 11:29:36 +0100 | [diff] [blame] | 45 | } |
| 46 | |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 47 | // RebuildResult temporarily contains the response from a Rebuild call. |
Jamie Hannaford | 01c1efe | 2014-10-16 15:08:59 +0200 | [diff] [blame] | 48 | type RebuildResult struct { |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 49 | serverResult |
| 50 | } |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 51 | |
Jamie Hannaford | 01c1efe | 2014-10-16 15:08:59 +0200 | [diff] [blame] | 52 | // ActionResult represents the result of server action operations, like reboot |
| 53 | type ActionResult struct { |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 54 | gophercloud.ErrResult |
Jamie Hannaford | 01c1efe | 2014-10-16 15:08:59 +0200 | [diff] [blame] | 55 | } |
| 56 | |
Alex Gaynor | 587e3e3 | 2014-11-13 10:39:09 -0800 | [diff] [blame] | 57 | // RescueResult represents the result of a server rescue operation |
Alex Gaynor | fbe61bb | 2014-11-12 13:35:03 -0800 | [diff] [blame] | 58 | type RescueResult struct { |
| 59 | ActionResult |
Alex Gaynor | 7f3b06e | 2014-11-13 09:54:03 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame^] | 62 | // Extract interprets any RescueResult as an AdminPass, if possible. |
Alex Gaynor | 7f3b06e | 2014-11-13 09:54:03 -0800 | [diff] [blame] | 63 | func (r RescueResult) Extract() (string, error) { |
| 64 | if r.Err != nil { |
Alex Gaynor | 94a28aa | 2014-11-13 10:09:56 -0800 | [diff] [blame] | 65 | return "", r.Err |
Alex Gaynor | 7f3b06e | 2014-11-13 09:54:03 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | var response struct { |
| 69 | AdminPass string `mapstructure:"adminPass"` |
| 70 | } |
| 71 | |
| 72 | err := mapstructure.Decode(r.Body, &response) |
Alex Gaynor | 94a28aa | 2014-11-13 10:09:56 -0800 | [diff] [blame] | 73 | return response.AdminPass, err |
Alex Gaynor | fbe61bb | 2014-11-12 13:35:03 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 76 | // Server exposes only the standard OpenStack fields corresponding to a given server on the user's account. |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 77 | type Server struct { |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 78 | // ID uniquely identifies this server amongst all other servers, including those not accessible to the current tenant. |
| 79 | ID string |
| 80 | |
| 81 | // TenantID identifies the tenant owning this server resource. |
| 82 | TenantID string `mapstructure:"tenant_id"` |
| 83 | |
| 84 | // UserID uniquely identifies the user account owning the tenant. |
| 85 | UserID string `mapstructure:"user_id"` |
| 86 | |
| 87 | // Name contains the human-readable name for the server. |
| 88 | Name string |
| 89 | |
| 90 | // Updated and Created contain ISO-8601 timestamps of when the state of the server last changed, and when it was created. |
| 91 | Updated string |
| 92 | Created string |
| 93 | |
| 94 | HostID string |
| 95 | |
| 96 | // Status contains the current operational status of the server, such as IN_PROGRESS or ACTIVE. |
| 97 | Status string |
| 98 | |
| 99 | // Progress ranges from 0..100. |
| 100 | // A request made against the server completes only once Progress reaches 100. |
| 101 | Progress int |
| 102 | |
| 103 | // AccessIPv4 and AccessIPv6 contain the IP addresses of the server, suitable for remote access for administration. |
Jamie Hannaford | 908e1e9 | 2014-10-27 14:41:17 +0100 | [diff] [blame] | 104 | AccessIPv4, AccessIPv6 string |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 105 | |
| 106 | // Image refers to a JSON object, which itself indicates the OS image used to deploy the server. |
| 107 | Image map[string]interface{} |
| 108 | |
| 109 | // Flavor refers to a JSON object, which itself indicates the hardware configuration of the deployed server. |
| 110 | Flavor map[string]interface{} |
| 111 | |
| 112 | // Addresses includes a list of all IP addresses assigned to the server, keyed by pool. |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 113 | Addresses map[string]interface{} |
| 114 | |
| 115 | // Metadata includes a list of all user-specified key-value pairs attached to the server. |
| 116 | Metadata map[string]interface{} |
| 117 | |
| 118 | // Links includes HTTP references to the itself, useful for passing along to other APIs that might want a server reference. |
| 119 | Links []interface{} |
| 120 | |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 121 | // KeyName indicates which public key was injected into the server on launch. |
Ash Wilson | 69b144f | 2014-10-21 14:03:52 -0400 | [diff] [blame] | 122 | KeyName string `json:"key_name" mapstructure:"key_name"` |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 123 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 124 | // 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. |
| 125 | // Note that this is the ONLY time this field will be valid. |
Ash Wilson | 69b144f | 2014-10-21 14:03:52 -0400 | [diff] [blame] | 126 | AdminPass string `json:"adminPass" mapstructure:"adminPass"` |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 129 | // ServerPage abstracts the raw results of making a List() request against the API. |
| 130 | // As OpenStack extensions may freely alter the response bodies of structures returned to the client, you may only safely access the |
| 131 | // data provided through the ExtractServers call. |
| 132 | type ServerPage struct { |
| 133 | pagination.LinkedPageBase |
Ash Wilson | f57381e | 2014-09-25 13:21:34 -0400 | [diff] [blame] | 134 | } |
| 135 | |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 136 | // IsEmpty returns true if a page contains no Server results. |
| 137 | func (page ServerPage) IsEmpty() (bool, error) { |
| 138 | servers, err := ExtractServers(page) |
| 139 | if err != nil { |
| 140 | return true, err |
| 141 | } |
| 142 | return len(servers) == 0, nil |
| 143 | } |
| 144 | |
| 145 | // NextPageURL uses the response's embedded link reference to navigate to the next page of results. |
| 146 | func (page ServerPage) NextPageURL() (string, error) { |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 147 | type resp struct { |
Jamie Hannaford | a581acd | 2014-10-08 17:14:13 +0200 | [diff] [blame] | 148 | Links []gophercloud.Link `mapstructure:"servers_links"` |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 149 | } |
| 150 | |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 151 | var r resp |
| 152 | err := mapstructure.Decode(page.Body, &r) |
| 153 | if err != nil { |
| 154 | return "", err |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 155 | } |
| 156 | |
Jamie Hannaford | a581acd | 2014-10-08 17:14:13 +0200 | [diff] [blame] | 157 | return gophercloud.ExtractNextURL(r.Links) |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 158 | } |
| 159 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 160 | // ExtractServers interprets the results of a single page from a List() call, producing a slice of Server entities. |
| 161 | func ExtractServers(page pagination.Page) ([]Server, error) { |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 162 | casted := page.(ServerPage).Body |
Ash Wilson | 1225939 | 2014-09-17 10:50:02 -0400 | [diff] [blame] | 163 | |
| 164 | var response struct { |
| 165 | Servers []Server `mapstructure:"servers"` |
| 166 | } |
| 167 | err := mapstructure.Decode(casted, &response) |
| 168 | return response.Servers, err |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 169 | } |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame^] | 170 | |
| 171 | // MetadatasResult contains the result of a call for (potentially) multiple key-value pairs. |
| 172 | type MetadatasResult struct { |
| 173 | gophercloud.Result |
| 174 | } |
| 175 | |
| 176 | // GetMetadatasResult temporarily contains the response from a metadatas Get call. |
| 177 | type GetMetadatasResult struct { |
| 178 | MetadatasResult |
| 179 | } |
| 180 | |
| 181 | // CreateMetadatasResult temporarily contains the response from a metadatas Create call. |
| 182 | type CreateMetadatasResult struct { |
| 183 | MetadatasResult |
| 184 | } |
| 185 | |
| 186 | // UpdateMetadatasResult temporarily contains the response from a metadatas Update call. |
| 187 | type UpdateMetadatasResult struct { |
| 188 | MetadatasResult |
| 189 | } |
| 190 | |
| 191 | // MetadataResult contains the result of a call for individual a single key-value pair. |
| 192 | type MetadataResult struct { |
| 193 | gophercloud.Result |
| 194 | } |
| 195 | |
| 196 | // GetMetadataResult temporarily contains the response from a metadata Get call. |
| 197 | type GetMetadataResult struct { |
| 198 | MetadataResult |
| 199 | } |
| 200 | |
| 201 | // CreateMetadataResult temporarily contains the response from a metadata Create call. |
| 202 | type CreateMetadataResult struct { |
| 203 | MetadataResult |
| 204 | } |
| 205 | |
| 206 | // DeleteMetadataResult temporarily contains the response from a metadata Delete call. |
| 207 | type DeleteMetadataResult struct { |
| 208 | gophercloud.ErrResult |
| 209 | } |
| 210 | |
| 211 | // Extract interprets any MetadatasResult as a Metadatas, if possible. |
| 212 | func (r MetadatasResult) Extract() (map[string]string, error) { |
| 213 | if r.Err != nil { |
| 214 | return nil, r.Err |
| 215 | } |
| 216 | |
| 217 | var response struct { |
| 218 | Metadatas map[string]string `mapstructure:"metadata"` |
| 219 | } |
| 220 | |
| 221 | err := mapstructure.Decode(r.Body, &response) |
| 222 | return response.Metadatas, err |
| 223 | } |
| 224 | |
| 225 | // Extract interprets any MetadataResult as a Metadata, if possible. |
| 226 | func (r MetadataResult) Extract() (map[string]string, error) { |
| 227 | if r.Err != nil { |
| 228 | return nil, r.Err |
| 229 | } |
| 230 | |
| 231 | var response struct { |
| 232 | Metadata map[string]string `mapstructure:"meta"` |
| 233 | } |
| 234 | |
| 235 | err := mapstructure.Decode(r.Body, &response) |
| 236 | return response.Metadata, err |
| 237 | } |