blob: 28d66d0050bea6accd8f958fd728bee1eb0552ef [file] [log] [blame]
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -08001package servers
2
3import (
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -08004 "fmt"
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -08005 "github.com/mitchellh/mapstructure"
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -08006)
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.
10var 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.
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -070039//
40// 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.
41// Note that this is the ONLY time this field will be valid.
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080042type Server struct {
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -080043 Id string
44 TenantId string `mapstructure:tenant_id`
45 UserId string `mapstructure:user_id`
46 Name string
47 Updated string
48 Created string
49 HostId string
50 Status string
51 Progress int
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080052 AccessIPv4 string
53 AccessIPv6 string
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -080054 Image map[string]interface{}
55 Flavor map[string]interface{}
56 Addresses map[string]interface{}
57 Metadata map[string]interface{}
58 Links []interface{}
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -070059 AdminPass string `mapstructure:adminPass`
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080060}
61
62// GetServers interprets the result of a List() call, producing a slice of Server entities.
63func GetServers(lr ListResult) ([]Server, error) {
64 sa, ok := lr["servers"]
65 if !ok {
66 return nil, ErrNotImplemented
67 }
68 serversArray := sa.([]interface{})
69
70 servers := make([]Server, len(serversArray))
71 for i, so := range serversArray {
72 serverObj := so.(map[string]interface{})
73 err := mapstructure.Decode(serverObj, &servers[i])
74 if err != nil {
75 return servers, err
76 }
77 }
78
79 return servers, nil
80}
81
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080082// GetServer interprets the result of a call expected to return data on a single server.
83func GetServer(sr ServerResult) (*Server, error) {
84 so, ok := sr["server"]
85 if !ok {
86 return nil, ErrNotImplemented
87 }
88 serverObj := so.(map[string]interface{})
89 s := new(Server)
90 err := mapstructure.Decode(serverObj, s)
91 return s, err
92}