blob: e0950e4e098d79805127e8e909cebf8a02ccc3d8 [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
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020076 return pagination.NewPager(client, url, createPageFn)
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080077}
78
Ash Wilson2206a112014-10-02 10:57:38 -040079// CreateOptsBuilder describes struct types that can be accepted by the Create call.
Ash Wilson6a310e02014-09-29 08:24:02 -040080// The CreateOpts struct in this package does.
Ash Wilson2206a112014-10-02 10:57:38 -040081type CreateOptsBuilder interface {
Jon Perritt4149d7c2014-10-23 21:23:46 -050082 ToServerCreateMap() (map[string]interface{}, error)
Ash Wilson6a310e02014-09-29 08:24:02 -040083}
84
85// Network is used within CreateOpts to control a new server's network attachments.
86type Network struct {
87 // UUID of a nova-network to attach to the newly provisioned server.
88 // Required unless Port is provided.
89 UUID string
90
91 // Port of a neutron network to attach to the newly provisioned server.
92 // Required unless UUID is provided.
93 Port string
94
95 // FixedIP [optional] specifies a fixed IPv4 address to be used on this network.
96 FixedIP string
97}
98
99// CreateOpts specifies server creation parameters.
100type CreateOpts struct {
101 // Name [required] is the name to assign to the newly launched server.
102 Name string
103
104 // ImageRef [required] is the ID or full URL to the image that contains the server's OS and initial state.
105 // Optional if using the boot-from-volume extension.
106 ImageRef string
107
108 // FlavorRef [required] is the ID or full URL to the flavor that describes the server's specs.
109 FlavorRef string
110
111 // SecurityGroups [optional] lists the names of the security groups to which this server should belong.
112 SecurityGroups []string
113
114 // UserData [optional] contains configuration information or scripts to use upon launch.
115 // Create will base64-encode it for you.
116 UserData []byte
117
118 // AvailabilityZone [optional] in which to launch the server.
119 AvailabilityZone string
120
121 // Networks [optional] dictates how this server will be attached to available networks.
122 // By default, the server will be attached to all isolated networks for the tenant.
123 Networks []Network
124
125 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
126 Metadata map[string]string
127
128 // Personality [optional] includes the path and contents of a file to inject into the server at launch.
129 // The maximum size of the file is 255 bytes (decoded).
130 Personality []byte
131
132 // ConfigDrive [optional] enables metadata injection through a configuration drive.
133 ConfigDrive bool
Jon Perrittf3b2e142014-11-04 16:00:19 -0600134
135 // AdminPass [optional] sets the root user password. If not set, a randomly-generated
136 // password will be created and returned in the response.
137 AdminPass string
Jon Perritt7b9671c2015-02-01 22:03:14 -0700138
139 // AccessIPv4 [optional] specifies an IPv4 address for the instance.
140 AccessIPv4 string
141
142 // AccessIPv6 [optional] specifies an IPv6 address for the instance.
143 AccessIPv6 string
Ash Wilson6a310e02014-09-29 08:24:02 -0400144}
145
Ash Wilsone45c9732014-09-29 10:54:12 -0400146// ToServerCreateMap assembles a request body based on the contents of a CreateOpts.
Jon Perritt4149d7c2014-10-23 21:23:46 -0500147func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) {
Ash Wilson6a310e02014-09-29 08:24:02 -0400148 server := make(map[string]interface{})
149
150 server["name"] = opts.Name
151 server["imageRef"] = opts.ImageRef
152 server["flavorRef"] = opts.FlavorRef
153
154 if opts.UserData != nil {
155 encoded := base64.StdEncoding.EncodeToString(opts.UserData)
156 server["user_data"] = &encoded
157 }
158 if opts.Personality != nil {
159 encoded := base64.StdEncoding.EncodeToString(opts.Personality)
160 server["personality"] = &encoded
161 }
162 if opts.ConfigDrive {
163 server["config_drive"] = "true"
164 }
165 if opts.AvailabilityZone != "" {
166 server["availability_zone"] = opts.AvailabilityZone
167 }
168 if opts.Metadata != nil {
169 server["metadata"] = opts.Metadata
170 }
Jon Perrittf3b2e142014-11-04 16:00:19 -0600171 if opts.AdminPass != "" {
172 server["adminPass"] = opts.AdminPass
173 }
Jon Perritt7b9671c2015-02-01 22:03:14 -0700174 if opts.AccessIPv4 != "" {
175 server["accessIPv4"] = opts.AccessIPv4
176 }
177 if opts.AccessIPv6 != "" {
178 server["accessIPv6"] = opts.AccessIPv6
179 }
Ash Wilson6a310e02014-09-29 08:24:02 -0400180
181 if len(opts.SecurityGroups) > 0 {
182 securityGroups := make([]map[string]interface{}, len(opts.SecurityGroups))
183 for i, groupName := range opts.SecurityGroups {
184 securityGroups[i] = map[string]interface{}{"name": groupName}
185 }
eselldf709942014-11-13 21:07:11 -0700186 server["security_groups"] = securityGroups
Ash Wilson6a310e02014-09-29 08:24:02 -0400187 }
Jon Perritt2a7797d2014-10-21 15:08:43 -0500188
Ash Wilson6a310e02014-09-29 08:24:02 -0400189 if len(opts.Networks) > 0 {
190 networks := make([]map[string]interface{}, len(opts.Networks))
191 for i, net := range opts.Networks {
192 networks[i] = make(map[string]interface{})
193 if net.UUID != "" {
194 networks[i]["uuid"] = net.UUID
195 }
196 if net.Port != "" {
197 networks[i]["port"] = net.Port
198 }
199 if net.FixedIP != "" {
200 networks[i]["fixed_ip"] = net.FixedIP
201 }
202 }
Jon Perritt2a7797d2014-10-21 15:08:43 -0500203 server["networks"] = networks
Ash Wilson6a310e02014-09-29 08:24:02 -0400204 }
205
Jon Perritt4149d7c2014-10-23 21:23:46 -0500206 return map[string]interface{}{"server": server}, nil
Ash Wilson6a310e02014-09-29 08:24:02 -0400207}
208
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800209// Create requests a server to be provisioned to the user in the current tenant.
Ash Wilson2206a112014-10-02 10:57:38 -0400210func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jon Perritt4149d7c2014-10-23 21:23:46 -0500211 var res CreateResult
212
213 reqBody, err := opts.ToServerCreateMap()
214 if err != nil {
215 res.Err = err
216 return res
217 }
218
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100219 _, res.Err = client.Post(listURL(client), reqBody, &res.Body, nil)
Jon Perritt4149d7c2014-10-23 21:23:46 -0500220 return res
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800221}
222
223// Delete requests that a server previously provisioned be removed from your account.
Jamie Hannaford34732fe2014-10-27 11:29:36 +0100224func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
225 var res DeleteResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100226 _, res.Err = client.Delete(deleteURL(client, id), nil)
Jamie Hannaford34732fe2014-10-27 11:29:36 +0100227 return res
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800228}
229
Ash Wilson7ddf0362014-09-17 10:59:09 -0400230// Get requests details on a single server, by ID.
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400231func Get(client *gophercloud.ServiceClient, id string) GetResult {
232 var result GetResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100233 _, result.Err = client.Get(getURL(client, id), &result.Body, &gophercloud.RequestOpts{
234 OkCodes: []int{200, 203},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800235 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400236 return result
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800237}
238
Alex Gaynora6d5f9f2014-10-27 10:52:32 -0700239// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
Jon Perritt82048212014-10-13 22:33:13 -0500240type UpdateOptsBuilder interface {
Ash Wilsone45c9732014-09-29 10:54:12 -0400241 ToServerUpdateMap() map[string]interface{}
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400242}
243
244// UpdateOpts specifies the base attributes that may be updated on an existing server.
245type UpdateOpts struct {
246 // Name [optional] changes the displayed name of the server.
247 // The server host name will *not* change.
248 // Server names are not constrained to be unique, even within the same tenant.
249 Name string
250
251 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
252 AccessIPv4 string
253
254 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
255 AccessIPv6 string
256}
257
Ash Wilsone45c9732014-09-29 10:54:12 -0400258// ToServerUpdateMap formats an UpdateOpts structure into a request body.
259func (opts UpdateOpts) ToServerUpdateMap() map[string]interface{} {
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400260 server := make(map[string]string)
261 if opts.Name != "" {
262 server["name"] = opts.Name
263 }
264 if opts.AccessIPv4 != "" {
265 server["accessIPv4"] = opts.AccessIPv4
266 }
267 if opts.AccessIPv6 != "" {
268 server["accessIPv6"] = opts.AccessIPv6
269 }
270 return map[string]interface{}{"server": server}
271}
272
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800273// Update requests that various attributes of the indicated server be changed.
Jon Perritt82048212014-10-13 22:33:13 -0500274func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400275 var result UpdateResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100276 reqBody := opts.ToServerUpdateMap()
277 _, result.Err = client.Put(updateURL(client, id), reqBody, &result.Body, &gophercloud.RequestOpts{
278 OkCodes: []int{200},
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800279 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400280 return result
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800281}
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700282
Ash Wilson01626a32014-09-17 10:38:07 -0400283// ChangeAdminPassword alters the administrator or root password for a specified server.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200284func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) ActionResult {
Ash Wilsondc7daa82014-09-23 16:34:42 -0400285 var req struct {
286 ChangePassword struct {
287 AdminPass string `json:"adminPass"`
288 } `json:"changePassword"`
289 }
290
291 req.ChangePassword.AdminPass = newPassword
292
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200293 var res ActionResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100294 _, res.Err = client.Post(actionURL(client, id), req, nil, nil)
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200295 return res
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700296}
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700297
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700298// ErrArgument errors occur when an argument supplied to a package function
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700299// fails to fall within acceptable values. For example, the Reboot() function
300// expects the "how" parameter to be one of HardReboot or SoftReboot. These
301// constants are (currently) strings, leading someone to wonder if they can pass
302// other string values instead, perhaps in an effort to break the API of their
303// provider. Reboot() returns this error in this situation.
304//
305// Function identifies which function was called/which function is generating
306// the error.
307// Argument identifies which formal argument was responsible for producing the
308// error.
309// Value provides the value as it was passed into the function.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700310type ErrArgument struct {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700311 Function, Argument string
Jon Perritt30558642014-04-14 17:07:12 -0500312 Value interface{}
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700313}
314
315// Error yields a useful diagnostic for debugging purposes.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700316func (e *ErrArgument) Error() string {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700317 return fmt.Sprintf("Bad argument in call to %s, formal parameter %s, value %#v", e.Function, e.Argument, e.Value)
318}
319
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700320func (e *ErrArgument) String() string {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700321 return e.Error()
322}
323
Ash Wilson01626a32014-09-17 10:38:07 -0400324// RebootMethod describes the mechanisms by which a server reboot can be requested.
325type RebootMethod string
326
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700327// These constants determine how a server should be rebooted.
328// See the Reboot() function for further details.
329const (
Ash Wilson01626a32014-09-17 10:38:07 -0400330 SoftReboot RebootMethod = "SOFT"
331 HardReboot RebootMethod = "HARD"
332 OSReboot = SoftReboot
333 PowerCycle = HardReboot
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700334)
335
336// Reboot requests that a given server reboot.
337// Two methods exist for rebooting a server:
338//
Ash Wilson01626a32014-09-17 10:38:07 -0400339// HardReboot (aka PowerCycle) restarts the server instance by physically cutting power to the machine, or if a VM,
340// terminating it at the hypervisor level.
341// It's done. Caput. Full stop.
342// Then, after a brief while, power is restored or the VM instance restarted.
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700343//
Ash Wilson01626a32014-09-17 10:38:07 -0400344// SoftReboot (aka OSReboot) simply tells the OS to restart under its own procedures.
345// 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 +0200346func Reboot(client *gophercloud.ServiceClient, id string, how RebootMethod) ActionResult {
347 var res ActionResult
348
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700349 if (how != SoftReboot) && (how != HardReboot) {
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200350 res.Err = &ErrArgument{
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700351 Function: "Reboot",
352 Argument: "how",
Jon Perritt30558642014-04-14 17:07:12 -0500353 Value: how,
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700354 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200355 return res
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700356 }
Jon Perritt30558642014-04-14 17:07:12 -0500357
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100358 reqBody := struct {
359 C map[string]string `json:"reboot"`
360 }{
361 map[string]string{"type": string(how)},
362 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200363
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100364 _, res.Err = client.Post(actionURL(client, id), reqBody, nil, nil)
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200365 return res
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700366}
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700367
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200368// RebuildOptsBuilder is an interface that allows extensions to override the
369// default behaviour of rebuild options
370type RebuildOptsBuilder interface {
371 ToServerRebuildMap() (map[string]interface{}, error)
372}
373
374// RebuildOpts represents the configuration options used in a server rebuild
375// operation
376type RebuildOpts struct {
377 // Required. The ID of the image you want your server to be provisioned on
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200378 ImageID string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200379
380 // Name to set the server to
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200381 Name string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200382
383 // Required. The server's admin password
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200384 AdminPass string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200385
386 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200387 AccessIPv4 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200388
389 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200390 AccessIPv6 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200391
392 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200393 Metadata map[string]string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200394
395 // Personality [optional] includes the path and contents of a file to inject into the server at launch.
396 // The maximum size of the file is 255 bytes (decoded).
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200397 Personality []byte
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200398}
399
400// ToServerRebuildMap formats a RebuildOpts struct into a map for use in JSON
401func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {
402 var err error
403 server := make(map[string]interface{})
404
405 if opts.AdminPass == "" {
406 err = fmt.Errorf("AdminPass is required")
407 }
408
409 if opts.ImageID == "" {
410 err = fmt.Errorf("ImageID is required")
411 }
412
413 if err != nil {
414 return server, err
415 }
416
417 server["name"] = opts.Name
418 server["adminPass"] = opts.AdminPass
419 server["imageRef"] = opts.ImageID
420
421 if opts.AccessIPv4 != "" {
422 server["accessIPv4"] = opts.AccessIPv4
423 }
424
425 if opts.AccessIPv6 != "" {
426 server["accessIPv6"] = opts.AccessIPv6
427 }
428
429 if opts.Metadata != nil {
430 server["metadata"] = opts.Metadata
431 }
432
433 if opts.Personality != nil {
434 encoded := base64.StdEncoding.EncodeToString(opts.Personality)
435 server["personality"] = &encoded
436 }
437
438 return map[string]interface{}{"rebuild": server}, nil
439}
440
441// Rebuild will reprovision the server according to the configuration options
442// provided in the RebuildOpts struct.
443func Rebuild(client *gophercloud.ServiceClient, id string, opts RebuildOptsBuilder) RebuildResult {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400444 var result RebuildResult
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700445
446 if id == "" {
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200447 result.Err = fmt.Errorf("ID is required")
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400448 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700449 }
450
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200451 reqBody, err := opts.ToServerRebuildMap()
452 if err != nil {
453 result.Err = err
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400454 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700455 }
456
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100457 _, result.Err = client.Post(actionURL(client, id), reqBody, &result.Body, nil)
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400458 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700459}
460
Ash Wilson5f7cf182014-10-23 08:35:24 -0400461// ResizeOptsBuilder is an interface that allows extensions to override the default structure of
462// a Resize request.
463type ResizeOptsBuilder interface {
464 ToServerResizeMap() (map[string]interface{}, error)
465}
466
467// ResizeOpts represents the configuration options used to control a Resize operation.
468type ResizeOpts struct {
469 // FlavorRef is the ID of the flavor you wish your server to become.
470 FlavorRef string
471}
472
Alex Gaynor266e9332014-10-28 14:44:04 -0700473// 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 -0400474// Resize request.
475func (opts ResizeOpts) ToServerResizeMap() (map[string]interface{}, error) {
476 resize := map[string]interface{}{
477 "flavorRef": opts.FlavorRef,
478 }
479
480 return map[string]interface{}{"resize": resize}, nil
481}
482
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700483// Resize instructs the provider to change the flavor of the server.
Ash Wilson01626a32014-09-17 10:38:07 -0400484// Note that this implies rebuilding it.
485// Unfortunately, one cannot pass rebuild parameters to the resize function.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700486// When the resize completes, the server will be in RESIZE_VERIFY state.
487// While in this state, you can explore the use of the new server's configuration.
488// If you like it, call ConfirmResize() to commit the resize permanently.
489// Otherwise, call RevertResize() to restore the old configuration.
Ash Wilson9e87a922014-10-23 14:29:22 -0400490func Resize(client *gophercloud.ServiceClient, id string, opts ResizeOptsBuilder) ActionResult {
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200491 var res ActionResult
Ash Wilson5f7cf182014-10-23 08:35:24 -0400492 reqBody, err := opts.ToServerResizeMap()
493 if err != nil {
494 res.Err = err
495 return res
496 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200497
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100498 _, res.Err = client.Post(actionURL(client, id), reqBody, nil, nil)
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200499 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700500}
501
502// ConfirmResize confirms a previous resize operation on a server.
503// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200504func ConfirmResize(client *gophercloud.ServiceClient, id string) ActionResult {
505 var res ActionResult
506
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100507 reqBody := map[string]interface{}{"confirmResize": nil}
508 _, res.Err = client.Post(actionURL(client, id), reqBody, nil, &gophercloud.RequestOpts{
509 OkCodes: []int{201, 202, 204},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700510 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200511 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700512}
513
514// RevertResize cancels a previous resize operation on a server.
515// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200516func RevertResize(client *gophercloud.ServiceClient, id string) ActionResult {
517 var res ActionResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100518 reqBody := map[string]interface{}{"revertResize": nil}
519 _, res.Err = client.Post(actionURL(client, id), reqBody, nil, nil)
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200520 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700521}
Alex Gaynor39584a02014-10-28 13:59:21 -0700522
Alex Gaynor266e9332014-10-28 14:44:04 -0700523// RescueOptsBuilder is an interface that allows extensions to override the
524// default structure of a Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700525type RescueOptsBuilder interface {
526 ToServerRescueMap() (map[string]interface{}, error)
527}
528
Alex Gaynor266e9332014-10-28 14:44:04 -0700529// RescueOpts represents the configuration options used to control a Rescue
530// option.
Alex Gaynor39584a02014-10-28 13:59:21 -0700531type RescueOpts struct {
Alex Gaynor266e9332014-10-28 14:44:04 -0700532 // AdminPass is the desired administrative password for the instance in
Alex Gaynorcfec7722014-11-13 13:33:49 -0800533 // RESCUE mode. If it's left blank, the server will generate a password.
Alex Gaynor39584a02014-10-28 13:59:21 -0700534 AdminPass string
535}
536
Jon Perrittcc77da62014-11-16 13:14:21 -0700537// ToServerRescueMap formats a RescueOpts as a map that can be used as a JSON
Alex Gaynor266e9332014-10-28 14:44:04 -0700538// request body for the Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700539func (opts RescueOpts) ToServerRescueMap() (map[string]interface{}, error) {
540 server := make(map[string]interface{})
541 if opts.AdminPass != "" {
542 server["adminPass"] = opts.AdminPass
543 }
544 return map[string]interface{}{"rescue": server}, nil
545}
546
Alex Gaynor266e9332014-10-28 14:44:04 -0700547// Rescue instructs the provider to place the server into RESCUE mode.
Alex Gaynorfbe61bb2014-11-12 13:35:03 -0800548func Rescue(client *gophercloud.ServiceClient, id string, opts RescueOptsBuilder) RescueResult {
549 var result RescueResult
Alex Gaynor39584a02014-10-28 13:59:21 -0700550
551 if id == "" {
552 result.Err = fmt.Errorf("ID is required")
553 return result
554 }
555 reqBody, err := opts.ToServerRescueMap()
556 if err != nil {
557 result.Err = err
558 return result
559 }
560
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100561 _, result.Err = client.Post(actionURL(client, id), reqBody, &result.Body, &gophercloud.RequestOpts{
562 OkCodes: []int{200},
Alex Gaynor39584a02014-10-28 13:59:21 -0700563 })
564
565 return result
566}
Jon Perrittcc77da62014-11-16 13:14:21 -0700567
Jon Perritt789f8322014-11-21 08:20:04 -0700568// ResetMetadataOptsBuilder allows extensions to add additional parameters to the
569// Reset request.
570type ResetMetadataOptsBuilder interface {
571 ToMetadataResetMap() (map[string]interface{}, error)
Jon Perrittcc77da62014-11-16 13:14:21 -0700572}
573
Jon Perritt78c57ce2014-11-20 11:07:18 -0700574// MetadataOpts is a map that contains key-value pairs.
575type MetadataOpts map[string]string
Jon Perrittcc77da62014-11-16 13:14:21 -0700576
Jon Perritt789f8322014-11-21 08:20:04 -0700577// ToMetadataResetMap assembles a body for a Reset request based on the contents of a MetadataOpts.
578func (opts MetadataOpts) ToMetadataResetMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700579 return map[string]interface{}{"metadata": opts}, nil
580}
581
Jon Perritt78c57ce2014-11-20 11:07:18 -0700582// ToMetadataUpdateMap assembles a body for an Update request based on the contents of a MetadataOpts.
583func (opts MetadataOpts) ToMetadataUpdateMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700584 return map[string]interface{}{"metadata": opts}, nil
585}
586
Jon Perritt789f8322014-11-21 08:20:04 -0700587// ResetMetadata will create multiple new key-value pairs for the given server ID.
Jon Perrittcc77da62014-11-16 13:14:21 -0700588// Note: Using this operation will erase any already-existing metadata and create
589// the new metadata provided. To keep any already-existing metadata, use the
590// UpdateMetadatas or UpdateMetadata function.
Jon Perritt789f8322014-11-21 08:20:04 -0700591func ResetMetadata(client *gophercloud.ServiceClient, id string, opts ResetMetadataOptsBuilder) ResetMetadataResult {
592 var res ResetMetadataResult
593 metadata, err := opts.ToMetadataResetMap()
Jon Perrittcc77da62014-11-16 13:14:21 -0700594 if err != nil {
595 res.Err = err
596 return res
597 }
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100598 _, res.Err = client.Put(metadataURL(client, id), metadata, &res.Body, &gophercloud.RequestOpts{
599 OkCodes: []int{200},
Jon Perrittcc77da62014-11-16 13:14:21 -0700600 })
601 return res
602}
603
Jon Perritt78c57ce2014-11-20 11:07:18 -0700604// Metadata requests all the metadata for the given server ID.
605func Metadata(client *gophercloud.ServiceClient, id string) GetMetadataResult {
Jon Perrittcc77da62014-11-16 13:14:21 -0700606 var res GetMetadataResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100607 _, res.Err = client.Get(metadataURL(client, id), &res.Body, nil)
Jon Perrittcc77da62014-11-16 13:14:21 -0700608 return res
609}
610
Jon Perritt78c57ce2014-11-20 11:07:18 -0700611// UpdateMetadataOptsBuilder allows extensions to add additional parameters to the
612// Create request.
613type UpdateMetadataOptsBuilder interface {
614 ToMetadataUpdateMap() (map[string]interface{}, error)
615}
616
617// UpdateMetadata updates (or creates) all the metadata specified by opts for the given server ID.
618// This operation does not affect already-existing metadata that is not specified
619// by opts.
620func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) UpdateMetadataResult {
621 var res UpdateMetadataResult
622 metadata, err := opts.ToMetadataUpdateMap()
623 if err != nil {
624 res.Err = err
625 return res
626 }
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100627 _, res.Err = client.Post(metadataURL(client, id), metadata, &res.Body, &gophercloud.RequestOpts{
628 OkCodes: []int{200},
Jon Perritt78c57ce2014-11-20 11:07:18 -0700629 })
630 return res
631}
632
633// MetadatumOptsBuilder allows extensions to add additional parameters to the
634// Create request.
635type MetadatumOptsBuilder interface {
636 ToMetadatumCreateMap() (map[string]interface{}, string, error)
637}
638
639// MetadatumOpts is a map of length one that contains a key-value pair.
640type MetadatumOpts map[string]string
641
642// ToMetadatumCreateMap assembles a body for a Create request based on the contents of a MetadataumOpts.
643func (opts MetadatumOpts) ToMetadatumCreateMap() (map[string]interface{}, string, error) {
644 if len(opts) != 1 {
645 return nil, "", errors.New("CreateMetadatum operation must have 1 and only 1 key-value pair.")
646 }
647 metadatum := map[string]interface{}{"meta": opts}
648 var key string
649 for k := range metadatum["meta"].(MetadatumOpts) {
650 key = k
651 }
652 return metadatum, key, nil
653}
654
655// CreateMetadatum will create or update the key-value pair with the given key for the given server ID.
656func CreateMetadatum(client *gophercloud.ServiceClient, id string, opts MetadatumOptsBuilder) CreateMetadatumResult {
657 var res CreateMetadatumResult
658 metadatum, key, err := opts.ToMetadatumCreateMap()
659 if err != nil {
660 res.Err = err
661 return res
662 }
663
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100664 _, res.Err = client.Put(metadatumURL(client, id, key), metadatum, &res.Body, &gophercloud.RequestOpts{
665 OkCodes: []int{200},
Jon Perritt78c57ce2014-11-20 11:07:18 -0700666 })
667 return res
668}
669
670// Metadatum requests the key-value pair with the given key for the given server ID.
671func Metadatum(client *gophercloud.ServiceClient, id, key string) GetMetadatumResult {
672 var res GetMetadatumResult
Ash Wilson59fb6c42015-02-12 16:21:13 -0500673 _, res.Err = client.Request("GET", metadatumURL(client, id, key), gophercloud.RequestOpts{
674 JSONResponse: &res.Body,
Jon Perritt78c57ce2014-11-20 11:07:18 -0700675 })
676 return res
677}
678
679// DeleteMetadatum will delete the key-value pair with the given key for the given server ID.
680func DeleteMetadatum(client *gophercloud.ServiceClient, id, key string) DeleteMetadatumResult {
681 var res DeleteMetadatumResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100682 _, res.Err = client.Delete(metadatumURL(client, id, key), &gophercloud.RequestOpts{
Ash Wilson59fb6c42015-02-12 16:21:13 -0500683 JSONResponse: &res.Body,
Jon Perrittcc77da62014-11-16 13:14:21 -0700684 })
685 return res
686}
Jon Perritt5cb49482015-02-19 12:19:58 -0700687
688// ListAddresses makes a request against the API to list the servers IP addresses.
689func ListAddresses(client *gophercloud.ServiceClient, id string) pagination.Pager {
690 createPageFn := func(r pagination.PageResult) pagination.Page {
691 return AddressPage{pagination.SinglePageBase(r)}
692 }
693 return pagination.NewPager(client, listAddressesURL(client, id), createPageFn)
694}
Jon Perritt04d073c2015-02-19 21:46:23 -0700695
696// ListAddressesByNetwork makes a request against the API to list the servers IP addresses
697// for the given network.
698func ListAddressesByNetwork(client *gophercloud.ServiceClient, id, network string) pagination.Pager {
699 createPageFn := func(r pagination.PageResult) pagination.Page {
700 return NetworkAddressPage{pagination.SinglePageBase(r)}
701 }
702 return pagination.NewPager(client, listAddressesByNetworkURL(client, id, network), createPageFn)
703}