blob: 2a9c87d91c0db76a00b0b7e03352ba81b90194dc [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 (
Samuel A. Falvo II5c305e12013-07-25 19:19:43 -07007 "fmt"
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -07008 "github.com/racker/perigee"
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 II8512e9a2013-07-26 22:53:29 -0700134// See the CloudServersProvider interface for details.
135func (gsp *genericServersProvider) ResizeServer(id, newName, newFlavor, newDiskConfig string) error {
136 err := gsp.context.WithReauth(gsp.access, func() error {
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700137 url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
138 rr := ResizeRequest{
139 Name: newName,
140 FlavorRef: newFlavor,
141 DiskConfig: newDiskConfig,
142 }
143 return perigee.Post(url, perigee.Options{
144 ReqBody: &struct {
145 Resize ResizeRequest `json:"resize"`
146 }{rr},
147 OkCodes: []int{202},
148 MoreHeaders: map[string]string{
149 "X-Auth-Token": gsp.access.AuthToken(),
150 },
151 })
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -0700152 })
153 return err
154}
155
156// See the CloudServersProvider interface for details.
157func (gsp *genericServersProvider) RevertResize(id string) error {
158 err := gsp.context.WithReauth(gsp.access, func() error {
159 url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
160 return perigee.Post(url, perigee.Options{
161 ReqBody: &struct {
162 RevertResize *int `json:"revertResize"`
163 }{nil},
164 OkCodes: []int{202},
165 MoreHeaders: map[string]string{
166 "X-Auth-Token": gsp.access.AuthToken(),
167 },
168 })
169 })
170 return err
171}
172
173// See the CloudServersProvider interface for details.
174func (gsp *genericServersProvider) ConfirmResize(id string) error {
175 err := gsp.context.WithReauth(gsp.access, func() error {
176 url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
177 return perigee.Post(url, perigee.Options{
178 ReqBody: &struct {
179 ConfirmResize *int `json:"confirmResize"`
180 }{nil},
181 OkCodes: []int{204},
182 MoreHeaders: map[string]string{
183 "X-Auth-Token": gsp.access.AuthToken(),
184 },
185 })
186 })
187 return err
188}
189
Samuel A. Falvo IIadbecf92013-07-30 13:13:59 -0700190// See the CloudServersProvider interface for details
191func (gsp *genericServersProvider) RebootServer(id string, hard bool) error {
192 return gsp.context.WithReauth(gsp.access, func() error {
193 url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
194 types := map[bool]string{false: "SOFT", true: "HARD"}
195 return perigee.Post(url, perigee.Options{
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700196 ReqBody: &struct {
Samuel A. Falvo IIadbecf92013-07-30 13:13:59 -0700197 Reboot struct {
198 Type string `json:"type"`
199 } `json:"reboot"`
200 }{
201 struct {
202 Type string `json:"type"`
203 }{types[hard]},
204 },
205 OkCodes: []int{202},
206 MoreHeaders: map[string]string{
207 "X-Auth-Token": gsp.access.AuthToken(),
208 },
209 })
210 })
211}
212
Samuel A. Falvo II15da6ab2013-07-30 14:02:11 -0700213// See the CloudServersProvider interface for details
214func (gsp *genericServersProvider) RescueServer(id string) (string, error) {
215 var pw *string
216
217 err := gsp.context.WithReauth(gsp.access, func() error {
218 url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
219 return perigee.Post(url, perigee.Options{
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700220 ReqBody: &struct {
Samuel A. Falvo II15da6ab2013-07-30 14:02:11 -0700221 Rescue string `json:"rescue"`
222 }{"none"},
223 MoreHeaders: map[string]string{
224 "X-Auth-Token": gsp.access.AuthToken(),
225 },
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700226 Results: &struct {
Samuel A. Falvo II15da6ab2013-07-30 14:02:11 -0700227 AdminPass **string `json:"adminPass"`
228 }{&pw},
229 })
230 })
231 return *pw, err
232}
233
234// See the CloudServersProvider interface for details
235func (gsp *genericServersProvider) UnrescueServer(id string) error {
236 return gsp.context.WithReauth(gsp.access, func() error {
237 url := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
238 return perigee.Post(url, perigee.Options{
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700239 ReqBody: &struct {
Samuel A. Falvo II15da6ab2013-07-30 14:02:11 -0700240 Unrescue *int `json:"unrescue"`
241 }{nil},
242 MoreHeaders: map[string]string{
243 "X-Auth-Token": gsp.access.AuthToken(),
244 },
245 OkCodes: []int{202},
246 })
247 })
248}
249
Samuel A. Falvo II72ac2dd2013-07-31 13:45:05 -0700250// See the CloudServersProvider interface for details
251func (gsp *genericServersProvider) UpdateServer(id string, changes NewServerSettings) (*Server, error) {
252 var svr *Server
253 err := gsp.context.WithReauth(gsp.access, func() error {
254 url := fmt.Sprintf("%s/servers/%s", gsp.endpoint, id)
255 return perigee.Put(url, perigee.Options{
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700256 ReqBody: &struct {
Samuel A. Falvo II72ac2dd2013-07-31 13:45:05 -0700257 Server NewServerSettings `json:"server"`
258 }{changes},
259 MoreHeaders: map[string]string{
260 "X-Auth-Token": gsp.access.AuthToken(),
261 },
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700262 Results: &struct {
Samuel A. Falvo II72ac2dd2013-07-31 13:45:05 -0700263 Server **Server `json:"server"`
264 }{&svr},
265 })
266 })
267 return svr, err
268}
269
Samuel A. Falvo II414c15c2013-08-01 15:16:46 -0700270// See the CloudServersProvider interface for details.
Samuel A. Falvo IIf3391602013-08-14 14:53:32 -0700271func (gsp *genericServersProvider) RebuildServer(id string, ns NewServer) (*Server, error) {
Samuel A. Falvo II414c15c2013-08-01 15:16:46 -0700272 var s *Server
273
274 err := gsp.context.WithReauth(gsp.access, func() error {
275 ep := fmt.Sprintf("%s/servers/%s/action", gsp.endpoint, id)
276 return perigee.Post(ep, perigee.Options{
277 ReqBody: &struct {
278 Rebuild *NewServer `json:"rebuild"`
279 }{&ns},
280 Results: &struct{ Server **Server }{&s},
281 MoreHeaders: map[string]string{
282 "X-Auth-Token": gsp.access.AuthToken(),
283 },
284 OkCodes: []int{202},
285 })
286 })
287
288 return s, err
289}
290
Samuel A. Falvo IIe21808f2013-08-14 14:48:09 -0700291// See the CloudServersProvider interface for details.
292func (gsp *genericServersProvider) ListAddresses(id string) (AddressSet, error) {
293 var pas *AddressSet
294 var statusCode int
295
296 err := gsp.context.WithReauth(gsp.access, func() error {
297 ep := fmt.Sprintf("%s/servers/%s/ips", gsp.endpoint, id)
298 return perigee.Get(ep, perigee.Options{
299 Results: &struct{ Addresses **AddressSet }{&pas},
300 MoreHeaders: map[string]string{
301 "X-Auth-Token": gsp.access.AuthToken(),
302 },
Samuel A. Falvo IIf3391602013-08-14 14:53:32 -0700303 OkCodes: []int{200, 203},
Samuel A. Falvo IIe21808f2013-08-14 14:48:09 -0700304 StatusCode: &statusCode,
305 })
306 })
307
308 if err != nil {
309 if statusCode == 203 {
310 err = WarnUnauthoritative
311 }
312 }
313
314 return *pas, err
315}
316
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -0700317// RaxBandwidth provides measurement of server bandwidth consumed over a given audit interval.
318type RaxBandwidth struct {
319 AuditPeriodEnd string `json:"audit_period_end"`
320 AuditPeriodStart string `json:"audit_period_start"`
321 BandwidthInbound int64 `json:"bandwidth_inbound"`
322 BandwidthOutbound int64 `json:"bandwidth_outbound"`
323 Interface string `json:"interface"`
324}
325
326// A VersionedAddress denotes either an IPv4 or IPv6 (depending on version indicated)
327// address.
328type VersionedAddress struct {
329 Addr string `json:"addr"`
330 Version int `json:"version"`
331}
332
333// An AddressSet provides a set of public and private IP addresses for a resource.
334// Each address has a version to identify if IPv4 or IPv6.
335type AddressSet struct {
336 Public []VersionedAddress `json:"public"`
337 Private []VersionedAddress `json:"private"`
338}
339
340// Server records represent (virtual) hardware instances (not configurations) accessible by the user.
341//
342// The AccessIPv4 / AccessIPv6 fields provides IP addresses for the server in the IPv4 or IPv6 format, respectively.
343//
344// Addresses provides addresses for any attached isolated networks.
345// The version field indicates whether the IP address is version 4 or 6.
346//
347// Created tells when the server entity was created.
348//
349// The Flavor field includes the flavor ID and flavor links.
350//
351// The compute provisioning algorithm has an anti-affinity property that
352// attempts to spread customer VMs across hosts.
353// Under certain situations,
354// VMs from the same customer might be placed on the same host.
355// The HostId field represents the host your server runs on and
356// can be used to determine this scenario if it is relevant to your application.
357// Note that HostId is unique only per account; it is not globally unique.
Mark Peeka2818af2013-08-24 15:01:12 -0700358//
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -0700359// Id provides the server's unique identifier.
360// This field must be treated opaquely.
361//
362// Image indicates which image is installed on the server.
363//
364// Links provides one or more means of accessing the server.
365//
366// Metadata provides a small key-value store for application-specific information.
367//
368// Name provides a human-readable name for the server.
369//
370// Progress indicates how far along it is towards being provisioned.
371// 100 represents complete, while 0 represents just beginning.
372//
373// Status provides an indication of what the server's doing at the moment.
374// A server will be in ACTIVE state if it's ready for use.
375//
376// OsDcfDiskConfig indicates the server's boot volume configuration.
377// Valid values are:
378// AUTO
379// ----
380// The server is built with a single partition the size of the target flavor disk.
381// The file system is automatically adjusted to fit the entire partition.
382// This keeps things simple and automated.
383// AUTO is valid only for images and servers with a single partition that use the EXT3 file system.
384// This is the default setting for applicable Rackspace base images.
385//
386// MANUAL
387// ------
388// The server is built using whatever partition scheme and file system is in the source image.
389// If the target flavor disk is larger,
390// the remaining disk space is left unpartitioned.
391// This enables images to have non-EXT3 file systems, multiple partitions, and so on,
392// and enables you to manage the disk configuration.
393//
394// RaxBandwidth provides measures of the server's inbound and outbound bandwidth per interface.
395//
396// OsExtStsPowerState provides an indication of the server's power.
397// This field appears to be a set of flag bits:
398//
399// ... 4 3 2 1 0
400// +--//--+---+---+---+---+
401// | .... | 0 | S | 0 | I |
402// +--//--+---+---+---+---+
403// | |
404// | +--- 0=Instance is down.
405// | 1=Instance is up.
406// |
407// +----------- 0=Server is switched ON.
408// 1=Server is switched OFF.
409// (note reverse logic.)
410//
411// Unused bits should be ignored when read, and written as 0 for future compatibility.
412//
413// OsExtStsTaskState and OsExtStsVmState work together
414// to provide visibility in the provisioning process for the instance.
415// Consult Rackspace documentation at
416// http://docs.rackspace.com/servers/api/v2/cs-devguide/content/ch_extensions.html#ext_status
417// for more details. It's too lengthy to include here.
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -0700418type Server struct {
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -0700419 AccessIPv4 string `json:"accessIPv4"`
420 AccessIPv6 string `json:"accessIPv6"`
421 Addresses AddressSet `json:"addresses"`
422 Created string `json:"created"`
423 Flavor FlavorLink `json:"flavor"`
424 HostId string `json:"hostId"`
425 Id string `json:"id"`
426 Image ImageLink `json:"image"`
427 Links []Link `json:"links"`
428 Metadata interface{} `json:"metadata"`
429 Name string `json:"name"`
430 Progress int `json:"progress"`
431 Status string `json:"status"`
432 TenantId string `json:"tenant_id"`
433 Updated string `json:"updated"`
434 UserId string `json:"user_id"`
435 OsDcfDiskConfig string `json:"OS-DCF:diskConfig"`
436 RaxBandwidth []RaxBandwidth `json:"rax-bandwidth:bandwidth"`
437 OsExtStsPowerState int `json:"OS-EXT-STS:power_state"`
438 OsExtStsTaskState string `json:"OS-EXT-STS:task_state"`
439 OsExtStsVmState string `json:"OS-EXT-STS:vm_state"`
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -0700440}
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700441
Samuel A. Falvo II72ac2dd2013-07-31 13:45:05 -0700442// NewServerSettings structures record those fields of the Server structure to change
443// when updating a server (see UpdateServer method).
444type NewServerSettings struct {
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700445 Name string `json:"name,omitempty"`
Samuel A. Falvo II72ac2dd2013-07-31 13:45:05 -0700446 AccessIPv4 string `json:"accessIPv4,omitempty"`
447 AccessIPv6 string `json:"accessIPv6,omitempty"`
448}
449
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700450// NewServer structures are used for both requests and responses.
451// The fields discussed below are relevent for server-creation purposes.
452//
453// The Name field contains the desired name of the server.
454// Note that (at present) Rackspace permits more than one server with the same name;
455// however, software should not depend on this.
456// Not only will Rackspace support thank you, so will your own devops engineers.
457// A name is required.
458//
459// The ImageRef field contains the ID of the desired software image to place on the server.
460// This ID must be found in the image slice returned by the Images() function.
461// This field is required.
462//
463// The FlavorRef field contains the ID of the server configuration desired for deployment.
464// This ID must be found in the flavor slice returned by the Flavors() function.
465// This field is required.
466//
467// For OsDcfDiskConfig, refer to the Image or Server structure documentation.
468// This field defaults to "AUTO" if not explicitly provided.
469//
470// Metadata contains a small key/value association of arbitrary data.
471// Neither Rackspace nor OpenStack places significance on this field in any way.
472// This field defaults to an empty map if not provided.
473//
474// Personality specifies the contents of certain files in the server's filesystem.
475// The files and their contents are mapped through a slice of FileConfig structures.
476// If not provided, all filesystem entities retain their image-specific configuration.
477//
478// Networks specifies an affinity for the server's various networks and interfaces.
479// Networks are identified through UUIDs; see NetworkConfig structure documentation for more details.
480// If not provided, network affinity is determined automatically.
481//
482// The AdminPass field may be used to provide a root- or administrator-password
483// during the server provisioning process.
484// If not provided, a random password will be automatically generated and returned in this field.
485//
486// The following fields are intended to be used to communicate certain results about the server being provisioned.
487// When attempting to create a new server, these fields MUST not be provided.
488// They'll be filled in by the response received from the Rackspace APIs.
489//
490// The Id field contains the server's unique identifier.
491// The identifier's scope is best assumed to be bound by the user's account, unless other arrangements have been made with Rackspace.
492//
493// Any Links provided are used to refer to the server specifically by URL.
494// These links are useful for making additional REST calls not explicitly supported by Gorax.
495type NewServer struct {
Mark Peeka739f222013-08-17 19:06:15 -0700496 Name string `json:"name,omitempty"`
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700497 ImageRef string `json:"imageRef,omitempty"`
498 FlavorRef string `json:"flavorRef,omitempty"`
499 Metadata interface{} `json:"metadata,omitempty"`
500 Personality []FileConfig `json:"personality,omitempty"`
501 Networks []NetworkConfig `json:"networks,omitempty"`
502 AdminPass string `json:"adminPass,omitempty"`
Mark Peeka2818af2013-08-24 15:01:12 -0700503 KeyPairName string `json:"key_name,omitempty"`
Samuel A. Falvo IIe91ff6d2013-07-11 15:46:10 -0700504 Id string `json:"id,omitempty"`
505 Links []Link `json:"links,omitempty"`
506 OsDcfDiskConfig string `json:"OS-DCF:diskConfig,omitempty"`
507}
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -0700508
509// ResizeRequest structures are used internally to encode to JSON the parameters required to resize a server instance.
510// Client applications will not use this structure (no API accepts an instance of this structure).
511// See the Region method ResizeServer() for more details on how to resize a server.
512type ResizeRequest struct {
Samuel A. Falvo II20f1aa42013-07-31 14:32:03 -0700513 Name string `json:"name,omitempty"`
514 FlavorRef string `json:"flavorRef"`
515 DiskConfig string `json:"OS-DCF:diskConfig,omitempty"`
Samuel A. Falvo II8512e9a2013-07-26 22:53:29 -0700516}