blob: 81a4fcad5d5ac9ae54ab5ef082e946c07b4bba2b [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"
Jon Perrittcc77da62014-11-16 13:14:21 -07005 "errors"
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -07006 "fmt"
Ash Wilson01626a32014-09-17 10:38:07 -04007
Jon Perritt30558642014-04-14 17:07:12 -05008 "github.com/racker/perigee"
Ash Wilson01626a32014-09-17 10:38:07 -04009 "github.com/rackspace/gophercloud"
10 "github.com/rackspace/gophercloud/pagination"
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080011)
12
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020013// ListOptsBuilder allows extensions to add additional parameters to the
14// List request.
15type ListOptsBuilder interface {
16 ToServerListQuery() (string, error)
17}
18
19// ListOpts allows the filtering and sorting of paginated collections through
20// the API. Filtering is achieved by passing in struct field values that map to
21// the server attributes you want to see returned. Marker and Limit are used
22// for pagination.
23type ListOpts struct {
24 // A time/date stamp for when the server last changed status.
25 ChangesSince string `q:"changes-since"`
26
27 // Name of the image in URL format.
28 Image string `q:"image"`
29
30 // Name of the flavor in URL format.
31 Flavor string `q:"flavor"`
32
33 // Name of the server as a string; can be queried with regular expressions.
34 // Realize that ?name=bob returns both bob and bobb. If you need to match bob
35 // only, you can use a regular expression matching the syntax of the
36 // underlying database server implemented for Compute.
37 Name string `q:"name"`
38
39 // Value of the status of the server so that you can filter on "ACTIVE" for example.
40 Status string `q:"status"`
41
42 // Name of the host as a string.
43 Host string `q:"host"`
44
45 // UUID of the server at which you want to set a marker.
46 Marker string `q:"marker"`
47
48 // Integer value for the limit of values to return.
49 Limit int `q:"limit"`
50}
51
52// ToServerListQuery formats a ListOpts into a query string.
53func (opts ListOpts) ToServerListQuery() (string, error) {
54 q, err := gophercloud.BuildQueryString(opts)
55 if err != nil {
56 return "", err
57 }
58 return q.String(), nil
59}
60
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080061// List makes a request against the API to list servers accessible to you.
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020062func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
63 url := listDetailURL(client)
64
65 if opts != nil {
66 query, err := opts.ToServerListQuery()
67 if err != nil {
68 return pagination.Pager{Err: err}
69 }
70 url += query
71 }
72
Ash Wilsonb8b16f82014-10-20 10:19:49 -040073 createPageFn := func(r pagination.PageResult) pagination.Page {
74 return ServerPage{pagination.LinkedPageBase{PageResult: r}}
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080075 }
76
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020077 return pagination.NewPager(client, url, createPageFn)
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080078}
79
Ash Wilson2206a112014-10-02 10:57:38 -040080// CreateOptsBuilder describes struct types that can be accepted by the Create call.
Ash Wilson6a310e02014-09-29 08:24:02 -040081// The CreateOpts struct in this package does.
Ash Wilson2206a112014-10-02 10:57:38 -040082type CreateOptsBuilder interface {
Jon Perritt4149d7c2014-10-23 21:23:46 -050083 ToServerCreateMap() (map[string]interface{}, error)
Ash Wilson6a310e02014-09-29 08:24:02 -040084}
85
86// Network is used within CreateOpts to control a new server's network attachments.
87type Network struct {
88 // UUID of a nova-network to attach to the newly provisioned server.
89 // Required unless Port is provided.
90 UUID string
91
92 // Port of a neutron network to attach to the newly provisioned server.
93 // Required unless UUID is provided.
94 Port string
95
96 // FixedIP [optional] specifies a fixed IPv4 address to be used on this network.
97 FixedIP string
98}
99
100// CreateOpts specifies server creation parameters.
101type CreateOpts struct {
102 // Name [required] is the name to assign to the newly launched server.
103 Name string
104
105 // ImageRef [required] is the ID or full URL to the image that contains the server's OS and initial state.
106 // Optional if using the boot-from-volume extension.
107 ImageRef string
108
109 // FlavorRef [required] is the ID or full URL to the flavor that describes the server's specs.
110 FlavorRef string
111
112 // SecurityGroups [optional] lists the names of the security groups to which this server should belong.
113 SecurityGroups []string
114
115 // UserData [optional] contains configuration information or scripts to use upon launch.
116 // Create will base64-encode it for you.
117 UserData []byte
118
119 // AvailabilityZone [optional] in which to launch the server.
120 AvailabilityZone string
121
122 // Networks [optional] dictates how this server will be attached to available networks.
123 // By default, the server will be attached to all isolated networks for the tenant.
124 Networks []Network
125
126 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
127 Metadata map[string]string
128
129 // Personality [optional] includes the path and contents of a file to inject into the server at launch.
130 // The maximum size of the file is 255 bytes (decoded).
131 Personality []byte
132
133 // ConfigDrive [optional] enables metadata injection through a configuration drive.
134 ConfigDrive bool
Jon Perrittf3b2e142014-11-04 16:00:19 -0600135
136 // AdminPass [optional] sets the root user password. If not set, a randomly-generated
137 // password will be created and returned in the response.
138 AdminPass string
Ash Wilson6a310e02014-09-29 08:24:02 -0400139}
140
Ash Wilsone45c9732014-09-29 10:54:12 -0400141// ToServerCreateMap assembles a request body based on the contents of a CreateOpts.
Jon Perritt4149d7c2014-10-23 21:23:46 -0500142func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) {
Ash Wilson6a310e02014-09-29 08:24:02 -0400143 server := make(map[string]interface{})
144
145 server["name"] = opts.Name
146 server["imageRef"] = opts.ImageRef
147 server["flavorRef"] = opts.FlavorRef
148
149 if opts.UserData != nil {
150 encoded := base64.StdEncoding.EncodeToString(opts.UserData)
151 server["user_data"] = &encoded
152 }
153 if opts.Personality != nil {
154 encoded := base64.StdEncoding.EncodeToString(opts.Personality)
155 server["personality"] = &encoded
156 }
157 if opts.ConfigDrive {
158 server["config_drive"] = "true"
159 }
160 if opts.AvailabilityZone != "" {
161 server["availability_zone"] = opts.AvailabilityZone
162 }
163 if opts.Metadata != nil {
164 server["metadata"] = opts.Metadata
165 }
Jon Perrittf3b2e142014-11-04 16:00:19 -0600166 if opts.AdminPass != "" {
167 server["adminPass"] = opts.AdminPass
168 }
Ash Wilson6a310e02014-09-29 08:24:02 -0400169
170 if len(opts.SecurityGroups) > 0 {
171 securityGroups := make([]map[string]interface{}, len(opts.SecurityGroups))
172 for i, groupName := range opts.SecurityGroups {
173 securityGroups[i] = map[string]interface{}{"name": groupName}
174 }
eselldf709942014-11-13 21:07:11 -0700175 server["security_groups"] = securityGroups
Ash Wilson6a310e02014-09-29 08:24:02 -0400176 }
Jon Perritt2a7797d2014-10-21 15:08:43 -0500177
Ash Wilson6a310e02014-09-29 08:24:02 -0400178 if len(opts.Networks) > 0 {
179 networks := make([]map[string]interface{}, len(opts.Networks))
180 for i, net := range opts.Networks {
181 networks[i] = make(map[string]interface{})
182 if net.UUID != "" {
183 networks[i]["uuid"] = net.UUID
184 }
185 if net.Port != "" {
186 networks[i]["port"] = net.Port
187 }
188 if net.FixedIP != "" {
189 networks[i]["fixed_ip"] = net.FixedIP
190 }
191 }
Jon Perritt2a7797d2014-10-21 15:08:43 -0500192 server["networks"] = networks
Ash Wilson6a310e02014-09-29 08:24:02 -0400193 }
194
Jon Perritt4149d7c2014-10-23 21:23:46 -0500195 return map[string]interface{}{"server": server}, nil
Ash Wilson6a310e02014-09-29 08:24:02 -0400196}
197
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800198// Create requests a server to be provisioned to the user in the current tenant.
Ash Wilson2206a112014-10-02 10:57:38 -0400199func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jon Perritt4149d7c2014-10-23 21:23:46 -0500200 var res CreateResult
201
202 reqBody, err := opts.ToServerCreateMap()
203 if err != nil {
204 res.Err = err
205 return res
206 }
207
208 _, res.Err = perigee.Request("POST", listURL(client), perigee.Options{
209 Results: &res.Body,
210 ReqBody: reqBody,
Ash Wilson77857dc2014-10-22 09:09:02 -0400211 MoreHeaders: client.AuthenticatedHeaders(),
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -0800212 OkCodes: []int{202},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800213 })
Jon Perritt4149d7c2014-10-23 21:23:46 -0500214 return res
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800215}
216
217// Delete requests that a server previously provisioned be removed from your account.
Jamie Hannaford34732fe2014-10-27 11:29:36 +0100218func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
219 var res DeleteResult
220 _, res.Err = perigee.Request("DELETE", deleteURL(client, id), perigee.Options{
Ash Wilson77857dc2014-10-22 09:09:02 -0400221 MoreHeaders: client.AuthenticatedHeaders(),
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -0800222 OkCodes: []int{204},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800223 })
Jamie Hannaford34732fe2014-10-27 11:29:36 +0100224 return res
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800225}
226
Ash Wilson7ddf0362014-09-17 10:59:09 -0400227// Get requests details on a single server, by ID.
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400228func Get(client *gophercloud.ServiceClient, id string) GetResult {
229 var result GetResult
Jon Perritt703bfc02014-10-08 14:35:00 -0500230 _, result.Err = perigee.Request("GET", getURL(client, id), perigee.Options{
Ash Wilsond3dc2542014-10-20 10:10:48 -0400231 Results: &result.Body,
Ash Wilson77857dc2014-10-22 09:09:02 -0400232 MoreHeaders: client.AuthenticatedHeaders(),
Julien Veyd7f07fc2015-01-31 18:46:17 +0100233 OkCodes: []int{200, 203},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800234 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400235 return result
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800236}
237
Alex Gaynora6d5f9f2014-10-27 10:52:32 -0700238// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
Jon Perritt82048212014-10-13 22:33:13 -0500239type UpdateOptsBuilder interface {
Ash Wilsone45c9732014-09-29 10:54:12 -0400240 ToServerUpdateMap() map[string]interface{}
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400241}
242
243// UpdateOpts specifies the base attributes that may be updated on an existing server.
244type UpdateOpts struct {
245 // Name [optional] changes the displayed name of the server.
246 // The server host name will *not* change.
247 // Server names are not constrained to be unique, even within the same tenant.
248 Name string
249
250 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
251 AccessIPv4 string
252
253 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
254 AccessIPv6 string
255}
256
Ash Wilsone45c9732014-09-29 10:54:12 -0400257// ToServerUpdateMap formats an UpdateOpts structure into a request body.
258func (opts UpdateOpts) ToServerUpdateMap() map[string]interface{} {
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400259 server := make(map[string]string)
260 if opts.Name != "" {
261 server["name"] = opts.Name
262 }
263 if opts.AccessIPv4 != "" {
264 server["accessIPv4"] = opts.AccessIPv4
265 }
266 if opts.AccessIPv6 != "" {
267 server["accessIPv6"] = opts.AccessIPv6
268 }
269 return map[string]interface{}{"server": server}
270}
271
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800272// Update requests that various attributes of the indicated server be changed.
Jon Perritt82048212014-10-13 22:33:13 -0500273func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400274 var result UpdateResult
Jon Perritt703bfc02014-10-08 14:35:00 -0500275 _, result.Err = perigee.Request("PUT", updateURL(client, id), perigee.Options{
Ash Wilsond3dc2542014-10-20 10:10:48 -0400276 Results: &result.Body,
Ash Wilsone45c9732014-09-29 10:54:12 -0400277 ReqBody: opts.ToServerUpdateMap(),
Ash Wilson77857dc2014-10-22 09:09:02 -0400278 MoreHeaders: client.AuthenticatedHeaders(),
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800279 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400280 return result
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800281}
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700282
Ash Wilson01626a32014-09-17 10:38:07 -0400283// ChangeAdminPassword alters the administrator or root password for a specified server.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200284func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) ActionResult {
Ash Wilsondc7daa82014-09-23 16:34:42 -0400285 var req struct {
286 ChangePassword struct {
287 AdminPass string `json:"adminPass"`
288 } `json:"changePassword"`
289 }
290
291 req.ChangePassword.AdminPass = newPassword
292
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200293 var res ActionResult
294
295 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Ash Wilsondc7daa82014-09-23 16:34:42 -0400296 ReqBody: req,
Ash Wilson77857dc2014-10-22 09:09:02 -0400297 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500298 OkCodes: []int{202},
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700299 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200300
301 return res
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700302}
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700303
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700304// ErrArgument errors occur when an argument supplied to a package function
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700305// fails to fall within acceptable values. For example, the Reboot() function
306// expects the "how" parameter to be one of HardReboot or SoftReboot. These
307// constants are (currently) strings, leading someone to wonder if they can pass
308// other string values instead, perhaps in an effort to break the API of their
309// provider. Reboot() returns this error in this situation.
310//
311// Function identifies which function was called/which function is generating
312// the error.
313// Argument identifies which formal argument was responsible for producing the
314// error.
315// Value provides the value as it was passed into the function.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700316type ErrArgument struct {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700317 Function, Argument string
Jon Perritt30558642014-04-14 17:07:12 -0500318 Value interface{}
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700319}
320
321// Error yields a useful diagnostic for debugging purposes.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700322func (e *ErrArgument) Error() string {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700323 return fmt.Sprintf("Bad argument in call to %s, formal parameter %s, value %#v", e.Function, e.Argument, e.Value)
324}
325
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700326func (e *ErrArgument) String() string {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700327 return e.Error()
328}
329
Ash Wilson01626a32014-09-17 10:38:07 -0400330// RebootMethod describes the mechanisms by which a server reboot can be requested.
331type RebootMethod string
332
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700333// These constants determine how a server should be rebooted.
334// See the Reboot() function for further details.
335const (
Ash Wilson01626a32014-09-17 10:38:07 -0400336 SoftReboot RebootMethod = "SOFT"
337 HardReboot RebootMethod = "HARD"
338 OSReboot = SoftReboot
339 PowerCycle = HardReboot
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700340)
341
342// Reboot requests that a given server reboot.
343// Two methods exist for rebooting a server:
344//
Ash Wilson01626a32014-09-17 10:38:07 -0400345// HardReboot (aka PowerCycle) restarts the server instance by physically cutting power to the machine, or if a VM,
346// terminating it at the hypervisor level.
347// It's done. Caput. Full stop.
348// Then, after a brief while, power is restored or the VM instance restarted.
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700349//
Ash Wilson01626a32014-09-17 10:38:07 -0400350// SoftReboot (aka OSReboot) simply tells the OS to restart under its own procedures.
351// 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 +0200352func Reboot(client *gophercloud.ServiceClient, id string, how RebootMethod) ActionResult {
353 var res ActionResult
354
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700355 if (how != SoftReboot) && (how != HardReboot) {
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200356 res.Err = &ErrArgument{
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700357 Function: "Reboot",
358 Argument: "how",
Jon Perritt30558642014-04-14 17:07:12 -0500359 Value: how,
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700360 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200361 return res
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700362 }
Jon Perritt30558642014-04-14 17:07:12 -0500363
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200364 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500365 ReqBody: struct {
366 C map[string]string `json:"reboot"`
367 }{
Ash Wilson01626a32014-09-17 10:38:07 -0400368 map[string]string{"type": string(how)},
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700369 },
Ash Wilson77857dc2014-10-22 09:09:02 -0400370 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500371 OkCodes: []int{202},
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700372 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200373
374 return res
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700375}
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700376
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200377// RebuildOptsBuilder is an interface that allows extensions to override the
378// default behaviour of rebuild options
379type RebuildOptsBuilder interface {
380 ToServerRebuildMap() (map[string]interface{}, error)
381}
382
383// RebuildOpts represents the configuration options used in a server rebuild
384// operation
385type RebuildOpts struct {
386 // Required. The ID of the image you want your server to be provisioned on
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200387 ImageID string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200388
389 // Name to set the server to
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200390 Name string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200391
392 // Required. The server's admin password
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200393 AdminPass string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200394
395 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200396 AccessIPv4 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200397
398 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200399 AccessIPv6 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200400
401 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200402 Metadata map[string]string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200403
404 // Personality [optional] includes the path and contents of a file to inject into the server at launch.
405 // The maximum size of the file is 255 bytes (decoded).
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200406 Personality []byte
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200407}
408
409// ToServerRebuildMap formats a RebuildOpts struct into a map for use in JSON
410func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {
411 var err error
412 server := make(map[string]interface{})
413
414 if opts.AdminPass == "" {
415 err = fmt.Errorf("AdminPass is required")
416 }
417
418 if opts.ImageID == "" {
419 err = fmt.Errorf("ImageID is required")
420 }
421
422 if err != nil {
423 return server, err
424 }
425
426 server["name"] = opts.Name
427 server["adminPass"] = opts.AdminPass
428 server["imageRef"] = opts.ImageID
429
430 if opts.AccessIPv4 != "" {
431 server["accessIPv4"] = opts.AccessIPv4
432 }
433
434 if opts.AccessIPv6 != "" {
435 server["accessIPv6"] = opts.AccessIPv6
436 }
437
438 if opts.Metadata != nil {
439 server["metadata"] = opts.Metadata
440 }
441
442 if opts.Personality != nil {
443 encoded := base64.StdEncoding.EncodeToString(opts.Personality)
444 server["personality"] = &encoded
445 }
446
447 return map[string]interface{}{"rebuild": server}, nil
448}
449
450// Rebuild will reprovision the server according to the configuration options
451// provided in the RebuildOpts struct.
452func Rebuild(client *gophercloud.ServiceClient, id string, opts RebuildOptsBuilder) RebuildResult {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400453 var result RebuildResult
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700454
455 if id == "" {
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200456 result.Err = fmt.Errorf("ID is required")
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400457 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700458 }
459
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200460 reqBody, err := opts.ToServerRebuildMap()
461 if err != nil {
462 result.Err = err
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400463 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700464 }
465
Ash Wilson31f6bde2014-09-25 14:52:12 -0400466 _, result.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200467 ReqBody: &reqBody,
Ash Wilsond3dc2542014-10-20 10:10:48 -0400468 Results: &result.Body,
Ash Wilson77857dc2014-10-22 09:09:02 -0400469 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500470 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700471 })
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200472
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400473 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700474}
475
Ash Wilson5f7cf182014-10-23 08:35:24 -0400476// ResizeOptsBuilder is an interface that allows extensions to override the default structure of
477// a Resize request.
478type ResizeOptsBuilder interface {
479 ToServerResizeMap() (map[string]interface{}, error)
480}
481
482// ResizeOpts represents the configuration options used to control a Resize operation.
483type ResizeOpts struct {
484 // FlavorRef is the ID of the flavor you wish your server to become.
485 FlavorRef string
486}
487
Alex Gaynor266e9332014-10-28 14:44:04 -0700488// 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 -0400489// Resize request.
490func (opts ResizeOpts) ToServerResizeMap() (map[string]interface{}, error) {
491 resize := map[string]interface{}{
492 "flavorRef": opts.FlavorRef,
493 }
494
495 return map[string]interface{}{"resize": resize}, nil
496}
497
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700498// Resize instructs the provider to change the flavor of the server.
Ash Wilson01626a32014-09-17 10:38:07 -0400499// Note that this implies rebuilding it.
500// Unfortunately, one cannot pass rebuild parameters to the resize function.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700501// When the resize completes, the server will be in RESIZE_VERIFY state.
502// While in this state, you can explore the use of the new server's configuration.
503// If you like it, call ConfirmResize() to commit the resize permanently.
504// Otherwise, call RevertResize() to restore the old configuration.
Ash Wilson9e87a922014-10-23 14:29:22 -0400505func Resize(client *gophercloud.ServiceClient, id string, opts ResizeOptsBuilder) ActionResult {
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200506 var res ActionResult
Ash Wilson5f7cf182014-10-23 08:35:24 -0400507 reqBody, err := opts.ToServerResizeMap()
508 if err != nil {
509 res.Err = err
510 return res
511 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200512
513 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Ash Wilson5f7cf182014-10-23 08:35:24 -0400514 ReqBody: reqBody,
Ash Wilson77857dc2014-10-22 09:09:02 -0400515 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500516 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700517 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200518
519 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700520}
521
522// ConfirmResize confirms a previous resize operation on a server.
523// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200524func ConfirmResize(client *gophercloud.ServiceClient, id string) ActionResult {
525 var res ActionResult
526
527 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500528 ReqBody: map[string]interface{}{"confirmResize": nil},
Ash Wilson77857dc2014-10-22 09:09:02 -0400529 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500530 OkCodes: []int{204},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700531 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200532
533 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700534}
535
536// RevertResize cancels a previous resize operation on a server.
537// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200538func RevertResize(client *gophercloud.ServiceClient, id string) ActionResult {
539 var res ActionResult
540
541 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500542 ReqBody: map[string]interface{}{"revertResize": nil},
Ash Wilson77857dc2014-10-22 09:09:02 -0400543 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500544 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700545 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200546
547 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700548}
Alex Gaynor39584a02014-10-28 13:59:21 -0700549
Alex Gaynor266e9332014-10-28 14:44:04 -0700550// RescueOptsBuilder is an interface that allows extensions to override the
551// default structure of a Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700552type RescueOptsBuilder interface {
553 ToServerRescueMap() (map[string]interface{}, error)
554}
555
Alex Gaynor266e9332014-10-28 14:44:04 -0700556// RescueOpts represents the configuration options used to control a Rescue
557// option.
Alex Gaynor39584a02014-10-28 13:59:21 -0700558type RescueOpts struct {
Alex Gaynor266e9332014-10-28 14:44:04 -0700559 // AdminPass is the desired administrative password for the instance in
Alex Gaynorcfec7722014-11-13 13:33:49 -0800560 // RESCUE mode. If it's left blank, the server will generate a password.
Alex Gaynor39584a02014-10-28 13:59:21 -0700561 AdminPass string
562}
563
Jon Perrittcc77da62014-11-16 13:14:21 -0700564// ToServerRescueMap formats a RescueOpts as a map that can be used as a JSON
Alex Gaynor266e9332014-10-28 14:44:04 -0700565// request body for the Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700566func (opts RescueOpts) ToServerRescueMap() (map[string]interface{}, error) {
567 server := make(map[string]interface{})
568 if opts.AdminPass != "" {
569 server["adminPass"] = opts.AdminPass
570 }
571 return map[string]interface{}{"rescue": server}, nil
572}
573
Alex Gaynor266e9332014-10-28 14:44:04 -0700574// Rescue instructs the provider to place the server into RESCUE mode.
Alex Gaynorfbe61bb2014-11-12 13:35:03 -0800575func Rescue(client *gophercloud.ServiceClient, id string, opts RescueOptsBuilder) RescueResult {
576 var result RescueResult
Alex Gaynor39584a02014-10-28 13:59:21 -0700577
578 if id == "" {
579 result.Err = fmt.Errorf("ID is required")
580 return result
581 }
582 reqBody, err := opts.ToServerRescueMap()
583 if err != nil {
584 result.Err = err
585 return result
586 }
587
588 _, result.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Alex Gaynorfbe61bb2014-11-12 13:35:03 -0800589 Results: &result.Body,
Alex Gaynor39584a02014-10-28 13:59:21 -0700590 ReqBody: &reqBody,
Alex Gaynor39584a02014-10-28 13:59:21 -0700591 MoreHeaders: client.AuthenticatedHeaders(),
592 OkCodes: []int{200},
593 })
594
595 return result
596}
Jon Perrittcc77da62014-11-16 13:14:21 -0700597
Jon Perritt789f8322014-11-21 08:20:04 -0700598// ResetMetadataOptsBuilder allows extensions to add additional parameters to the
599// Reset request.
600type ResetMetadataOptsBuilder interface {
601 ToMetadataResetMap() (map[string]interface{}, error)
Jon Perrittcc77da62014-11-16 13:14:21 -0700602}
603
Jon Perritt78c57ce2014-11-20 11:07:18 -0700604// MetadataOpts is a map that contains key-value pairs.
605type MetadataOpts map[string]string
Jon Perrittcc77da62014-11-16 13:14:21 -0700606
Jon Perritt789f8322014-11-21 08:20:04 -0700607// ToMetadataResetMap assembles a body for a Reset request based on the contents of a MetadataOpts.
608func (opts MetadataOpts) ToMetadataResetMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700609 return map[string]interface{}{"metadata": opts}, nil
610}
611
Jon Perritt78c57ce2014-11-20 11:07:18 -0700612// ToMetadataUpdateMap assembles a body for an Update request based on the contents of a MetadataOpts.
613func (opts MetadataOpts) ToMetadataUpdateMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700614 return map[string]interface{}{"metadata": opts}, nil
615}
616
Jon Perritt789f8322014-11-21 08:20:04 -0700617// ResetMetadata will create multiple new key-value pairs for the given server ID.
Jon Perrittcc77da62014-11-16 13:14:21 -0700618// Note: Using this operation will erase any already-existing metadata and create
619// the new metadata provided. To keep any already-existing metadata, use the
620// UpdateMetadatas or UpdateMetadata function.
Jon Perritt789f8322014-11-21 08:20:04 -0700621func ResetMetadata(client *gophercloud.ServiceClient, id string, opts ResetMetadataOptsBuilder) ResetMetadataResult {
622 var res ResetMetadataResult
623 metadata, err := opts.ToMetadataResetMap()
Jon Perrittcc77da62014-11-16 13:14:21 -0700624 if err != nil {
625 res.Err = err
626 return res
627 }
Jon Perritt78c57ce2014-11-20 11:07:18 -0700628 _, res.Err = perigee.Request("PUT", metadataURL(client, id), perigee.Options{
Jon Perrittcc77da62014-11-16 13:14:21 -0700629 ReqBody: metadata,
630 Results: &res.Body,
631 MoreHeaders: client.AuthenticatedHeaders(),
632 })
633 return res
634}
635
Jon Perritt78c57ce2014-11-20 11:07:18 -0700636// Metadata requests all the metadata for the given server ID.
637func Metadata(client *gophercloud.ServiceClient, id string) GetMetadataResult {
Jon Perrittcc77da62014-11-16 13:14:21 -0700638 var res GetMetadataResult
Jon Perritt78c57ce2014-11-20 11:07:18 -0700639 _, res.Err = perigee.Request("GET", metadataURL(client, id), perigee.Options{
Jon Perrittcc77da62014-11-16 13:14:21 -0700640 Results: &res.Body,
641 MoreHeaders: client.AuthenticatedHeaders(),
642 })
643 return res
644}
645
Jon Perritt78c57ce2014-11-20 11:07:18 -0700646// UpdateMetadataOptsBuilder allows extensions to add additional parameters to the
647// Create request.
648type UpdateMetadataOptsBuilder interface {
649 ToMetadataUpdateMap() (map[string]interface{}, error)
650}
651
652// UpdateMetadata updates (or creates) all the metadata specified by opts for the given server ID.
653// This operation does not affect already-existing metadata that is not specified
654// by opts.
655func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) UpdateMetadataResult {
656 var res UpdateMetadataResult
657 metadata, err := opts.ToMetadataUpdateMap()
658 if err != nil {
659 res.Err = err
660 return res
661 }
662 _, res.Err = perigee.Request("POST", metadataURL(client, id), perigee.Options{
663 ReqBody: metadata,
664 Results: &res.Body,
665 MoreHeaders: client.AuthenticatedHeaders(),
666 })
667 return res
668}
669
670// MetadatumOptsBuilder allows extensions to add additional parameters to the
671// Create request.
672type MetadatumOptsBuilder interface {
673 ToMetadatumCreateMap() (map[string]interface{}, string, error)
674}
675
676// MetadatumOpts is a map of length one that contains a key-value pair.
677type MetadatumOpts map[string]string
678
679// ToMetadatumCreateMap assembles a body for a Create request based on the contents of a MetadataumOpts.
680func (opts MetadatumOpts) ToMetadatumCreateMap() (map[string]interface{}, string, error) {
681 if len(opts) != 1 {
682 return nil, "", errors.New("CreateMetadatum operation must have 1 and only 1 key-value pair.")
683 }
684 metadatum := map[string]interface{}{"meta": opts}
685 var key string
686 for k := range metadatum["meta"].(MetadatumOpts) {
687 key = k
688 }
689 return metadatum, key, nil
690}
691
692// CreateMetadatum will create or update the key-value pair with the given key for the given server ID.
693func CreateMetadatum(client *gophercloud.ServiceClient, id string, opts MetadatumOptsBuilder) CreateMetadatumResult {
694 var res CreateMetadatumResult
695 metadatum, key, err := opts.ToMetadatumCreateMap()
696 if err != nil {
697 res.Err = err
698 return res
699 }
700
701 _, res.Err = perigee.Request("PUT", metadatumURL(client, id, key), perigee.Options{
702 ReqBody: metadatum,
703 Results: &res.Body,
704 MoreHeaders: client.AuthenticatedHeaders(),
705 })
706 return res
707}
708
709// Metadatum requests the key-value pair with the given key for the given server ID.
710func Metadatum(client *gophercloud.ServiceClient, id, key string) GetMetadatumResult {
711 var res GetMetadatumResult
712 _, res.Err = perigee.Request("GET", metadatumURL(client, id, key), perigee.Options{
713 Results: &res.Body,
714 MoreHeaders: client.AuthenticatedHeaders(),
715 })
716 return res
717}
718
719// DeleteMetadatum will delete the key-value pair with the given key for the given server ID.
720func DeleteMetadatum(client *gophercloud.ServiceClient, id, key string) DeleteMetadatumResult {
721 var res DeleteMetadatumResult
722 _, res.Err = perigee.Request("DELETE", metadatumURL(client, id, key), perigee.Options{
Jon Perrittcc77da62014-11-16 13:14:21 -0700723 Results: &res.Body,
724 MoreHeaders: client.AuthenticatedHeaders(),
725 })
726 return res
727}