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 | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame^] | 7 | "github.com/rackspace/gophercloud/pagination" |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 8 | ) |
| 9 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame^] | 10 | // ErrCannotInterpret is returned by an Extract call if the response body doesn't have the expected structure. |
| 11 | var ErrCannotInterpet = errors.New("Unable to interpret a response body.") |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 12 | |
| 13 | // 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] | 14 | type Server struct { |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame^] | 15 | // ID uniquely identifies this server amongst all other servers, including those not accessible to the current tenant. |
| 16 | ID string |
| 17 | |
| 18 | // TenantID identifies the tenant owning this server resource. |
| 19 | TenantID string `mapstructure:"tenant_id"` |
| 20 | |
| 21 | // UserID uniquely identifies the user account owning the tenant. |
| 22 | UserID string `mapstructure:"user_id"` |
| 23 | |
| 24 | // Name contains the human-readable name for the server. |
| 25 | Name string |
| 26 | |
| 27 | // Updated and Created contain ISO-8601 timestamps of when the state of the server last changed, and when it was created. |
| 28 | Updated string |
| 29 | Created string |
| 30 | |
| 31 | HostID string |
| 32 | |
| 33 | // Status contains the current operational status of the server, such as IN_PROGRESS or ACTIVE. |
| 34 | Status string |
| 35 | |
| 36 | // Progress ranges from 0..100. |
| 37 | // A request made against the server completes only once Progress reaches 100. |
| 38 | Progress int |
| 39 | |
| 40 | // 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] | 41 | AccessIPv4 string |
| 42 | AccessIPv6 string |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame^] | 43 | |
| 44 | // Image refers to a JSON object, which itself indicates the OS image used to deploy the server. |
| 45 | Image map[string]interface{} |
| 46 | |
| 47 | // Flavor refers to a JSON object, which itself indicates the hardware configuration of the deployed server. |
| 48 | Flavor map[string]interface{} |
| 49 | |
| 50 | // Addresses includes a list of all IP addresses assigned to the server, keyed by pool. |
| 51 | |
| 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 | |
| 60 | // 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. |
| 61 | // Note that this is the ONLY time this field will be valid. |
| 62 | AdminPass string `mapstructure:"adminPass"` |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 63 | } |
| 64 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame^] | 65 | // ExtractServers interprets the results of a single page from a List() call, producing a slice of Server entities. |
| 66 | func ExtractServers(page pagination.Page) ([]Server, error) { |
| 67 | casted := page.(ListResult).Body |
| 68 | var servers []Server |
| 69 | err := mapstructure.Decode(servers, casted) |
| 70 | return servers, err |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame^] | 73 | // ExtractServer interprets the result of a call expected to return data on a single server. |
| 74 | func ExtractServer(sr ServerResult) (*Server, error) { |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 75 | so, ok := sr["server"] |
| 76 | if !ok { |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame^] | 77 | return nil, ErrCannotInterpet |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 78 | } |
| 79 | serverObj := so.(map[string]interface{}) |
| 80 | s := new(Server) |
| 81 | err := mapstructure.Decode(serverObj, s) |
| 82 | return s, err |
| 83 | } |