blob: 52983914088854ff25eceb5c0974bb3a43b0bcc9 [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(),
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800233 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400234 return result
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800235}
236
Alex Gaynora6d5f9f2014-10-27 10:52:32 -0700237// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
Jon Perritt82048212014-10-13 22:33:13 -0500238type UpdateOptsBuilder interface {
Ash Wilsone45c9732014-09-29 10:54:12 -0400239 ToServerUpdateMap() map[string]interface{}
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400240}
241
242// UpdateOpts specifies the base attributes that may be updated on an existing server.
243type UpdateOpts struct {
244 // Name [optional] changes the displayed name of the server.
245 // The server host name will *not* change.
246 // Server names are not constrained to be unique, even within the same tenant.
247 Name string
248
249 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
250 AccessIPv4 string
251
252 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
253 AccessIPv6 string
254}
255
Ash Wilsone45c9732014-09-29 10:54:12 -0400256// ToServerUpdateMap formats an UpdateOpts structure into a request body.
257func (opts UpdateOpts) ToServerUpdateMap() map[string]interface{} {
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400258 server := make(map[string]string)
259 if opts.Name != "" {
260 server["name"] = opts.Name
261 }
262 if opts.AccessIPv4 != "" {
263 server["accessIPv4"] = opts.AccessIPv4
264 }
265 if opts.AccessIPv6 != "" {
266 server["accessIPv6"] = opts.AccessIPv6
267 }
268 return map[string]interface{}{"server": server}
269}
270
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800271// Update requests that various attributes of the indicated server be changed.
Jon Perritt82048212014-10-13 22:33:13 -0500272func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400273 var result UpdateResult
Jon Perritt703bfc02014-10-08 14:35:00 -0500274 _, result.Err = perigee.Request("PUT", updateURL(client, id), perigee.Options{
Ash Wilsond3dc2542014-10-20 10:10:48 -0400275 Results: &result.Body,
Ash Wilsone45c9732014-09-29 10:54:12 -0400276 ReqBody: opts.ToServerUpdateMap(),
Ash Wilson77857dc2014-10-22 09:09:02 -0400277 MoreHeaders: client.AuthenticatedHeaders(),
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800278 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400279 return result
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800280}
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700281
Ash Wilson01626a32014-09-17 10:38:07 -0400282// ChangeAdminPassword alters the administrator or root password for a specified server.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200283func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) ActionResult {
Ash Wilsondc7daa82014-09-23 16:34:42 -0400284 var req struct {
285 ChangePassword struct {
286 AdminPass string `json:"adminPass"`
287 } `json:"changePassword"`
288 }
289
290 req.ChangePassword.AdminPass = newPassword
291
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200292 var res ActionResult
293
294 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Ash Wilsondc7daa82014-09-23 16:34:42 -0400295 ReqBody: req,
Ash Wilson77857dc2014-10-22 09:09:02 -0400296 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500297 OkCodes: []int{202},
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700298 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200299
300 return res
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700301}
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700302
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700303// ErrArgument errors occur when an argument supplied to a package function
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700304// fails to fall within acceptable values. For example, the Reboot() function
305// expects the "how" parameter to be one of HardReboot or SoftReboot. These
306// constants are (currently) strings, leading someone to wonder if they can pass
307// other string values instead, perhaps in an effort to break the API of their
308// provider. Reboot() returns this error in this situation.
309//
310// Function identifies which function was called/which function is generating
311// the error.
312// Argument identifies which formal argument was responsible for producing the
313// error.
314// Value provides the value as it was passed into the function.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700315type ErrArgument struct {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700316 Function, Argument string
Jon Perritt30558642014-04-14 17:07:12 -0500317 Value interface{}
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700318}
319
320// Error yields a useful diagnostic for debugging purposes.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700321func (e *ErrArgument) Error() string {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700322 return fmt.Sprintf("Bad argument in call to %s, formal parameter %s, value %#v", e.Function, e.Argument, e.Value)
323}
324
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700325func (e *ErrArgument) String() string {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700326 return e.Error()
327}
328
Ash Wilson01626a32014-09-17 10:38:07 -0400329// RebootMethod describes the mechanisms by which a server reboot can be requested.
330type RebootMethod string
331
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700332// These constants determine how a server should be rebooted.
333// See the Reboot() function for further details.
334const (
Ash Wilson01626a32014-09-17 10:38:07 -0400335 SoftReboot RebootMethod = "SOFT"
336 HardReboot RebootMethod = "HARD"
337 OSReboot = SoftReboot
338 PowerCycle = HardReboot
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700339)
340
341// Reboot requests that a given server reboot.
342// Two methods exist for rebooting a server:
343//
Ash Wilson01626a32014-09-17 10:38:07 -0400344// HardReboot (aka PowerCycle) restarts the server instance by physically cutting power to the machine, or if a VM,
345// terminating it at the hypervisor level.
346// It's done. Caput. Full stop.
347// Then, after a brief while, power is restored or the VM instance restarted.
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700348//
Ash Wilson01626a32014-09-17 10:38:07 -0400349// SoftReboot (aka OSReboot) simply tells the OS to restart under its own procedures.
350// 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 +0200351func Reboot(client *gophercloud.ServiceClient, id string, how RebootMethod) ActionResult {
352 var res ActionResult
353
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700354 if (how != SoftReboot) && (how != HardReboot) {
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200355 res.Err = &ErrArgument{
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700356 Function: "Reboot",
357 Argument: "how",
Jon Perritt30558642014-04-14 17:07:12 -0500358 Value: how,
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700359 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200360 return res
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700361 }
Jon Perritt30558642014-04-14 17:07:12 -0500362
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200363 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500364 ReqBody: struct {
365 C map[string]string `json:"reboot"`
366 }{
Ash Wilson01626a32014-09-17 10:38:07 -0400367 map[string]string{"type": string(how)},
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700368 },
Ash Wilson77857dc2014-10-22 09:09:02 -0400369 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500370 OkCodes: []int{202},
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700371 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200372
373 return res
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700374}
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700375
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200376// RebuildOptsBuilder is an interface that allows extensions to override the
377// default behaviour of rebuild options
378type RebuildOptsBuilder interface {
379 ToServerRebuildMap() (map[string]interface{}, error)
380}
381
382// RebuildOpts represents the configuration options used in a server rebuild
383// operation
384type RebuildOpts struct {
385 // Required. The ID of the image you want your server to be provisioned on
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200386 ImageID string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200387
388 // Name to set the server to
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200389 Name string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200390
391 // Required. The server's admin password
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200392 AdminPass string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200393
394 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200395 AccessIPv4 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200396
397 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200398 AccessIPv6 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200399
400 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200401 Metadata map[string]string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200402
403 // Personality [optional] includes the path and contents of a file to inject into the server at launch.
404 // The maximum size of the file is 255 bytes (decoded).
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200405 Personality []byte
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200406}
407
408// ToServerRebuildMap formats a RebuildOpts struct into a map for use in JSON
409func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {
410 var err error
411 server := make(map[string]interface{})
412
413 if opts.AdminPass == "" {
414 err = fmt.Errorf("AdminPass is required")
415 }
416
417 if opts.ImageID == "" {
418 err = fmt.Errorf("ImageID is required")
419 }
420
421 if err != nil {
422 return server, err
423 }
424
425 server["name"] = opts.Name
426 server["adminPass"] = opts.AdminPass
427 server["imageRef"] = opts.ImageID
428
429 if opts.AccessIPv4 != "" {
430 server["accessIPv4"] = opts.AccessIPv4
431 }
432
433 if opts.AccessIPv6 != "" {
434 server["accessIPv6"] = opts.AccessIPv6
435 }
436
437 if opts.Metadata != nil {
438 server["metadata"] = opts.Metadata
439 }
440
441 if opts.Personality != nil {
442 encoded := base64.StdEncoding.EncodeToString(opts.Personality)
443 server["personality"] = &encoded
444 }
445
446 return map[string]interface{}{"rebuild": server}, nil
447}
448
449// Rebuild will reprovision the server according to the configuration options
450// provided in the RebuildOpts struct.
451func Rebuild(client *gophercloud.ServiceClient, id string, opts RebuildOptsBuilder) RebuildResult {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400452 var result RebuildResult
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700453
454 if id == "" {
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200455 result.Err = fmt.Errorf("ID is required")
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400456 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700457 }
458
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200459 reqBody, err := opts.ToServerRebuildMap()
460 if err != nil {
461 result.Err = err
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400462 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700463 }
464
Ash Wilson31f6bde2014-09-25 14:52:12 -0400465 _, result.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200466 ReqBody: &reqBody,
Ash Wilsond3dc2542014-10-20 10:10:48 -0400467 Results: &result.Body,
Ash Wilson77857dc2014-10-22 09:09:02 -0400468 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500469 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700470 })
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200471
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400472 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700473}
474
Ash Wilson5f7cf182014-10-23 08:35:24 -0400475// ResizeOptsBuilder is an interface that allows extensions to override the default structure of
476// a Resize request.
477type ResizeOptsBuilder interface {
478 ToServerResizeMap() (map[string]interface{}, error)
479}
480
481// ResizeOpts represents the configuration options used to control a Resize operation.
482type ResizeOpts struct {
483 // FlavorRef is the ID of the flavor you wish your server to become.
484 FlavorRef string
485}
486
Alex Gaynor266e9332014-10-28 14:44:04 -0700487// 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 -0400488// Resize request.
489func (opts ResizeOpts) ToServerResizeMap() (map[string]interface{}, error) {
490 resize := map[string]interface{}{
491 "flavorRef": opts.FlavorRef,
492 }
493
494 return map[string]interface{}{"resize": resize}, nil
495}
496
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700497// Resize instructs the provider to change the flavor of the server.
Ash Wilson01626a32014-09-17 10:38:07 -0400498// Note that this implies rebuilding it.
499// Unfortunately, one cannot pass rebuild parameters to the resize function.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700500// When the resize completes, the server will be in RESIZE_VERIFY state.
501// While in this state, you can explore the use of the new server's configuration.
502// If you like it, call ConfirmResize() to commit the resize permanently.
503// Otherwise, call RevertResize() to restore the old configuration.
Ash Wilson9e87a922014-10-23 14:29:22 -0400504func Resize(client *gophercloud.ServiceClient, id string, opts ResizeOptsBuilder) ActionResult {
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200505 var res ActionResult
Ash Wilson5f7cf182014-10-23 08:35:24 -0400506 reqBody, err := opts.ToServerResizeMap()
507 if err != nil {
508 res.Err = err
509 return res
510 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200511
512 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Ash Wilson5f7cf182014-10-23 08:35:24 -0400513 ReqBody: reqBody,
Ash Wilson77857dc2014-10-22 09:09:02 -0400514 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500515 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700516 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200517
518 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700519}
520
521// ConfirmResize confirms a previous resize operation on a server.
522// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200523func ConfirmResize(client *gophercloud.ServiceClient, id string) ActionResult {
524 var res ActionResult
525
526 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500527 ReqBody: map[string]interface{}{"confirmResize": nil},
Ash Wilson77857dc2014-10-22 09:09:02 -0400528 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500529 OkCodes: []int{204},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700530 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200531
532 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700533}
534
535// RevertResize cancels a previous resize operation on a server.
536// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200537func RevertResize(client *gophercloud.ServiceClient, id string) ActionResult {
538 var res ActionResult
539
540 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500541 ReqBody: map[string]interface{}{"revertResize": nil},
Ash Wilson77857dc2014-10-22 09:09:02 -0400542 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500543 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700544 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200545
546 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700547}
Alex Gaynor39584a02014-10-28 13:59:21 -0700548
Alex Gaynor266e9332014-10-28 14:44:04 -0700549// RescueOptsBuilder is an interface that allows extensions to override the
550// default structure of a Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700551type RescueOptsBuilder interface {
552 ToServerRescueMap() (map[string]interface{}, error)
553}
554
Alex Gaynor266e9332014-10-28 14:44:04 -0700555// RescueOpts represents the configuration options used to control a Rescue
556// option.
Alex Gaynor39584a02014-10-28 13:59:21 -0700557type RescueOpts struct {
Alex Gaynor266e9332014-10-28 14:44:04 -0700558 // AdminPass is the desired administrative password for the instance in
Alex Gaynorcfec7722014-11-13 13:33:49 -0800559 // RESCUE mode. If it's left blank, the server will generate a password.
Alex Gaynor39584a02014-10-28 13:59:21 -0700560 AdminPass string
561}
562
Jon Perrittcc77da62014-11-16 13:14:21 -0700563// ToServerRescueMap formats a RescueOpts as a map that can be used as a JSON
Alex Gaynor266e9332014-10-28 14:44:04 -0700564// request body for the Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700565func (opts RescueOpts) ToServerRescueMap() (map[string]interface{}, error) {
566 server := make(map[string]interface{})
567 if opts.AdminPass != "" {
568 server["adminPass"] = opts.AdminPass
569 }
570 return map[string]interface{}{"rescue": server}, nil
571}
572
Alex Gaynor266e9332014-10-28 14:44:04 -0700573// Rescue instructs the provider to place the server into RESCUE mode.
Alex Gaynorfbe61bb2014-11-12 13:35:03 -0800574func Rescue(client *gophercloud.ServiceClient, id string, opts RescueOptsBuilder) RescueResult {
575 var result RescueResult
Alex Gaynor39584a02014-10-28 13:59:21 -0700576
577 if id == "" {
578 result.Err = fmt.Errorf("ID is required")
579 return result
580 }
581 reqBody, err := opts.ToServerRescueMap()
582 if err != nil {
583 result.Err = err
584 return result
585 }
586
587 _, result.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Alex Gaynorfbe61bb2014-11-12 13:35:03 -0800588 Results: &result.Body,
Alex Gaynor39584a02014-10-28 13:59:21 -0700589 ReqBody: &reqBody,
Alex Gaynor39584a02014-10-28 13:59:21 -0700590 MoreHeaders: client.AuthenticatedHeaders(),
591 OkCodes: []int{200},
592 })
593
594 return result
595}
Jon Perrittcc77da62014-11-16 13:14:21 -0700596
Jon Perritt78c57ce2014-11-20 11:07:18 -0700597// CreateMetadataOptsBuilder allows extensions to add additional parameters to the
Jon Perrittcc77da62014-11-16 13:14:21 -0700598// Create request.
Jon Perritt78c57ce2014-11-20 11:07:18 -0700599type CreateMetadataOptsBuilder interface {
600 ToMetadataCreateMap() (map[string]interface{}, error)
Jon Perrittcc77da62014-11-16 13:14:21 -0700601}
602
Jon Perritt78c57ce2014-11-20 11:07:18 -0700603// MetadataOpts is a map that contains key-value pairs.
604type MetadataOpts map[string]string
Jon Perrittcc77da62014-11-16 13:14:21 -0700605
Jon Perritt78c57ce2014-11-20 11:07:18 -0700606// ToMetadataCreateMap assembles a body for a Create request based on the contents of a MetadataOpts.
607func (opts MetadataOpts) ToMetadataCreateMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700608 return map[string]interface{}{"metadata": opts}, nil
609}
610
Jon Perritt78c57ce2014-11-20 11:07:18 -0700611// ToMetadataUpdateMap assembles a body for an Update request based on the contents of a MetadataOpts.
612func (opts MetadataOpts) ToMetadataUpdateMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700613 return map[string]interface{}{"metadata": opts}, nil
614}
615
Jon Perritt78c57ce2014-11-20 11:07:18 -0700616// CreateMetadata will create multiple new key-value pairs for the given server ID.
Jon Perrittcc77da62014-11-16 13:14:21 -0700617// Note: Using this operation will erase any already-existing metadata and create
618// the new metadata provided. To keep any already-existing metadata, use the
619// UpdateMetadatas or UpdateMetadata function.
Jon Perritt78c57ce2014-11-20 11:07:18 -0700620func CreateMetadata(client *gophercloud.ServiceClient, id string, opts CreateMetadataOptsBuilder) CreateMetadataResult {
Jon Perrittcc77da62014-11-16 13:14:21 -0700621 var res CreateMetadataResult
Jon Perritt78c57ce2014-11-20 11:07:18 -0700622 metadata, err := opts.ToMetadataCreateMap()
Jon Perrittcc77da62014-11-16 13:14:21 -0700623 if err != nil {
624 res.Err = err
625 return res
626 }
Jon Perritt78c57ce2014-11-20 11:07:18 -0700627 _, res.Err = perigee.Request("PUT", metadataURL(client, id), perigee.Options{
Jon Perrittcc77da62014-11-16 13:14:21 -0700628 ReqBody: metadata,
629 Results: &res.Body,
630 MoreHeaders: client.AuthenticatedHeaders(),
631 })
632 return res
633}
634
Jon Perritt78c57ce2014-11-20 11:07:18 -0700635// Metadata requests all the metadata for the given server ID.
636func Metadata(client *gophercloud.ServiceClient, id string) GetMetadataResult {
Jon Perrittcc77da62014-11-16 13:14:21 -0700637 var res GetMetadataResult
Jon Perritt78c57ce2014-11-20 11:07:18 -0700638 _, res.Err = perigee.Request("GET", metadataURL(client, id), perigee.Options{
Jon Perrittcc77da62014-11-16 13:14:21 -0700639 Results: &res.Body,
640 MoreHeaders: client.AuthenticatedHeaders(),
641 })
642 return res
643}
644
Jon Perritt78c57ce2014-11-20 11:07:18 -0700645// UpdateMetadataOptsBuilder allows extensions to add additional parameters to the
646// Create request.
647type UpdateMetadataOptsBuilder interface {
648 ToMetadataUpdateMap() (map[string]interface{}, error)
649}
650
651// UpdateMetadata updates (or creates) all the metadata specified by opts for the given server ID.
652// This operation does not affect already-existing metadata that is not specified
653// by opts.
654func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) UpdateMetadataResult {
655 var res UpdateMetadataResult
656 metadata, err := opts.ToMetadataUpdateMap()
657 if err != nil {
658 res.Err = err
659 return res
660 }
661 _, res.Err = perigee.Request("POST", metadataURL(client, id), perigee.Options{
662 ReqBody: metadata,
663 Results: &res.Body,
664 MoreHeaders: client.AuthenticatedHeaders(),
665 })
666 return res
667}
668
669// MetadatumOptsBuilder allows extensions to add additional parameters to the
670// Create request.
671type MetadatumOptsBuilder interface {
672 ToMetadatumCreateMap() (map[string]interface{}, string, error)
673}
674
675// MetadatumOpts is a map of length one that contains a key-value pair.
676type MetadatumOpts map[string]string
677
678// ToMetadatumCreateMap assembles a body for a Create request based on the contents of a MetadataumOpts.
679func (opts MetadatumOpts) ToMetadatumCreateMap() (map[string]interface{}, string, error) {
680 if len(opts) != 1 {
681 return nil, "", errors.New("CreateMetadatum operation must have 1 and only 1 key-value pair.")
682 }
683 metadatum := map[string]interface{}{"meta": opts}
684 var key string
685 for k := range metadatum["meta"].(MetadatumOpts) {
686 key = k
687 }
688 return metadatum, key, nil
689}
690
691// CreateMetadatum will create or update the key-value pair with the given key for the given server ID.
692func CreateMetadatum(client *gophercloud.ServiceClient, id string, opts MetadatumOptsBuilder) CreateMetadatumResult {
693 var res CreateMetadatumResult
694 metadatum, key, err := opts.ToMetadatumCreateMap()
695 if err != nil {
696 res.Err = err
697 return res
698 }
699
700 _, res.Err = perigee.Request("PUT", metadatumURL(client, id, key), perigee.Options{
701 ReqBody: metadatum,
702 Results: &res.Body,
703 MoreHeaders: client.AuthenticatedHeaders(),
704 })
705 return res
706}
707
708// Metadatum requests the key-value pair with the given key for the given server ID.
709func Metadatum(client *gophercloud.ServiceClient, id, key string) GetMetadatumResult {
710 var res GetMetadatumResult
711 _, res.Err = perigee.Request("GET", metadatumURL(client, id, key), perigee.Options{
712 Results: &res.Body,
713 MoreHeaders: client.AuthenticatedHeaders(),
714 })
715 return res
716}
717
718// DeleteMetadatum will delete the key-value pair with the given key for the given server ID.
719func DeleteMetadatum(client *gophercloud.ServiceClient, id, key string) DeleteMetadatumResult {
720 var res DeleteMetadatumResult
721 _, res.Err = perigee.Request("DELETE", metadatumURL(client, id, key), perigee.Options{
Jon Perrittcc77da62014-11-16 13:14:21 -0700722 Results: &res.Body,
723 MoreHeaders: client.AuthenticatedHeaders(),
724 })
725 return res
726}