Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 1 | package servers |
| 2 | |
| 3 | import ( |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 4 | "errors" |
| 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 | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 11 | // ErrCannotInterpret is returned by an Extract call if the response body doesn't have the expected structure. |
| 12 | var ErrCannotInterpet = errors.New("Unable to interpret a response body.") |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 13 | |
| 14 | // 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] | 15 | type Server struct { |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 16 | // ID uniquely identifies this server amongst all other servers, including those not accessible to the current tenant. |
| 17 | ID string |
| 18 | |
| 19 | // TenantID identifies the tenant owning this server resource. |
| 20 | TenantID string `mapstructure:"tenant_id"` |
| 21 | |
| 22 | // UserID uniquely identifies the user account owning the tenant. |
| 23 | UserID string `mapstructure:"user_id"` |
| 24 | |
| 25 | // Name contains the human-readable name for the server. |
| 26 | Name string |
| 27 | |
| 28 | // Updated and Created contain ISO-8601 timestamps of when the state of the server last changed, and when it was created. |
| 29 | Updated string |
| 30 | Created string |
| 31 | |
| 32 | HostID string |
| 33 | |
| 34 | // Status contains the current operational status of the server, such as IN_PROGRESS or ACTIVE. |
| 35 | Status string |
| 36 | |
| 37 | // Progress ranges from 0..100. |
| 38 | // A request made against the server completes only once Progress reaches 100. |
| 39 | Progress int |
| 40 | |
| 41 | // AccessIPv4 and AccessIPv6 contain the IP addresses of the server, suitable for remote access for administration. |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 42 | AccessIPv4 string |
| 43 | AccessIPv6 string |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 44 | |
| 45 | // Image refers to a JSON object, which itself indicates the OS image used to deploy the server. |
| 46 | Image map[string]interface{} |
| 47 | |
| 48 | // Flavor refers to a JSON object, which itself indicates the hardware configuration of the deployed server. |
| 49 | Flavor map[string]interface{} |
| 50 | |
| 51 | // 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] | 52 | Addresses map[string]interface{} |
| 53 | |
| 54 | // Metadata includes a list of all user-specified key-value pairs attached to the server. |
| 55 | Metadata map[string]interface{} |
| 56 | |
| 57 | // Links includes HTTP references to the itself, useful for passing along to other APIs that might want a server reference. |
| 58 | Links []interface{} |
| 59 | |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 60 | // KeyName indicates which public key was injected into the server on launch. |
| 61 | KeyName string `mapstructure:"keyname"` |
| 62 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 63 | // 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. |
| 64 | // Note that this is the ONLY time this field will be valid. |
| 65 | AdminPass string `mapstructure:"adminPass"` |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Ash Wilson | f57381e | 2014-09-25 13:21:34 -0400 | [diff] [blame] | 68 | type serverResult struct { |
| 69 | gophercloud.CommonResult |
| 70 | } |
| 71 | |
| 72 | // Extract interprets any serverResult as a Server, if possible. |
| 73 | func (r serverResult) Extract() (*Server, error) { |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 74 | if r.Err != nil { |
| 75 | return nil, r.Err |
| 76 | } |
| 77 | |
| 78 | var response struct { |
| 79 | Server Server `mapstructure:"server"` |
| 80 | } |
| 81 | |
| 82 | err := mapstructure.Decode(r.Resp, &response) |
| 83 | return &response.Server, err |
| 84 | } |
| 85 | |
| 86 | // CreateResult temporarily contains the response from a Create call. |
| 87 | type CreateResult struct { |
Ash Wilson | f57381e | 2014-09-25 13:21:34 -0400 | [diff] [blame] | 88 | serverResult |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // GetResult temporarily contains the response from a Get call. |
| 92 | type GetResult struct { |
Ash Wilson | f57381e | 2014-09-25 13:21:34 -0400 | [diff] [blame] | 93 | serverResult |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | // UpdateResult temporarily contains the response from an Update call. |
| 97 | type UpdateResult struct { |
Ash Wilson | f57381e | 2014-09-25 13:21:34 -0400 | [diff] [blame] | 98 | serverResult |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // RebuildResult temporarily contains the response from a Rebuild call. |
| 102 | type RebuildResult struct { |
Ash Wilson | f57381e | 2014-09-25 13:21:34 -0400 | [diff] [blame] | 103 | serverResult |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 104 | } |
| 105 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 106 | // ExtractServers interprets the results of a single page from a List() call, producing a slice of Server entities. |
| 107 | func ExtractServers(page pagination.Page) ([]Server, error) { |
Ash Wilson | fd04379 | 2014-09-17 10:40:17 -0400 | [diff] [blame] | 108 | casted := page.(ListPage).Body |
Ash Wilson | 1225939 | 2014-09-17 10:50:02 -0400 | [diff] [blame] | 109 | |
| 110 | var response struct { |
| 111 | Servers []Server `mapstructure:"servers"` |
| 112 | } |
| 113 | err := mapstructure.Decode(casted, &response) |
| 114 | return response.Servers, err |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 115 | } |