blob: b0d1e6a013d2385f16709c92e23662a2270133a3 [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
134}
135
Ash Wilsone45c9732014-09-29 10:54:12 -0400136// ToServerCreateMap assembles a request body based on the contents of a CreateOpts.
Jon Perritt4149d7c2014-10-23 21:23:46 -0500137func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) {
Ash Wilson6a310e02014-09-29 08:24:02 -0400138 server := make(map[string]interface{})
139
140 server["name"] = opts.Name
141 server["imageRef"] = opts.ImageRef
142 server["flavorRef"] = opts.FlavorRef
143
144 if opts.UserData != nil {
145 encoded := base64.StdEncoding.EncodeToString(opts.UserData)
146 server["user_data"] = &encoded
147 }
148 if opts.Personality != nil {
149 encoded := base64.StdEncoding.EncodeToString(opts.Personality)
150 server["personality"] = &encoded
151 }
152 if opts.ConfigDrive {
153 server["config_drive"] = "true"
154 }
155 if opts.AvailabilityZone != "" {
156 server["availability_zone"] = opts.AvailabilityZone
157 }
158 if opts.Metadata != nil {
159 server["metadata"] = opts.Metadata
160 }
161
162 if len(opts.SecurityGroups) > 0 {
163 securityGroups := make([]map[string]interface{}, len(opts.SecurityGroups))
164 for i, groupName := range opts.SecurityGroups {
165 securityGroups[i] = map[string]interface{}{"name": groupName}
166 }
167 }
Jon Perritt2a7797d2014-10-21 15:08:43 -0500168
Ash Wilson6a310e02014-09-29 08:24:02 -0400169 if len(opts.Networks) > 0 {
170 networks := make([]map[string]interface{}, len(opts.Networks))
171 for i, net := range opts.Networks {
172 networks[i] = make(map[string]interface{})
173 if net.UUID != "" {
174 networks[i]["uuid"] = net.UUID
175 }
176 if net.Port != "" {
177 networks[i]["port"] = net.Port
178 }
179 if net.FixedIP != "" {
180 networks[i]["fixed_ip"] = net.FixedIP
181 }
182 }
Jon Perritt2a7797d2014-10-21 15:08:43 -0500183 server["networks"] = networks
Ash Wilson6a310e02014-09-29 08:24:02 -0400184 }
185
Jon Perritt4149d7c2014-10-23 21:23:46 -0500186 return map[string]interface{}{"server": server}, nil
Ash Wilson6a310e02014-09-29 08:24:02 -0400187}
188
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800189// Create requests a server to be provisioned to the user in the current tenant.
Ash Wilson2206a112014-10-02 10:57:38 -0400190func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jon Perritt4149d7c2014-10-23 21:23:46 -0500191 var res CreateResult
192
193 reqBody, err := opts.ToServerCreateMap()
194 if err != nil {
195 res.Err = err
196 return res
197 }
198
199 _, res.Err = perigee.Request("POST", listURL(client), perigee.Options{
200 Results: &res.Body,
201 ReqBody: reqBody,
Ash Wilson77857dc2014-10-22 09:09:02 -0400202 MoreHeaders: client.AuthenticatedHeaders(),
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -0800203 OkCodes: []int{202},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800204 })
Jon Perritt4149d7c2014-10-23 21:23:46 -0500205 return res
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800206}
207
208// Delete requests that a server previously provisioned be removed from your account.
Jamie Hannaford34732fe2014-10-27 11:29:36 +0100209func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
210 var res DeleteResult
211 _, res.Err = perigee.Request("DELETE", deleteURL(client, id), perigee.Options{
Ash Wilson77857dc2014-10-22 09:09:02 -0400212 MoreHeaders: client.AuthenticatedHeaders(),
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -0800213 OkCodes: []int{204},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800214 })
Jamie Hannaford34732fe2014-10-27 11:29:36 +0100215 return res
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800216}
217
Ash Wilson7ddf0362014-09-17 10:59:09 -0400218// Get requests details on a single server, by ID.
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400219func Get(client *gophercloud.ServiceClient, id string) GetResult {
220 var result GetResult
Jon Perritt703bfc02014-10-08 14:35:00 -0500221 _, result.Err = perigee.Request("GET", getURL(client, id), perigee.Options{
Ash Wilsond3dc2542014-10-20 10:10:48 -0400222 Results: &result.Body,
Ash Wilson77857dc2014-10-22 09:09:02 -0400223 MoreHeaders: client.AuthenticatedHeaders(),
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800224 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400225 return result
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800226}
227
Jon Perritt82048212014-10-13 22:33:13 -0500228// UpdateOptsBuilder allows extentions to add additional attributes to the Update request.
229type UpdateOptsBuilder interface {
Ash Wilsone45c9732014-09-29 10:54:12 -0400230 ToServerUpdateMap() map[string]interface{}
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400231}
232
233// UpdateOpts specifies the base attributes that may be updated on an existing server.
234type UpdateOpts struct {
235 // Name [optional] changes the displayed name of the server.
236 // The server host name will *not* change.
237 // Server names are not constrained to be unique, even within the same tenant.
238 Name string
239
240 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
241 AccessIPv4 string
242
243 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
244 AccessIPv6 string
245}
246
Ash Wilsone45c9732014-09-29 10:54:12 -0400247// ToServerUpdateMap formats an UpdateOpts structure into a request body.
248func (opts UpdateOpts) ToServerUpdateMap() map[string]interface{} {
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400249 server := make(map[string]string)
250 if opts.Name != "" {
251 server["name"] = opts.Name
252 }
253 if opts.AccessIPv4 != "" {
254 server["accessIPv4"] = opts.AccessIPv4
255 }
256 if opts.AccessIPv6 != "" {
257 server["accessIPv6"] = opts.AccessIPv6
258 }
259 return map[string]interface{}{"server": server}
260}
261
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800262// Update requests that various attributes of the indicated server be changed.
Jon Perritt82048212014-10-13 22:33:13 -0500263func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400264 var result UpdateResult
Jon Perritt703bfc02014-10-08 14:35:00 -0500265 _, result.Err = perigee.Request("PUT", updateURL(client, id), perigee.Options{
Ash Wilsond3dc2542014-10-20 10:10:48 -0400266 Results: &result.Body,
Ash Wilsone45c9732014-09-29 10:54:12 -0400267 ReqBody: opts.ToServerUpdateMap(),
Ash Wilson77857dc2014-10-22 09:09:02 -0400268 MoreHeaders: client.AuthenticatedHeaders(),
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800269 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400270 return result
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800271}
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700272
Ash Wilson01626a32014-09-17 10:38:07 -0400273// ChangeAdminPassword alters the administrator or root password for a specified server.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200274func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) ActionResult {
Ash Wilsondc7daa82014-09-23 16:34:42 -0400275 var req struct {
276 ChangePassword struct {
277 AdminPass string `json:"adminPass"`
278 } `json:"changePassword"`
279 }
280
281 req.ChangePassword.AdminPass = newPassword
282
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200283 var res ActionResult
284
285 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Ash Wilsondc7daa82014-09-23 16:34:42 -0400286 ReqBody: req,
Ash Wilson262bcc82014-10-20 12:13:11 -0400287 Results: &res.Body,
Ash Wilson77857dc2014-10-22 09:09:02 -0400288 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500289 OkCodes: []int{202},
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700290 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200291
292 return res
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700293}
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700294
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700295// ErrArgument errors occur when an argument supplied to a package function
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700296// fails to fall within acceptable values. For example, the Reboot() function
297// expects the "how" parameter to be one of HardReboot or SoftReboot. These
298// constants are (currently) strings, leading someone to wonder if they can pass
299// other string values instead, perhaps in an effort to break the API of their
300// provider. Reboot() returns this error in this situation.
301//
302// Function identifies which function was called/which function is generating
303// the error.
304// Argument identifies which formal argument was responsible for producing the
305// error.
306// Value provides the value as it was passed into the function.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700307type ErrArgument struct {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700308 Function, Argument string
Jon Perritt30558642014-04-14 17:07:12 -0500309 Value interface{}
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700310}
311
312// Error yields a useful diagnostic for debugging purposes.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700313func (e *ErrArgument) Error() string {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700314 return fmt.Sprintf("Bad argument in call to %s, formal parameter %s, value %#v", e.Function, e.Argument, e.Value)
315}
316
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700317func (e *ErrArgument) String() string {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700318 return e.Error()
319}
320
Ash Wilson01626a32014-09-17 10:38:07 -0400321// RebootMethod describes the mechanisms by which a server reboot can be requested.
322type RebootMethod string
323
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700324// These constants determine how a server should be rebooted.
325// See the Reboot() function for further details.
326const (
Ash Wilson01626a32014-09-17 10:38:07 -0400327 SoftReboot RebootMethod = "SOFT"
328 HardReboot RebootMethod = "HARD"
329 OSReboot = SoftReboot
330 PowerCycle = HardReboot
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700331)
332
333// Reboot requests that a given server reboot.
334// Two methods exist for rebooting a server:
335//
Ash Wilson01626a32014-09-17 10:38:07 -0400336// HardReboot (aka PowerCycle) restarts the server instance by physically cutting power to the machine, or if a VM,
337// terminating it at the hypervisor level.
338// It's done. Caput. Full stop.
339// Then, after a brief while, power is restored or the VM instance restarted.
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700340//
Ash Wilson01626a32014-09-17 10:38:07 -0400341// SoftReboot (aka OSReboot) simply tells the OS to restart under its own procedures.
342// 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 +0200343func Reboot(client *gophercloud.ServiceClient, id string, how RebootMethod) ActionResult {
344 var res ActionResult
345
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700346 if (how != SoftReboot) && (how != HardReboot) {
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200347 res.Err = &ErrArgument{
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700348 Function: "Reboot",
349 Argument: "how",
Jon Perritt30558642014-04-14 17:07:12 -0500350 Value: how,
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700351 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200352 return res
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700353 }
Jon Perritt30558642014-04-14 17:07:12 -0500354
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200355 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500356 ReqBody: struct {
357 C map[string]string `json:"reboot"`
358 }{
Ash Wilson01626a32014-09-17 10:38:07 -0400359 map[string]string{"type": string(how)},
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700360 },
Ash Wilson77857dc2014-10-22 09:09:02 -0400361 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500362 OkCodes: []int{202},
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700363 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200364
365 return res
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700366}
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700367
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200368// RebuildOptsBuilder is an interface that allows extensions to override the
369// default behaviour of rebuild options
370type RebuildOptsBuilder interface {
371 ToServerRebuildMap() (map[string]interface{}, error)
372}
373
374// RebuildOpts represents the configuration options used in a server rebuild
375// operation
376type RebuildOpts struct {
377 // Required. The ID of the image you want your server to be provisioned on
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200378 ImageID string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200379
380 // Name to set the server to
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200381 Name string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200382
383 // Required. The server's admin password
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200384 AdminPass string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200385
386 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200387 AccessIPv4 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200388
389 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200390 AccessIPv6 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200391
392 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200393 Metadata map[string]string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200394
395 // Personality [optional] includes the path and contents of a file to inject into the server at launch.
396 // The maximum size of the file is 255 bytes (decoded).
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200397 Personality []byte
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200398}
399
400// ToServerRebuildMap formats a RebuildOpts struct into a map for use in JSON
401func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {
402 var err error
403 server := make(map[string]interface{})
404
405 if opts.AdminPass == "" {
406 err = fmt.Errorf("AdminPass is required")
407 }
408
409 if opts.ImageID == "" {
410 err = fmt.Errorf("ImageID is required")
411 }
412
413 if err != nil {
414 return server, err
415 }
416
417 server["name"] = opts.Name
418 server["adminPass"] = opts.AdminPass
419 server["imageRef"] = opts.ImageID
420
421 if opts.AccessIPv4 != "" {
422 server["accessIPv4"] = opts.AccessIPv4
423 }
424
425 if opts.AccessIPv6 != "" {
426 server["accessIPv6"] = opts.AccessIPv6
427 }
428
429 if opts.Metadata != nil {
430 server["metadata"] = opts.Metadata
431 }
432
433 if opts.Personality != nil {
434 encoded := base64.StdEncoding.EncodeToString(opts.Personality)
435 server["personality"] = &encoded
436 }
437
438 return map[string]interface{}{"rebuild": server}, nil
439}
440
441// Rebuild will reprovision the server according to the configuration options
442// provided in the RebuildOpts struct.
443func Rebuild(client *gophercloud.ServiceClient, id string, opts RebuildOptsBuilder) RebuildResult {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400444 var result RebuildResult
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700445
446 if id == "" {
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200447 result.Err = fmt.Errorf("ID is required")
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400448 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700449 }
450
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200451 reqBody, err := opts.ToServerRebuildMap()
452 if err != nil {
453 result.Err = err
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400454 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700455 }
456
Ash Wilson31f6bde2014-09-25 14:52:12 -0400457 _, result.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200458 ReqBody: &reqBody,
Ash Wilsond3dc2542014-10-20 10:10:48 -0400459 Results: &result.Body,
Ash Wilson77857dc2014-10-22 09:09:02 -0400460 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500461 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700462 })
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200463
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400464 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700465}
466
Ash Wilson5f7cf182014-10-23 08:35:24 -0400467// ResizeOptsBuilder is an interface that allows extensions to override the default structure of
468// a Resize request.
469type ResizeOptsBuilder interface {
470 ToServerResizeMap() (map[string]interface{}, error)
471}
472
473// ResizeOpts represents the configuration options used to control a Resize operation.
474type ResizeOpts struct {
475 // FlavorRef is the ID of the flavor you wish your server to become.
476 FlavorRef string
477}
478
479// ToServerResizeMap formats a ResizeOpts as a map that can be used as a JSON request body to the
480// Resize request.
481func (opts ResizeOpts) ToServerResizeMap() (map[string]interface{}, error) {
482 resize := map[string]interface{}{
483 "flavorRef": opts.FlavorRef,
484 }
485
486 return map[string]interface{}{"resize": resize}, nil
487}
488
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700489// Resize instructs the provider to change the flavor of the server.
Ash Wilson01626a32014-09-17 10:38:07 -0400490// Note that this implies rebuilding it.
491// Unfortunately, one cannot pass rebuild parameters to the resize function.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700492// When the resize completes, the server will be in RESIZE_VERIFY state.
493// While in this state, you can explore the use of the new server's configuration.
494// If you like it, call ConfirmResize() to commit the resize permanently.
495// Otherwise, call RevertResize() to restore the old configuration.
Ash Wilson9e87a922014-10-23 14:29:22 -0400496func Resize(client *gophercloud.ServiceClient, id string, opts ResizeOptsBuilder) ActionResult {
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200497 var res ActionResult
Ash Wilson5f7cf182014-10-23 08:35:24 -0400498 reqBody, err := opts.ToServerResizeMap()
499 if err != nil {
500 res.Err = err
501 return res
502 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200503
504 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Ash Wilson5f7cf182014-10-23 08:35:24 -0400505 ReqBody: reqBody,
Ash Wilson77857dc2014-10-22 09:09:02 -0400506 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500507 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700508 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200509
510 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700511}
512
513// ConfirmResize confirms a previous resize operation on a server.
514// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200515func ConfirmResize(client *gophercloud.ServiceClient, id string) ActionResult {
516 var res ActionResult
517
518 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500519 ReqBody: map[string]interface{}{"confirmResize": nil},
Ash Wilson77857dc2014-10-22 09:09:02 -0400520 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500521 OkCodes: []int{204},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700522 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200523
524 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700525}
526
527// RevertResize cancels a previous resize operation on a server.
528// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200529func RevertResize(client *gophercloud.ServiceClient, id string) ActionResult {
530 var res ActionResult
531
532 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500533 ReqBody: map[string]interface{}{"revertResize": nil},
Ash Wilson77857dc2014-10-22 09:09:02 -0400534 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500535 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700536 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200537
538 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700539}