Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame^] | 1 | package servers |
| 2 | |
| 3 | import ( |
| 4 | "github.com/mitchellh/mapstructure" |
| 5 | "fmt" |
| 6 | ) |
| 7 | |
| 8 | // ErrNotImplemented indicates a failure to discover a feature of the response from the API. |
| 9 | // E.g., a missing server field, a missing extension, etc. |
| 10 | var ErrNotImplemented = fmt.Errorf("Compute Servers feature not implemented.") |
| 11 | |
| 12 | // Server exposes only the standard OpenStack fields corresponding to a given server on the user's account. |
| 13 | // |
| 14 | // Id uniquely identifies this server amongst all other servers, including those not accessible to the current tenant. |
| 15 | // |
| 16 | // TenantId identifies the tenant owning this server resource. |
| 17 | // |
| 18 | // UserId uniquely identifies the user account owning the tenant. |
| 19 | // |
| 20 | // Name contains the human-readable name for the server. |
| 21 | // |
| 22 | // Updated and Created contain ISO-8601 timestamps of when the state of the server last changed, and when it was created. |
| 23 | // |
| 24 | // Status contains the current operational status of the server, such as IN_PROGRESS or ACTIVE. |
| 25 | // |
| 26 | // Progress ranges from 0..100. A request made against the server completes only once Progress reaches 100. |
| 27 | // |
| 28 | // AccessIPv4 and AccessIPv6 contain the IP addresses of the server, suitable for remote access for administration. |
| 29 | // |
| 30 | // Image refers to a JSON object, which itself indicates the OS image used to deploy the server. |
| 31 | // |
| 32 | // Flavor refers to a JSON object, which itself indicates the hardware configuration of the deployed server. |
| 33 | // |
| 34 | // Addresses includes a list of all IP addresses assigned to the server, keyed by pool. |
| 35 | // |
| 36 | // Metadata includes a list of all user-specified key-value pairs attached to the server. |
| 37 | // |
| 38 | // Links includes HTTP references to the itself, useful for passing along to other APIs that might want a server reference. |
| 39 | type Server struct { |
| 40 | Id string |
| 41 | TenantId string `mapstructure:tenant_id` |
| 42 | UserId string `mapstructure:user_id` |
| 43 | Name string |
| 44 | Updated string |
| 45 | Created string |
| 46 | HostId string |
| 47 | Status string |
| 48 | Progress int |
| 49 | AccessIPv4 string |
| 50 | AccessIPv6 string |
| 51 | Image map[string]interface{} |
| 52 | Flavor map[string]interface{} |
| 53 | Addresses map[string]interface{} |
| 54 | Metadata map[string]interface{} |
| 55 | Links []interface{} |
| 56 | } |
| 57 | |
| 58 | // GetServers interprets the result of a List() call, producing a slice of Server entities. |
| 59 | func GetServers(lr ListResult) ([]Server, error) { |
| 60 | sa, ok := lr["servers"] |
| 61 | if !ok { |
| 62 | return nil, ErrNotImplemented |
| 63 | } |
| 64 | serversArray := sa.([]interface{}) |
| 65 | |
| 66 | servers := make([]Server, len(serversArray)) |
| 67 | for i, so := range serversArray { |
| 68 | serverObj := so.(map[string]interface{}) |
| 69 | err := mapstructure.Decode(serverObj, &servers[i]) |
| 70 | if err != nil { |
| 71 | return servers, err |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return servers, nil |
| 76 | } |
| 77 | |