Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 1 | package servers |
| 2 | |
| 3 | import ( |
Jon Perritt | 9a0980e | 2015-01-14 21:29:44 -0700 | [diff] [blame] | 4 | "reflect" |
| 5 | |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 6 | "github.com/mitchellh/mapstructure" |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud" |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/pagination" |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 9 | ) |
| 10 | |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 11 | type serverResult struct { |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 12 | gophercloud.Result |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 13 | } |
| 14 | |
| 15 | // Extract interprets any serverResult as a Server, if possible. |
| 16 | func (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 Perritt | 9a0980e | 2015-01-14 21:29:44 -0700 | [diff] [blame] | 25 | config := &mapstructure.DecoderConfig{ |
Jon Perritt | 0e19f60 | 2015-01-15 09:35:46 -0700 | [diff] [blame] | 26 | DecodeHook: toMapFromString, |
| 27 | Result: &response, |
Jon Perritt | 9a0980e | 2015-01-14 21:29:44 -0700 | [diff] [blame] | 28 | } |
| 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 Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | // CreateResult temporarily contains the response from a Create call. |
| 43 | type CreateResult struct { |
| 44 | serverResult |
| 45 | } |
| 46 | |
| 47 | // GetResult temporarily contains the response from a Get call. |
| 48 | type GetResult struct { |
| 49 | serverResult |
| 50 | } |
| 51 | |
| 52 | // UpdateResult temporarily contains the response from an Update call. |
| 53 | type UpdateResult struct { |
| 54 | serverResult |
| 55 | } |
| 56 | |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 57 | // DeleteResult temporarily contains the response from a Delete call. |
Jamie Hannaford | 34732fe | 2014-10-27 11:29:36 +0100 | [diff] [blame] | 58 | type DeleteResult struct { |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 59 | gophercloud.ErrResult |
Jamie Hannaford | 34732fe | 2014-10-27 11:29:36 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 62 | // RebuildResult temporarily contains the response from a Rebuild call. |
Jamie Hannaford | 01c1efe | 2014-10-16 15:08:59 +0200 | [diff] [blame] | 63 | type RebuildResult struct { |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 64 | serverResult |
| 65 | } |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 66 | |
Jamie Hannaford | 01c1efe | 2014-10-16 15:08:59 +0200 | [diff] [blame] | 67 | // ActionResult represents the result of server action operations, like reboot |
| 68 | type ActionResult struct { |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 69 | gophercloud.ErrResult |
Jamie Hannaford | 01c1efe | 2014-10-16 15:08:59 +0200 | [diff] [blame] | 70 | } |
| 71 | |
Alex Gaynor | 587e3e3 | 2014-11-13 10:39:09 -0800 | [diff] [blame] | 72 | // RescueResult represents the result of a server rescue operation |
Alex Gaynor | fbe61bb | 2014-11-12 13:35:03 -0800 | [diff] [blame] | 73 | type RescueResult struct { |
| 74 | ActionResult |
Alex Gaynor | 7f3b06e | 2014-11-13 09:54:03 -0800 | [diff] [blame] | 75 | } |
| 76 | |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 77 | // Extract interprets any RescueResult as an AdminPass, if possible. |
Alex Gaynor | 7f3b06e | 2014-11-13 09:54:03 -0800 | [diff] [blame] | 78 | func (r RescueResult) Extract() (string, error) { |
| 79 | if r.Err != nil { |
Alex Gaynor | 94a28aa | 2014-11-13 10:09:56 -0800 | [diff] [blame] | 80 | return "", r.Err |
Alex Gaynor | 7f3b06e | 2014-11-13 09:54:03 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | var response struct { |
| 84 | AdminPass string `mapstructure:"adminPass"` |
| 85 | } |
| 86 | |
| 87 | err := mapstructure.Decode(r.Body, &response) |
Alex Gaynor | 94a28aa | 2014-11-13 10:09:56 -0800 | [diff] [blame] | 88 | return response.AdminPass, err |
Alex Gaynor | fbe61bb | 2014-11-12 13:35:03 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 91 | // 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] | 92 | type Server struct { |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 93 | // 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 Hannaford | 908e1e9 | 2014-10-27 14:41:17 +0100 | [diff] [blame] | 119 | AccessIPv4, AccessIPv6 string |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 120 | |
| 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 Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 128 | 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 Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 136 | // KeyName indicates which public key was injected into the server on launch. |
Ash Wilson | 69b144f | 2014-10-21 14:03:52 -0400 | [diff] [blame] | 137 | KeyName string `json:"key_name" mapstructure:"key_name"` |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 138 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 139 | // 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 Wilson | 69b144f | 2014-10-21 14:03:52 -0400 | [diff] [blame] | 141 | AdminPass string `json:"adminPass" mapstructure:"adminPass"` |
Joe Topjian | 978bb50 | 2015-02-12 20:55:31 +0000 | [diff] [blame] | 142 | |
| 143 | // SecurityGroups includes the security groups that this instance has applied to it |
| 144 | SecurityGroups []map[string]interface{} `json:"security_groups" mapstructure:"security_groups"` |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 145 | } |
| 146 | |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 147 | // ServerPage abstracts the raw results of making a List() request against the API. |
| 148 | // As OpenStack extensions may freely alter the response bodies of structures returned to the client, you may only safely access the |
| 149 | // data provided through the ExtractServers call. |
| 150 | type ServerPage struct { |
| 151 | pagination.LinkedPageBase |
Ash Wilson | f57381e | 2014-09-25 13:21:34 -0400 | [diff] [blame] | 152 | } |
| 153 | |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 154 | // IsEmpty returns true if a page contains no Server results. |
| 155 | func (page ServerPage) IsEmpty() (bool, error) { |
| 156 | servers, err := ExtractServers(page) |
| 157 | if err != nil { |
| 158 | return true, err |
| 159 | } |
| 160 | return len(servers) == 0, nil |
| 161 | } |
| 162 | |
| 163 | // NextPageURL uses the response's embedded link reference to navigate to the next page of results. |
| 164 | func (page ServerPage) NextPageURL() (string, error) { |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 165 | type resp struct { |
Jamie Hannaford | a581acd | 2014-10-08 17:14:13 +0200 | [diff] [blame] | 166 | Links []gophercloud.Link `mapstructure:"servers_links"` |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 167 | } |
| 168 | |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 169 | var r resp |
| 170 | err := mapstructure.Decode(page.Body, &r) |
| 171 | if err != nil { |
| 172 | return "", err |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 173 | } |
| 174 | |
Jamie Hannaford | a581acd | 2014-10-08 17:14:13 +0200 | [diff] [blame] | 175 | return gophercloud.ExtractNextURL(r.Links) |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 176 | } |
| 177 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 178 | // ExtractServers interprets the results of a single page from a List() call, producing a slice of Server entities. |
| 179 | func ExtractServers(page pagination.Page) ([]Server, error) { |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 180 | casted := page.(ServerPage).Body |
Ash Wilson | 1225939 | 2014-09-17 10:50:02 -0400 | [diff] [blame] | 181 | |
| 182 | var response struct { |
| 183 | Servers []Server `mapstructure:"servers"` |
| 184 | } |
Jon Perritt | 9a0980e | 2015-01-14 21:29:44 -0700 | [diff] [blame] | 185 | |
| 186 | config := &mapstructure.DecoderConfig{ |
Jon Perritt | 0e19f60 | 2015-01-15 09:35:46 -0700 | [diff] [blame] | 187 | DecodeHook: toMapFromString, |
| 188 | Result: &response, |
Jon Perritt | 9a0980e | 2015-01-14 21:29:44 -0700 | [diff] [blame] | 189 | } |
| 190 | decoder, err := mapstructure.NewDecoder(config) |
| 191 | if err != nil { |
| 192 | return nil, err |
| 193 | } |
| 194 | |
| 195 | err = decoder.Decode(casted) |
| 196 | |
| 197 | //err := mapstructure.Decode(casted, &response) |
Ash Wilson | 1225939 | 2014-09-17 10:50:02 -0400 | [diff] [blame] | 198 | return response.Servers, err |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 199 | } |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 200 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 201 | // MetadataResult contains the result of a call for (potentially) multiple key-value pairs. |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 202 | type MetadataResult struct { |
| 203 | gophercloud.Result |
| 204 | } |
| 205 | |
| 206 | // GetMetadataResult temporarily contains the response from a metadata Get call. |
| 207 | type GetMetadataResult struct { |
| 208 | MetadataResult |
| 209 | } |
| 210 | |
Jon Perritt | 789f832 | 2014-11-21 08:20:04 -0700 | [diff] [blame] | 211 | // ResetMetadataResult temporarily contains the response from a metadata Reset call. |
| 212 | type ResetMetadataResult struct { |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 213 | MetadataResult |
| 214 | } |
| 215 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 216 | // UpdateMetadataResult temporarily contains the response from a metadata Update call. |
| 217 | type UpdateMetadataResult struct { |
| 218 | MetadataResult |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 221 | // MetadatumResult contains the result of a call for individual a single key-value pair. |
| 222 | type MetadatumResult struct { |
| 223 | gophercloud.Result |
| 224 | } |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 225 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 226 | // GetMetadatumResult temporarily contains the response from a metadatum Get call. |
| 227 | type GetMetadatumResult struct { |
| 228 | MetadatumResult |
| 229 | } |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 230 | |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 231 | // CreateMetadatumResult temporarily contains the response from a metadatum Create call. |
| 232 | type CreateMetadatumResult struct { |
| 233 | MetadatumResult |
| 234 | } |
| 235 | |
| 236 | // DeleteMetadatumResult temporarily contains the response from a metadatum Delete call. |
| 237 | type DeleteMetadatumResult struct { |
| 238 | gophercloud.ErrResult |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // Extract interprets any MetadataResult as a Metadata, if possible. |
| 242 | func (r MetadataResult) Extract() (map[string]string, error) { |
| 243 | if r.Err != nil { |
| 244 | return nil, r.Err |
| 245 | } |
| 246 | |
| 247 | var response struct { |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 248 | Metadata map[string]string `mapstructure:"metadata"` |
Jon Perritt | cc77da6 | 2014-11-16 13:14:21 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | err := mapstructure.Decode(r.Body, &response) |
| 252 | return response.Metadata, err |
| 253 | } |
Jon Perritt | 78c57ce | 2014-11-20 11:07:18 -0700 | [diff] [blame] | 254 | |
| 255 | // Extract interprets any MetadatumResult as a Metadatum, if possible. |
| 256 | func (r MetadatumResult) Extract() (map[string]string, error) { |
| 257 | if r.Err != nil { |
| 258 | return nil, r.Err |
| 259 | } |
| 260 | |
| 261 | var response struct { |
| 262 | Metadatum map[string]string `mapstructure:"meta"` |
| 263 | } |
| 264 | |
| 265 | err := mapstructure.Decode(r.Body, &response) |
| 266 | return response.Metadatum, err |
| 267 | } |
Jon Perritt | 0e19f60 | 2015-01-15 09:35:46 -0700 | [diff] [blame] | 268 | |
| 269 | func toMapFromString(from reflect.Kind, to reflect.Kind, data interface{}) (interface{}, error) { |
| 270 | if (from == reflect.String) && (to == reflect.Map) { |
| 271 | return map[string]interface{}{}, nil |
| 272 | } |
| 273 | return data, nil |
| 274 | } |