blob: be8ab73b75b58dc9466fbd3a25749b7e82144c5f [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
Jon Perritt7b9671c2015-02-01 22:03:14 -0700139
140 // AccessIPv4 [optional] specifies an IPv4 address for the instance.
141 AccessIPv4 string
142
143 // AccessIPv6 [optional] specifies an IPv6 address for the instance.
144 AccessIPv6 string
Ash Wilson6a310e02014-09-29 08:24:02 -0400145}
146
Ash Wilsone45c9732014-09-29 10:54:12 -0400147// ToServerCreateMap assembles a request body based on the contents of a CreateOpts.
Jon Perritt4149d7c2014-10-23 21:23:46 -0500148func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) {
Ash Wilson6a310e02014-09-29 08:24:02 -0400149 server := make(map[string]interface{})
150
151 server["name"] = opts.Name
152 server["imageRef"] = opts.ImageRef
153 server["flavorRef"] = opts.FlavorRef
154
155 if opts.UserData != nil {
156 encoded := base64.StdEncoding.EncodeToString(opts.UserData)
157 server["user_data"] = &encoded
158 }
159 if opts.Personality != nil {
160 encoded := base64.StdEncoding.EncodeToString(opts.Personality)
161 server["personality"] = &encoded
162 }
163 if opts.ConfigDrive {
164 server["config_drive"] = "true"
165 }
166 if opts.AvailabilityZone != "" {
167 server["availability_zone"] = opts.AvailabilityZone
168 }
169 if opts.Metadata != nil {
170 server["metadata"] = opts.Metadata
171 }
Jon Perrittf3b2e142014-11-04 16:00:19 -0600172 if opts.AdminPass != "" {
173 server["adminPass"] = opts.AdminPass
174 }
Jon Perritt7b9671c2015-02-01 22:03:14 -0700175 if opts.AccessIPv4 != "" {
176 server["accessIPv4"] = opts.AccessIPv4
177 }
178 if opts.AccessIPv6 != "" {
179 server["accessIPv6"] = opts.AccessIPv6
180 }
Ash Wilson6a310e02014-09-29 08:24:02 -0400181
182 if len(opts.SecurityGroups) > 0 {
183 securityGroups := make([]map[string]interface{}, len(opts.SecurityGroups))
184 for i, groupName := range opts.SecurityGroups {
185 securityGroups[i] = map[string]interface{}{"name": groupName}
186 }
eselldf709942014-11-13 21:07:11 -0700187 server["security_groups"] = securityGroups
Ash Wilson6a310e02014-09-29 08:24:02 -0400188 }
Jon Perritt2a7797d2014-10-21 15:08:43 -0500189
Ash Wilson6a310e02014-09-29 08:24:02 -0400190 if len(opts.Networks) > 0 {
191 networks := make([]map[string]interface{}, len(opts.Networks))
192 for i, net := range opts.Networks {
193 networks[i] = make(map[string]interface{})
194 if net.UUID != "" {
195 networks[i]["uuid"] = net.UUID
196 }
197 if net.Port != "" {
198 networks[i]["port"] = net.Port
199 }
200 if net.FixedIP != "" {
201 networks[i]["fixed_ip"] = net.FixedIP
202 }
203 }
Jon Perritt2a7797d2014-10-21 15:08:43 -0500204 server["networks"] = networks
Ash Wilson6a310e02014-09-29 08:24:02 -0400205 }
206
Jon Perritt4149d7c2014-10-23 21:23:46 -0500207 return map[string]interface{}{"server": server}, nil
Ash Wilson6a310e02014-09-29 08:24:02 -0400208}
209
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800210// Create requests a server to be provisioned to the user in the current tenant.
Ash Wilson2206a112014-10-02 10:57:38 -0400211func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jon Perritt4149d7c2014-10-23 21:23:46 -0500212 var res CreateResult
213
214 reqBody, err := opts.ToServerCreateMap()
215 if err != nil {
216 res.Err = err
217 return res
218 }
219
220 _, res.Err = perigee.Request("POST", listURL(client), perigee.Options{
221 Results: &res.Body,
222 ReqBody: reqBody,
Ash Wilson77857dc2014-10-22 09:09:02 -0400223 MoreHeaders: client.AuthenticatedHeaders(),
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -0800224 OkCodes: []int{202},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800225 })
Jon Perritt4149d7c2014-10-23 21:23:46 -0500226 return res
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800227}
228
229// Delete requests that a server previously provisioned be removed from your account.
Jamie Hannaford34732fe2014-10-27 11:29:36 +0100230func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
231 var res DeleteResult
232 _, res.Err = perigee.Request("DELETE", deleteURL(client, id), perigee.Options{
Ash Wilson77857dc2014-10-22 09:09:02 -0400233 MoreHeaders: client.AuthenticatedHeaders(),
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -0800234 OkCodes: []int{204},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800235 })
Jamie Hannaford34732fe2014-10-27 11:29:36 +0100236 return res
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800237}
238
Ash Wilson7ddf0362014-09-17 10:59:09 -0400239// Get requests details on a single server, by ID.
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400240func Get(client *gophercloud.ServiceClient, id string) GetResult {
241 var result GetResult
Jon Perritt703bfc02014-10-08 14:35:00 -0500242 _, result.Err = perigee.Request("GET", getURL(client, id), perigee.Options{
Ash Wilsond3dc2542014-10-20 10:10:48 -0400243 Results: &result.Body,
Ash Wilson77857dc2014-10-22 09:09:02 -0400244 MoreHeaders: client.AuthenticatedHeaders(),
Julien Veyd7f07fc2015-01-31 18:46:17 +0100245 OkCodes: []int{200, 203},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800246 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400247 return result
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800248}
249
Alex Gaynora6d5f9f2014-10-27 10:52:32 -0700250// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
Jon Perritt82048212014-10-13 22:33:13 -0500251type UpdateOptsBuilder interface {
Ash Wilsone45c9732014-09-29 10:54:12 -0400252 ToServerUpdateMap() map[string]interface{}
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400253}
254
255// UpdateOpts specifies the base attributes that may be updated on an existing server.
256type UpdateOpts struct {
257 // Name [optional] changes the displayed name of the server.
258 // The server host name will *not* change.
259 // Server names are not constrained to be unique, even within the same tenant.
260 Name string
261
262 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
263 AccessIPv4 string
264
265 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
266 AccessIPv6 string
267}
268
Ash Wilsone45c9732014-09-29 10:54:12 -0400269// ToServerUpdateMap formats an UpdateOpts structure into a request body.
270func (opts UpdateOpts) ToServerUpdateMap() map[string]interface{} {
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400271 server := make(map[string]string)
272 if opts.Name != "" {
273 server["name"] = opts.Name
274 }
275 if opts.AccessIPv4 != "" {
276 server["accessIPv4"] = opts.AccessIPv4
277 }
278 if opts.AccessIPv6 != "" {
279 server["accessIPv6"] = opts.AccessIPv6
280 }
281 return map[string]interface{}{"server": server}
282}
283
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800284// Update requests that various attributes of the indicated server be changed.
Jon Perritt82048212014-10-13 22:33:13 -0500285func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400286 var result UpdateResult
Jon Perritt703bfc02014-10-08 14:35:00 -0500287 _, result.Err = perigee.Request("PUT", updateURL(client, id), perigee.Options{
Ash Wilsond3dc2542014-10-20 10:10:48 -0400288 Results: &result.Body,
Ash Wilsone45c9732014-09-29 10:54:12 -0400289 ReqBody: opts.ToServerUpdateMap(),
Ash Wilson77857dc2014-10-22 09:09:02 -0400290 MoreHeaders: client.AuthenticatedHeaders(),
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800291 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400292 return result
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800293}
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700294
Ash Wilson01626a32014-09-17 10:38:07 -0400295// ChangeAdminPassword alters the administrator or root password for a specified server.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200296func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) ActionResult {
Ash Wilsondc7daa82014-09-23 16:34:42 -0400297 var req struct {
298 ChangePassword struct {
299 AdminPass string `json:"adminPass"`
300 } `json:"changePassword"`
301 }
302
303 req.ChangePassword.AdminPass = newPassword
304
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200305 var res ActionResult
306
307 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Ash Wilsondc7daa82014-09-23 16:34:42 -0400308 ReqBody: req,
Ash Wilson77857dc2014-10-22 09:09:02 -0400309 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500310 OkCodes: []int{202},
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700311 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200312
313 return res
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700314}
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700315
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700316// ErrArgument errors occur when an argument supplied to a package function
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700317// fails to fall within acceptable values. For example, the Reboot() function
318// expects the "how" parameter to be one of HardReboot or SoftReboot. These
319// constants are (currently) strings, leading someone to wonder if they can pass
320// other string values instead, perhaps in an effort to break the API of their
321// provider. Reboot() returns this error in this situation.
322//
323// Function identifies which function was called/which function is generating
324// the error.
325// Argument identifies which formal argument was responsible for producing the
326// error.
327// Value provides the value as it was passed into the function.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700328type ErrArgument struct {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700329 Function, Argument string
Jon Perritt30558642014-04-14 17:07:12 -0500330 Value interface{}
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700331}
332
333// Error yields a useful diagnostic for debugging purposes.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700334func (e *ErrArgument) Error() string {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700335 return fmt.Sprintf("Bad argument in call to %s, formal parameter %s, value %#v", e.Function, e.Argument, e.Value)
336}
337
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700338func (e *ErrArgument) String() string {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700339 return e.Error()
340}
341
Ash Wilson01626a32014-09-17 10:38:07 -0400342// RebootMethod describes the mechanisms by which a server reboot can be requested.
343type RebootMethod string
344
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700345// These constants determine how a server should be rebooted.
346// See the Reboot() function for further details.
347const (
Ash Wilson01626a32014-09-17 10:38:07 -0400348 SoftReboot RebootMethod = "SOFT"
349 HardReboot RebootMethod = "HARD"
350 OSReboot = SoftReboot
351 PowerCycle = HardReboot
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700352)
353
354// Reboot requests that a given server reboot.
355// Two methods exist for rebooting a server:
356//
Ash Wilson01626a32014-09-17 10:38:07 -0400357// HardReboot (aka PowerCycle) restarts the server instance by physically cutting power to the machine, or if a VM,
358// terminating it at the hypervisor level.
359// It's done. Caput. Full stop.
360// Then, after a brief while, power is restored or the VM instance restarted.
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700361//
Ash Wilson01626a32014-09-17 10:38:07 -0400362// SoftReboot (aka OSReboot) simply tells the OS to restart under its own procedures.
363// 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 +0200364func Reboot(client *gophercloud.ServiceClient, id string, how RebootMethod) ActionResult {
365 var res ActionResult
366
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700367 if (how != SoftReboot) && (how != HardReboot) {
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200368 res.Err = &ErrArgument{
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700369 Function: "Reboot",
370 Argument: "how",
Jon Perritt30558642014-04-14 17:07:12 -0500371 Value: how,
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700372 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200373 return res
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700374 }
Jon Perritt30558642014-04-14 17:07:12 -0500375
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200376 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500377 ReqBody: struct {
378 C map[string]string `json:"reboot"`
379 }{
Ash Wilson01626a32014-09-17 10:38:07 -0400380 map[string]string{"type": string(how)},
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700381 },
Ash Wilson77857dc2014-10-22 09:09:02 -0400382 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500383 OkCodes: []int{202},
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700384 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200385
386 return res
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700387}
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700388
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200389// RebuildOptsBuilder is an interface that allows extensions to override the
390// default behaviour of rebuild options
391type RebuildOptsBuilder interface {
392 ToServerRebuildMap() (map[string]interface{}, error)
393}
394
395// RebuildOpts represents the configuration options used in a server rebuild
396// operation
397type RebuildOpts struct {
398 // Required. The ID of the image you want your server to be provisioned on
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200399 ImageID string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200400
401 // Name to set the server to
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200402 Name string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200403
404 // Required. The server's admin password
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200405 AdminPass string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200406
407 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200408 AccessIPv4 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200409
410 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200411 AccessIPv6 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200412
413 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200414 Metadata map[string]string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200415
416 // Personality [optional] includes the path and contents of a file to inject into the server at launch.
417 // The maximum size of the file is 255 bytes (decoded).
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200418 Personality []byte
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200419}
420
421// ToServerRebuildMap formats a RebuildOpts struct into a map for use in JSON
422func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {
423 var err error
424 server := make(map[string]interface{})
425
426 if opts.AdminPass == "" {
427 err = fmt.Errorf("AdminPass is required")
428 }
429
430 if opts.ImageID == "" {
431 err = fmt.Errorf("ImageID is required")
432 }
433
434 if err != nil {
435 return server, err
436 }
437
438 server["name"] = opts.Name
439 server["adminPass"] = opts.AdminPass
440 server["imageRef"] = opts.ImageID
441
442 if opts.AccessIPv4 != "" {
443 server["accessIPv4"] = opts.AccessIPv4
444 }
445
446 if opts.AccessIPv6 != "" {
447 server["accessIPv6"] = opts.AccessIPv6
448 }
449
450 if opts.Metadata != nil {
451 server["metadata"] = opts.Metadata
452 }
453
454 if opts.Personality != nil {
455 encoded := base64.StdEncoding.EncodeToString(opts.Personality)
456 server["personality"] = &encoded
457 }
458
459 return map[string]interface{}{"rebuild": server}, nil
460}
461
462// Rebuild will reprovision the server according to the configuration options
463// provided in the RebuildOpts struct.
464func Rebuild(client *gophercloud.ServiceClient, id string, opts RebuildOptsBuilder) RebuildResult {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400465 var result RebuildResult
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700466
467 if id == "" {
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200468 result.Err = fmt.Errorf("ID is required")
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400469 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700470 }
471
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200472 reqBody, err := opts.ToServerRebuildMap()
473 if err != nil {
474 result.Err = err
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400475 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700476 }
477
Ash Wilson31f6bde2014-09-25 14:52:12 -0400478 _, result.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200479 ReqBody: &reqBody,
Ash Wilsond3dc2542014-10-20 10:10:48 -0400480 Results: &result.Body,
Ash Wilson77857dc2014-10-22 09:09:02 -0400481 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500482 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700483 })
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200484
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400485 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700486}
487
Ash Wilson5f7cf182014-10-23 08:35:24 -0400488// ResizeOptsBuilder is an interface that allows extensions to override the default structure of
489// a Resize request.
490type ResizeOptsBuilder interface {
491 ToServerResizeMap() (map[string]interface{}, error)
492}
493
494// ResizeOpts represents the configuration options used to control a Resize operation.
495type ResizeOpts struct {
496 // FlavorRef is the ID of the flavor you wish your server to become.
497 FlavorRef string
498}
499
Alex Gaynor266e9332014-10-28 14:44:04 -0700500// 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 -0400501// Resize request.
502func (opts ResizeOpts) ToServerResizeMap() (map[string]interface{}, error) {
503 resize := map[string]interface{}{
504 "flavorRef": opts.FlavorRef,
505 }
506
507 return map[string]interface{}{"resize": resize}, nil
508}
509
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700510// Resize instructs the provider to change the flavor of the server.
Ash Wilson01626a32014-09-17 10:38:07 -0400511// Note that this implies rebuilding it.
512// Unfortunately, one cannot pass rebuild parameters to the resize function.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700513// When the resize completes, the server will be in RESIZE_VERIFY state.
514// While in this state, you can explore the use of the new server's configuration.
515// If you like it, call ConfirmResize() to commit the resize permanently.
516// Otherwise, call RevertResize() to restore the old configuration.
Ash Wilson9e87a922014-10-23 14:29:22 -0400517func Resize(client *gophercloud.ServiceClient, id string, opts ResizeOptsBuilder) ActionResult {
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200518 var res ActionResult
Ash Wilson5f7cf182014-10-23 08:35:24 -0400519 reqBody, err := opts.ToServerResizeMap()
520 if err != nil {
521 res.Err = err
522 return res
523 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200524
525 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Ash Wilson5f7cf182014-10-23 08:35:24 -0400526 ReqBody: reqBody,
Ash Wilson77857dc2014-10-22 09:09:02 -0400527 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500528 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700529 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200530
531 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700532}
533
534// ConfirmResize confirms a previous resize operation on a server.
535// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200536func ConfirmResize(client *gophercloud.ServiceClient, id string) ActionResult {
537 var res ActionResult
538
539 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500540 ReqBody: map[string]interface{}{"confirmResize": nil},
Ash Wilson77857dc2014-10-22 09:09:02 -0400541 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500542 OkCodes: []int{204},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700543 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200544
545 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700546}
547
548// RevertResize cancels a previous resize operation on a server.
549// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200550func RevertResize(client *gophercloud.ServiceClient, id string) ActionResult {
551 var res ActionResult
552
553 _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Jon Perritt30558642014-04-14 17:07:12 -0500554 ReqBody: map[string]interface{}{"revertResize": nil},
Ash Wilson77857dc2014-10-22 09:09:02 -0400555 MoreHeaders: client.AuthenticatedHeaders(),
Jon Perritt30558642014-04-14 17:07:12 -0500556 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700557 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200558
559 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700560}
Alex Gaynor39584a02014-10-28 13:59:21 -0700561
Alex Gaynor266e9332014-10-28 14:44:04 -0700562// RescueOptsBuilder is an interface that allows extensions to override the
563// default structure of a Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700564type RescueOptsBuilder interface {
565 ToServerRescueMap() (map[string]interface{}, error)
566}
567
Alex Gaynor266e9332014-10-28 14:44:04 -0700568// RescueOpts represents the configuration options used to control a Rescue
569// option.
Alex Gaynor39584a02014-10-28 13:59:21 -0700570type RescueOpts struct {
Alex Gaynor266e9332014-10-28 14:44:04 -0700571 // AdminPass is the desired administrative password for the instance in
Alex Gaynorcfec7722014-11-13 13:33:49 -0800572 // RESCUE mode. If it's left blank, the server will generate a password.
Alex Gaynor39584a02014-10-28 13:59:21 -0700573 AdminPass string
574}
575
Jon Perrittcc77da62014-11-16 13:14:21 -0700576// ToServerRescueMap formats a RescueOpts as a map that can be used as a JSON
Alex Gaynor266e9332014-10-28 14:44:04 -0700577// request body for the Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700578func (opts RescueOpts) ToServerRescueMap() (map[string]interface{}, error) {
579 server := make(map[string]interface{})
580 if opts.AdminPass != "" {
581 server["adminPass"] = opts.AdminPass
582 }
583 return map[string]interface{}{"rescue": server}, nil
584}
585
Alex Gaynor266e9332014-10-28 14:44:04 -0700586// Rescue instructs the provider to place the server into RESCUE mode.
Alex Gaynorfbe61bb2014-11-12 13:35:03 -0800587func Rescue(client *gophercloud.ServiceClient, id string, opts RescueOptsBuilder) RescueResult {
588 var result RescueResult
Alex Gaynor39584a02014-10-28 13:59:21 -0700589
590 if id == "" {
591 result.Err = fmt.Errorf("ID is required")
592 return result
593 }
594 reqBody, err := opts.ToServerRescueMap()
595 if err != nil {
596 result.Err = err
597 return result
598 }
599
600 _, result.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{
Alex Gaynorfbe61bb2014-11-12 13:35:03 -0800601 Results: &result.Body,
Alex Gaynor39584a02014-10-28 13:59:21 -0700602 ReqBody: &reqBody,
Alex Gaynor39584a02014-10-28 13:59:21 -0700603 MoreHeaders: client.AuthenticatedHeaders(),
604 OkCodes: []int{200},
605 })
606
607 return result
608}
Jon Perrittcc77da62014-11-16 13:14:21 -0700609
Jon Perritt789f8322014-11-21 08:20:04 -0700610// ResetMetadataOptsBuilder allows extensions to add additional parameters to the
611// Reset request.
612type ResetMetadataOptsBuilder interface {
613 ToMetadataResetMap() (map[string]interface{}, error)
Jon Perrittcc77da62014-11-16 13:14:21 -0700614}
615
Jon Perritt78c57ce2014-11-20 11:07:18 -0700616// MetadataOpts is a map that contains key-value pairs.
617type MetadataOpts map[string]string
Jon Perrittcc77da62014-11-16 13:14:21 -0700618
Jon Perritt789f8322014-11-21 08:20:04 -0700619// ToMetadataResetMap assembles a body for a Reset request based on the contents of a MetadataOpts.
620func (opts MetadataOpts) ToMetadataResetMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700621 return map[string]interface{}{"metadata": opts}, nil
622}
623
Jon Perritt78c57ce2014-11-20 11:07:18 -0700624// ToMetadataUpdateMap assembles a body for an Update request based on the contents of a MetadataOpts.
625func (opts MetadataOpts) ToMetadataUpdateMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700626 return map[string]interface{}{"metadata": opts}, nil
627}
628
Jon Perritt789f8322014-11-21 08:20:04 -0700629// ResetMetadata will create multiple new key-value pairs for the given server ID.
Jon Perrittcc77da62014-11-16 13:14:21 -0700630// Note: Using this operation will erase any already-existing metadata and create
631// the new metadata provided. To keep any already-existing metadata, use the
632// UpdateMetadatas or UpdateMetadata function.
Jon Perritt789f8322014-11-21 08:20:04 -0700633func ResetMetadata(client *gophercloud.ServiceClient, id string, opts ResetMetadataOptsBuilder) ResetMetadataResult {
634 var res ResetMetadataResult
635 metadata, err := opts.ToMetadataResetMap()
Jon Perrittcc77da62014-11-16 13:14:21 -0700636 if err != nil {
637 res.Err = err
638 return res
639 }
Jon Perritt78c57ce2014-11-20 11:07:18 -0700640 _, res.Err = perigee.Request("PUT", metadataURL(client, id), perigee.Options{
Jon Perrittcc77da62014-11-16 13:14:21 -0700641 ReqBody: metadata,
642 Results: &res.Body,
643 MoreHeaders: client.AuthenticatedHeaders(),
644 })
645 return res
646}
647
Jon Perritt78c57ce2014-11-20 11:07:18 -0700648// Metadata requests all the metadata for the given server ID.
649func Metadata(client *gophercloud.ServiceClient, id string) GetMetadataResult {
Jon Perrittcc77da62014-11-16 13:14:21 -0700650 var res GetMetadataResult
Jon Perritt78c57ce2014-11-20 11:07:18 -0700651 _, res.Err = perigee.Request("GET", metadataURL(client, id), perigee.Options{
Jon Perrittcc77da62014-11-16 13:14:21 -0700652 Results: &res.Body,
653 MoreHeaders: client.AuthenticatedHeaders(),
654 })
655 return res
656}
657
Jon Perritt78c57ce2014-11-20 11:07:18 -0700658// UpdateMetadataOptsBuilder allows extensions to add additional parameters to the
659// Create request.
660type UpdateMetadataOptsBuilder interface {
661 ToMetadataUpdateMap() (map[string]interface{}, error)
662}
663
664// UpdateMetadata updates (or creates) all the metadata specified by opts for the given server ID.
665// This operation does not affect already-existing metadata that is not specified
666// by opts.
667func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) UpdateMetadataResult {
668 var res UpdateMetadataResult
669 metadata, err := opts.ToMetadataUpdateMap()
670 if err != nil {
671 res.Err = err
672 return res
673 }
674 _, res.Err = perigee.Request("POST", metadataURL(client, id), perigee.Options{
675 ReqBody: metadata,
676 Results: &res.Body,
677 MoreHeaders: client.AuthenticatedHeaders(),
678 })
679 return res
680}
681
682// MetadatumOptsBuilder allows extensions to add additional parameters to the
683// Create request.
684type MetadatumOptsBuilder interface {
685 ToMetadatumCreateMap() (map[string]interface{}, string, error)
686}
687
688// MetadatumOpts is a map of length one that contains a key-value pair.
689type MetadatumOpts map[string]string
690
691// ToMetadatumCreateMap assembles a body for a Create request based on the contents of a MetadataumOpts.
692func (opts MetadatumOpts) ToMetadatumCreateMap() (map[string]interface{}, string, error) {
693 if len(opts) != 1 {
694 return nil, "", errors.New("CreateMetadatum operation must have 1 and only 1 key-value pair.")
695 }
696 metadatum := map[string]interface{}{"meta": opts}
697 var key string
698 for k := range metadatum["meta"].(MetadatumOpts) {
699 key = k
700 }
701 return metadatum, key, nil
702}
703
704// CreateMetadatum will create or update the key-value pair with the given key for the given server ID.
705func CreateMetadatum(client *gophercloud.ServiceClient, id string, opts MetadatumOptsBuilder) CreateMetadatumResult {
706 var res CreateMetadatumResult
707 metadatum, key, err := opts.ToMetadatumCreateMap()
708 if err != nil {
709 res.Err = err
710 return res
711 }
712
713 _, res.Err = perigee.Request("PUT", metadatumURL(client, id, key), perigee.Options{
714 ReqBody: metadatum,
715 Results: &res.Body,
716 MoreHeaders: client.AuthenticatedHeaders(),
717 })
718 return res
719}
720
721// Metadatum requests the key-value pair with the given key for the given server ID.
722func Metadatum(client *gophercloud.ServiceClient, id, key string) GetMetadatumResult {
723 var res GetMetadatumResult
724 _, res.Err = perigee.Request("GET", metadatumURL(client, id, key), perigee.Options{
725 Results: &res.Body,
726 MoreHeaders: client.AuthenticatedHeaders(),
727 })
728 return res
729}
730
731// DeleteMetadatum will delete the key-value pair with the given key for the given server ID.
732func DeleteMetadatum(client *gophercloud.ServiceClient, id, key string) DeleteMetadatumResult {
733 var res DeleteMetadatumResult
734 _, res.Err = perigee.Request("DELETE", metadatumURL(client, id, key), perigee.Options{
Jon Perrittcc77da62014-11-16 13:14:21 -0700735 Results: &res.Body,
736 MoreHeaders: client.AuthenticatedHeaders(),
737 })
738 return res
739}