blob: ed35b33e72949782ebbc0b639c643ec8b678a031 [file] [log] [blame]
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -08001package servers
2
3import (
Ash Wilson6a310e02014-09-29 08:24:02 -04004 "encoding/base64"
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -07005 "fmt"
Ash Wilson01626a32014-09-17 10:38:07 -04006
Jon Perritt30558642014-04-14 17:07:12 -05007 "github.com/racker/perigee"
Ash Wilson01626a32014-09-17 10:38:07 -04008 "github.com/rackspace/gophercloud"
9 "github.com/rackspace/gophercloud/pagination"
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080010)
11
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020012// ListOptsBuilder allows extensions to add additional parameters to the
13// List request.
14type ListOptsBuilder interface {
15 ToServerListQuery() (string, error)
16}
17
18// ListOpts allows the filtering and sorting of paginated collections through
19// the API. Filtering is achieved by passing in struct field values that map to
20// the server attributes you want to see returned. Marker and Limit are used
21// for pagination.
22type ListOpts struct {
23 // A time/date stamp for when the server last changed status.
24 ChangesSince string `q:"changes-since"`
25
26 // Name of the image in URL format.
27 Image string `q:"image"`
28
29 // Name of the flavor in URL format.
30 Flavor string `q:"flavor"`
31
32 // Name of the server as a string; can be queried with regular expressions.
33 // Realize that ?name=bob returns both bob and bobb. If you need to match bob
34 // only, you can use a regular expression matching the syntax of the
35 // underlying database server implemented for Compute.
36 Name string `q:"name"`
37
38 // Value of the status of the server so that you can filter on "ACTIVE" for example.
39 Status string `q:"status"`
40
41 // Name of the host as a string.
42 Host string `q:"host"`
43
44 // UUID of the server at which you want to set a marker.
45 Marker string `q:"marker"`
46
47 // Integer value for the limit of values to return.
48 Limit int `q:"limit"`
49}
50
51// ToServerListQuery formats a ListOpts into a query string.
52func (opts ListOpts) ToServerListQuery() (string, error) {
53 q, err := gophercloud.BuildQueryString(opts)
54 if err != nil {
55 return "", err
56 }
57 return q.String(), nil
58}
59
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080060// List makes a request against the API to list servers accessible to you.
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020061func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
62 url := listDetailURL(client)
63
64 if opts != nil {
65 query, err := opts.ToServerListQuery()
66 if err != nil {
67 return pagination.Pager{Err: err}
68 }
69 url += query
70 }
71
Ash Wilsonb8b16f82014-10-20 10:19:49 -040072 createPageFn := func(r pagination.PageResult) pagination.Page {
73 return ServerPage{pagination.LinkedPageBase{PageResult: r}}
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080074 }
75
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020076 return pagination.NewPager(client, url, createPageFn)
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080077}
78
Ash Wilson2206a112014-10-02 10:57:38 -040079// CreateOptsBuilder describes struct types that can be accepted by the Create call.
Ash Wilson6a310e02014-09-29 08:24:02 -040080// The CreateOpts struct in this package does.
Ash Wilson2206a112014-10-02 10:57:38 -040081type CreateOptsBuilder interface {
Jon Perritt4149d7c2014-10-23 21:23:46 -050082 ToServerCreateMap() (map[string]interface{}, error)
Ash Wilson6a310e02014-09-29 08:24:02 -040083}
84
85// Network is used within CreateOpts to control a new server's network attachments.
86type Network struct {
87 // UUID of a nova-network to attach to the newly provisioned server.
88 // Required unless Port is provided.
89 UUID string
90
91 // Port of a neutron network to attach to the newly provisioned server.
92 // Required unless UUID is provided.
93 Port string
94
95 // FixedIP [optional] specifies a fixed IPv4 address to be used on this network.
96 FixedIP string
97}
98
99// CreateOpts specifies server creation parameters.
100type CreateOpts struct {
101 // Name [required] is the name to assign to the newly launched server.
102 Name string
103
104 // ImageRef [required] is the ID or full URL to the image that contains the server's OS and initial state.
105 // Optional if using the boot-from-volume extension.
106 ImageRef string
107
108 // FlavorRef [required] is the ID or full URL to the flavor that describes the server's specs.
109 FlavorRef string
110
111 // SecurityGroups [optional] lists the names of the security groups to which this server should belong.
112 SecurityGroups []string
113
114 // UserData [optional] contains configuration information or scripts to use upon launch.
115 // Create will base64-encode it for you.
116 UserData []byte
117
118 // AvailabilityZone [optional] in which to launch the server.
119 AvailabilityZone string
120
121 // Networks [optional] dictates how this server will be attached to available networks.
122 // By default, the server will be attached to all isolated networks for the tenant.
123 Networks []Network
124
125 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
126 Metadata map[string]string
127
128 // Personality [optional] includes the path and contents of a file to inject into the server at launch.
129 // The maximum size of the file is 255 bytes (decoded).
130 Personality []byte
131
132 // ConfigDrive [optional] enables metadata injection through a configuration drive.
133 ConfigDrive bool
Jon Perrittf3b2e142014-11-04 16:00:19 -0600134
135 // AdminPass [optional] sets the root user password. If not set, a randomly-generated
136 // password will be created and returned in the response.
137 AdminPass string
Ash Wilson6a310e02014-09-29 08:24:02 -0400138}
139
Ash Wilsone45c9732014-09-29 10:54:12 -0400140// ToServerCreateMap assembles a request body based on the contents of a CreateOpts.
Jon Perritt4149d7c2014-10-23 21:23:46 -0500141func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) {
Ash Wilson6a310e02014-09-29 08:24:02 -0400142 server := make(map[string]interface{})
143
144 server["name"] = opts.Name
145 server["imageRef"] = opts.ImageRef
146 server["flavorRef"] = opts.FlavorRef
147
148 if opts.UserData != nil {
149 encoded := base64.StdEncoding.EncodeToString(opts.UserData)
150 server["user_data"] = &encoded
151 }
152 if opts.Personality != nil {
153 encoded := base64.StdEncoding.EncodeToString(opts.Personality)
154 server["personality"] = &encoded
155 }
156 if opts.ConfigDrive {
157 server["config_drive"] = "true"
158 }
159 if opts.AvailabilityZone != "" {
160 server["availability_zone"] = opts.AvailabilityZone
161 }
162 if opts.Metadata != nil {
163 server["metadata"] = opts.Metadata
164 }
Jon Perrittf3b2e142014-11-04 16:00:19 -0600165 if opts.AdminPass != "" {
166 server["adminPass"] = opts.AdminPass
167 }
Ash Wilson6a310e02014-09-29 08:24:02 -0400168
169 if len(opts.SecurityGroups) > 0 {
170 securityGroups := make([]map[string]interface{}, len(opts.SecurityGroups))
171 for i, groupName := range opts.SecurityGroups {
172 securityGroups[i] = map[string]interface{}{"name": groupName}
173 }
eselldf709942014-11-13 21:07:11 -0700174 server["security_groups"] = securityGroups
Ash Wilson6a310e02014-09-29 08:24:02 -0400175 }
Jon Perritt2a7797d2014-10-21 15:08:43 -0500176
Ash Wilson6a310e02014-09-29 08:24:02 -0400177 if len(opts.Networks) > 0 {
178 networks := make([]map[string]interface{}, len(opts.Networks))
179 for i, net := range opts.Networks {
180 networks[i] = make(map[string]interface{})
181 if net.UUID != "" {
182 networks[i]["uuid"] = net.UUID
183 }
184 if net.Port != "" {
185 networks[i]["port"] = net.Port
186 }
187 if net.FixedIP != "" {
188 networks[i]["fixed_ip"] = net.FixedIP
189 }
190 }
Jon Perritt2a7797d2014-10-21 15:08:43 -0500191 server["networks"] = networks
Ash Wilson6a310e02014-09-29 08:24:02 -0400192 }
193
Jon Perritt4149d7c2014-10-23 21:23:46 -0500194 return map[string]interface{}{"server": server}, nil
Ash Wilson6a310e02014-09-29 08:24:02 -0400195}
196
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800197// Create requests a server to be provisioned to the user in the current tenant.
Ash Wilson2206a112014-10-02 10:57:38 -0400198func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jon Perritt4149d7c2014-10-23 21:23:46 -0500199 var res CreateResult
200
201 reqBody, err := opts.ToServerCreateMap()
202 if err != nil {
203 res.Err = err
204 return res
205 }
206
207 _, res.Err = perigee.Request("POST", listURL(client), perigee.Options{
208 Results: &res.Body,
209 ReqBody: reqBody,
Ash Wilson77857dc2014-10-22 09:09:02 -0400210 MoreHeaders: client.AuthenticatedHeaders(),
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -0800211 OkCodes: []int{202},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800212 })
Jon Perritt4149d7c2014-10-23 21:23:46 -0500213 return res
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800214}
215
216// Delete requests that a server previously provisioned be removed from your account.
Jamie Hannaford34732fe2014-10-27 11:29:36 +0100217func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
218 var res DeleteResult
219 _, res.Err = perigee.Request("DELETE", deleteURL(client, id), perigee.Options{
Ash Wilson77857dc2014-10-22 09:09:02 -0400220 MoreHeaders: client.AuthenticatedHeaders(),
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -0800221 OkCodes: []int{204},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800222 })
Jamie Hannaford34732fe2014-10-27 11:29:36 +0100223 return res
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800224}
225
Ash Wilson7ddf0362014-09-17 10:59:09 -0400226// Get requests details on a single server, by ID.
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400227func Get(client *gophercloud.ServiceClient, id string) GetResult {
228 var result GetResult
Jon Perritt703bfc02014-10-08 14:35:00 -0500229 _, result.Err = perigee.Request("GET", getURL(client, id), perigee.Options{
Ash Wilsond3dc2542014-10-20 10:10:48 -0400230 Results: &result.Body,
Ash Wilson77857dc2014-10-22 09:09:02 -0400231 MoreHeaders: client.AuthenticatedHeaders(),
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800232 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400233 return result
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800234}
235
Alex Gaynora6d5f9f2014-10-27 10:52:32 -0700236// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
Jon Perritt82048212014-10-13 22:33:13 -0500237type UpdateOptsBuilder interface {
Ash Wilsone45c9732014-09-29 10:54:12 -0400238 ToServerUpdateMap() map[string]interface{}
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400239}
240
241// UpdateOpts specifies the base attributes that may be updated on an existing server.
242type UpdateOpts struct {
243 // Name [optional] changes the displayed name of the server.
244 // The server host name will *not* change.
245 // Server names are not constrained to be unique, even within the same tenant.
246 Name string
247
248 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
249 AccessIPv4 string
250
251 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
252 AccessIPv6 string
253}
254
Ash Wilsone45c9732014-09-29 10:54:12 -0400255// ToServerUpdateMap formats an UpdateOpts structure into a request body.
256func (opts UpdateOpts) ToServerUpdateMap() map[string]interface{} {
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400257 server := make(map[string]string)
258 if opts.Name != "" {
259 server["name"] = opts.Name
260 }
261 if opts.AccessIPv4 != "" {
262 server["accessIPv4"] = opts.AccessIPv4
263 }
264 if opts.AccessIPv6 != "" {
265 server["accessIPv6"] = opts.AccessIPv6
266 }
267 return map[string]interface{}{"server": server}
268}
269
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800270// Update requests that various attributes of the indicated server be changed.
Jon Perritt82048212014-10-13 22:33:13 -0500271func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400272 var result UpdateResult
Jon Perritt703bfc02014-10-08 14:35:00 -0500273 _, result.Err = perigee.Request("PUT", updateURL(client, id), perigee.Options{
Ash Wilsond3dc2542014-10-20 10:10:48 -0400274 Results: &result.Body,
Ash Wilsone45c9732014-09-29 10:54:12 -0400275 ReqBody: opts.ToServerUpdateMap(),
Ash Wilson77857dc2014-10-22 09:09:02 -0400276 MoreHeaders: client.AuthenticatedHeaders(),
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800277 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400278 return result
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800279}
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700280
Ash Wilson01626a32014-09-17 10:38:07 -0400281// ChangeAdminPassword alters the administrator or root password for a specified server.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200282func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) ActionResult {
Ash Wilsondc7daa82014-09-23 16:34:42 -0400283 var req struct {
284 ChangePassword struct {
285 AdminPass string `json:"adminPass"`
286 } `json:"changePassword"`
287 }
288
289 req.ChangePassword.AdminPass = newPassword
290
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200291 var res ActionResult
292
293 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Ash Wilsondc7daa82014-09-23 16:34:42 -0400294 ReqBody: req,
Ash Wilson77857dc2014-10-22 09:09:02 -0400295 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500296 OkCodes: []int{202},
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700297 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200298
299 return res
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700300}
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700301
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700302// ErrArgument errors occur when an argument supplied to a package function
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700303// fails to fall within acceptable values. For example, the Reboot() function
304// expects the "how" parameter to be one of HardReboot or SoftReboot. These
305// constants are (currently) strings, leading someone to wonder if they can pass
306// other string values instead, perhaps in an effort to break the API of their
307// provider. Reboot() returns this error in this situation.
308//
309// Function identifies which function was called/which function is generating
310// the error.
311// Argument identifies which formal argument was responsible for producing the
312// error.
313// Value provides the value as it was passed into the function.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700314type ErrArgument struct {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700315 Function, Argument string
Jon Perritt30558642014-04-14 17:07:12 -0500316 Value interface{}
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700317}
318
319// Error yields a useful diagnostic for debugging purposes.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700320func (e *ErrArgument) Error() string {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700321 return fmt.Sprintf("Bad argument in call to %s, formal parameter %s, value %#v", e.Function, e.Argument, e.Value)
322}
323
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700324func (e *ErrArgument) String() string {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700325 return e.Error()
326}
327
Ash Wilson01626a32014-09-17 10:38:07 -0400328// RebootMethod describes the mechanisms by which a server reboot can be requested.
329type RebootMethod string
330
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700331// These constants determine how a server should be rebooted.
332// See the Reboot() function for further details.
333const (
Ash Wilson01626a32014-09-17 10:38:07 -0400334 SoftReboot RebootMethod = "SOFT"
335 HardReboot RebootMethod = "HARD"
336 OSReboot = SoftReboot
337 PowerCycle = HardReboot
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700338)
339
340// Reboot requests that a given server reboot.
341// Two methods exist for rebooting a server:
342//
Ash Wilson01626a32014-09-17 10:38:07 -0400343// HardReboot (aka PowerCycle) restarts the server instance by physically cutting power to the machine, or if a VM,
344// terminating it at the hypervisor level.
345// It's done. Caput. Full stop.
346// Then, after a brief while, power is restored or the VM instance restarted.
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700347//
Ash Wilson01626a32014-09-17 10:38:07 -0400348// SoftReboot (aka OSReboot) simply tells the OS to restart under its own procedures.
349// E.g., in Linux, asking it to enter runlevel 6, or executing "sudo shutdown -r now", or by asking Windows to restart the machine.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200350func Reboot(client *gophercloud.ServiceClient, id string, how RebootMethod) ActionResult {
351 var res ActionResult
352
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700353 if (how != SoftReboot) && (how != HardReboot) {
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200354 res.Err = &ErrArgument{
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700355 Function: "Reboot",
356 Argument: "how",
Jon Perritt30558642014-04-14 17:07:12 -0500357 Value: how,
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700358 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200359 return res
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700360 }
Jon Perritt30558642014-04-14 17:07:12 -0500361
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200362 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500363 ReqBody: struct {
364 C map[string]string `json:"reboot"`
365 }{
Ash Wilson01626a32014-09-17 10:38:07 -0400366 map[string]string{"type": string(how)},
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700367 },
Ash Wilson77857dc2014-10-22 09:09:02 -0400368 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500369 OkCodes: []int{202},
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700370 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200371
372 return res
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700373}
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700374
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200375// RebuildOptsBuilder is an interface that allows extensions to override the
376// default behaviour of rebuild options
377type RebuildOptsBuilder interface {
378 ToServerRebuildMap() (map[string]interface{}, error)
379}
380
381// RebuildOpts represents the configuration options used in a server rebuild
382// operation
383type RebuildOpts struct {
384 // Required. The ID of the image you want your server to be provisioned on
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200385 ImageID string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200386
387 // Name to set the server to
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200388 Name string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200389
390 // Required. The server's admin password
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200391 AdminPass string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200392
393 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200394 AccessIPv4 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200395
396 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200397 AccessIPv6 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200398
399 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200400 Metadata map[string]string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200401
402 // Personality [optional] includes the path and contents of a file to inject into the server at launch.
403 // The maximum size of the file is 255 bytes (decoded).
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200404 Personality []byte
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200405}
406
407// ToServerRebuildMap formats a RebuildOpts struct into a map for use in JSON
408func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {
409 var err error
410 server := make(map[string]interface{})
411
412 if opts.AdminPass == "" {
413 err = fmt.Errorf("AdminPass is required")
414 }
415
416 if opts.ImageID == "" {
417 err = fmt.Errorf("ImageID is required")
418 }
419
420 if err != nil {
421 return server, err
422 }
423
424 server["name"] = opts.Name
425 server["adminPass"] = opts.AdminPass
426 server["imageRef"] = opts.ImageID
427
428 if opts.AccessIPv4 != "" {
429 server["accessIPv4"] = opts.AccessIPv4
430 }
431
432 if opts.AccessIPv6 != "" {
433 server["accessIPv6"] = opts.AccessIPv6
434 }
435
436 if opts.Metadata != nil {
437 server["metadata"] = opts.Metadata
438 }
439
440 if opts.Personality != nil {
441 encoded := base64.StdEncoding.EncodeToString(opts.Personality)
442 server["personality"] = &encoded
443 }
444
445 return map[string]interface{}{"rebuild": server}, nil
446}
447
448// Rebuild will reprovision the server according to the configuration options
449// provided in the RebuildOpts struct.
450func Rebuild(client *gophercloud.ServiceClient, id string, opts RebuildOptsBuilder) RebuildResult {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400451 var result RebuildResult
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700452
453 if id == "" {
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200454 result.Err = fmt.Errorf("ID is required")
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400455 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700456 }
457
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200458 reqBody, err := opts.ToServerRebuildMap()
459 if err != nil {
460 result.Err = err
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400461 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700462 }
463
Ash Wilson31f6bde2014-09-25 14:52:12 -0400464 _, result.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200465 ReqBody: &reqBody,
Ash Wilsond3dc2542014-10-20 10:10:48 -0400466 Results: &result.Body,
Ash Wilson77857dc2014-10-22 09:09:02 -0400467 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500468 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700469 })
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200470
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400471 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700472}
473
Ash Wilson5f7cf182014-10-23 08:35:24 -0400474// ResizeOptsBuilder is an interface that allows extensions to override the default structure of
475// a Resize request.
476type ResizeOptsBuilder interface {
477 ToServerResizeMap() (map[string]interface{}, error)
478}
479
480// ResizeOpts represents the configuration options used to control a Resize operation.
481type ResizeOpts struct {
482 // FlavorRef is the ID of the flavor you wish your server to become.
483 FlavorRef string
484}
485
Alex Gaynor266e9332014-10-28 14:44:04 -0700486// ToServerResizeMap formats a ResizeOpts as a map that can be used as a JSON request body for the
Ash Wilson5f7cf182014-10-23 08:35:24 -0400487// Resize request.
488func (opts ResizeOpts) ToServerResizeMap() (map[string]interface{}, error) {
489 resize := map[string]interface{}{
490 "flavorRef": opts.FlavorRef,
491 }
492
493 return map[string]interface{}{"resize": resize}, nil
494}
495
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700496// Resize instructs the provider to change the flavor of the server.
Ash Wilson01626a32014-09-17 10:38:07 -0400497// Note that this implies rebuilding it.
498// Unfortunately, one cannot pass rebuild parameters to the resize function.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700499// When the resize completes, the server will be in RESIZE_VERIFY state.
500// While in this state, you can explore the use of the new server's configuration.
501// If you like it, call ConfirmResize() to commit the resize permanently.
502// Otherwise, call RevertResize() to restore the old configuration.
Ash Wilson9e87a922014-10-23 14:29:22 -0400503func Resize(client *gophercloud.ServiceClient, id string, opts ResizeOptsBuilder) ActionResult {
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200504 var res ActionResult
Ash Wilson5f7cf182014-10-23 08:35:24 -0400505 reqBody, err := opts.ToServerResizeMap()
506 if err != nil {
507 res.Err = err
508 return res
509 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200510
511 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Ash Wilson5f7cf182014-10-23 08:35:24 -0400512 ReqBody: reqBody,
Ash Wilson77857dc2014-10-22 09:09:02 -0400513 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500514 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700515 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200516
517 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700518}
519
520// ConfirmResize confirms a previous resize operation on a server.
521// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200522func ConfirmResize(client *gophercloud.ServiceClient, id string) ActionResult {
523 var res ActionResult
524
525 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500526 ReqBody: map[string]interface{}{"confirmResize": nil},
Ash Wilson77857dc2014-10-22 09:09:02 -0400527 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500528 OkCodes: []int{204},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700529 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200530
531 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700532}
533
534// RevertResize cancels a previous resize operation on a server.
535// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200536func RevertResize(client *gophercloud.ServiceClient, id string) ActionResult {
537 var res ActionResult
538
539 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500540 ReqBody: map[string]interface{}{"revertResize": nil},
Ash Wilson77857dc2014-10-22 09:09:02 -0400541 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500542 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700543 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200544
545 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700546}
Alex Gaynor39584a02014-10-28 13:59:21 -0700547
Alex Gaynor266e9332014-10-28 14:44:04 -0700548// RescueOptsBuilder is an interface that allows extensions to override the
549// default structure of a Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700550type RescueOptsBuilder interface {
551 ToServerRescueMap() (map[string]interface{}, error)
552}
553
Alex Gaynor266e9332014-10-28 14:44:04 -0700554// RescueOpts represents the configuration options used to control a Rescue
555// option.
Alex Gaynor39584a02014-10-28 13:59:21 -0700556type RescueOpts struct {
Alex Gaynor266e9332014-10-28 14:44:04 -0700557 // AdminPass is the desired administrative password for the instance in
Alex Gaynorcfec7722014-11-13 13:33:49 -0800558 // RESCUE mode. If it's left blank, the server will generate a password.
Alex Gaynor39584a02014-10-28 13:59:21 -0700559 AdminPass string
560}
561
Alex Gaynorcfec7722014-11-13 13:33:49 -0800562// ToRescueResizeMap formats a RescueOpts as a map that can be used as a JSON
Alex Gaynor266e9332014-10-28 14:44:04 -0700563// request body for the Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700564func (opts RescueOpts) ToServerRescueMap() (map[string]interface{}, error) {
565 server := make(map[string]interface{})
566 if opts.AdminPass != "" {
567 server["adminPass"] = opts.AdminPass
568 }
569 return map[string]interface{}{"rescue": server}, nil
570}
571
Alex Gaynor266e9332014-10-28 14:44:04 -0700572// Rescue instructs the provider to place the server into RESCUE mode.
Alex Gaynorfbe61bb2014-11-12 13:35:03 -0800573func Rescue(client *gophercloud.ServiceClient, id string, opts RescueOptsBuilder) RescueResult {
574 var result RescueResult
Alex Gaynor39584a02014-10-28 13:59:21 -0700575
576 if id == "" {
577 result.Err = fmt.Errorf("ID is required")
578 return result
579 }
580 reqBody, err := opts.ToServerRescueMap()
581 if err != nil {
582 result.Err = err
583 return result
584 }
585
586 _, result.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Alex Gaynorfbe61bb2014-11-12 13:35:03 -0800587 Results: &result.Body,
Alex Gaynor39584a02014-10-28 13:59:21 -0700588 ReqBody: &reqBody,
Alex Gaynor39584a02014-10-28 13:59:21 -0700589 MoreHeaders: client.AuthenticatedHeaders(),
590 OkCodes: []int{200},
591 })
592
593 return result
594}