blob: c862b4dccab838d1c6564dc37c4b9131bb0d9aa6 [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(),
Jamie Hannafordc530ba12015-03-23 17:50:46 +0100286 OkCodes: []int{200},
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800287 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400288 return result
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800289}
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700290
Ash Wilson01626a32014-09-17 10:38:07 -0400291// ChangeAdminPassword alters the administrator or root password for a specified server.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200292func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) ActionResult {
Ash Wilsondc7daa82014-09-23 16:34:42 -0400293 var req struct {
294 ChangePassword struct {
295 AdminPass string `json:"adminPass"`
296 } `json:"changePassword"`
297 }
298
299 req.ChangePassword.AdminPass = newPassword
300
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200301 var res ActionResult
302
Ash Wilson59fb6c42015-02-12 16:21:13 -0500303 _, res.Err = client.Request("POST", actionURL(client, id), gophercloud.RequestOpts{
304 JSONBody: req,
305 OkCodes: []int{202},
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700306 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200307
308 return res
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700309}
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700310
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700311// ErrArgument errors occur when an argument supplied to a package function
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700312// fails to fall within acceptable values. For example, the Reboot() function
313// expects the "how" parameter to be one of HardReboot or SoftReboot. These
314// constants are (currently) strings, leading someone to wonder if they can pass
315// other string values instead, perhaps in an effort to break the API of their
316// provider. Reboot() returns this error in this situation.
317//
318// Function identifies which function was called/which function is generating
319// the error.
320// Argument identifies which formal argument was responsible for producing the
321// error.
322// Value provides the value as it was passed into the function.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700323type ErrArgument struct {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700324 Function, Argument string
Jon Perritt30558642014-04-14 17:07:12 -0500325 Value interface{}
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700326}
327
328// Error yields a useful diagnostic for debugging purposes.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700329func (e *ErrArgument) Error() string {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700330 return fmt.Sprintf("Bad argument in call to %s, formal parameter %s, value %#v", e.Function, e.Argument, e.Value)
331}
332
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700333func (e *ErrArgument) String() string {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700334 return e.Error()
335}
336
Ash Wilson01626a32014-09-17 10:38:07 -0400337// RebootMethod describes the mechanisms by which a server reboot can be requested.
338type RebootMethod string
339
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700340// These constants determine how a server should be rebooted.
341// See the Reboot() function for further details.
342const (
Ash Wilson01626a32014-09-17 10:38:07 -0400343 SoftReboot RebootMethod = "SOFT"
344 HardReboot RebootMethod = "HARD"
345 OSReboot = SoftReboot
346 PowerCycle = HardReboot
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700347)
348
349// Reboot requests that a given server reboot.
350// Two methods exist for rebooting a server:
351//
Ash Wilson01626a32014-09-17 10:38:07 -0400352// HardReboot (aka PowerCycle) restarts the server instance by physically cutting power to the machine, or if a VM,
353// terminating it at the hypervisor level.
354// It's done. Caput. Full stop.
355// Then, after a brief while, power is restored or the VM instance restarted.
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700356//
Ash Wilson01626a32014-09-17 10:38:07 -0400357// SoftReboot (aka OSReboot) simply tells the OS to restart under its own procedures.
358// 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 +0200359func Reboot(client *gophercloud.ServiceClient, id string, how RebootMethod) ActionResult {
360 var res ActionResult
361
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700362 if (how != SoftReboot) && (how != HardReboot) {
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200363 res.Err = &ErrArgument{
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700364 Function: "Reboot",
365 Argument: "how",
Jon Perritt30558642014-04-14 17:07:12 -0500366 Value: how,
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700367 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200368 return res
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700369 }
Jon Perritt30558642014-04-14 17:07:12 -0500370
Ash Wilson59fb6c42015-02-12 16:21:13 -0500371 _, res.Err = client.Request("POST", actionURL(client, id), gophercloud.RequestOpts{
372 JSONBody: struct {
Jon Perritt30558642014-04-14 17:07:12 -0500373 C map[string]string `json:"reboot"`
374 }{
Ash Wilson01626a32014-09-17 10:38:07 -0400375 map[string]string{"type": string(how)},
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700376 },
Ash Wilson59fb6c42015-02-12 16:21:13 -0500377 OkCodes: []int{202},
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700378 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200379
380 return res
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700381}
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700382
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200383// RebuildOptsBuilder is an interface that allows extensions to override the
384// default behaviour of rebuild options
385type RebuildOptsBuilder interface {
386 ToServerRebuildMap() (map[string]interface{}, error)
387}
388
389// RebuildOpts represents the configuration options used in a server rebuild
390// operation
391type RebuildOpts struct {
392 // Required. The ID of the image you want your server to be provisioned on
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200393 ImageID string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200394
395 // Name to set the server to
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200396 Name string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200397
398 // Required. The server's admin password
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200399 AdminPass string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200400
401 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200402 AccessIPv4 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200403
404 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200405 AccessIPv6 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200406
407 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200408 Metadata map[string]string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200409
410 // Personality [optional] includes the path and contents of a file to inject into the server at launch.
411 // The maximum size of the file is 255 bytes (decoded).
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200412 Personality []byte
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200413}
414
415// ToServerRebuildMap formats a RebuildOpts struct into a map for use in JSON
416func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {
417 var err error
418 server := make(map[string]interface{})
419
420 if opts.AdminPass == "" {
421 err = fmt.Errorf("AdminPass is required")
422 }
423
424 if opts.ImageID == "" {
425 err = fmt.Errorf("ImageID is required")
426 }
427
428 if err != nil {
429 return server, err
430 }
431
432 server["name"] = opts.Name
433 server["adminPass"] = opts.AdminPass
434 server["imageRef"] = opts.ImageID
435
436 if opts.AccessIPv4 != "" {
437 server["accessIPv4"] = opts.AccessIPv4
438 }
439
440 if opts.AccessIPv6 != "" {
441 server["accessIPv6"] = opts.AccessIPv6
442 }
443
444 if opts.Metadata != nil {
445 server["metadata"] = opts.Metadata
446 }
447
448 if opts.Personality != nil {
449 encoded := base64.StdEncoding.EncodeToString(opts.Personality)
450 server["personality"] = &encoded
451 }
452
453 return map[string]interface{}{"rebuild": server}, nil
454}
455
456// Rebuild will reprovision the server according to the configuration options
457// provided in the RebuildOpts struct.
458func Rebuild(client *gophercloud.ServiceClient, id string, opts RebuildOptsBuilder) RebuildResult {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400459 var result RebuildResult
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700460
461 if id == "" {
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200462 result.Err = fmt.Errorf("ID is required")
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400463 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700464 }
465
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200466 reqBody, err := opts.ToServerRebuildMap()
467 if err != nil {
468 result.Err = err
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400469 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700470 }
471
Ash Wilson59fb6c42015-02-12 16:21:13 -0500472 _, result.Err = client.Request("POST", actionURL(client, id), gophercloud.RequestOpts{
473 JSONBody: &reqBody,
474 JSONResponse: &result.Body,
475 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700476 })
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200477
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400478 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700479}
480
Ash Wilson5f7cf182014-10-23 08:35:24 -0400481// ResizeOptsBuilder is an interface that allows extensions to override the default structure of
482// a Resize request.
483type ResizeOptsBuilder interface {
484 ToServerResizeMap() (map[string]interface{}, error)
485}
486
487// ResizeOpts represents the configuration options used to control a Resize operation.
488type ResizeOpts struct {
489 // FlavorRef is the ID of the flavor you wish your server to become.
490 FlavorRef string
491}
492
Alex Gaynor266e9332014-10-28 14:44:04 -0700493// 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 -0400494// Resize request.
495func (opts ResizeOpts) ToServerResizeMap() (map[string]interface{}, error) {
496 resize := map[string]interface{}{
497 "flavorRef": opts.FlavorRef,
498 }
499
500 return map[string]interface{}{"resize": resize}, nil
501}
502
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700503// Resize instructs the provider to change the flavor of the server.
Ash Wilson01626a32014-09-17 10:38:07 -0400504// Note that this implies rebuilding it.
505// Unfortunately, one cannot pass rebuild parameters to the resize function.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700506// When the resize completes, the server will be in RESIZE_VERIFY state.
507// While in this state, you can explore the use of the new server's configuration.
508// If you like it, call ConfirmResize() to commit the resize permanently.
509// Otherwise, call RevertResize() to restore the old configuration.
Ash Wilson9e87a922014-10-23 14:29:22 -0400510func Resize(client *gophercloud.ServiceClient, id string, opts ResizeOptsBuilder) ActionResult {
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200511 var res ActionResult
Ash Wilson5f7cf182014-10-23 08:35:24 -0400512 reqBody, err := opts.ToServerResizeMap()
513 if err != nil {
514 res.Err = err
515 return res
516 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200517
Ash Wilson59fb6c42015-02-12 16:21:13 -0500518 _, res.Err = client.Request("POST", actionURL(client, id), gophercloud.RequestOpts{
519 JSONBody: reqBody,
520 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700521 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200522
523 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700524}
525
526// ConfirmResize confirms a previous resize operation on a server.
527// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200528func ConfirmResize(client *gophercloud.ServiceClient, id string) ActionResult {
529 var res ActionResult
530
Ash Wilson59fb6c42015-02-12 16:21:13 -0500531 _, res.Err = client.Request("POST", actionURL(client, id), gophercloud.RequestOpts{
532 JSONBody: map[string]interface{}{"confirmResize": nil},
533 OkCodes: []int{204},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700534 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200535
536 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700537}
538
539// RevertResize cancels a previous resize operation on a server.
540// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200541func RevertResize(client *gophercloud.ServiceClient, id string) ActionResult {
542 var res ActionResult
543
Ash Wilson59fb6c42015-02-12 16:21:13 -0500544 _, res.Err = client.Request("POST", actionURL(client, id), gophercloud.RequestOpts{
545 JSONBody: map[string]interface{}{"revertResize": nil},
546 OkCodes: []int{202},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700547 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200548
549 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700550}
Alex Gaynor39584a02014-10-28 13:59:21 -0700551
Alex Gaynor266e9332014-10-28 14:44:04 -0700552// RescueOptsBuilder is an interface that allows extensions to override the
553// default structure of a Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700554type RescueOptsBuilder interface {
555 ToServerRescueMap() (map[string]interface{}, error)
556}
557
Alex Gaynor266e9332014-10-28 14:44:04 -0700558// RescueOpts represents the configuration options used to control a Rescue
559// option.
Alex Gaynor39584a02014-10-28 13:59:21 -0700560type RescueOpts struct {
Alex Gaynor266e9332014-10-28 14:44:04 -0700561 // AdminPass is the desired administrative password for the instance in
Alex Gaynorcfec7722014-11-13 13:33:49 -0800562 // RESCUE mode. If it's left blank, the server will generate a password.
Alex Gaynor39584a02014-10-28 13:59:21 -0700563 AdminPass string
564}
565
Jon Perrittcc77da62014-11-16 13:14:21 -0700566// ToServerRescueMap formats a RescueOpts as a map that can be used as a JSON
Alex Gaynor266e9332014-10-28 14:44:04 -0700567// request body for the Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700568func (opts RescueOpts) ToServerRescueMap() (map[string]interface{}, error) {
569 server := make(map[string]interface{})
570 if opts.AdminPass != "" {
571 server["adminPass"] = opts.AdminPass
572 }
573 return map[string]interface{}{"rescue": server}, nil
574}
575
Alex Gaynor266e9332014-10-28 14:44:04 -0700576// Rescue instructs the provider to place the server into RESCUE mode.
Alex Gaynorfbe61bb2014-11-12 13:35:03 -0800577func Rescue(client *gophercloud.ServiceClient, id string, opts RescueOptsBuilder) RescueResult {
578 var result RescueResult
Alex Gaynor39584a02014-10-28 13:59:21 -0700579
580 if id == "" {
581 result.Err = fmt.Errorf("ID is required")
582 return result
583 }
584 reqBody, err := opts.ToServerRescueMap()
585 if err != nil {
586 result.Err = err
587 return result
588 }
589
Ash Wilson59fb6c42015-02-12 16:21:13 -0500590 _, result.Err = client.Request("POST", actionURL(client, id), gophercloud.RequestOpts{
591 JSONResponse: &result.Body,
592 JSONBody: &reqBody,
593 OkCodes: []int{200},
Alex Gaynor39584a02014-10-28 13:59:21 -0700594 })
595
596 return result
597}
Jon Perrittcc77da62014-11-16 13:14:21 -0700598
Jon Perritt789f8322014-11-21 08:20:04 -0700599// ResetMetadataOptsBuilder allows extensions to add additional parameters to the
600// Reset request.
601type ResetMetadataOptsBuilder interface {
602 ToMetadataResetMap() (map[string]interface{}, error)
Jon Perrittcc77da62014-11-16 13:14:21 -0700603}
604
Jon Perritt78c57ce2014-11-20 11:07:18 -0700605// MetadataOpts is a map that contains key-value pairs.
606type MetadataOpts map[string]string
Jon Perrittcc77da62014-11-16 13:14:21 -0700607
Jon Perritt789f8322014-11-21 08:20:04 -0700608// ToMetadataResetMap assembles a body for a Reset request based on the contents of a MetadataOpts.
609func (opts MetadataOpts) ToMetadataResetMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700610 return map[string]interface{}{"metadata": opts}, nil
611}
612
Jon Perritt78c57ce2014-11-20 11:07:18 -0700613// ToMetadataUpdateMap assembles a body for an Update request based on the contents of a MetadataOpts.
614func (opts MetadataOpts) ToMetadataUpdateMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700615 return map[string]interface{}{"metadata": opts}, nil
616}
617
Jon Perritt789f8322014-11-21 08:20:04 -0700618// ResetMetadata will create multiple new key-value pairs for the given server ID.
Jon Perrittcc77da62014-11-16 13:14:21 -0700619// Note: Using this operation will erase any already-existing metadata and create
620// the new metadata provided. To keep any already-existing metadata, use the
621// UpdateMetadatas or UpdateMetadata function.
Jon Perritt789f8322014-11-21 08:20:04 -0700622func ResetMetadata(client *gophercloud.ServiceClient, id string, opts ResetMetadataOptsBuilder) ResetMetadataResult {
623 var res ResetMetadataResult
624 metadata, err := opts.ToMetadataResetMap()
Jon Perrittcc77da62014-11-16 13:14:21 -0700625 if err != nil {
626 res.Err = err
627 return res
628 }
Ash Wilson59fb6c42015-02-12 16:21:13 -0500629 _, res.Err = client.Request("PUT", metadataURL(client, id), gophercloud.RequestOpts{
630 JSONBody: metadata,
631 JSONResponse: &res.Body,
Jamie Hannafordc530ba12015-03-23 17:50:46 +0100632 OkCodes: []int{200},
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,
Jamie Hannafordc530ba12015-03-23 17:50:46 +0100665 OkCodes: []int{200},
Jon Perritt78c57ce2014-11-20 11:07:18 -0700666 })
667 return res
668}
669
670// MetadatumOptsBuilder allows extensions to add additional parameters to the
671// Create request.
672type MetadatumOptsBuilder interface {
673 ToMetadatumCreateMap() (map[string]interface{}, string, error)
674}
675
676// MetadatumOpts is a map of length one that contains a key-value pair.
677type MetadatumOpts map[string]string
678
679// ToMetadatumCreateMap assembles a body for a Create request based on the contents of a MetadataumOpts.
680func (opts MetadatumOpts) ToMetadatumCreateMap() (map[string]interface{}, string, error) {
681 if len(opts) != 1 {
682 return nil, "", errors.New("CreateMetadatum operation must have 1 and only 1 key-value pair.")
683 }
684 metadatum := map[string]interface{}{"meta": opts}
685 var key string
686 for k := range metadatum["meta"].(MetadatumOpts) {
687 key = k
688 }
689 return metadatum, key, nil
690}
691
692// CreateMetadatum will create or update the key-value pair with the given key for the given server ID.
693func CreateMetadatum(client *gophercloud.ServiceClient, id string, opts MetadatumOptsBuilder) CreateMetadatumResult {
694 var res CreateMetadatumResult
695 metadatum, key, err := opts.ToMetadatumCreateMap()
696 if err != nil {
697 res.Err = err
698 return res
699 }
700
Ash Wilson59fb6c42015-02-12 16:21:13 -0500701 _, res.Err = client.Request("PUT", metadatumURL(client, id, key), gophercloud.RequestOpts{
702 JSONBody: metadatum,
703 JSONResponse: &res.Body,
Jamie Hannafordc530ba12015-03-23 17:50:46 +0100704 OkCodes: []int{200},
Jon Perritt78c57ce2014-11-20 11:07:18 -0700705 })
706 return res
707}
708
709// Metadatum requests the key-value pair with the given key for the given server ID.
710func Metadatum(client *gophercloud.ServiceClient, id, key string) GetMetadatumResult {
711 var res GetMetadatumResult
Ash Wilson59fb6c42015-02-12 16:21:13 -0500712 _, res.Err = client.Request("GET", metadatumURL(client, id, key), gophercloud.RequestOpts{
713 JSONResponse: &res.Body,
Jon Perritt78c57ce2014-11-20 11:07:18 -0700714 })
715 return res
716}
717
718// DeleteMetadatum will delete the key-value pair with the given key for the given server ID.
719func DeleteMetadatum(client *gophercloud.ServiceClient, id, key string) DeleteMetadatumResult {
720 var res DeleteMetadatumResult
Ash Wilson59fb6c42015-02-12 16:21:13 -0500721 _, res.Err = client.Request("DELETE", metadatumURL(client, id, key), gophercloud.RequestOpts{
722 JSONResponse: &res.Body,
Jon Perrittcc77da62014-11-16 13:14:21 -0700723 })
724 return res
725}
Jon Perritt5cb49482015-02-19 12:19:58 -0700726
727// ListAddresses makes a request against the API to list the servers IP addresses.
728func ListAddresses(client *gophercloud.ServiceClient, id string) pagination.Pager {
729 createPageFn := func(r pagination.PageResult) pagination.Page {
730 return AddressPage{pagination.SinglePageBase(r)}
731 }
732 return pagination.NewPager(client, listAddressesURL(client, id), createPageFn)
733}
Jon Perritt04d073c2015-02-19 21:46:23 -0700734
735// ListAddressesByNetwork makes a request against the API to list the servers IP addresses
736// for the given network.
737func ListAddressesByNetwork(client *gophercloud.ServiceClient, id, network string) pagination.Pager {
738 createPageFn := func(r pagination.PageResult) pagination.Page {
739 return NetworkAddressPage{pagination.SinglePageBase(r)}
740 }
741 return pagination.NewPager(client, listAddressesByNetworkURL(client, id, network), createPageFn)
742}