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 | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame^] | 68 | // extractServer interprets the result of a call expected to return data on a single server. |
| 69 | func extractServer(r gophercloud.CommonResult) (*Server, error) { |
| 70 | if r.Err != nil { |
| 71 | return nil, r.Err |
| 72 | } |
| 73 | |
| 74 | var response struct { |
| 75 | Server Server `mapstructure:"server"` |
| 76 | } |
| 77 | |
| 78 | err := mapstructure.Decode(r.Resp, &response) |
| 79 | return &response.Server, err |
| 80 | } |
| 81 | |
| 82 | // CreateResult temporarily contains the response from a Create call. |
| 83 | type CreateResult struct { |
| 84 | gophercloud.CommonResult |
| 85 | } |
| 86 | |
| 87 | // Extract interprets a CreateResult as a Server, if possible. |
| 88 | func (r CreateResult) Extract() (*Server, error) { |
| 89 | return extractServer(r.CommonResult) |
| 90 | } |
| 91 | |
| 92 | // GetResult temporarily contains the response from a Get call. |
| 93 | type GetResult struct { |
| 94 | gophercloud.CommonResult |
| 95 | } |
| 96 | |
| 97 | // Extract interprets a GetResult as a Server, if possible. |
| 98 | func (r GetResult) Extract() (*Server, error) { |
| 99 | return extractServer(r.CommonResult) |
| 100 | } |
| 101 | |
| 102 | // UpdateResult temporarily contains the response from an Update call. |
| 103 | type UpdateResult struct { |
| 104 | gophercloud.CommonResult |
| 105 | } |
| 106 | |
| 107 | // Extract interprets an UpdateResult as a Server, if possible. |
| 108 | func (r UpdateResult) Extract() (*Server, error) { |
| 109 | return extractServer(r.CommonResult) |
| 110 | } |
| 111 | |
| 112 | // RebuildResult temporarily contains the response from a Rebuild call. |
| 113 | type RebuildResult struct { |
| 114 | gophercloud.CommonResult |
| 115 | } |
| 116 | |
| 117 | // Extract interprets a RebuildResult as a Server, if possible. |
| 118 | func (r RebuildResult) Extract() (*Server, error) { |
| 119 | return extractServer(r.CommonResult) |
| 120 | } |
| 121 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 122 | // ExtractServers interprets the results of a single page from a List() call, producing a slice of Server entities. |
| 123 | func ExtractServers(page pagination.Page) ([]Server, error) { |
Ash Wilson | fd04379 | 2014-09-17 10:40:17 -0400 | [diff] [blame] | 124 | casted := page.(ListPage).Body |
Ash Wilson | 1225939 | 2014-09-17 10:50:02 -0400 | [diff] [blame] | 125 | |
| 126 | var response struct { |
| 127 | Servers []Server `mapstructure:"servers"` |
| 128 | } |
| 129 | err := mapstructure.Decode(casted, &response) |
| 130 | return response.Servers, err |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 131 | } |