blob: ee38e9ff2510e10fecc169b7f593d9e7c5748e66 [file] [log] [blame]
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -07001// TODO(sfalvo): Remove Rackspace-specific Server structure fields and refactor them into a provider-specific access method.
2// Be sure to update godocs accordingly.
3
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -07004package gophercloud
5
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -07006import (
7 "github.com/racker/perigee"
Samuel A. Falvo II5c305e12013-07-25 19:19:43 -07008 "fmt"
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -07009)
10
Samuel A. Falvo II1dd740a2013-07-08 15:48:40 -070011// genericServersProvider structures provide the implementation for generic OpenStack-compatible
12// CloudServersProvider interfaces.
13type genericServersProvider struct {
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -070014 // endpoint refers to the provider's API endpoint base URL. This will be used to construct
15 // and issue queries.
16 endpoint string
17
18 // Test context (if any) in which to issue requests.
19 context *Context
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -070020
21 // access associates this API provider with a set of credentials,
22 // which may be automatically renewed if they near expiration.
23 access AccessProvider
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -070024}
25
Samuel A. Falvo II1dd740a2013-07-08 15:48:40 -070026// See the CloudServersProvider interface for details.
Samuel A. Falvo IIa0a55842013-07-24 13:14:17 -070027func (gcp *genericServersProvider) ListServersLinksOnly() ([]Server, error) {
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -070028 var ss []Server
29
Samuel A. Falvo II7bd1fba2013-07-16 17:30:43 -070030 err := gcp.context.WithReauth(gcp.access, func() error {
31 url := gcp.endpoint + "/servers"
32 return perigee.Get(url, perigee.Options{
33 CustomClient: gcp.context.httpClient,
34 Results: &struct{ Servers *[]Server }{&ss},
35 MoreHeaders: map[string]string{
36 "X-Auth-Token": gcp.access.AuthToken(),
37 },
38 })
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -070039 })
40 return ss, err
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -070041}
42
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -070043// See the CloudServersProvider interface for details.
Samuel A. Falvo IIa0a55842013-07-24 13:14:17 -070044func (gcp *genericServersProvider) ListServers() ([]Server, error) {
45 var ss []Server
46
47 err := gcp.context.WithReauth(gcp.access, func() error {
48 url := gcp.endpoint + "/servers/detail"
49 return perigee.Get(url, perigee.Options{
50 CustomClient: gcp.context.httpClient,
51 Results: &struct{ Servers *[]Server }{&ss},
52 MoreHeaders: map[string]string{
53 "X-Auth-Token": gcp.access.AuthToken(),
54 },
55 })
56 })
57 return ss, err
58}
59
60// See the CloudServersProvider interface for details.
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -070061func (gsp *genericServersProvider) ServerById(id string) (*Server, error) {
62 var s *Server
63
Samuel A. Falvo II7bd1fba2013-07-16 17:30:43 -070064 err := gsp.context.WithReauth(gsp.access, func() error {
65 url := gsp.endpoint + "/servers/" + id
66 return perigee.Get(url, perigee.Options{
67 Results: &struct{ Server **Server }{&s},
68 MoreHeaders: map[string]string{
69 "X-Auth-Token": gsp.access.AuthToken(),
70 },
71 })
Samuel A. Falvo II02f5e832013-07-10 13:52:27 -070072 })
73 return s, err
74}
75
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -070076// See the CloudServersProvider interface for details.
77func (gsp *genericServersProvider) CreateServer(ns NewServer) (*NewServer, error) {
78 var s *NewServer
79
Samuel A. Falvo II7bd1fba2013-07-16 17:30:43 -070080 err := gsp.context.WithReauth(gsp.access, func() error {
81 ep := gsp.endpoint + "/servers"
82 return perigee.Post(ep, perigee.Options{
83 ReqBody: &struct {
84 Server *NewServer `json:"server"`
85 }{&ns},
86 Results: &struct{ Server **NewServer }{&s},
87 MoreHeaders: map[string]string{
88 "X-Auth-Token": gsp.access.AuthToken(),
89 },
90 OkCodes: []int{202},
91 })
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -070092 })
Samuel A. Falvo II7bd1fba2013-07-16 17:30:43 -070093
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -070094 return s, err
95}
96
Samuel A. Falvo II286e4de2013-07-12 11:33:31 -070097// See the CloudServersProvider interface for details.
98func (gsp *genericServersProvider) DeleteServerById(id string) error {
Samuel A. Falvo II7bd1fba2013-07-16 17:30:43 -070099 err := gsp.context.WithReauth(gsp.access, func() error {
100 url := gsp.endpoint + "/servers/" + id
101 return perigee.Delete(url, perigee.Options{
102 MoreHeaders: map[string]string{
103 "X-Auth-Token": gsp.access.AuthToken(),
104 },
105 OkCodes: []int{204},
106 })
Samuel A. Falvo II286e4de2013-07-12 11:33:31 -0700107 })
108 return err
109}
110
Samuel A. Falvo II5c305e12013-07-25 19:19:43 -0700111// See the CloudServersProvider interface for details.
112func (gsp *genericServersProvider) SetAdminPassword(id, pw string) error {
113 err := gsp.context.WithReauth(gsp.access, func() error {
114 url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
115 return perigee.Post(url, perigee.Options{
116 ReqBody: &struct {
117 ChangePassword struct {
118 AdminPass string `json:"adminPass"`
119 } `json:"changePassword"`
120 }{
121 struct {
122 AdminPass string `json:"adminPass"`
123 }{pw},
124 },
125 OkCodes: []int{202},
126 MoreHeaders: map[string]string{
127 "X-Auth-Token": gsp.access.AuthToken(),
128 },
129 })
130 })
131 return err
132}
133
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -0700134// RaxBandwidth provides measurement of server bandwidth consumed over a given audit interval.
135type RaxBandwidth struct {
136 AuditPeriodEnd string `json:"audit_period_end"`
137 AuditPeriodStart string `json:"audit_period_start"`
138 BandwidthInbound int64 `json:"bandwidth_inbound"`
139 BandwidthOutbound int64 `json:"bandwidth_outbound"`
140 Interface string `json:"interface"`
141}
142
143// A VersionedAddress denotes either an IPv4 or IPv6 (depending on version indicated)
144// address.
145type VersionedAddress struct {
146 Addr string `json:"addr"`
147 Version int `json:"version"`
148}
149
150// An AddressSet provides a set of public and private IP addresses for a resource.
151// Each address has a version to identify if IPv4 or IPv6.
152type AddressSet struct {
153 Public []VersionedAddress `json:"public"`
154 Private []VersionedAddress `json:"private"`
155}
156
157// Server records represent (virtual) hardware instances (not configurations) accessible by the user.
158//
159// The AccessIPv4 / AccessIPv6 fields provides IP addresses for the server in the IPv4 or IPv6 format, respectively.
160//
161// Addresses provides addresses for any attached isolated networks.
162// The version field indicates whether the IP address is version 4 or 6.
163//
164// Created tells when the server entity was created.
165//
166// The Flavor field includes the flavor ID and flavor links.
167//
168// The compute provisioning algorithm has an anti-affinity property that
169// attempts to spread customer VMs across hosts.
170// Under certain situations,
171// VMs from the same customer might be placed on the same host.
172// The HostId field represents the host your server runs on and
173// can be used to determine this scenario if it is relevant to your application.
174// Note that HostId is unique only per account; it is not globally unique.
175//
176// Id provides the server's unique identifier.
177// This field must be treated opaquely.
178//
179// Image indicates which image is installed on the server.
180//
181// Links provides one or more means of accessing the server.
182//
183// Metadata provides a small key-value store for application-specific information.
184//
185// Name provides a human-readable name for the server.
186//
187// Progress indicates how far along it is towards being provisioned.
188// 100 represents complete, while 0 represents just beginning.
189//
190// Status provides an indication of what the server's doing at the moment.
191// A server will be in ACTIVE state if it's ready for use.
192//
193// OsDcfDiskConfig indicates the server's boot volume configuration.
194// Valid values are:
195// AUTO
196// ----
197// The server is built with a single partition the size of the target flavor disk.
198// The file system is automatically adjusted to fit the entire partition.
199// This keeps things simple and automated.
200// AUTO is valid only for images and servers with a single partition that use the EXT3 file system.
201// This is the default setting for applicable Rackspace base images.
202//
203// MANUAL
204// ------
205// The server is built using whatever partition scheme and file system is in the source image.
206// If the target flavor disk is larger,
207// the remaining disk space is left unpartitioned.
208// This enables images to have non-EXT3 file systems, multiple partitions, and so on,
209// and enables you to manage the disk configuration.
210//
211// RaxBandwidth provides measures of the server's inbound and outbound bandwidth per interface.
212//
213// OsExtStsPowerState provides an indication of the server's power.
214// This field appears to be a set of flag bits:
215//
216// ... 4 3 2 1 0
217// +--//--+---+---+---+---+
218// | .... | 0 | S | 0 | I |
219// +--//--+---+---+---+---+
220// | |
221// | +--- 0=Instance is down.
222// | 1=Instance is up.
223// |
224// +----------- 0=Server is switched ON.
225// 1=Server is switched OFF.
226// (note reverse logic.)
227//
228// Unused bits should be ignored when read, and written as 0 for future compatibility.
229//
230// OsExtStsTaskState and OsExtStsVmState work together
231// to provide visibility in the provisioning process for the instance.
232// Consult Rackspace documentation at
233// http://docs.rackspace.com/servers/api/v2/cs-devguide/content/ch_extensions.html#ext_status
234// for more details. It's too lengthy to include here.
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -0700235type Server struct {
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -0700236 AccessIPv4 string `json:"accessIPv4"`
237 AccessIPv6 string `json:"accessIPv6"`
238 Addresses AddressSet `json:"addresses"`
239 Created string `json:"created"`
240 Flavor FlavorLink `json:"flavor"`
241 HostId string `json:"hostId"`
242 Id string `json:"id"`
243 Image ImageLink `json:"image"`
244 Links []Link `json:"links"`
245 Metadata interface{} `json:"metadata"`
246 Name string `json:"name"`
247 Progress int `json:"progress"`
248 Status string `json:"status"`
249 TenantId string `json:"tenant_id"`
250 Updated string `json:"updated"`
251 UserId string `json:"user_id"`
252 OsDcfDiskConfig string `json:"OS-DCF:diskConfig"`
253 RaxBandwidth []RaxBandwidth `json:"rax-bandwidth:bandwidth"`
254 OsExtStsPowerState int `json:"OS-EXT-STS:power_state"`
255 OsExtStsTaskState string `json:"OS-EXT-STS:task_state"`
256 OsExtStsVmState string `json:"OS-EXT-STS:vm_state"`
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -0700257}
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700258
259// NewServer structures are used for both requests and responses.
260// The fields discussed below are relevent for server-creation purposes.
261//
262// The Name field contains the desired name of the server.
263// Note that (at present) Rackspace permits more than one server with the same name;
264// however, software should not depend on this.
265// Not only will Rackspace support thank you, so will your own devops engineers.
266// A name is required.
267//
268// The ImageRef field contains the ID of the desired software image to place on the server.
269// This ID must be found in the image slice returned by the Images() function.
270// This field is required.
271//
272// The FlavorRef field contains the ID of the server configuration desired for deployment.
273// This ID must be found in the flavor slice returned by the Flavors() function.
274// This field is required.
275//
276// For OsDcfDiskConfig, refer to the Image or Server structure documentation.
277// This field defaults to "AUTO" if not explicitly provided.
278//
279// Metadata contains a small key/value association of arbitrary data.
280// Neither Rackspace nor OpenStack places significance on this field in any way.
281// This field defaults to an empty map if not provided.
282//
283// Personality specifies the contents of certain files in the server's filesystem.
284// The files and their contents are mapped through a slice of FileConfig structures.
285// If not provided, all filesystem entities retain their image-specific configuration.
286//
287// Networks specifies an affinity for the server's various networks and interfaces.
288// Networks are identified through UUIDs; see NetworkConfig structure documentation for more details.
289// If not provided, network affinity is determined automatically.
290//
291// The AdminPass field may be used to provide a root- or administrator-password
292// during the server provisioning process.
293// If not provided, a random password will be automatically generated and returned in this field.
294//
295// The following fields are intended to be used to communicate certain results about the server being provisioned.
296// When attempting to create a new server, these fields MUST not be provided.
297// They'll be filled in by the response received from the Rackspace APIs.
298//
299// The Id field contains the server's unique identifier.
300// The identifier's scope is best assumed to be bound by the user's account, unless other arrangements have been made with Rackspace.
301//
302// Any Links provided are used to refer to the server specifically by URL.
303// These links are useful for making additional REST calls not explicitly supported by Gorax.
304type NewServer struct {
305 Name string `json:"name",omitempty`
306 ImageRef string `json:"imageRef,omitempty"`
307 FlavorRef string `json:"flavorRef,omitempty"`
308 Metadata interface{} `json:"metadata,omitempty"`
309 Personality []FileConfig `json:"personality,omitempty"`
310 Networks []NetworkConfig `json:"networks,omitempty"`
311 AdminPass string `json:"adminPass,omitempty"`
312 Id string `json:"id,omitempty"`
313 Links []Link `json:"links,omitempty"`
314 OsDcfDiskConfig string `json:"OS-DCF:diskConfig,omitempty"`
315}