blob: 3a75b8f033b89aadde582f04d583daa89a50c95f [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
Ash Wilson4bf41a32015-02-12 15:52:44 -0500219 _, res.Err = client.Request("POST", listURL(client), gophercloud.RequestOpts{
220 JSONResponse: &res.Body,
221 JSONBody: reqBody,
222 OkCodes: []int{202},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800223 })
Jon Perritt4149d7c2014-10-23 21:23:46 -0500224 return res
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800225}
226
227// Delete requests that a server previously provisioned be removed from your account.
Jamie Hannaford34732fe2014-10-27 11:29:36 +0100228func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
229 var res DeleteResult
Ash Wilson59fb6c42015-02-12 16:21:13 -0500230 _, res.Err = client.Request("DELETE", deleteURL(client, id), gophercloud.RequestOpts{
231 OkCodes: []int{204},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800232 })
Jamie Hannaford34732fe2014-10-27 11:29:36 +0100233 return res
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800234}
235
Ash Wilson7ddf0362014-09-17 10:59:09 -0400236// Get requests details on a single server, by ID.
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400237func Get(client *gophercloud.ServiceClient, id string) GetResult {
238 var result GetResult
Ash Wilson59fb6c42015-02-12 16:21:13 -0500239 _, result.Err = client.Request("GET", getURL(client, id), gophercloud.RequestOpts{
240 JSONResponse: &result.Body,
241 OkCodes: []int{200, 203},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800242 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400243 return result
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800244}
245
Alex Gaynora6d5f9f2014-10-27 10:52:32 -0700246// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
Jon Perritt82048212014-10-13 22:33:13 -0500247type UpdateOptsBuilder interface {
Ash Wilsone45c9732014-09-29 10:54:12 -0400248 ToServerUpdateMap() map[string]interface{}
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400249}
250
251// UpdateOpts specifies the base attributes that may be updated on an existing server.
252type UpdateOpts struct {
253 // Name [optional] changes the displayed name of the server.
254 // The server host name will *not* change.
255 // Server names are not constrained to be unique, even within the same tenant.
256 Name string
257
258 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
259 AccessIPv4 string
260
261 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
262 AccessIPv6 string
263}
264
Ash Wilsone45c9732014-09-29 10:54:12 -0400265// ToServerUpdateMap formats an UpdateOpts structure into a request body.
266func (opts UpdateOpts) ToServerUpdateMap() map[string]interface{} {
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400267 server := make(map[string]string)
268 if opts.Name != "" {
269 server["name"] = opts.Name
270 }
271 if opts.AccessIPv4 != "" {
272 server["accessIPv4"] = opts.AccessIPv4
273 }
274 if opts.AccessIPv6 != "" {
275 server["accessIPv6"] = opts.AccessIPv6
276 }
277 return map[string]interface{}{"server": server}
278}
279
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800280// Update requests that various attributes of the indicated server be changed.
Jon Perritt82048212014-10-13 22:33:13 -0500281func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400282 var result UpdateResult
Ash Wilson59fb6c42015-02-12 16:21:13 -0500283 _, result.Err = client.Request("PUT", updateURL(client, id), gophercloud.RequestOpts{
284 JSONResponse: &result.Body,
285 JSONBody: opts.ToServerUpdateMap(),
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800286 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400287 return result
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800288}
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700289
Ash Wilson01626a32014-09-17 10:38:07 -0400290// ChangeAdminPassword alters the administrator or root password for a specified server.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200291func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) ActionResult {
Ash Wilsondc7daa82014-09-23 16:34:42 -0400292 var req struct {
293 ChangePassword struct {
294 AdminPass string `json:"adminPass"`
295 } `json:"changePassword"`
296 }
297
298 req.ChangePassword.AdminPass = newPassword
299
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200300 var res ActionResult
301
Ash Wilson59fb6c42015-02-12 16:21:13 -0500302 _, res.Err = client.Request("POST", actionURL(client, id), gophercloud.RequestOpts{
303 JSONBody: req,
304 OkCodes: []int{202},
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700305 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200306
307 return res
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700308}
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700309
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700310// ErrArgument errors occur when an argument supplied to a package function
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700311// fails to fall within acceptable values. For example, the Reboot() function
312// expects the "how" parameter to be one of HardReboot or SoftReboot. These
313// constants are (currently) strings, leading someone to wonder if they can pass
314// other string values instead, perhaps in an effort to break the API of their
315// provider. Reboot() returns this error in this situation.
316//
317// Function identifies which function was called/which function is generating
318// the error.
319// Argument identifies which formal argument was responsible for producing the
320// error.
321// Value provides the value as it was passed into the function.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700322type ErrArgument struct {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700323 Function, Argument string
Jon Perritt30558642014-04-14 17:07:12 -0500324 Value interface{}
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700325}
326
327// Error yields a useful diagnostic for debugging purposes.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700328func (e *ErrArgument) Error() string {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700329 return fmt.Sprintf("Bad argument in call to %s, formal parameter %s, value %#v", e.Function, e.Argument, e.Value)
330}
331
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700332func (e *ErrArgument) String() string {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700333 return e.Error()
334}
335
Ash Wilson01626a32014-09-17 10:38:07 -0400336// RebootMethod describes the mechanisms by which a server reboot can be requested.
337type RebootMethod string
338
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700339// These constants determine how a server should be rebooted.
340// See the Reboot() function for further details.
341const (
Ash Wilson01626a32014-09-17 10:38:07 -0400342 SoftReboot RebootMethod = "SOFT"
343 HardReboot RebootMethod = "HARD"
344 OSReboot = SoftReboot
345 PowerCycle = HardReboot
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700346)
347
348// Reboot requests that a given server reboot.
349// Two methods exist for rebooting a server:
350//
Ash Wilson01626a32014-09-17 10:38:07 -0400351// HardReboot (aka PowerCycle) restarts the server instance by physically cutting power to the machine, or if a VM,
352// terminating it at the hypervisor level.
353// It's done. Caput. Full stop.
354// Then, after a brief while, power is restored or the VM instance restarted.
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700355//
Ash Wilson01626a32014-09-17 10:38:07 -0400356// SoftReboot (aka OSReboot) simply tells the OS to restart under its own procedures.
357// 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 +0200358func Reboot(client *gophercloud.ServiceClient, id string, how RebootMethod) ActionResult {
359 var res ActionResult
360
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700361 if (how != SoftReboot) && (how != HardReboot) {
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200362 res.Err = &ErrArgument{
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700363 Function: "Reboot",
364 Argument: "how",
Jon Perritt30558642014-04-14 17:07:12 -0500365 Value: how,
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700366 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200367 return res
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700368 }
Jon Perritt30558642014-04-14 17:07:12 -0500369
Ash Wilson59fb6c42015-02-12 16:21:13 -0500370 _, res.Err = client.Request("POST", actionURL(client, id), gophercloud.RequestOpts{
371 JSONBody: struct {
Jon Perritt30558642014-04-14 17:07:12 -0500372 C map[string]string `json:"reboot"`
373 }{
Ash Wilson01626a32014-09-17 10:38:07 -0400374 map[string]string{"type": string(how)},
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700375 },
Ash Wilson59fb6c42015-02-12 16:21:13 -0500376 OkCodes: []int{202},
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700377 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200378
379 return res
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700380}
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700381
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200382// RebuildOptsBuilder is an interface that allows extensions to override the
383// default behaviour of rebuild options
384type RebuildOptsBuilder interface {
385 ToServerRebuildMap() (map[string]interface{}, error)
386}
387
388// RebuildOpts represents the configuration options used in a server rebuild
389// operation
390type RebuildOpts struct {
391 // Required. The ID of the image you want your server to be provisioned on
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200392 ImageID string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200393
394 // Name to set the server to
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200395 Name string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200396
397 // Required. The server's admin password
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200398 AdminPass string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200399
400 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200401 AccessIPv4 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200402
403 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200404 AccessIPv6 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200405
406 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200407 Metadata map[string]string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200408
409 // Personality [optional] includes the path and contents of a file to inject into the server at launch.
410 // The maximum size of the file is 255 bytes (decoded).
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200411 Personality []byte
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200412}
413
414// ToServerRebuildMap formats a RebuildOpts struct into a map for use in JSON
415func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {
416 var err error
417 server := make(map[string]interface{})
418
419 if opts.AdminPass == "" {
420 err = fmt.Errorf("AdminPass is required")
421 }
422
423 if opts.ImageID == "" {
424 err = fmt.Errorf("ImageID is required")
425 }
426
427 if err != nil {
428 return server, err
429 }
430
431 server["name"] = opts.Name
432 server["adminPass"] = opts.AdminPass
433 server["imageRef"] = opts.ImageID
434
435 if opts.AccessIPv4 != "" {
436 server["accessIPv4"] = opts.AccessIPv4
437 }
438
439 if opts.AccessIPv6 != "" {
440 server["accessIPv6"] = opts.AccessIPv6
441 }
442
443 if opts.Metadata != nil {
444 server["metadata"] = opts.Metadata
445 }
446
447 if opts.Personality != nil {
448 encoded := base64.StdEncoding.EncodeToString(opts.Personality)
449 server["personality"] = &encoded
450 }
451
452 return map[string]interface{}{"rebuild": server}, nil
453}
454
455// Rebuild will reprovision the server according to the configuration options
456// provided in the RebuildOpts struct.
457func Rebuild(client *gophercloud.ServiceClient, id string, opts RebuildOptsBuilder) RebuildResult {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400458 var result RebuildResult
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700459
460 if id == "" {
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200461 result.Err = fmt.Errorf("ID is required")
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400462 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700463 }
464
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200465 reqBody, err := opts.ToServerRebuildMap()
466 if err != nil {
467 result.Err = err
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400468 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700469 }
470
Ash Wilson59fb6c42015-02-12 16:21:13 -0500471 _, result.Err = client.Request("POST", actionURL(client, id), gophercloud.RequestOpts{
472 JSONBody: &reqBody,
473 JSONResponse: &result.Body,
474 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700475 })
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200476
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400477 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700478}
479
Ash Wilson5f7cf182014-10-23 08:35:24 -0400480// ResizeOptsBuilder is an interface that allows extensions to override the default structure of
481// a Resize request.
482type ResizeOptsBuilder interface {
483 ToServerResizeMap() (map[string]interface{}, error)
484}
485
486// ResizeOpts represents the configuration options used to control a Resize operation.
487type ResizeOpts struct {
488 // FlavorRef is the ID of the flavor you wish your server to become.
489 FlavorRef string
490}
491
Alex Gaynor266e9332014-10-28 14:44:04 -0700492// 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 -0400493// Resize request.
494func (opts ResizeOpts) ToServerResizeMap() (map[string]interface{}, error) {
495 resize := map[string]interface{}{
496 "flavorRef": opts.FlavorRef,
497 }
498
499 return map[string]interface{}{"resize": resize}, nil
500}
501
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700502// Resize instructs the provider to change the flavor of the server.
Ash Wilson01626a32014-09-17 10:38:07 -0400503// Note that this implies rebuilding it.
504// Unfortunately, one cannot pass rebuild parameters to the resize function.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700505// When the resize completes, the server will be in RESIZE_VERIFY state.
506// While in this state, you can explore the use of the new server's configuration.
507// If you like it, call ConfirmResize() to commit the resize permanently.
508// Otherwise, call RevertResize() to restore the old configuration.
Ash Wilson9e87a922014-10-23 14:29:22 -0400509func Resize(client *gophercloud.ServiceClient, id string, opts ResizeOptsBuilder) ActionResult {
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200510 var res ActionResult
Ash Wilson5f7cf182014-10-23 08:35:24 -0400511 reqBody, err := opts.ToServerResizeMap()
512 if err != nil {
513 res.Err = err
514 return res
515 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200516
Ash Wilson59fb6c42015-02-12 16:21:13 -0500517 _, res.Err = client.Request("POST", actionURL(client, id), gophercloud.RequestOpts{
518 JSONBody: reqBody,
519 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700520 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200521
522 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700523}
524
525// ConfirmResize confirms a previous resize operation on a server.
526// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200527func ConfirmResize(client *gophercloud.ServiceClient, id string) ActionResult {
528 var res ActionResult
529
Ash Wilson59fb6c42015-02-12 16:21:13 -0500530 _, res.Err = client.Request("POST", actionURL(client, id), gophercloud.RequestOpts{
531 JSONBody: map[string]interface{}{"confirmResize": nil},
532 OkCodes: []int{204},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700533 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200534
535 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700536}
537
538// RevertResize cancels a previous resize operation on a server.
539// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200540func RevertResize(client *gophercloud.ServiceClient, id string) ActionResult {
541 var res ActionResult
542
Ash Wilson59fb6c42015-02-12 16:21:13 -0500543 _, res.Err = client.Request("POST", actionURL(client, id), gophercloud.RequestOpts{
544 JSONBody: map[string]interface{}{"revertResize": nil},
545 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700546 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200547
548 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700549}
Alex Gaynor39584a02014-10-28 13:59:21 -0700550
Alex Gaynor266e9332014-10-28 14:44:04 -0700551// RescueOptsBuilder is an interface that allows extensions to override the
552// default structure of a Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700553type RescueOptsBuilder interface {
554 ToServerRescueMap() (map[string]interface{}, error)
555}
556
Alex Gaynor266e9332014-10-28 14:44:04 -0700557// RescueOpts represents the configuration options used to control a Rescue
558// option.
Alex Gaynor39584a02014-10-28 13:59:21 -0700559type RescueOpts struct {
Alex Gaynor266e9332014-10-28 14:44:04 -0700560 // AdminPass is the desired administrative password for the instance in
Alex Gaynorcfec7722014-11-13 13:33:49 -0800561 // RESCUE mode. If it's left blank, the server will generate a password.
Alex Gaynor39584a02014-10-28 13:59:21 -0700562 AdminPass string
563}
564
Jon Perrittcc77da62014-11-16 13:14:21 -0700565// ToServerRescueMap formats a RescueOpts as a map that can be used as a JSON
Alex Gaynor266e9332014-10-28 14:44:04 -0700566// request body for the Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700567func (opts RescueOpts) ToServerRescueMap() (map[string]interface{}, error) {
568 server := make(map[string]interface{})
569 if opts.AdminPass != "" {
570 server["adminPass"] = opts.AdminPass
571 }
572 return map[string]interface{}{"rescue": server}, nil
573}
574
Alex Gaynor266e9332014-10-28 14:44:04 -0700575// Rescue instructs the provider to place the server into RESCUE mode.
Alex Gaynorfbe61bb2014-11-12 13:35:03 -0800576func Rescue(client *gophercloud.ServiceClient, id string, opts RescueOptsBuilder) RescueResult {
577 var result RescueResult
Alex Gaynor39584a02014-10-28 13:59:21 -0700578
579 if id == "" {
580 result.Err = fmt.Errorf("ID is required")
581 return result
582 }
583 reqBody, err := opts.ToServerRescueMap()
584 if err != nil {
585 result.Err = err
586 return result
587 }
588
Ash Wilson59fb6c42015-02-12 16:21:13 -0500589 _, result.Err = client.Request("POST", actionURL(client, id), gophercloud.RequestOpts{
590 JSONResponse: &result.Body,
591 JSONBody: &reqBody,
592 OkCodes: []int{200},
Alex Gaynor39584a02014-10-28 13:59:21 -0700593 })
594
595 return result
596}
Jon Perrittcc77da62014-11-16 13:14:21 -0700597
Jon Perritt789f8322014-11-21 08:20:04 -0700598// ResetMetadataOptsBuilder allows extensions to add additional parameters to the
599// Reset request.
600type ResetMetadataOptsBuilder interface {
601 ToMetadataResetMap() (map[string]interface{}, error)
Jon Perrittcc77da62014-11-16 13:14:21 -0700602}
603
Jon Perritt78c57ce2014-11-20 11:07:18 -0700604// MetadataOpts is a map that contains key-value pairs.
605type MetadataOpts map[string]string
Jon Perrittcc77da62014-11-16 13:14:21 -0700606
Jon Perritt789f8322014-11-21 08:20:04 -0700607// ToMetadataResetMap assembles a body for a Reset request based on the contents of a MetadataOpts.
608func (opts MetadataOpts) ToMetadataResetMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700609 return map[string]interface{}{"metadata": opts}, nil
610}
611
Jon Perritt78c57ce2014-11-20 11:07:18 -0700612// ToMetadataUpdateMap assembles a body for an Update request based on the contents of a MetadataOpts.
613func (opts MetadataOpts) ToMetadataUpdateMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700614 return map[string]interface{}{"metadata": opts}, nil
615}
616
Jon Perritt789f8322014-11-21 08:20:04 -0700617// ResetMetadata will create multiple new key-value pairs for the given server ID.
Jon Perrittcc77da62014-11-16 13:14:21 -0700618// Note: Using this operation will erase any already-existing metadata and create
619// the new metadata provided. To keep any already-existing metadata, use the
620// UpdateMetadatas or UpdateMetadata function.
Jon Perritt789f8322014-11-21 08:20:04 -0700621func ResetMetadata(client *gophercloud.ServiceClient, id string, opts ResetMetadataOptsBuilder) ResetMetadataResult {
622 var res ResetMetadataResult
623 metadata, err := opts.ToMetadataResetMap()
Jon Perrittcc77da62014-11-16 13:14:21 -0700624 if err != nil {
625 res.Err = err
626 return res
627 }
Ash Wilson59fb6c42015-02-12 16:21:13 -0500628 _, res.Err = client.Request("PUT", metadataURL(client, id), gophercloud.RequestOpts{
629 JSONBody: metadata,
630 JSONResponse: &res.Body,
Jon Perrittcc77da62014-11-16 13:14:21 -0700631 })
632 return res
633}
634
Jon Perritt78c57ce2014-11-20 11:07:18 -0700635// Metadata requests all the metadata for the given server ID.
636func Metadata(client *gophercloud.ServiceClient, id string) GetMetadataResult {
Jon Perrittcc77da62014-11-16 13:14:21 -0700637 var res GetMetadataResult
Ash Wilson59fb6c42015-02-12 16:21:13 -0500638 _, res.Err = client.Request("GET", metadataURL(client, id), gophercloud.RequestOpts{
639 JSONResponse: &res.Body,
Jon Perrittcc77da62014-11-16 13:14:21 -0700640 })
641 return res
642}
643
Jon Perritt78c57ce2014-11-20 11:07:18 -0700644// UpdateMetadataOptsBuilder allows extensions to add additional parameters to the
645// Create request.
646type UpdateMetadataOptsBuilder interface {
647 ToMetadataUpdateMap() (map[string]interface{}, error)
648}
649
650// UpdateMetadata updates (or creates) all the metadata specified by opts for the given server ID.
651// This operation does not affect already-existing metadata that is not specified
652// by opts.
653func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) UpdateMetadataResult {
654 var res UpdateMetadataResult
655 metadata, err := opts.ToMetadataUpdateMap()
656 if err != nil {
657 res.Err = err
658 return res
659 }
Ash Wilson59fb6c42015-02-12 16:21:13 -0500660 _, res.Err = client.Request("POST", metadataURL(client, id), gophercloud.RequestOpts{
661 JSONBody: metadata,
662 JSONResponse: &res.Body,
Jon Perritt78c57ce2014-11-20 11:07:18 -0700663 })
664 return res
665}
666
667// MetadatumOptsBuilder allows extensions to add additional parameters to the
668// Create request.
669type MetadatumOptsBuilder interface {
670 ToMetadatumCreateMap() (map[string]interface{}, string, error)
671}
672
673// MetadatumOpts is a map of length one that contains a key-value pair.
674type MetadatumOpts map[string]string
675
676// ToMetadatumCreateMap assembles a body for a Create request based on the contents of a MetadataumOpts.
677func (opts MetadatumOpts) ToMetadatumCreateMap() (map[string]interface{}, string, error) {
678 if len(opts) != 1 {
679 return nil, "", errors.New("CreateMetadatum operation must have 1 and only 1 key-value pair.")
680 }
681 metadatum := map[string]interface{}{"meta": opts}
682 var key string
683 for k := range metadatum["meta"].(MetadatumOpts) {
684 key = k
685 }
686 return metadatum, key, nil
687}
688
689// CreateMetadatum will create or update the key-value pair with the given key for the given server ID.
690func CreateMetadatum(client *gophercloud.ServiceClient, id string, opts MetadatumOptsBuilder) CreateMetadatumResult {
691 var res CreateMetadatumResult
692 metadatum, key, err := opts.ToMetadatumCreateMap()
693 if err != nil {
694 res.Err = err
695 return res
696 }
697
Ash Wilson59fb6c42015-02-12 16:21:13 -0500698 _, res.Err = client.Request("PUT", metadatumURL(client, id, key), gophercloud.RequestOpts{
699 JSONBody: metadatum,
700 JSONResponse: &res.Body,
Jon Perritt78c57ce2014-11-20 11:07:18 -0700701 })
702 return res
703}
704
705// Metadatum requests the key-value pair with the given key for the given server ID.
706func Metadatum(client *gophercloud.ServiceClient, id, key string) GetMetadatumResult {
707 var res GetMetadatumResult
Ash Wilson59fb6c42015-02-12 16:21:13 -0500708 _, res.Err = client.Request("GET", metadatumURL(client, id, key), gophercloud.RequestOpts{
709 JSONResponse: &res.Body,
Jon Perritt78c57ce2014-11-20 11:07:18 -0700710 })
711 return res
712}
713
714// DeleteMetadatum will delete the key-value pair with the given key for the given server ID.
715func DeleteMetadatum(client *gophercloud.ServiceClient, id, key string) DeleteMetadatumResult {
716 var res DeleteMetadatumResult
Ash Wilson59fb6c42015-02-12 16:21:13 -0500717 _, res.Err = client.Request("DELETE", metadatumURL(client, id, key), gophercloud.RequestOpts{
718 JSONResponse: &res.Body,
Jon Perrittcc77da62014-11-16 13:14:21 -0700719 })
720 return res
721}
Jon Perritt5cb49482015-02-19 12:19:58 -0700722
723// ListAddresses makes a request against the API to list the servers IP addresses.
724func ListAddresses(client *gophercloud.ServiceClient, id string) pagination.Pager {
725 createPageFn := func(r pagination.PageResult) pagination.Page {
726 return AddressPage{pagination.SinglePageBase(r)}
727 }
728 return pagination.NewPager(client, listAddressesURL(client, id), createPageFn)
729}