blob: 7bbdb56ed5bf15b69961e900fac46fc8a0698f47 [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
72 createPageFn := func(r pagination.LastHTTPResponse) pagination.Page {
Ash Wilson397c78b2014-09-25 15:19:14 -040073 return ServerPage{pagination.LinkedPageBase{LastHTTPResponse: 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 {
Ash Wilsone45c9732014-09-29 10:54:12 -040082 ToServerCreateMap() map[string]interface{}
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.
137func (opts CreateOpts) ToServerCreateMap() map[string]interface{} {
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 }
168 if len(opts.Networks) > 0 {
169 networks := make([]map[string]interface{}, len(opts.Networks))
170 for i, net := range opts.Networks {
171 networks[i] = make(map[string]interface{})
172 if net.UUID != "" {
173 networks[i]["uuid"] = net.UUID
174 }
175 if net.Port != "" {
176 networks[i]["port"] = net.Port
177 }
178 if net.FixedIP != "" {
179 networks[i]["fixed_ip"] = net.FixedIP
180 }
181 }
182 }
183
184 return map[string]interface{}{"server": server}
185}
186
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800187// Create requests a server to be provisioned to the user in the current tenant.
Ash Wilson2206a112014-10-02 10:57:38 -0400188func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400189 var result CreateResult
Ash Wilson31f6bde2014-09-25 14:52:12 -0400190 _, result.Err = perigee.Request("POST", listURL(client), perigee.Options{
Ash Wilson6a310e02014-09-29 08:24:02 -0400191 Results: &result.Resp,
Ash Wilsone45c9732014-09-29 10:54:12 -0400192 ReqBody: opts.ToServerCreateMap(),
Ash Wilson01626a32014-09-17 10:38:07 -0400193 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -0800194 OkCodes: []int{202},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800195 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400196 return result
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800197}
198
199// Delete requests that a server previously provisioned be removed from your account.
Ash Wilson01626a32014-09-17 10:38:07 -0400200func Delete(client *gophercloud.ServiceClient, id string) error {
Jon Perritt703bfc02014-10-08 14:35:00 -0500201 _, err := perigee.Request("DELETE", deleteURL(client, id), perigee.Options{
Ash Wilson01626a32014-09-17 10:38:07 -0400202 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -0800203 OkCodes: []int{204},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800204 })
205 return err
206}
207
Ash Wilson7ddf0362014-09-17 10:59:09 -0400208// Get requests details on a single server, by ID.
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400209func Get(client *gophercloud.ServiceClient, id string) GetResult {
210 var result GetResult
Jon Perritt703bfc02014-10-08 14:35:00 -0500211 _, result.Err = perigee.Request("GET", getURL(client, id), perigee.Options{
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400212 Results: &result.Resp,
Ash Wilson01626a32014-09-17 10:38:07 -0400213 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800214 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400215 return result
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800216}
217
Jon Perritt82048212014-10-13 22:33:13 -0500218// UpdateOptsBuilder allows extentions to add additional attributes to the Update request.
219type UpdateOptsBuilder interface {
Ash Wilsone45c9732014-09-29 10:54:12 -0400220 ToServerUpdateMap() map[string]interface{}
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400221}
222
223// UpdateOpts specifies the base attributes that may be updated on an existing server.
224type UpdateOpts struct {
225 // Name [optional] changes the displayed name of the server.
226 // The server host name will *not* change.
227 // Server names are not constrained to be unique, even within the same tenant.
228 Name string
229
230 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
231 AccessIPv4 string
232
233 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
234 AccessIPv6 string
235}
236
Ash Wilsone45c9732014-09-29 10:54:12 -0400237// ToServerUpdateMap formats an UpdateOpts structure into a request body.
238func (opts UpdateOpts) ToServerUpdateMap() map[string]interface{} {
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400239 server := make(map[string]string)
240 if opts.Name != "" {
241 server["name"] = opts.Name
242 }
243 if opts.AccessIPv4 != "" {
244 server["accessIPv4"] = opts.AccessIPv4
245 }
246 if opts.AccessIPv6 != "" {
247 server["accessIPv6"] = opts.AccessIPv6
248 }
249 return map[string]interface{}{"server": server}
250}
251
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800252// Update requests that various attributes of the indicated server be changed.
Jon Perritt82048212014-10-13 22:33:13 -0500253func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400254 var result UpdateResult
Jon Perritt703bfc02014-10-08 14:35:00 -0500255 _, result.Err = perigee.Request("PUT", updateURL(client, id), perigee.Options{
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400256 Results: &result.Resp,
Ash Wilsone45c9732014-09-29 10:54:12 -0400257 ReqBody: opts.ToServerUpdateMap(),
Ash Wilson01626a32014-09-17 10:38:07 -0400258 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800259 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400260 return result
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800261}
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700262
Ash Wilson01626a32014-09-17 10:38:07 -0400263// ChangeAdminPassword alters the administrator or root password for a specified server.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200264func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) ActionResult {
Ash Wilsondc7daa82014-09-23 16:34:42 -0400265 var req struct {
266 ChangePassword struct {
267 AdminPass string `json:"adminPass"`
268 } `json:"changePassword"`
269 }
270
271 req.ChangePassword.AdminPass = newPassword
272
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200273 var res ActionResult
274
275 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Ash Wilsondc7daa82014-09-23 16:34:42 -0400276 ReqBody: req,
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200277 Results: &res.Resp,
Ash Wilson01626a32014-09-17 10:38:07 -0400278 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500279 OkCodes: []int{202},
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700280 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200281
282 return res
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700283}
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700284
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700285// ErrArgument errors occur when an argument supplied to a package function
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700286// fails to fall within acceptable values. For example, the Reboot() function
287// expects the "how" parameter to be one of HardReboot or SoftReboot. These
288// constants are (currently) strings, leading someone to wonder if they can pass
289// other string values instead, perhaps in an effort to break the API of their
290// provider. Reboot() returns this error in this situation.
291//
292// Function identifies which function was called/which function is generating
293// the error.
294// Argument identifies which formal argument was responsible for producing the
295// error.
296// Value provides the value as it was passed into the function.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700297type ErrArgument struct {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700298 Function, Argument string
Jon Perritt30558642014-04-14 17:07:12 -0500299 Value interface{}
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700300}
301
302// Error yields a useful diagnostic for debugging purposes.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700303func (e *ErrArgument) Error() string {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700304 return fmt.Sprintf("Bad argument in call to %s, formal parameter %s, value %#v", e.Function, e.Argument, e.Value)
305}
306
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700307func (e *ErrArgument) String() string {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700308 return e.Error()
309}
310
Ash Wilson01626a32014-09-17 10:38:07 -0400311// RebootMethod describes the mechanisms by which a server reboot can be requested.
312type RebootMethod string
313
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700314// These constants determine how a server should be rebooted.
315// See the Reboot() function for further details.
316const (
Ash Wilson01626a32014-09-17 10:38:07 -0400317 SoftReboot RebootMethod = "SOFT"
318 HardReboot RebootMethod = "HARD"
319 OSReboot = SoftReboot
320 PowerCycle = HardReboot
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700321)
322
323// Reboot requests that a given server reboot.
324// Two methods exist for rebooting a server:
325//
Ash Wilson01626a32014-09-17 10:38:07 -0400326// HardReboot (aka PowerCycle) restarts the server instance by physically cutting power to the machine, or if a VM,
327// terminating it at the hypervisor level.
328// It's done. Caput. Full stop.
329// Then, after a brief while, power is restored or the VM instance restarted.
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700330//
Ash Wilson01626a32014-09-17 10:38:07 -0400331// SoftReboot (aka OSReboot) simply tells the OS to restart under its own procedures.
332// 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 +0200333func Reboot(client *gophercloud.ServiceClient, id string, how RebootMethod) ActionResult {
334 var res ActionResult
335
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700336 if (how != SoftReboot) && (how != HardReboot) {
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200337 res.Err = &ErrArgument{
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700338 Function: "Reboot",
339 Argument: "how",
Jon Perritt30558642014-04-14 17:07:12 -0500340 Value: how,
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700341 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200342 return res
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700343 }
Jon Perritt30558642014-04-14 17:07:12 -0500344
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200345 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500346 ReqBody: struct {
347 C map[string]string `json:"reboot"`
348 }{
Ash Wilson01626a32014-09-17 10:38:07 -0400349 map[string]string{"type": string(how)},
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700350 },
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200351 Results: &res.Resp,
Ash Wilson01626a32014-09-17 10:38:07 -0400352 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500353 OkCodes: []int{202},
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700354 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200355
356 return res
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700357}
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700358
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200359// RebuildOptsBuilder is an interface that allows extensions to override the
360// default behaviour of rebuild options
361type RebuildOptsBuilder interface {
362 ToServerRebuildMap() (map[string]interface{}, error)
363}
364
365// RebuildOpts represents the configuration options used in a server rebuild
366// operation
367type RebuildOpts struct {
368 // Required. The ID of the image you want your server to be provisioned on
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200369 ImageID string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200370
371 // Name to set the server to
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200372 Name string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200373
374 // Required. The server's admin password
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200375 AdminPass string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200376
377 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200378 AccessIPv4 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200379
380 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200381 AccessIPv6 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200382
383 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200384 Metadata map[string]string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200385
386 // Personality [optional] includes the path and contents of a file to inject into the server at launch.
387 // The maximum size of the file is 255 bytes (decoded).
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200388 Personality []byte
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200389}
390
391// ToServerRebuildMap formats a RebuildOpts struct into a map for use in JSON
392func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {
393 var err error
394 server := make(map[string]interface{})
395
396 if opts.AdminPass == "" {
397 err = fmt.Errorf("AdminPass is required")
398 }
399
400 if opts.ImageID == "" {
401 err = fmt.Errorf("ImageID is required")
402 }
403
404 if err != nil {
405 return server, err
406 }
407
408 server["name"] = opts.Name
409 server["adminPass"] = opts.AdminPass
410 server["imageRef"] = opts.ImageID
411
412 if opts.AccessIPv4 != "" {
413 server["accessIPv4"] = opts.AccessIPv4
414 }
415
416 if opts.AccessIPv6 != "" {
417 server["accessIPv6"] = opts.AccessIPv6
418 }
419
420 if opts.Metadata != nil {
421 server["metadata"] = opts.Metadata
422 }
423
424 if opts.Personality != nil {
425 encoded := base64.StdEncoding.EncodeToString(opts.Personality)
426 server["personality"] = &encoded
427 }
428
429 return map[string]interface{}{"rebuild": server}, nil
430}
431
432// Rebuild will reprovision the server according to the configuration options
433// provided in the RebuildOpts struct.
434func Rebuild(client *gophercloud.ServiceClient, id string, opts RebuildOptsBuilder) RebuildResult {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400435 var result RebuildResult
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700436
437 if id == "" {
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200438 result.Err = fmt.Errorf("ID is required")
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400439 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700440 }
441
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200442 reqBody, err := opts.ToServerRebuildMap()
443 if err != nil {
444 result.Err = err
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400445 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700446 }
447
Ash Wilson31f6bde2014-09-25 14:52:12 -0400448 _, result.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200449 ReqBody: &reqBody,
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400450 Results: &result.Resp,
Ash Wilson01626a32014-09-17 10:38:07 -0400451 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500452 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700453 })
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200454
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400455 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700456}
457
458// Resize instructs the provider to change the flavor of the server.
Ash Wilson01626a32014-09-17 10:38:07 -0400459// Note that this implies rebuilding it.
460// Unfortunately, one cannot pass rebuild parameters to the resize function.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700461// When the resize completes, the server will be in RESIZE_VERIFY state.
462// While in this state, you can explore the use of the new server's configuration.
463// If you like it, call ConfirmResize() to commit the resize permanently.
464// Otherwise, call RevertResize() to restore the old configuration.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200465func Resize(client *gophercloud.ServiceClient, id, flavorRef string) ActionResult {
466 var res ActionResult
467
468 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500469 ReqBody: struct {
470 R map[string]interface{} `json:"resize"`
471 }{
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700472 map[string]interface{}{"flavorRef": flavorRef},
473 },
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200474 Results: &res.Resp,
Ash Wilson01626a32014-09-17 10:38:07 -0400475 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500476 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700477 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200478
479 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700480}
481
482// ConfirmResize confirms a previous resize operation on a server.
483// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200484func ConfirmResize(client *gophercloud.ServiceClient, id string) ActionResult {
485 var res ActionResult
486
487 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500488 ReqBody: map[string]interface{}{"confirmResize": nil},
Ash Wilson01626a32014-09-17 10:38:07 -0400489 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200490 Results: &res.Resp,
Jon Perritt30558642014-04-14 17:07:12 -0500491 OkCodes: []int{204},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700492 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200493
494 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700495}
496
497// RevertResize cancels a previous resize operation on a server.
498// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200499func RevertResize(client *gophercloud.ServiceClient, id string) ActionResult {
500 var res ActionResult
501
502 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500503 ReqBody: map[string]interface{}{"revertResize": nil},
Ash Wilson01626a32014-09-17 10:38:07 -0400504 MoreHeaders: client.Provider.AuthenticatedHeaders(),
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200505 Results: &res.Resp,
Jon Perritt30558642014-04-14 17:07:12 -0500506 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700507 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200508
509 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700510}