blob: da0c737b584e70ed004cf8c3aafc48be5fe60289 [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"
Kevin Pikea2bfaea2015-04-21 11:45:59 -07005 "encoding/json"
Jon Perrittcc77da62014-11-16 13:14:21 -07006 "errors"
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -07007 "fmt"
Ash Wilson01626a32014-09-17 10:38:07 -04008
Ash Wilson01626a32014-09-17 10:38:07 -04009 "github.com/rackspace/gophercloud"
10 "github.com/rackspace/gophercloud/pagination"
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080011)
12
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020013// ListOptsBuilder allows extensions to add additional parameters to the
14// List request.
15type ListOptsBuilder interface {
16 ToServerListQuery() (string, error)
17}
18
19// ListOpts allows the filtering and sorting of paginated collections through
20// the API. Filtering is achieved by passing in struct field values that map to
21// the server attributes you want to see returned. Marker and Limit are used
22// for pagination.
23type ListOpts struct {
24 // A time/date stamp for when the server last changed status.
25 ChangesSince string `q:"changes-since"`
26
27 // Name of the image in URL format.
28 Image string `q:"image"`
29
30 // Name of the flavor in URL format.
31 Flavor string `q:"flavor"`
32
33 // Name of the server as a string; can be queried with regular expressions.
34 // Realize that ?name=bob returns both bob and bobb. If you need to match bob
35 // only, you can use a regular expression matching the syntax of the
36 // underlying database server implemented for Compute.
37 Name string `q:"name"`
38
39 // Value of the status of the server so that you can filter on "ACTIVE" for example.
40 Status string `q:"status"`
41
42 // Name of the host as a string.
43 Host string `q:"host"`
44
45 // UUID of the server at which you want to set a marker.
46 Marker string `q:"marker"`
47
48 // Integer value for the limit of values to return.
49 Limit int `q:"limit"`
50}
51
52// ToServerListQuery formats a ListOpts into a query string.
53func (opts ListOpts) ToServerListQuery() (string, error) {
54 q, err := gophercloud.BuildQueryString(opts)
55 if err != nil {
56 return "", err
57 }
58 return q.String(), nil
59}
60
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080061// List makes a request against the API to list servers accessible to you.
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020062func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
63 url := listDetailURL(client)
64
65 if opts != nil {
66 query, err := opts.ToServerListQuery()
67 if err != nil {
68 return pagination.Pager{Err: err}
69 }
70 url += query
71 }
72
Ash Wilsonb8b16f82014-10-20 10:19:49 -040073 createPageFn := func(r pagination.PageResult) pagination.Page {
74 return ServerPage{pagination.LinkedPageBase{PageResult: r}}
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080075 }
76
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020077 return pagination.NewPager(client, url, createPageFn)
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080078}
79
Ash Wilson2206a112014-10-02 10:57:38 -040080// CreateOptsBuilder describes struct types that can be accepted by the Create call.
Ash Wilson6a310e02014-09-29 08:24:02 -040081// The CreateOpts struct in this package does.
Ash Wilson2206a112014-10-02 10:57:38 -040082type CreateOptsBuilder interface {
Jon Perritt4149d7c2014-10-23 21:23:46 -050083 ToServerCreateMap() (map[string]interface{}, error)
Ash Wilson6a310e02014-09-29 08:24:02 -040084}
85
86// Network is used within CreateOpts to control a new server's network attachments.
87type Network struct {
88 // UUID of a nova-network to attach to the newly provisioned server.
89 // Required unless Port is provided.
90 UUID string
91
92 // Port of a neutron network to attach to the newly provisioned server.
93 // Required unless UUID is provided.
94 Port string
95
96 // FixedIP [optional] specifies a fixed IPv4 address to be used on this network.
97 FixedIP string
98}
99
Kevin Pike92e10b52015-04-10 15:16:57 -0700100// Personality is an array of files that are injected into the server at launch.
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700101type Personality []*File
Kevin Pike92e10b52015-04-10 15:16:57 -0700102
103// File is used within CreateOpts and RebuildOpts to inject a file into the server at launch.
104type File struct {
105 // Path of the file
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700106 Path string
Kevin Pike92e10b52015-04-10 15:16:57 -0700107 // Contents of the file. Maximum content size is 255 bytes.
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700108 Contents []byte
Kevin Pike92e10b52015-04-10 15:16:57 -0700109}
110
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700111// MarshalJSON marshals the escaped file, base64 encoding the contents.
112func (f *File) MarshalJSON() ([]byte, error) {
113 file := struct {
114 Path string `json:"path"`
115 Contents string `json:"contents"`
116 }{
117 Path: f.Path,
118 Contents: base64.StdEncoding.EncodeToString(f.Contents),
Kevin Pike92e10b52015-04-10 15:16:57 -0700119 }
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700120 return json.Marshal(file)
Kevin Pike92e10b52015-04-10 15:16:57 -0700121}
122
Ash Wilson6a310e02014-09-29 08:24:02 -0400123// CreateOpts specifies server creation parameters.
124type CreateOpts struct {
125 // Name [required] is the name to assign to the newly launched server.
126 Name string
127
128 // ImageRef [required] is the ID or full URL to the image that contains the server's OS and initial state.
129 // Optional if using the boot-from-volume extension.
130 ImageRef string
131
132 // FlavorRef [required] is the ID or full URL to the flavor that describes the server's specs.
133 FlavorRef string
134
135 // SecurityGroups [optional] lists the names of the security groups to which this server should belong.
136 SecurityGroups []string
137
138 // UserData [optional] contains configuration information or scripts to use upon launch.
139 // Create will base64-encode it for you.
140 UserData []byte
141
142 // AvailabilityZone [optional] in which to launch the server.
143 AvailabilityZone string
144
145 // Networks [optional] dictates how this server will be attached to available networks.
146 // By default, the server will be attached to all isolated networks for the tenant.
147 Networks []Network
148
149 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
150 Metadata map[string]string
151
Kevin Pike92e10b52015-04-10 15:16:57 -0700152 // Personality [optional] includes files to inject into the server at launch.
153 // Create will base64-encode file contents for you.
154 Personality Personality
Ash Wilson6a310e02014-09-29 08:24:02 -0400155
156 // ConfigDrive [optional] enables metadata injection through a configuration drive.
157 ConfigDrive bool
Jon Perrittf3b2e142014-11-04 16:00:19 -0600158
159 // AdminPass [optional] sets the root user password. If not set, a randomly-generated
160 // password will be created and returned in the response.
161 AdminPass string
Jon Perritt7b9671c2015-02-01 22:03:14 -0700162
163 // AccessIPv4 [optional] specifies an IPv4 address for the instance.
164 AccessIPv4 string
165
166 // AccessIPv6 [optional] specifies an IPv6 address for the instance.
167 AccessIPv6 string
Ash Wilson6a310e02014-09-29 08:24:02 -0400168}
169
Ash Wilsone45c9732014-09-29 10:54:12 -0400170// ToServerCreateMap assembles a request body based on the contents of a CreateOpts.
Jon Perritt4149d7c2014-10-23 21:23:46 -0500171func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) {
Ash Wilson6a310e02014-09-29 08:24:02 -0400172 server := make(map[string]interface{})
173
174 server["name"] = opts.Name
175 server["imageRef"] = opts.ImageRef
176 server["flavorRef"] = opts.FlavorRef
177
178 if opts.UserData != nil {
179 encoded := base64.StdEncoding.EncodeToString(opts.UserData)
180 server["user_data"] = &encoded
181 }
Ash Wilson6a310e02014-09-29 08:24:02 -0400182 if opts.ConfigDrive {
183 server["config_drive"] = "true"
184 }
185 if opts.AvailabilityZone != "" {
186 server["availability_zone"] = opts.AvailabilityZone
187 }
188 if opts.Metadata != nil {
189 server["metadata"] = opts.Metadata
190 }
Jon Perrittf3b2e142014-11-04 16:00:19 -0600191 if opts.AdminPass != "" {
192 server["adminPass"] = opts.AdminPass
193 }
Jon Perritt7b9671c2015-02-01 22:03:14 -0700194 if opts.AccessIPv4 != "" {
195 server["accessIPv4"] = opts.AccessIPv4
196 }
197 if opts.AccessIPv6 != "" {
198 server["accessIPv6"] = opts.AccessIPv6
199 }
Ash Wilson6a310e02014-09-29 08:24:02 -0400200
201 if len(opts.SecurityGroups) > 0 {
202 securityGroups := make([]map[string]interface{}, len(opts.SecurityGroups))
203 for i, groupName := range opts.SecurityGroups {
204 securityGroups[i] = map[string]interface{}{"name": groupName}
205 }
eselldf709942014-11-13 21:07:11 -0700206 server["security_groups"] = securityGroups
Ash Wilson6a310e02014-09-29 08:24:02 -0400207 }
Jon Perritt2a7797d2014-10-21 15:08:43 -0500208
Ash Wilson6a310e02014-09-29 08:24:02 -0400209 if len(opts.Networks) > 0 {
210 networks := make([]map[string]interface{}, len(opts.Networks))
211 for i, net := range opts.Networks {
212 networks[i] = make(map[string]interface{})
213 if net.UUID != "" {
214 networks[i]["uuid"] = net.UUID
215 }
216 if net.Port != "" {
217 networks[i]["port"] = net.Port
218 }
219 if net.FixedIP != "" {
220 networks[i]["fixed_ip"] = net.FixedIP
221 }
222 }
Jon Perritt2a7797d2014-10-21 15:08:43 -0500223 server["networks"] = networks
Ash Wilson6a310e02014-09-29 08:24:02 -0400224 }
225
Kevin Pike92e10b52015-04-10 15:16:57 -0700226 if len(opts.Personality) > 0 {
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700227 server["personality"] = opts.Personality
Kevin Pike92e10b52015-04-10 15:16:57 -0700228 }
229
Jon Perritt4149d7c2014-10-23 21:23:46 -0500230 return map[string]interface{}{"server": server}, nil
Ash Wilson6a310e02014-09-29 08:24:02 -0400231}
232
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800233// Create requests a server to be provisioned to the user in the current tenant.
Ash Wilson2206a112014-10-02 10:57:38 -0400234func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jon Perritt4149d7c2014-10-23 21:23:46 -0500235 var res CreateResult
236
237 reqBody, err := opts.ToServerCreateMap()
238 if err != nil {
239 res.Err = err
240 return res
241 }
242
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100243 _, res.Err = client.Post(listURL(client), reqBody, &res.Body, nil)
Jon Perritt4149d7c2014-10-23 21:23:46 -0500244 return res
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800245}
246
247// Delete requests that a server previously provisioned be removed from your account.
Jamie Hannaford34732fe2014-10-27 11:29:36 +0100248func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
249 var res DeleteResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100250 _, res.Err = client.Delete(deleteURL(client, id), nil)
Jamie Hannaford34732fe2014-10-27 11:29:36 +0100251 return res
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800252}
253
Ash Wilson7ddf0362014-09-17 10:59:09 -0400254// Get requests details on a single server, by ID.
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400255func Get(client *gophercloud.ServiceClient, id string) GetResult {
256 var result GetResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100257 _, result.Err = client.Get(getURL(client, id), &result.Body, &gophercloud.RequestOpts{
258 OkCodes: []int{200, 203},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800259 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400260 return result
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800261}
262
Alex Gaynora6d5f9f2014-10-27 10:52:32 -0700263// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
Jon Perritt82048212014-10-13 22:33:13 -0500264type UpdateOptsBuilder interface {
Ash Wilsone45c9732014-09-29 10:54:12 -0400265 ToServerUpdateMap() map[string]interface{}
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400266}
267
268// UpdateOpts specifies the base attributes that may be updated on an existing server.
269type UpdateOpts struct {
270 // Name [optional] changes the displayed name of the server.
271 // The server host name will *not* change.
272 // Server names are not constrained to be unique, even within the same tenant.
273 Name string
274
275 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
276 AccessIPv4 string
277
278 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
279 AccessIPv6 string
280}
281
Ash Wilsone45c9732014-09-29 10:54:12 -0400282// ToServerUpdateMap formats an UpdateOpts structure into a request body.
283func (opts UpdateOpts) ToServerUpdateMap() map[string]interface{} {
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400284 server := make(map[string]string)
285 if opts.Name != "" {
286 server["name"] = opts.Name
287 }
288 if opts.AccessIPv4 != "" {
289 server["accessIPv4"] = opts.AccessIPv4
290 }
291 if opts.AccessIPv6 != "" {
292 server["accessIPv6"] = opts.AccessIPv6
293 }
294 return map[string]interface{}{"server": server}
295}
296
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800297// Update requests that various attributes of the indicated server be changed.
Jon Perritt82048212014-10-13 22:33:13 -0500298func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400299 var result UpdateResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100300 reqBody := opts.ToServerUpdateMap()
301 _, result.Err = client.Put(updateURL(client, id), reqBody, &result.Body, &gophercloud.RequestOpts{
302 OkCodes: []int{200},
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800303 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400304 return result
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800305}
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700306
Ash Wilson01626a32014-09-17 10:38:07 -0400307// ChangeAdminPassword alters the administrator or root password for a specified server.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200308func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) ActionResult {
Ash Wilsondc7daa82014-09-23 16:34:42 -0400309 var req struct {
310 ChangePassword struct {
311 AdminPass string `json:"adminPass"`
312 } `json:"changePassword"`
313 }
314
315 req.ChangePassword.AdminPass = newPassword
316
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200317 var res ActionResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100318 _, res.Err = client.Post(actionURL(client, id), req, nil, nil)
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200319 return res
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700320}
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700321
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700322// ErrArgument errors occur when an argument supplied to a package function
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700323// fails to fall within acceptable values. For example, the Reboot() function
324// expects the "how" parameter to be one of HardReboot or SoftReboot. These
325// constants are (currently) strings, leading someone to wonder if they can pass
326// other string values instead, perhaps in an effort to break the API of their
327// provider. Reboot() returns this error in this situation.
328//
329// Function identifies which function was called/which function is generating
330// the error.
331// Argument identifies which formal argument was responsible for producing the
332// error.
333// Value provides the value as it was passed into the function.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700334type ErrArgument struct {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700335 Function, Argument string
Jon Perritt30558642014-04-14 17:07:12 -0500336 Value interface{}
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700337}
338
339// Error yields a useful diagnostic for debugging purposes.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700340func (e *ErrArgument) Error() string {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700341 return fmt.Sprintf("Bad argument in call to %s, formal parameter %s, value %#v", e.Function, e.Argument, e.Value)
342}
343
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700344func (e *ErrArgument) String() string {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700345 return e.Error()
346}
347
Ash Wilson01626a32014-09-17 10:38:07 -0400348// RebootMethod describes the mechanisms by which a server reboot can be requested.
349type RebootMethod string
350
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700351// These constants determine how a server should be rebooted.
352// See the Reboot() function for further details.
353const (
Ash Wilson01626a32014-09-17 10:38:07 -0400354 SoftReboot RebootMethod = "SOFT"
355 HardReboot RebootMethod = "HARD"
356 OSReboot = SoftReboot
357 PowerCycle = HardReboot
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700358)
359
360// Reboot requests that a given server reboot.
361// Two methods exist for rebooting a server:
362//
Ash Wilson01626a32014-09-17 10:38:07 -0400363// HardReboot (aka PowerCycle) restarts the server instance by physically cutting power to the machine, or if a VM,
364// terminating it at the hypervisor level.
365// It's done. Caput. Full stop.
366// Then, after a brief while, power is restored or the VM instance restarted.
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700367//
Ash Wilson01626a32014-09-17 10:38:07 -0400368// SoftReboot (aka OSReboot) simply tells the OS to restart under its own procedures.
369// 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 +0200370func Reboot(client *gophercloud.ServiceClient, id string, how RebootMethod) ActionResult {
371 var res ActionResult
372
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700373 if (how != SoftReboot) && (how != HardReboot) {
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200374 res.Err = &ErrArgument{
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700375 Function: "Reboot",
376 Argument: "how",
Jon Perritt30558642014-04-14 17:07:12 -0500377 Value: how,
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700378 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200379 return res
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700380 }
Jon Perritt30558642014-04-14 17:07:12 -0500381
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100382 reqBody := struct {
383 C map[string]string `json:"reboot"`
384 }{
385 map[string]string{"type": string(how)},
386 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200387
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100388 _, res.Err = client.Post(actionURL(client, id), reqBody, nil, nil)
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200389 return res
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700390}
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700391
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200392// RebuildOptsBuilder is an interface that allows extensions to override the
393// default behaviour of rebuild options
394type RebuildOptsBuilder interface {
395 ToServerRebuildMap() (map[string]interface{}, error)
396}
397
398// RebuildOpts represents the configuration options used in a server rebuild
399// operation
400type RebuildOpts struct {
401 // Required. The ID of the image you want your server to be provisioned on
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200402 ImageID string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200403
404 // Name to set the server to
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200405 Name string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200406
407 // Required. The server's admin password
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200408 AdminPass string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200409
410 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200411 AccessIPv4 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200412
413 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200414 AccessIPv6 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200415
416 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200417 Metadata map[string]string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200418
Kevin Pike92e10b52015-04-10 15:16:57 -0700419 // Personality [optional] includes files to inject into the server at launch.
420 // Rebuild will base64-encode file contents for you.
421 Personality Personality
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200422}
423
424// ToServerRebuildMap formats a RebuildOpts struct into a map for use in JSON
425func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {
426 var err error
427 server := make(map[string]interface{})
428
429 if opts.AdminPass == "" {
430 err = fmt.Errorf("AdminPass is required")
431 }
432
433 if opts.ImageID == "" {
434 err = fmt.Errorf("ImageID is required")
435 }
436
437 if err != nil {
438 return server, err
439 }
440
441 server["name"] = opts.Name
442 server["adminPass"] = opts.AdminPass
443 server["imageRef"] = opts.ImageID
444
445 if opts.AccessIPv4 != "" {
446 server["accessIPv4"] = opts.AccessIPv4
447 }
448
449 if opts.AccessIPv6 != "" {
450 server["accessIPv6"] = opts.AccessIPv6
451 }
452
453 if opts.Metadata != nil {
454 server["metadata"] = opts.Metadata
455 }
456
Kevin Pike92e10b52015-04-10 15:16:57 -0700457 if len(opts.Personality) > 0 {
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700458 server["personality"] = opts.Personality
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200459 }
460
461 return map[string]interface{}{"rebuild": server}, nil
462}
463
464// Rebuild will reprovision the server according to the configuration options
465// provided in the RebuildOpts struct.
466func Rebuild(client *gophercloud.ServiceClient, id string, opts RebuildOptsBuilder) RebuildResult {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400467 var result RebuildResult
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700468
469 if id == "" {
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200470 result.Err = fmt.Errorf("ID is required")
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400471 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700472 }
473
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200474 reqBody, err := opts.ToServerRebuildMap()
475 if err != nil {
476 result.Err = err
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400477 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700478 }
479
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100480 _, result.Err = client.Post(actionURL(client, id), reqBody, &result.Body, nil)
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400481 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700482}
483
Ash Wilson5f7cf182014-10-23 08:35:24 -0400484// ResizeOptsBuilder is an interface that allows extensions to override the default structure of
485// a Resize request.
486type ResizeOptsBuilder interface {
487 ToServerResizeMap() (map[string]interface{}, error)
488}
489
490// ResizeOpts represents the configuration options used to control a Resize operation.
491type ResizeOpts struct {
492 // FlavorRef is the ID of the flavor you wish your server to become.
493 FlavorRef string
494}
495
Alex Gaynor266e9332014-10-28 14:44:04 -0700496// 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 -0400497// Resize request.
498func (opts ResizeOpts) ToServerResizeMap() (map[string]interface{}, error) {
499 resize := map[string]interface{}{
500 "flavorRef": opts.FlavorRef,
501 }
502
503 return map[string]interface{}{"resize": resize}, nil
504}
505
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700506// Resize instructs the provider to change the flavor of the server.
Ash Wilson01626a32014-09-17 10:38:07 -0400507// Note that this implies rebuilding it.
508// Unfortunately, one cannot pass rebuild parameters to the resize function.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700509// When the resize completes, the server will be in RESIZE_VERIFY state.
510// While in this state, you can explore the use of the new server's configuration.
511// If you like it, call ConfirmResize() to commit the resize permanently.
512// Otherwise, call RevertResize() to restore the old configuration.
Ash Wilson9e87a922014-10-23 14:29:22 -0400513func Resize(client *gophercloud.ServiceClient, id string, opts ResizeOptsBuilder) ActionResult {
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200514 var res ActionResult
Ash Wilson5f7cf182014-10-23 08:35:24 -0400515 reqBody, err := opts.ToServerResizeMap()
516 if err != nil {
517 res.Err = err
518 return res
519 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200520
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100521 _, res.Err = client.Post(actionURL(client, id), reqBody, nil, nil)
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200522 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
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100530 reqBody := map[string]interface{}{"confirmResize": nil}
531 _, res.Err = client.Post(actionURL(client, id), reqBody, nil, &gophercloud.RequestOpts{
532 OkCodes: []int{201, 202, 204},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700533 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200534 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700535}
536
537// RevertResize cancels a previous resize operation on a server.
538// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200539func RevertResize(client *gophercloud.ServiceClient, id string) ActionResult {
540 var res ActionResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100541 reqBody := map[string]interface{}{"revertResize": nil}
542 _, res.Err = client.Post(actionURL(client, id), reqBody, nil, nil)
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200543 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700544}
Alex Gaynor39584a02014-10-28 13:59:21 -0700545
Alex Gaynor266e9332014-10-28 14:44:04 -0700546// RescueOptsBuilder is an interface that allows extensions to override the
547// default structure of a Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700548type RescueOptsBuilder interface {
549 ToServerRescueMap() (map[string]interface{}, error)
550}
551
Alex Gaynor266e9332014-10-28 14:44:04 -0700552// RescueOpts represents the configuration options used to control a Rescue
553// option.
Alex Gaynor39584a02014-10-28 13:59:21 -0700554type RescueOpts struct {
Alex Gaynor266e9332014-10-28 14:44:04 -0700555 // AdminPass is the desired administrative password for the instance in
Alex Gaynorcfec7722014-11-13 13:33:49 -0800556 // RESCUE mode. If it's left blank, the server will generate a password.
Alex Gaynor39584a02014-10-28 13:59:21 -0700557 AdminPass string
558}
559
Jon Perrittcc77da62014-11-16 13:14:21 -0700560// ToServerRescueMap formats a RescueOpts as a map that can be used as a JSON
Alex Gaynor266e9332014-10-28 14:44:04 -0700561// request body for the Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700562func (opts RescueOpts) ToServerRescueMap() (map[string]interface{}, error) {
563 server := make(map[string]interface{})
564 if opts.AdminPass != "" {
565 server["adminPass"] = opts.AdminPass
566 }
567 return map[string]interface{}{"rescue": server}, nil
568}
569
Alex Gaynor266e9332014-10-28 14:44:04 -0700570// Rescue instructs the provider to place the server into RESCUE mode.
Alex Gaynorfbe61bb2014-11-12 13:35:03 -0800571func Rescue(client *gophercloud.ServiceClient, id string, opts RescueOptsBuilder) RescueResult {
572 var result RescueResult
Alex Gaynor39584a02014-10-28 13:59:21 -0700573
574 if id == "" {
575 result.Err = fmt.Errorf("ID is required")
576 return result
577 }
578 reqBody, err := opts.ToServerRescueMap()
579 if err != nil {
580 result.Err = err
581 return result
582 }
583
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100584 _, result.Err = client.Post(actionURL(client, id), reqBody, &result.Body, &gophercloud.RequestOpts{
585 OkCodes: []int{200},
Alex Gaynor39584a02014-10-28 13:59:21 -0700586 })
587
588 return result
589}
Jon Perrittcc77da62014-11-16 13:14:21 -0700590
Jon Perritt789f8322014-11-21 08:20:04 -0700591// ResetMetadataOptsBuilder allows extensions to add additional parameters to the
592// Reset request.
593type ResetMetadataOptsBuilder interface {
594 ToMetadataResetMap() (map[string]interface{}, error)
Jon Perrittcc77da62014-11-16 13:14:21 -0700595}
596
Jon Perritt78c57ce2014-11-20 11:07:18 -0700597// MetadataOpts is a map that contains key-value pairs.
598type MetadataOpts map[string]string
Jon Perrittcc77da62014-11-16 13:14:21 -0700599
Jon Perritt789f8322014-11-21 08:20:04 -0700600// ToMetadataResetMap assembles a body for a Reset request based on the contents of a MetadataOpts.
601func (opts MetadataOpts) ToMetadataResetMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700602 return map[string]interface{}{"metadata": opts}, nil
603}
604
Jon Perritt78c57ce2014-11-20 11:07:18 -0700605// ToMetadataUpdateMap assembles a body for an Update request based on the contents of a MetadataOpts.
606func (opts MetadataOpts) ToMetadataUpdateMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700607 return map[string]interface{}{"metadata": opts}, nil
608}
609
Jon Perritt789f8322014-11-21 08:20:04 -0700610// ResetMetadata will create multiple new key-value pairs for the given server ID.
Jon Perrittcc77da62014-11-16 13:14:21 -0700611// Note: Using this operation will erase any already-existing metadata and create
612// the new metadata provided. To keep any already-existing metadata, use the
613// UpdateMetadatas or UpdateMetadata function.
Jon Perritt789f8322014-11-21 08:20:04 -0700614func ResetMetadata(client *gophercloud.ServiceClient, id string, opts ResetMetadataOptsBuilder) ResetMetadataResult {
615 var res ResetMetadataResult
616 metadata, err := opts.ToMetadataResetMap()
Jon Perrittcc77da62014-11-16 13:14:21 -0700617 if err != nil {
618 res.Err = err
619 return res
620 }
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100621 _, res.Err = client.Put(metadataURL(client, id), metadata, &res.Body, &gophercloud.RequestOpts{
622 OkCodes: []int{200},
Jon Perrittcc77da62014-11-16 13:14:21 -0700623 })
624 return res
625}
626
Jon Perritt78c57ce2014-11-20 11:07:18 -0700627// Metadata requests all the metadata for the given server ID.
628func Metadata(client *gophercloud.ServiceClient, id string) GetMetadataResult {
Jon Perrittcc77da62014-11-16 13:14:21 -0700629 var res GetMetadataResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100630 _, res.Err = client.Get(metadataURL(client, id), &res.Body, nil)
Jon Perrittcc77da62014-11-16 13:14:21 -0700631 return res
632}
633
Jon Perritt78c57ce2014-11-20 11:07:18 -0700634// UpdateMetadataOptsBuilder allows extensions to add additional parameters to the
635// Create request.
636type UpdateMetadataOptsBuilder interface {
637 ToMetadataUpdateMap() (map[string]interface{}, error)
638}
639
640// UpdateMetadata updates (or creates) all the metadata specified by opts for the given server ID.
641// This operation does not affect already-existing metadata that is not specified
642// by opts.
643func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) UpdateMetadataResult {
644 var res UpdateMetadataResult
645 metadata, err := opts.ToMetadataUpdateMap()
646 if err != nil {
647 res.Err = err
648 return res
649 }
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100650 _, res.Err = client.Post(metadataURL(client, id), metadata, &res.Body, &gophercloud.RequestOpts{
651 OkCodes: []int{200},
Jon Perritt78c57ce2014-11-20 11:07:18 -0700652 })
653 return res
654}
655
656// MetadatumOptsBuilder allows extensions to add additional parameters to the
657// Create request.
658type MetadatumOptsBuilder interface {
659 ToMetadatumCreateMap() (map[string]interface{}, string, error)
660}
661
662// MetadatumOpts is a map of length one that contains a key-value pair.
663type MetadatumOpts map[string]string
664
665// ToMetadatumCreateMap assembles a body for a Create request based on the contents of a MetadataumOpts.
666func (opts MetadatumOpts) ToMetadatumCreateMap() (map[string]interface{}, string, error) {
667 if len(opts) != 1 {
668 return nil, "", errors.New("CreateMetadatum operation must have 1 and only 1 key-value pair.")
669 }
670 metadatum := map[string]interface{}{"meta": opts}
671 var key string
672 for k := range metadatum["meta"].(MetadatumOpts) {
673 key = k
674 }
675 return metadatum, key, nil
676}
677
678// CreateMetadatum will create or update the key-value pair with the given key for the given server ID.
679func CreateMetadatum(client *gophercloud.ServiceClient, id string, opts MetadatumOptsBuilder) CreateMetadatumResult {
680 var res CreateMetadatumResult
681 metadatum, key, err := opts.ToMetadatumCreateMap()
682 if err != nil {
683 res.Err = err
684 return res
685 }
686
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100687 _, res.Err = client.Put(metadatumURL(client, id, key), metadatum, &res.Body, &gophercloud.RequestOpts{
688 OkCodes: []int{200},
Jon Perritt78c57ce2014-11-20 11:07:18 -0700689 })
690 return res
691}
692
693// Metadatum requests the key-value pair with the given key for the given server ID.
694func Metadatum(client *gophercloud.ServiceClient, id, key string) GetMetadatumResult {
695 var res GetMetadatumResult
Ash Wilson59fb6c42015-02-12 16:21:13 -0500696 _, res.Err = client.Request("GET", metadatumURL(client, id, key), gophercloud.RequestOpts{
697 JSONResponse: &res.Body,
Jon Perritt78c57ce2014-11-20 11:07:18 -0700698 })
699 return res
700}
701
702// DeleteMetadatum will delete the key-value pair with the given key for the given server ID.
703func DeleteMetadatum(client *gophercloud.ServiceClient, id, key string) DeleteMetadatumResult {
704 var res DeleteMetadatumResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100705 _, res.Err = client.Delete(metadatumURL(client, id, key), &gophercloud.RequestOpts{
Ash Wilson59fb6c42015-02-12 16:21:13 -0500706 JSONResponse: &res.Body,
Jon Perrittcc77da62014-11-16 13:14:21 -0700707 })
708 return res
709}
Jon Perritt5cb49482015-02-19 12:19:58 -0700710
711// ListAddresses makes a request against the API to list the servers IP addresses.
712func ListAddresses(client *gophercloud.ServiceClient, id string) pagination.Pager {
713 createPageFn := func(r pagination.PageResult) pagination.Page {
714 return AddressPage{pagination.SinglePageBase(r)}
715 }
716 return pagination.NewPager(client, listAddressesURL(client, id), createPageFn)
717}
Jon Perritt04d073c2015-02-19 21:46:23 -0700718
719// ListAddressesByNetwork makes a request against the API to list the servers IP addresses
720// for the given network.
721func ListAddressesByNetwork(client *gophercloud.ServiceClient, id, network string) pagination.Pager {
722 createPageFn := func(r pagination.PageResult) pagination.Page {
723 return NetworkAddressPage{pagination.SinglePageBase(r)}
724 }
725 return pagination.NewPager(client, listAddressesByNetworkURL(client, id, network), createPageFn)
726}