blob: f8c2dc306ef1489885cbdc8822a66a1098ec2cdc [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
Ash Wilson01626a32014-09-17 10:38:07 -04008 "github.com/rackspace/gophercloud"
9 "github.com/rackspace/gophercloud/pagination"
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080010)
11
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020012// ListOptsBuilder allows extensions to add additional parameters to the
13// List request.
14type ListOptsBuilder interface {
15 ToServerListQuery() (string, error)
16}
17
18// ListOpts allows the filtering and sorting of paginated collections through
19// the API. Filtering is achieved by passing in struct field values that map to
20// the server attributes you want to see returned. Marker and Limit are used
21// for pagination.
22type ListOpts struct {
23 // A time/date stamp for when the server last changed status.
24 ChangesSince string `q:"changes-since"`
25
26 // Name of the image in URL format.
27 Image string `q:"image"`
28
29 // Name of the flavor in URL format.
30 Flavor string `q:"flavor"`
31
32 // Name of the server as a string; can be queried with regular expressions.
33 // Realize that ?name=bob returns both bob and bobb. If you need to match bob
34 // only, you can use a regular expression matching the syntax of the
35 // underlying database server implemented for Compute.
36 Name string `q:"name"`
37
38 // Value of the status of the server so that you can filter on "ACTIVE" for example.
39 Status string `q:"status"`
40
41 // Name of the host as a string.
42 Host string `q:"host"`
43
44 // UUID of the server at which you want to set a marker.
45 Marker string `q:"marker"`
46
47 // Integer value for the limit of values to return.
48 Limit int `q:"limit"`
49}
50
51// ToServerListQuery formats a ListOpts into a query string.
52func (opts ListOpts) ToServerListQuery() (string, error) {
53 q, err := gophercloud.BuildQueryString(opts)
54 if err != nil {
55 return "", err
56 }
57 return q.String(), nil
58}
59
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080060// List makes a request against the API to list servers accessible to you.
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020061func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
62 url := listDetailURL(client)
63
64 if opts != nil {
65 query, err := opts.ToServerListQuery()
66 if err != nil {
67 return pagination.Pager{Err: err}
68 }
69 url += query
70 }
71
Ash Wilsonb8b16f82014-10-20 10:19:49 -040072 createPageFn := func(r pagination.PageResult) pagination.Page {
73 return ServerPage{pagination.LinkedPageBase{PageResult: r}}
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080074 }
75
Jon Perrittd27a9c72015-02-18 11:33:28 -070076 p := pagination.NewPager(client, url, createPageFn)
77 p.PageType = ServerPage{}
78 return p
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080079}
80
Ash Wilson2206a112014-10-02 10:57:38 -040081// CreateOptsBuilder describes struct types that can be accepted by the Create call.
Ash Wilson6a310e02014-09-29 08:24:02 -040082// The CreateOpts struct in this package does.
Ash Wilson2206a112014-10-02 10:57:38 -040083type CreateOptsBuilder interface {
Jon Perritt4149d7c2014-10-23 21:23:46 -050084 ToServerCreateMap() (map[string]interface{}, error)
Ash Wilson6a310e02014-09-29 08:24:02 -040085}
86
87// Network is used within CreateOpts to control a new server's network attachments.
88type Network struct {
89 // UUID of a nova-network to attach to the newly provisioned server.
90 // Required unless Port is provided.
91 UUID string
92
93 // Port of a neutron network to attach to the newly provisioned server.
94 // Required unless UUID is provided.
95 Port string
96
97 // FixedIP [optional] specifies a fixed IPv4 address to be used on this network.
98 FixedIP string
99}
100
101// CreateOpts specifies server creation parameters.
102type CreateOpts struct {
103 // Name [required] is the name to assign to the newly launched server.
104 Name string
105
106 // ImageRef [required] is the ID or full URL to the image that contains the server's OS and initial state.
107 // Optional if using the boot-from-volume extension.
108 ImageRef string
109
110 // FlavorRef [required] is the ID or full URL to the flavor that describes the server's specs.
111 FlavorRef string
112
113 // SecurityGroups [optional] lists the names of the security groups to which this server should belong.
114 SecurityGroups []string
115
116 // UserData [optional] contains configuration information or scripts to use upon launch.
117 // Create will base64-encode it for you.
118 UserData []byte
119
120 // AvailabilityZone [optional] in which to launch the server.
121 AvailabilityZone string
122
123 // Networks [optional] dictates how this server will be attached to available networks.
124 // By default, the server will be attached to all isolated networks for the tenant.
125 Networks []Network
126
127 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
128 Metadata map[string]string
129
130 // Personality [optional] includes the path and contents of a file to inject into the server at launch.
131 // The maximum size of the file is 255 bytes (decoded).
132 Personality []byte
133
134 // ConfigDrive [optional] enables metadata injection through a configuration drive.
135 ConfigDrive bool
Jon Perrittf3b2e142014-11-04 16:00:19 -0600136
137 // AdminPass [optional] sets the root user password. If not set, a randomly-generated
138 // password will be created and returned in the response.
139 AdminPass string
Jon Perritt7b9671c2015-02-01 22:03:14 -0700140
141 // AccessIPv4 [optional] specifies an IPv4 address for the instance.
142 AccessIPv4 string
143
144 // AccessIPv6 [optional] specifies an IPv6 address for the instance.
145 AccessIPv6 string
Ash Wilson6a310e02014-09-29 08:24:02 -0400146}
147
Ash Wilsone45c9732014-09-29 10:54:12 -0400148// ToServerCreateMap assembles a request body based on the contents of a CreateOpts.
Jon Perritt4149d7c2014-10-23 21:23:46 -0500149func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) {
Ash Wilson6a310e02014-09-29 08:24:02 -0400150 server := make(map[string]interface{})
151
152 server["name"] = opts.Name
153 server["imageRef"] = opts.ImageRef
154 server["flavorRef"] = opts.FlavorRef
155
156 if opts.UserData != nil {
157 encoded := base64.StdEncoding.EncodeToString(opts.UserData)
158 server["user_data"] = &encoded
159 }
160 if opts.Personality != nil {
161 encoded := base64.StdEncoding.EncodeToString(opts.Personality)
162 server["personality"] = &encoded
163 }
164 if opts.ConfigDrive {
165 server["config_drive"] = "true"
166 }
167 if opts.AvailabilityZone != "" {
168 server["availability_zone"] = opts.AvailabilityZone
169 }
170 if opts.Metadata != nil {
171 server["metadata"] = opts.Metadata
172 }
Jon Perrittf3b2e142014-11-04 16:00:19 -0600173 if opts.AdminPass != "" {
174 server["adminPass"] = opts.AdminPass
175 }
Jon Perritt7b9671c2015-02-01 22:03:14 -0700176 if opts.AccessIPv4 != "" {
177 server["accessIPv4"] = opts.AccessIPv4
178 }
179 if opts.AccessIPv6 != "" {
180 server["accessIPv6"] = opts.AccessIPv6
181 }
Ash Wilson6a310e02014-09-29 08:24:02 -0400182
183 if len(opts.SecurityGroups) > 0 {
184 securityGroups := make([]map[string]interface{}, len(opts.SecurityGroups))
185 for i, groupName := range opts.SecurityGroups {
186 securityGroups[i] = map[string]interface{}{"name": groupName}
187 }
eselldf709942014-11-13 21:07:11 -0700188 server["security_groups"] = securityGroups
Ash Wilson6a310e02014-09-29 08:24:02 -0400189 }
Jon Perritt2a7797d2014-10-21 15:08:43 -0500190
Ash Wilson6a310e02014-09-29 08:24:02 -0400191 if len(opts.Networks) > 0 {
192 networks := make([]map[string]interface{}, len(opts.Networks))
193 for i, net := range opts.Networks {
194 networks[i] = make(map[string]interface{})
195 if net.UUID != "" {
196 networks[i]["uuid"] = net.UUID
197 }
198 if net.Port != "" {
199 networks[i]["port"] = net.Port
200 }
201 if net.FixedIP != "" {
202 networks[i]["fixed_ip"] = net.FixedIP
203 }
204 }
Jon Perritt2a7797d2014-10-21 15:08:43 -0500205 server["networks"] = networks
Ash Wilson6a310e02014-09-29 08:24:02 -0400206 }
207
Jon Perritt4149d7c2014-10-23 21:23:46 -0500208 return map[string]interface{}{"server": server}, nil
Ash Wilson6a310e02014-09-29 08:24:02 -0400209}
210
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800211// Create requests a server to be provisioned to the user in the current tenant.
Ash Wilson2206a112014-10-02 10:57:38 -0400212func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jon Perritt4149d7c2014-10-23 21:23:46 -0500213 var res CreateResult
214
215 reqBody, err := opts.ToServerCreateMap()
216 if err != nil {
217 res.Err = err
218 return res
219 }
220
Ash Wilson4bf41a32015-02-12 15:52:44 -0500221 _, res.Err = client.Request("POST", listURL(client), gophercloud.RequestOpts{
222 JSONResponse: &res.Body,
223 JSONBody: reqBody,
224 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
Ash Wilson59fb6c42015-02-12 16:21:13 -0500232 _, res.Err = client.Request("DELETE", deleteURL(client, id), gophercloud.RequestOpts{
233 OkCodes: []int{204},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800234 })
Jamie Hannaford34732fe2014-10-27 11:29:36 +0100235 return res
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800236}
237
Ash Wilson7ddf0362014-09-17 10:59:09 -0400238// Get requests details on a single server, by ID.
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400239func Get(client *gophercloud.ServiceClient, id string) GetResult {
240 var result GetResult
Ash Wilson59fb6c42015-02-12 16:21:13 -0500241 _, result.Err = client.Request("GET", getURL(client, id), gophercloud.RequestOpts{
242 JSONResponse: &result.Body,
243 OkCodes: []int{200, 203},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800244 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400245 return result
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800246}
247
Alex Gaynora6d5f9f2014-10-27 10:52:32 -0700248// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
Jon Perritt82048212014-10-13 22:33:13 -0500249type UpdateOptsBuilder interface {
Ash Wilsone45c9732014-09-29 10:54:12 -0400250 ToServerUpdateMap() map[string]interface{}
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400251}
252
253// UpdateOpts specifies the base attributes that may be updated on an existing server.
254type UpdateOpts struct {
255 // Name [optional] changes the displayed name of the server.
256 // The server host name will *not* change.
257 // Server names are not constrained to be unique, even within the same tenant.
258 Name string
259
260 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
261 AccessIPv4 string
262
263 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
264 AccessIPv6 string
265}
266
Ash Wilsone45c9732014-09-29 10:54:12 -0400267// ToServerUpdateMap formats an UpdateOpts structure into a request body.
268func (opts UpdateOpts) ToServerUpdateMap() map[string]interface{} {
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400269 server := make(map[string]string)
270 if opts.Name != "" {
271 server["name"] = opts.Name
272 }
273 if opts.AccessIPv4 != "" {
274 server["accessIPv4"] = opts.AccessIPv4
275 }
276 if opts.AccessIPv6 != "" {
277 server["accessIPv6"] = opts.AccessIPv6
278 }
279 return map[string]interface{}{"server": server}
280}
281
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800282// Update requests that various attributes of the indicated server be changed.
Jon Perritt82048212014-10-13 22:33:13 -0500283func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400284 var result UpdateResult
Ash Wilson59fb6c42015-02-12 16:21:13 -0500285 _, result.Err = client.Request("PUT", updateURL(client, id), gophercloud.RequestOpts{
286 JSONResponse: &result.Body,
287 JSONBody: opts.ToServerUpdateMap(),
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800288 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400289 return result
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800290}
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700291
Ash Wilson01626a32014-09-17 10:38:07 -0400292// ChangeAdminPassword alters the administrator or root password for a specified server.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200293func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) ActionResult {
Ash Wilsondc7daa82014-09-23 16:34:42 -0400294 var req struct {
295 ChangePassword struct {
296 AdminPass string `json:"adminPass"`
297 } `json:"changePassword"`
298 }
299
300 req.ChangePassword.AdminPass = newPassword
301
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200302 var res ActionResult
303
Ash Wilson59fb6c42015-02-12 16:21:13 -0500304 _, res.Err = client.Request("POST", actionURL(client, id), gophercloud.RequestOpts{
305 JSONBody: req,
306 OkCodes: []int{202},
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700307 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200308
309 return res
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700310}
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700311
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700312// ErrArgument errors occur when an argument supplied to a package function
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700313// fails to fall within acceptable values. For example, the Reboot() function
314// expects the "how" parameter to be one of HardReboot or SoftReboot. These
315// constants are (currently) strings, leading someone to wonder if they can pass
316// other string values instead, perhaps in an effort to break the API of their
317// provider. Reboot() returns this error in this situation.
318//
319// Function identifies which function was called/which function is generating
320// the error.
321// Argument identifies which formal argument was responsible for producing the
322// error.
323// Value provides the value as it was passed into the function.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700324type ErrArgument struct {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700325 Function, Argument string
Jon Perritt30558642014-04-14 17:07:12 -0500326 Value interface{}
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700327}
328
329// Error yields a useful diagnostic for debugging purposes.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700330func (e *ErrArgument) Error() string {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700331 return fmt.Sprintf("Bad argument in call to %s, formal parameter %s, value %#v", e.Function, e.Argument, e.Value)
332}
333
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700334func (e *ErrArgument) String() string {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700335 return e.Error()
336}
337
Ash Wilson01626a32014-09-17 10:38:07 -0400338// RebootMethod describes the mechanisms by which a server reboot can be requested.
339type RebootMethod string
340
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700341// These constants determine how a server should be rebooted.
342// See the Reboot() function for further details.
343const (
Ash Wilson01626a32014-09-17 10:38:07 -0400344 SoftReboot RebootMethod = "SOFT"
345 HardReboot RebootMethod = "HARD"
346 OSReboot = SoftReboot
347 PowerCycle = HardReboot
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700348)
349
350// Reboot requests that a given server reboot.
351// Two methods exist for rebooting a server:
352//
Ash Wilson01626a32014-09-17 10:38:07 -0400353// HardReboot (aka PowerCycle) restarts the server instance by physically cutting power to the machine, or if a VM,
354// terminating it at the hypervisor level.
355// It's done. Caput. Full stop.
356// Then, after a brief while, power is restored or the VM instance restarted.
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700357//
Ash Wilson01626a32014-09-17 10:38:07 -0400358// SoftReboot (aka OSReboot) simply tells the OS to restart under its own procedures.
359// 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 +0200360func Reboot(client *gophercloud.ServiceClient, id string, how RebootMethod) ActionResult {
361 var res ActionResult
362
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700363 if (how != SoftReboot) && (how != HardReboot) {
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200364 res.Err = &ErrArgument{
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700365 Function: "Reboot",
366 Argument: "how",
Jon Perritt30558642014-04-14 17:07:12 -0500367 Value: how,
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700368 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200369 return res
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700370 }
Jon Perritt30558642014-04-14 17:07:12 -0500371
Ash Wilson59fb6c42015-02-12 16:21:13 -0500372 _, res.Err = client.Request("POST", actionURL(client, id), gophercloud.RequestOpts{
373 JSONBody: struct {
Jon Perritt30558642014-04-14 17:07:12 -0500374 C map[string]string `json:"reboot"`
375 }{
Ash Wilson01626a32014-09-17 10:38:07 -0400376 map[string]string{"type": string(how)},
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700377 },
Ash Wilson59fb6c42015-02-12 16:21:13 -0500378 OkCodes: []int{202},
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700379 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200380
381 return res
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700382}
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700383
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200384// RebuildOptsBuilder is an interface that allows extensions to override the
385// default behaviour of rebuild options
386type RebuildOptsBuilder interface {
387 ToServerRebuildMap() (map[string]interface{}, error)
388}
389
390// RebuildOpts represents the configuration options used in a server rebuild
391// operation
392type RebuildOpts struct {
393 // Required. The ID of the image you want your server to be provisioned on
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200394 ImageID string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200395
396 // Name to set the server to
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200397 Name string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200398
399 // Required. The server's admin password
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200400 AdminPass string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200401
402 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200403 AccessIPv4 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200404
405 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200406 AccessIPv6 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200407
408 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200409 Metadata map[string]string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200410
411 // Personality [optional] includes the path and contents of a file to inject into the server at launch.
412 // The maximum size of the file is 255 bytes (decoded).
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200413 Personality []byte
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200414}
415
416// ToServerRebuildMap formats a RebuildOpts struct into a map for use in JSON
417func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {
418 var err error
419 server := make(map[string]interface{})
420
421 if opts.AdminPass == "" {
422 err = fmt.Errorf("AdminPass is required")
423 }
424
425 if opts.ImageID == "" {
426 err = fmt.Errorf("ImageID is required")
427 }
428
429 if err != nil {
430 return server, err
431 }
432
433 server["name"] = opts.Name
434 server["adminPass"] = opts.AdminPass
435 server["imageRef"] = opts.ImageID
436
437 if opts.AccessIPv4 != "" {
438 server["accessIPv4"] = opts.AccessIPv4
439 }
440
441 if opts.AccessIPv6 != "" {
442 server["accessIPv6"] = opts.AccessIPv6
443 }
444
445 if opts.Metadata != nil {
446 server["metadata"] = opts.Metadata
447 }
448
449 if opts.Personality != nil {
450 encoded := base64.StdEncoding.EncodeToString(opts.Personality)
451 server["personality"] = &encoded
452 }
453
454 return map[string]interface{}{"rebuild": server}, nil
455}
456
457// Rebuild will reprovision the server according to the configuration options
458// provided in the RebuildOpts struct.
459func Rebuild(client *gophercloud.ServiceClient, id string, opts RebuildOptsBuilder) RebuildResult {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400460 var result RebuildResult
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700461
462 if id == "" {
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200463 result.Err = fmt.Errorf("ID is required")
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400464 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700465 }
466
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200467 reqBody, err := opts.ToServerRebuildMap()
468 if err != nil {
469 result.Err = err
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400470 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700471 }
472
Ash Wilson59fb6c42015-02-12 16:21:13 -0500473 _, result.Err = client.Request("POST", actionURL(client, id), gophercloud.RequestOpts{
474 JSONBody: &reqBody,
475 JSONResponse: &result.Body,
476 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700477 })
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200478
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400479 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700480}
481
Ash Wilson5f7cf182014-10-23 08:35:24 -0400482// ResizeOptsBuilder is an interface that allows extensions to override the default structure of
483// a Resize request.
484type ResizeOptsBuilder interface {
485 ToServerResizeMap() (map[string]interface{}, error)
486}
487
488// ResizeOpts represents the configuration options used to control a Resize operation.
489type ResizeOpts struct {
490 // FlavorRef is the ID of the flavor you wish your server to become.
491 FlavorRef string
492}
493
Alex Gaynor266e9332014-10-28 14:44:04 -0700494// 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 -0400495// Resize request.
496func (opts ResizeOpts) ToServerResizeMap() (map[string]interface{}, error) {
497 resize := map[string]interface{}{
498 "flavorRef": opts.FlavorRef,
499 }
500
501 return map[string]interface{}{"resize": resize}, nil
502}
503
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700504// Resize instructs the provider to change the flavor of the server.
Ash Wilson01626a32014-09-17 10:38:07 -0400505// Note that this implies rebuilding it.
506// Unfortunately, one cannot pass rebuild parameters to the resize function.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700507// When the resize completes, the server will be in RESIZE_VERIFY state.
508// While in this state, you can explore the use of the new server's configuration.
509// If you like it, call ConfirmResize() to commit the resize permanently.
510// Otherwise, call RevertResize() to restore the old configuration.
Ash Wilson9e87a922014-10-23 14:29:22 -0400511func Resize(client *gophercloud.ServiceClient, id string, opts ResizeOptsBuilder) ActionResult {
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200512 var res ActionResult
Ash Wilson5f7cf182014-10-23 08:35:24 -0400513 reqBody, err := opts.ToServerResizeMap()
514 if err != nil {
515 res.Err = err
516 return res
517 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200518
Ash Wilson59fb6c42015-02-12 16:21:13 -0500519 _, res.Err = client.Request("POST", actionURL(client, id), gophercloud.RequestOpts{
520 JSONBody: reqBody,
521 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700522 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200523
524 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700525}
526
527// ConfirmResize confirms a previous resize operation on a server.
528// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200529func ConfirmResize(client *gophercloud.ServiceClient, id string) ActionResult {
530 var res ActionResult
531
Ash Wilson59fb6c42015-02-12 16:21:13 -0500532 _, res.Err = client.Request("POST", actionURL(client, id), gophercloud.RequestOpts{
533 JSONBody: map[string]interface{}{"confirmResize": nil},
534 OkCodes: []int{204},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700535 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200536
537 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700538}
539
540// RevertResize cancels a previous resize operation on a server.
541// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200542func RevertResize(client *gophercloud.ServiceClient, id string) ActionResult {
543 var res ActionResult
544
Ash Wilson59fb6c42015-02-12 16:21:13 -0500545 _, res.Err = client.Request("POST", actionURL(client, id), gophercloud.RequestOpts{
546 JSONBody: map[string]interface{}{"revertResize": nil},
547 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700548 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200549
550 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700551}
Alex Gaynor39584a02014-10-28 13:59:21 -0700552
Alex Gaynor266e9332014-10-28 14:44:04 -0700553// RescueOptsBuilder is an interface that allows extensions to override the
554// default structure of a Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700555type RescueOptsBuilder interface {
556 ToServerRescueMap() (map[string]interface{}, error)
557}
558
Alex Gaynor266e9332014-10-28 14:44:04 -0700559// RescueOpts represents the configuration options used to control a Rescue
560// option.
Alex Gaynor39584a02014-10-28 13:59:21 -0700561type RescueOpts struct {
Alex Gaynor266e9332014-10-28 14:44:04 -0700562 // AdminPass is the desired administrative password for the instance in
Alex Gaynorcfec7722014-11-13 13:33:49 -0800563 // RESCUE mode. If it's left blank, the server will generate a password.
Alex Gaynor39584a02014-10-28 13:59:21 -0700564 AdminPass string
565}
566
Jon Perrittcc77da62014-11-16 13:14:21 -0700567// ToServerRescueMap formats a RescueOpts as a map that can be used as a JSON
Alex Gaynor266e9332014-10-28 14:44:04 -0700568// request body for the Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700569func (opts RescueOpts) ToServerRescueMap() (map[string]interface{}, error) {
570 server := make(map[string]interface{})
571 if opts.AdminPass != "" {
572 server["adminPass"] = opts.AdminPass
573 }
574 return map[string]interface{}{"rescue": server}, nil
575}
576
Alex Gaynor266e9332014-10-28 14:44:04 -0700577// Rescue instructs the provider to place the server into RESCUE mode.
Alex Gaynorfbe61bb2014-11-12 13:35:03 -0800578func Rescue(client *gophercloud.ServiceClient, id string, opts RescueOptsBuilder) RescueResult {
579 var result RescueResult
Alex Gaynor39584a02014-10-28 13:59:21 -0700580
581 if id == "" {
582 result.Err = fmt.Errorf("ID is required")
583 return result
584 }
585 reqBody, err := opts.ToServerRescueMap()
586 if err != nil {
587 result.Err = err
588 return result
589 }
590
Ash Wilson59fb6c42015-02-12 16:21:13 -0500591 _, result.Err = client.Request("POST", actionURL(client, id), gophercloud.RequestOpts{
592 JSONResponse: &result.Body,
593 JSONBody: &reqBody,
594 OkCodes: []int{200},
Alex Gaynor39584a02014-10-28 13:59:21 -0700595 })
596
597 return result
598}
Jon Perrittcc77da62014-11-16 13:14:21 -0700599
Jon Perritt789f8322014-11-21 08:20:04 -0700600// ResetMetadataOptsBuilder allows extensions to add additional parameters to the
601// Reset request.
602type ResetMetadataOptsBuilder interface {
603 ToMetadataResetMap() (map[string]interface{}, error)
Jon Perrittcc77da62014-11-16 13:14:21 -0700604}
605
Jon Perritt78c57ce2014-11-20 11:07:18 -0700606// MetadataOpts is a map that contains key-value pairs.
607type MetadataOpts map[string]string
Jon Perrittcc77da62014-11-16 13:14:21 -0700608
Jon Perritt789f8322014-11-21 08:20:04 -0700609// ToMetadataResetMap assembles a body for a Reset request based on the contents of a MetadataOpts.
610func (opts MetadataOpts) ToMetadataResetMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700611 return map[string]interface{}{"metadata": opts}, nil
612}
613
Jon Perritt78c57ce2014-11-20 11:07:18 -0700614// ToMetadataUpdateMap assembles a body for an Update request based on the contents of a MetadataOpts.
615func (opts MetadataOpts) ToMetadataUpdateMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700616 return map[string]interface{}{"metadata": opts}, nil
617}
618
Jon Perritt789f8322014-11-21 08:20:04 -0700619// ResetMetadata will create multiple new key-value pairs for the given server ID.
Jon Perrittcc77da62014-11-16 13:14:21 -0700620// Note: Using this operation will erase any already-existing metadata and create
621// the new metadata provided. To keep any already-existing metadata, use the
622// UpdateMetadatas or UpdateMetadata function.
Jon Perritt789f8322014-11-21 08:20:04 -0700623func ResetMetadata(client *gophercloud.ServiceClient, id string, opts ResetMetadataOptsBuilder) ResetMetadataResult {
624 var res ResetMetadataResult
625 metadata, err := opts.ToMetadataResetMap()
Jon Perrittcc77da62014-11-16 13:14:21 -0700626 if err != nil {
627 res.Err = err
628 return res
629 }
Ash Wilson59fb6c42015-02-12 16:21:13 -0500630 _, res.Err = client.Request("PUT", metadataURL(client, id), gophercloud.RequestOpts{
631 JSONBody: metadata,
632 JSONResponse: &res.Body,
Jon Perrittcc77da62014-11-16 13:14:21 -0700633 })
634 return res
635}
636
Jon Perritt78c57ce2014-11-20 11:07:18 -0700637// Metadata requests all the metadata for the given server ID.
638func Metadata(client *gophercloud.ServiceClient, id string) GetMetadataResult {
Jon Perrittcc77da62014-11-16 13:14:21 -0700639 var res GetMetadataResult
Ash Wilson59fb6c42015-02-12 16:21:13 -0500640 _, res.Err = client.Request("GET", metadataURL(client, id), gophercloud.RequestOpts{
641 JSONResponse: &res.Body,
Jon Perrittcc77da62014-11-16 13:14:21 -0700642 })
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 }
Ash Wilson59fb6c42015-02-12 16:21:13 -0500662 _, res.Err = client.Request("POST", metadataURL(client, id), gophercloud.RequestOpts{
663 JSONBody: metadata,
664 JSONResponse: &res.Body,
Jon Perritt78c57ce2014-11-20 11:07:18 -0700665 })
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
Ash Wilson59fb6c42015-02-12 16:21:13 -0500700 _, res.Err = client.Request("PUT", metadatumURL(client, id, key), gophercloud.RequestOpts{
701 JSONBody: metadatum,
702 JSONResponse: &res.Body,
Jon Perritt78c57ce2014-11-20 11:07:18 -0700703 })
704 return res
705}
706
707// Metadatum requests the key-value pair with the given key for the given server ID.
708func Metadatum(client *gophercloud.ServiceClient, id, key string) GetMetadatumResult {
709 var res GetMetadatumResult
Ash Wilson59fb6c42015-02-12 16:21:13 -0500710 _, res.Err = client.Request("GET", metadatumURL(client, id, key), gophercloud.RequestOpts{
711 JSONResponse: &res.Body,
Jon Perritt78c57ce2014-11-20 11:07:18 -0700712 })
713 return res
714}
715
716// DeleteMetadatum will delete the key-value pair with the given key for the given server ID.
717func DeleteMetadatum(client *gophercloud.ServiceClient, id, key string) DeleteMetadatumResult {
718 var res DeleteMetadatumResult
Ash Wilson59fb6c42015-02-12 16:21:13 -0500719 _, res.Err = client.Request("DELETE", metadatumURL(client, id, key), gophercloud.RequestOpts{
720 JSONResponse: &res.Body,
Jon Perrittcc77da62014-11-16 13:14:21 -0700721 })
722 return res
723}