| Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 1 | package servers | 
 | 2 |  | 
 | 3 | import ( | 
| Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 4 | 	"github.com/mitchellh/mapstructure" | 
| Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 5 | 	"github.com/rackspace/gophercloud" | 
| Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 6 | 	"github.com/rackspace/gophercloud/pagination" | 
| Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 7 | ) | 
 | 8 |  | 
| Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 9 | type serverResult struct { | 
| Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 10 | 	gophercloud.Result | 
| Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 11 | } | 
 | 12 |  | 
 | 13 | // Extract interprets any serverResult as a Server, if possible. | 
 | 14 | func (r serverResult) Extract() (*Server, error) { | 
 | 15 | 	if r.Err != nil { | 
 | 16 | 		return nil, r.Err | 
 | 17 | 	} | 
 | 18 |  | 
 | 19 | 	var response struct { | 
 | 20 | 		Server Server `mapstructure:"server"` | 
 | 21 | 	} | 
 | 22 |  | 
| Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 23 | 	err := mapstructure.Decode(r.Body, &response) | 
| Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 24 | 	return &response.Server, err | 
 | 25 | } | 
 | 26 |  | 
 | 27 | // CreateResult temporarily contains the response from a Create call. | 
 | 28 | type CreateResult struct { | 
 | 29 | 	serverResult | 
 | 30 | } | 
 | 31 |  | 
 | 32 | // GetResult temporarily contains the response from a Get call. | 
 | 33 | type GetResult struct { | 
 | 34 | 	serverResult | 
 | 35 | } | 
 | 36 |  | 
 | 37 | // UpdateResult temporarily contains the response from an Update call. | 
 | 38 | type UpdateResult struct { | 
 | 39 | 	serverResult | 
 | 40 | } | 
 | 41 |  | 
| Jamie Hannaford | 34732fe | 2014-10-27 11:29:36 +0100 | [diff] [blame] | 42 | // DeleteResult temporarily contains the response from an Delete call. | 
 | 43 | type DeleteResult struct { | 
| Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 44 | 	gophercloud.ErrResult | 
| Jamie Hannaford | 34732fe | 2014-10-27 11:29:36 +0100 | [diff] [blame] | 45 | } | 
 | 46 |  | 
| Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 47 | // RebuildResult temporarily contains the response from a Rebuild call. | 
| Jamie Hannaford | 01c1efe | 2014-10-16 15:08:59 +0200 | [diff] [blame] | 48 | type RebuildResult struct { | 
| Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 49 | 	serverResult | 
 | 50 | } | 
| Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 51 |  | 
| Jamie Hannaford | 01c1efe | 2014-10-16 15:08:59 +0200 | [diff] [blame] | 52 | // ActionResult represents the result of server action operations, like reboot | 
 | 53 | type ActionResult struct { | 
| Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 54 | 	gophercloud.ErrResult | 
| Jamie Hannaford | 01c1efe | 2014-10-16 15:08:59 +0200 | [diff] [blame] | 55 | } | 
 | 56 |  | 
| Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 57 | // 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] | 58 | type Server struct { | 
| Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 59 | 	// ID uniquely identifies this server amongst all other servers, including those not accessible to the current tenant. | 
 | 60 | 	ID string | 
 | 61 |  | 
 | 62 | 	// TenantID identifies the tenant owning this server resource. | 
 | 63 | 	TenantID string `mapstructure:"tenant_id"` | 
 | 64 |  | 
 | 65 | 	// UserID uniquely identifies the user account owning the tenant. | 
 | 66 | 	UserID string `mapstructure:"user_id"` | 
 | 67 |  | 
 | 68 | 	// Name contains the human-readable name for the server. | 
 | 69 | 	Name string | 
 | 70 |  | 
 | 71 | 	// Updated and Created contain ISO-8601 timestamps of when the state of the server last changed, and when it was created. | 
 | 72 | 	Updated string | 
 | 73 | 	Created string | 
 | 74 |  | 
 | 75 | 	HostID string | 
 | 76 |  | 
 | 77 | 	// Status contains the current operational status of the server, such as IN_PROGRESS or ACTIVE. | 
 | 78 | 	Status string | 
 | 79 |  | 
 | 80 | 	// Progress ranges from 0..100. | 
 | 81 | 	// A request made against the server completes only once Progress reaches 100. | 
 | 82 | 	Progress int | 
 | 83 |  | 
 | 84 | 	// AccessIPv4 and AccessIPv6 contain the IP addresses of the server, suitable for remote access for administration. | 
| Jamie Hannaford | 908e1e9 | 2014-10-27 14:41:17 +0100 | [diff] [blame] | 85 | 	AccessIPv4, AccessIPv6 string | 
| Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 86 |  | 
 | 87 | 	// Image refers to a JSON object, which itself indicates the OS image used to deploy the server. | 
 | 88 | 	Image map[string]interface{} | 
 | 89 |  | 
 | 90 | 	// Flavor refers to a JSON object, which itself indicates the hardware configuration of the deployed server. | 
 | 91 | 	Flavor map[string]interface{} | 
 | 92 |  | 
 | 93 | 	// 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] | 94 | 	Addresses map[string]interface{} | 
 | 95 |  | 
 | 96 | 	// Metadata includes a list of all user-specified key-value pairs attached to the server. | 
 | 97 | 	Metadata map[string]interface{} | 
 | 98 |  | 
 | 99 | 	// Links includes HTTP references to the itself, useful for passing along to other APIs that might want a server reference. | 
 | 100 | 	Links []interface{} | 
 | 101 |  | 
| Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 102 | 	// KeyName indicates which public key was injected into the server on launch. | 
| Ash Wilson | 69b144f | 2014-10-21 14:03:52 -0400 | [diff] [blame] | 103 | 	KeyName string `json:"key_name" mapstructure:"key_name"` | 
| Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 104 |  | 
| Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 105 | 	// 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. | 
 | 106 | 	// Note that this is the ONLY time this field will be valid. | 
| Ash Wilson | 69b144f | 2014-10-21 14:03:52 -0400 | [diff] [blame] | 107 | 	AdminPass string `json:"adminPass" mapstructure:"adminPass"` | 
| Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 108 | } | 
 | 109 |  | 
| Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 110 | // ServerPage abstracts the raw results of making a List() request against the API. | 
 | 111 | // As OpenStack extensions may freely alter the response bodies of structures returned to the client, you may only safely access the | 
 | 112 | // data provided through the ExtractServers call. | 
 | 113 | type ServerPage struct { | 
 | 114 | 	pagination.LinkedPageBase | 
| Ash Wilson | f57381e | 2014-09-25 13:21:34 -0400 | [diff] [blame] | 115 | } | 
 | 116 |  | 
| Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 117 | // IsEmpty returns true if a page contains no Server results. | 
 | 118 | func (page ServerPage) IsEmpty() (bool, error) { | 
 | 119 | 	servers, err := ExtractServers(page) | 
 | 120 | 	if err != nil { | 
 | 121 | 		return true, err | 
 | 122 | 	} | 
 | 123 | 	return len(servers) == 0, nil | 
 | 124 | } | 
 | 125 |  | 
 | 126 | // NextPageURL uses the response's embedded link reference to navigate to the next page of results. | 
 | 127 | func (page ServerPage) NextPageURL() (string, error) { | 
| Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 128 | 	type resp struct { | 
| Jamie Hannaford | a581acd | 2014-10-08 17:14:13 +0200 | [diff] [blame] | 129 | 		Links []gophercloud.Link `mapstructure:"servers_links"` | 
| Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 130 | 	} | 
 | 131 |  | 
| Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 132 | 	var r resp | 
 | 133 | 	err := mapstructure.Decode(page.Body, &r) | 
 | 134 | 	if err != nil { | 
 | 135 | 		return "", err | 
| Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 136 | 	} | 
 | 137 |  | 
| Jamie Hannaford | a581acd | 2014-10-08 17:14:13 +0200 | [diff] [blame] | 138 | 	return gophercloud.ExtractNextURL(r.Links) | 
| Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 139 | } | 
 | 140 |  | 
| Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 141 | // ExtractServers interprets the results of a single page from a List() call, producing a slice of Server entities. | 
 | 142 | func ExtractServers(page pagination.Page) ([]Server, error) { | 
| Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 143 | 	casted := page.(ServerPage).Body | 
| Ash Wilson | 1225939 | 2014-09-17 10:50:02 -0400 | [diff] [blame] | 144 |  | 
 | 145 | 	var response struct { | 
 | 146 | 		Servers []Server `mapstructure:"servers"` | 
 | 147 | 	} | 
 | 148 | 	err := mapstructure.Decode(casted, &response) | 
 | 149 | 	return response.Servers, err | 
| Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 150 | } |