blob: aa8c1a87b277c23347b4a10a4bb9c7938f23fbf7 [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}
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020017// ListOpts allows the filtering and sorting of paginated collections through
18// the API. Filtering is achieved by passing in struct field values that map to
19// the server attributes you want to see returned. Marker and Limit are used
20// for pagination.
21type ListOpts struct {
22 // A time/date stamp for when the server last changed status.
23 ChangesSince string `q:"changes-since"`
24
25 // Name of the image in URL format.
26 Image string `q:"image"`
27
28 // Name of the flavor in URL format.
29 Flavor string `q:"flavor"`
30
31 // Name of the server as a string; can be queried with regular expressions.
32 // Realize that ?name=bob returns both bob and bobb. If you need to match bob
33 // only, you can use a regular expression matching the syntax of the
34 // underlying database server implemented for Compute.
35 Name string `q:"name"`
36
37 // Value of the status of the server so that you can filter on "ACTIVE" for example.
38 Status string `q:"status"`
39
40 // Name of the host as a string.
41 Host string `q:"host"`
42
43 // UUID of the server at which you want to set a marker.
44 Marker string `q:"marker"`
45
46 // Integer value for the limit of values to return.
47 Limit int `q:"limit"`
48}
49
50// ToServerListQuery formats a ListOpts into a query string.
51func (opts ListOpts) ToServerListQuery() (string, error) {
52 q, err := gophercloud.BuildQueryString(opts)
53 if err != nil {
54 return "", err
55 }
56 return q.String(), nil
57}
58
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080059// List makes a request against the API to list servers accessible to you.
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020060func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
61 url := listDetailURL(client)
62
63 if opts != nil {
64 query, err := opts.ToServerListQuery()
65 if err != nil {
66 return pagination.Pager{Err: err}
67 }
68 url += query
69 }
70
Ash Wilsonb8b16f82014-10-20 10:19:49 -040071 createPageFn := func(r pagination.PageResult) pagination.Page {
72 return ServerPage{pagination.LinkedPageBase{PageResult: r}}
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080073 }
74
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020075 return pagination.NewPager(client, url, createPageFn)
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080076}
77
Ash Wilson2206a112014-10-02 10:57:38 -040078// CreateOptsBuilder describes struct types that can be accepted by the Create call.
Ash Wilson6a310e02014-09-29 08:24:02 -040079// The CreateOpts struct in this package does.
Ash Wilson2206a112014-10-02 10:57:38 -040080type CreateOptsBuilder interface {
Jon Perritt4149d7c2014-10-23 21:23:46 -050081 ToServerCreateMap() (map[string]interface{}, error)
Ash Wilson6a310e02014-09-29 08:24:02 -040082}
83
84// Network is used within CreateOpts to control a new server's network attachments.
85type Network struct {
86 // UUID of a nova-network to attach to the newly provisioned server.
87 // Required unless Port is provided.
88 UUID string
89
90 // Port of a neutron network to attach to the newly provisioned server.
91 // Required unless UUID is provided.
92 Port string
93
94 // FixedIP [optional] specifies a fixed IPv4 address to be used on this network.
95 FixedIP string
96}
97
98// CreateOpts specifies server creation parameters.
99type CreateOpts struct {
100 // Name [required] is the name to assign to the newly launched server.
101 Name string
102
103 // ImageRef [required] is the ID or full URL to the image that contains the server's OS and initial state.
104 // Optional if using the boot-from-volume extension.
105 ImageRef string
106
107 // FlavorRef [required] is the ID or full URL to the flavor that describes the server's specs.
108 FlavorRef string
109
110 // SecurityGroups [optional] lists the names of the security groups to which this server should belong.
111 SecurityGroups []string
112
113 // UserData [optional] contains configuration information or scripts to use upon launch.
114 // Create will base64-encode it for you.
115 UserData []byte
116
117 // AvailabilityZone [optional] in which to launch the server.
118 AvailabilityZone string
119
120 // Networks [optional] dictates how this server will be attached to available networks.
121 // By default, the server will be attached to all isolated networks for the tenant.
122 Networks []Network
123
124 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
125 Metadata map[string]string
126
127 // Personality [optional] includes the path and contents of a file to inject into the server at launch.
128 // The maximum size of the file is 255 bytes (decoded).
129 Personality []byte
130
131 // ConfigDrive [optional] enables metadata injection through a configuration drive.
132 ConfigDrive bool
Jon Perrittf3b2e142014-11-04 16:00:19 -0600133
134 // AdminPass [optional] sets the root user password. If not set, a randomly-generated
135 // password will be created and returned in the response.
136 AdminPass string
Jon Perritt7b9671c2015-02-01 22:03:14 -0700137
138 // AccessIPv4 [optional] specifies an IPv4 address for the instance.
139 AccessIPv4 string
140
141 // AccessIPv6 [optional] specifies an IPv6 address for the instance.
142 AccessIPv6 string
Ash Wilson6a310e02014-09-29 08:24:02 -0400143}
144
Ash Wilsone45c9732014-09-29 10:54:12 -0400145// ToServerCreateMap assembles a request body based on the contents of a CreateOpts.
Jon Perritt4149d7c2014-10-23 21:23:46 -0500146func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) {
Ash Wilson6a310e02014-09-29 08:24:02 -0400147 server := make(map[string]interface{})
148
149 server["name"] = opts.Name
150 server["imageRef"] = opts.ImageRef
151 server["flavorRef"] = opts.FlavorRef
152
153 if opts.UserData != nil {
154 encoded := base64.StdEncoding.EncodeToString(opts.UserData)
155 server["user_data"] = &encoded
156 }
157 if opts.Personality != nil {
158 encoded := base64.StdEncoding.EncodeToString(opts.Personality)
159 server["personality"] = &encoded
160 }
161 if opts.ConfigDrive {
162 server["config_drive"] = "true"
163 }
164 if opts.AvailabilityZone != "" {
165 server["availability_zone"] = opts.AvailabilityZone
166 }
167 if opts.Metadata != nil {
168 server["metadata"] = opts.Metadata
169 }
Jon Perrittf3b2e142014-11-04 16:00:19 -0600170 if opts.AdminPass != "" {
171 server["adminPass"] = opts.AdminPass
172 }
Jon Perritt7b9671c2015-02-01 22:03:14 -0700173 if opts.AccessIPv4 != "" {
174 server["accessIPv4"] = opts.AccessIPv4
175 }
176 if opts.AccessIPv6 != "" {
177 server["accessIPv6"] = opts.AccessIPv6
178 }
Ash Wilson6a310e02014-09-29 08:24:02 -0400179
180 if len(opts.SecurityGroups) > 0 {
181 securityGroups := make([]map[string]interface{}, len(opts.SecurityGroups))
182 for i, groupName := range opts.SecurityGroups {
183 securityGroups[i] = map[string]interface{}{"name": groupName}
184 }
eselldf709942014-11-13 21:07:11 -0700185 server["security_groups"] = securityGroups
Ash Wilson6a310e02014-09-29 08:24:02 -0400186 }
Jon Perritt2a7797d2014-10-21 15:08:43 -0500187
Ash Wilson6a310e02014-09-29 08:24:02 -0400188 if len(opts.Networks) > 0 {
189 networks := make([]map[string]interface{}, len(opts.Networks))
190 for i, net := range opts.Networks {
191 networks[i] = make(map[string]interface{})
192 if net.UUID != "" {
193 networks[i]["uuid"] = net.UUID
194 }
195 if net.Port != "" {
196 networks[i]["port"] = net.Port
197 }
198 if net.FixedIP != "" {
199 networks[i]["fixed_ip"] = net.FixedIP
200 }
201 }
Jon Perritt2a7797d2014-10-21 15:08:43 -0500202 server["networks"] = networks
Ash Wilson6a310e02014-09-29 08:24:02 -0400203 }
204
Jon Perritt4149d7c2014-10-23 21:23:46 -0500205 return map[string]interface{}{"server": server}, nil
Ash Wilson6a310e02014-09-29 08:24:02 -0400206}
207
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800208// Create requests a server to be provisioned to the user in the current tenant.
Ash Wilson2206a112014-10-02 10:57:38 -0400209func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jon Perritt4149d7c2014-10-23 21:23:46 -0500210 var res CreateResult
211
212 reqBody, err := opts.ToServerCreateMap()
213 if err != nil {
214 res.Err = err
215 return res
216 }
217
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100218 _, res.Err = client.Post(listURL(client), reqBody, &res.Body, nil)
Jon Perritt4149d7c2014-10-23 21:23:46 -0500219 return res
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800220}
221
222// Delete requests that a server previously provisioned be removed from your account.
Jamie Hannaford34732fe2014-10-27 11:29:36 +0100223func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
224 var res DeleteResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100225 _, res.Err = client.Delete(deleteURL(client, id), nil)
Jamie Hannaford34732fe2014-10-27 11:29:36 +0100226 return res
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800227}
228
Ash Wilson7ddf0362014-09-17 10:59:09 -0400229// Get requests details on a single server, by ID.
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400230func Get(client *gophercloud.ServiceClient, id string) GetResult {
231 var result GetResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100232 _, result.Err = client.Get(getURL(client, id), &result.Body, &gophercloud.RequestOpts{
233 OkCodes: []int{200, 203},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800234 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400235 return result
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800236}
237
Alex Gaynora6d5f9f2014-10-27 10:52:32 -0700238// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
Jon Perritt82048212014-10-13 22:33:13 -0500239type UpdateOptsBuilder interface {
Ash Wilsone45c9732014-09-29 10:54:12 -0400240 ToServerUpdateMap() map[string]interface{}
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400241}
242
243// UpdateOpts specifies the base attributes that may be updated on an existing server.
244type UpdateOpts struct {
245 // Name [optional] changes the displayed name of the server.
246 // The server host name will *not* change.
247 // Server names are not constrained to be unique, even within the same tenant.
248 Name string
249
250 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
251 AccessIPv4 string
252
253 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
254 AccessIPv6 string
255}
256
Ash Wilsone45c9732014-09-29 10:54:12 -0400257// ToServerUpdateMap formats an UpdateOpts structure into a request body.
258func (opts UpdateOpts) ToServerUpdateMap() map[string]interface{} {
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400259 server := make(map[string]string)
260 if opts.Name != "" {
261 server["name"] = opts.Name
262 }
263 if opts.AccessIPv4 != "" {
264 server["accessIPv4"] = opts.AccessIPv4
265 }
266 if opts.AccessIPv6 != "" {
267 server["accessIPv6"] = opts.AccessIPv6
268 }
269 return map[string]interface{}{"server": server}
270}
271
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800272// Update requests that various attributes of the indicated server be changed.
Jon Perritt82048212014-10-13 22:33:13 -0500273func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400274 var result UpdateResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100275 reqBody := opts.ToServerUpdateMap()
276 _, result.Err = client.Put(updateURL(client, id), reqBody, &result.Body, &gophercloud.RequestOpts{
277 OkCodes: []int{200},
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800278 })
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400279 return result
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800280}
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700281
Ash Wilson01626a32014-09-17 10:38:07 -0400282// ChangeAdminPassword alters the administrator or root password for a specified server.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200283func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) ActionResult {
Ash Wilsondc7daa82014-09-23 16:34:42 -0400284 var req struct {
285 ChangePassword struct {
286 AdminPass string `json:"adminPass"`
287 } `json:"changePassword"`
288 }
289
290 req.ChangePassword.AdminPass = newPassword
291
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200292 var res ActionResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100293 _, res.Err = client.Post(actionURL(client, id), req, nil, nil)
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200294 return res
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700295}
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700296
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700297// ErrArgument errors occur when an argument supplied to a package function
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700298// fails to fall within acceptable values. For example, the Reboot() function
299// expects the "how" parameter to be one of HardReboot or SoftReboot. These
300// constants are (currently) strings, leading someone to wonder if they can pass
301// other string values instead, perhaps in an effort to break the API of their
302// provider. Reboot() returns this error in this situation.
303//
304// Function identifies which function was called/which function is generating
305// the error.
306// Argument identifies which formal argument was responsible for producing the
307// error.
308// Value provides the value as it was passed into the function.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700309type ErrArgument struct {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700310 Function, Argument string
Jon Perritt30558642014-04-14 17:07:12 -0500311 Value interface{}
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700312}
313
314// Error yields a useful diagnostic for debugging purposes.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700315func (e *ErrArgument) Error() string {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700316 return fmt.Sprintf("Bad argument in call to %s, formal parameter %s, value %#v", e.Function, e.Argument, e.Value)
317}
318
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700319func (e *ErrArgument) String() string {
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700320 return e.Error()
321}
322
Ash Wilson01626a32014-09-17 10:38:07 -0400323// RebootMethod describes the mechanisms by which a server reboot can be requested.
324type RebootMethod string
325
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700326// These constants determine how a server should be rebooted.
327// See the Reboot() function for further details.
328const (
Ash Wilson01626a32014-09-17 10:38:07 -0400329 SoftReboot RebootMethod = "SOFT"
330 HardReboot RebootMethod = "HARD"
331 OSReboot = SoftReboot
332 PowerCycle = HardReboot
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700333)
334
335// Reboot requests that a given server reboot.
336// Two methods exist for rebooting a server:
337//
Ash Wilson01626a32014-09-17 10:38:07 -0400338// HardReboot (aka PowerCycle) restarts the server instance by physically cutting power to the machine, or if a VM,
339// terminating it at the hypervisor level.
340// It's done. Caput. Full stop.
341// Then, after a brief while, power is restored or the VM instance restarted.
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700342//
Ash Wilson01626a32014-09-17 10:38:07 -0400343// SoftReboot (aka OSReboot) simply tells the OS to restart under its own procedures.
344// 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 +0200345func Reboot(client *gophercloud.ServiceClient, id string, how RebootMethod) ActionResult {
346 var res ActionResult
347
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700348 if (how != SoftReboot) && (how != HardReboot) {
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200349 res.Err = &ErrArgument{
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700350 Function: "Reboot",
351 Argument: "how",
Jon Perritt30558642014-04-14 17:07:12 -0500352 Value: how,
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700353 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200354 return res
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700355 }
Jon Perritt30558642014-04-14 17:07:12 -0500356
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100357 reqBody := struct {
358 C map[string]string `json:"reboot"`
359 }{
360 map[string]string{"type": string(how)},
361 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200362
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100363 _, res.Err = client.Post(actionURL(client, id), reqBody, nil, nil)
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200364 return res
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700365}
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700366
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200367// RebuildOptsBuilder is an interface that allows extensions to override the
368// default behaviour of rebuild options
369type RebuildOptsBuilder interface {
370 ToServerRebuildMap() (map[string]interface{}, error)
371}
372
373// RebuildOpts represents the configuration options used in a server rebuild
374// operation
375type RebuildOpts struct {
376 // Required. The ID of the image you want your server to be provisioned on
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200377 ImageID string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200378
379 // Name to set the server to
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200380 Name string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200381
382 // Required. The server's admin password
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200383 AdminPass string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200384
385 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200386 AccessIPv4 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200387
388 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200389 AccessIPv6 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200390
391 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200392 Metadata map[string]string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200393
394 // Personality [optional] includes the path and contents of a file to inject into the server at launch.
395 // The maximum size of the file is 255 bytes (decoded).
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200396 Personality []byte
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200397}
398
399// ToServerRebuildMap formats a RebuildOpts struct into a map for use in JSON
400func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {
401 var err error
402 server := make(map[string]interface{})
403
404 if opts.AdminPass == "" {
405 err = fmt.Errorf("AdminPass is required")
406 }
407
408 if opts.ImageID == "" {
409 err = fmt.Errorf("ImageID is required")
410 }
411
412 if err != nil {
413 return server, err
414 }
415
416 server["name"] = opts.Name
417 server["adminPass"] = opts.AdminPass
418 server["imageRef"] = opts.ImageID
419
420 if opts.AccessIPv4 != "" {
421 server["accessIPv4"] = opts.AccessIPv4
422 }
423
424 if opts.AccessIPv6 != "" {
425 server["accessIPv6"] = opts.AccessIPv6
426 }
427
428 if opts.Metadata != nil {
429 server["metadata"] = opts.Metadata
430 }
431
432 if opts.Personality != nil {
433 encoded := base64.StdEncoding.EncodeToString(opts.Personality)
434 server["personality"] = &encoded
435 }
436
437 return map[string]interface{}{"rebuild": server}, nil
438}
439
440// Rebuild will reprovision the server according to the configuration options
441// provided in the RebuildOpts struct.
442func Rebuild(client *gophercloud.ServiceClient, id string, opts RebuildOptsBuilder) RebuildResult {
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400443 var result RebuildResult
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700444
445 if id == "" {
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200446 result.Err = fmt.Errorf("ID is required")
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400447 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700448 }
449
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200450 reqBody, err := opts.ToServerRebuildMap()
451 if err != nil {
452 result.Err = err
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400453 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700454 }
455
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100456 _, result.Err = client.Post(actionURL(client, id), reqBody, &result.Body, nil)
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400457 return result
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700458}
459
Ash Wilson5f7cf182014-10-23 08:35:24 -0400460// ResizeOptsBuilder is an interface that allows extensions to override the default structure of
461// a Resize request.
462type ResizeOptsBuilder interface {
463 ToServerResizeMap() (map[string]interface{}, error)
464}
465
466// ResizeOpts represents the configuration options used to control a Resize operation.
467type ResizeOpts struct {
468 // FlavorRef is the ID of the flavor you wish your server to become.
469 FlavorRef string
470}
471
Alex Gaynor266e9332014-10-28 14:44:04 -0700472// 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 -0400473// Resize request.
474func (opts ResizeOpts) ToServerResizeMap() (map[string]interface{}, error) {
475 resize := map[string]interface{}{
476 "flavorRef": opts.FlavorRef,
477 }
478
479 return map[string]interface{}{"resize": resize}, nil
480}
481
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700482// Resize instructs the provider to change the flavor of the server.
Ash Wilson01626a32014-09-17 10:38:07 -0400483// Note that this implies rebuilding it.
484// Unfortunately, one cannot pass rebuild parameters to the resize function.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700485// When the resize completes, the server will be in RESIZE_VERIFY state.
486// While in this state, you can explore the use of the new server's configuration.
487// If you like it, call ConfirmResize() to commit the resize permanently.
488// Otherwise, call RevertResize() to restore the old configuration.
Ash Wilson9e87a922014-10-23 14:29:22 -0400489func Resize(client *gophercloud.ServiceClient, id string, opts ResizeOptsBuilder) ActionResult {
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200490 var res ActionResult
Ash Wilson5f7cf182014-10-23 08:35:24 -0400491 reqBody, err := opts.ToServerResizeMap()
492 if err != nil {
493 res.Err = err
494 return res
495 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200496
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100497 _, res.Err = client.Post(actionURL(client, id), reqBody, nil, nil)
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200498 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700499}
500
501// ConfirmResize confirms a previous resize operation on a server.
502// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200503func ConfirmResize(client *gophercloud.ServiceClient, id string) ActionResult {
504 var res ActionResult
505
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100506 reqBody := map[string]interface{}{"confirmResize": nil}
507 _, res.Err = client.Post(actionURL(client, id), reqBody, nil, &gophercloud.RequestOpts{
508 OkCodes: []int{201, 202, 204},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700509 })
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200510 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700511}
512
513// RevertResize cancels a previous resize operation on a server.
514// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200515func RevertResize(client *gophercloud.ServiceClient, id string) ActionResult {
516 var res ActionResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100517 reqBody := map[string]interface{}{"revertResize": nil}
518 _, res.Err = client.Post(actionURL(client, id), reqBody, nil, nil)
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200519 return res
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700520}
Alex Gaynor39584a02014-10-28 13:59:21 -0700521
Alex Gaynor266e9332014-10-28 14:44:04 -0700522// RescueOptsBuilder is an interface that allows extensions to override the
523// default structure of a Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700524type RescueOptsBuilder interface {
525 ToServerRescueMap() (map[string]interface{}, error)
526}
527
Alex Gaynor266e9332014-10-28 14:44:04 -0700528// RescueOpts represents the configuration options used to control a Rescue
529// option.
Alex Gaynor39584a02014-10-28 13:59:21 -0700530type RescueOpts struct {
Alex Gaynor266e9332014-10-28 14:44:04 -0700531 // AdminPass is the desired administrative password for the instance in
Alex Gaynorcfec7722014-11-13 13:33:49 -0800532 // RESCUE mode. If it's left blank, the server will generate a password.
Alex Gaynor39584a02014-10-28 13:59:21 -0700533 AdminPass string
534}
535
Jon Perrittcc77da62014-11-16 13:14:21 -0700536// ToServerRescueMap formats a RescueOpts as a map that can be used as a JSON
Alex Gaynor266e9332014-10-28 14:44:04 -0700537// request body for the Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700538func (opts RescueOpts) ToServerRescueMap() (map[string]interface{}, error) {
539 server := make(map[string]interface{})
540 if opts.AdminPass != "" {
541 server["adminPass"] = opts.AdminPass
542 }
543 return map[string]interface{}{"rescue": server}, nil
544}
545
Alex Gaynor266e9332014-10-28 14:44:04 -0700546// Rescue instructs the provider to place the server into RESCUE mode.
Alex Gaynorfbe61bb2014-11-12 13:35:03 -0800547func Rescue(client *gophercloud.ServiceClient, id string, opts RescueOptsBuilder) RescueResult {
548 var result RescueResult
Alex Gaynor39584a02014-10-28 13:59:21 -0700549
550 if id == "" {
551 result.Err = fmt.Errorf("ID is required")
552 return result
553 }
554 reqBody, err := opts.ToServerRescueMap()
555 if err != nil {
556 result.Err = err
557 return result
558 }
559
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100560 _, result.Err = client.Post(actionURL(client, id), reqBody, &result.Body, &gophercloud.RequestOpts{
561 OkCodes: []int{200},
Alex Gaynor39584a02014-10-28 13:59:21 -0700562 })
563
564 return result
565}
Jon Perrittcc77da62014-11-16 13:14:21 -0700566
Jon Perritt789f8322014-11-21 08:20:04 -0700567// ResetMetadataOptsBuilder allows extensions to add additional parameters to the
568// Reset request.
569type ResetMetadataOptsBuilder interface {
570 ToMetadataResetMap() (map[string]interface{}, error)
Jon Perrittcc77da62014-11-16 13:14:21 -0700571}
572
Jon Perritt78c57ce2014-11-20 11:07:18 -0700573// MetadataOpts is a map that contains key-value pairs.
574type MetadataOpts map[string]string
Jon Perrittcc77da62014-11-16 13:14:21 -0700575
Jon Perritt789f8322014-11-21 08:20:04 -0700576// ToMetadataResetMap assembles a body for a Reset request based on the contents of a MetadataOpts.
577func (opts MetadataOpts) ToMetadataResetMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700578 return map[string]interface{}{"metadata": opts}, nil
579}
580
Jon Perritt78c57ce2014-11-20 11:07:18 -0700581// ToMetadataUpdateMap assembles a body for an Update request based on the contents of a MetadataOpts.
582func (opts MetadataOpts) ToMetadataUpdateMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700583 return map[string]interface{}{"metadata": opts}, nil
584}
585
Jon Perritt789f8322014-11-21 08:20:04 -0700586// ResetMetadata will create multiple new key-value pairs for the given server ID.
Jon Perrittcc77da62014-11-16 13:14:21 -0700587// Note: Using this operation will erase any already-existing metadata and create
588// the new metadata provided. To keep any already-existing metadata, use the
589// UpdateMetadatas or UpdateMetadata function.
Jon Perritt789f8322014-11-21 08:20:04 -0700590func ResetMetadata(client *gophercloud.ServiceClient, id string, opts ResetMetadataOptsBuilder) ResetMetadataResult {
591 var res ResetMetadataResult
592 metadata, err := opts.ToMetadataResetMap()
Jon Perrittcc77da62014-11-16 13:14:21 -0700593 if err != nil {
594 res.Err = err
595 return res
596 }
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100597 _, res.Err = client.Put(metadataURL(client, id), metadata, &res.Body, &gophercloud.RequestOpts{
598 OkCodes: []int{200},
Jon Perrittcc77da62014-11-16 13:14:21 -0700599 })
600 return res
601}
602
Jon Perritt78c57ce2014-11-20 11:07:18 -0700603// Metadata requests all the metadata for the given server ID.
604func Metadata(client *gophercloud.ServiceClient, id string) GetMetadataResult {
Jon Perrittcc77da62014-11-16 13:14:21 -0700605 var res GetMetadataResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100606 _, res.Err = client.Get(metadataURL(client, id), &res.Body, nil)
Jon Perrittcc77da62014-11-16 13:14:21 -0700607 return res
608}
609
Jon Perritt78c57ce2014-11-20 11:07:18 -0700610// UpdateMetadataOptsBuilder allows extensions to add additional parameters to the
611// Create request.
612type UpdateMetadataOptsBuilder interface {
613 ToMetadataUpdateMap() (map[string]interface{}, error)
614}
615
616// UpdateMetadata updates (or creates) all the metadata specified by opts for the given server ID.
617// This operation does not affect already-existing metadata that is not specified
618// by opts.
619func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) UpdateMetadataResult {
620 var res UpdateMetadataResult
621 metadata, err := opts.ToMetadataUpdateMap()
622 if err != nil {
623 res.Err = err
624 return res
625 }
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100626 _, res.Err = client.Post(metadataURL(client, id), metadata, &res.Body, &gophercloud.RequestOpts{
627 OkCodes: []int{200},
Jon Perritt78c57ce2014-11-20 11:07:18 -0700628 })
629 return res
630}
631
632// MetadatumOptsBuilder allows extensions to add additional parameters to the
633// Create request.
634type MetadatumOptsBuilder interface {
635 ToMetadatumCreateMap() (map[string]interface{}, string, error)
636}
637
638// MetadatumOpts is a map of length one that contains a key-value pair.
639type MetadatumOpts map[string]string
640
641// ToMetadatumCreateMap assembles a body for a Create request based on the contents of a MetadataumOpts.
642func (opts MetadatumOpts) ToMetadatumCreateMap() (map[string]interface{}, string, error) {
643 if len(opts) != 1 {
644 return nil, "", errors.New("CreateMetadatum operation must have 1 and only 1 key-value pair.")
645 }
646 metadatum := map[string]interface{}{"meta": opts}
647 var key string
648 for k := range metadatum["meta"].(MetadatumOpts) {
649 key = k
650 }
651 return metadatum, key, nil
652}
653
654// CreateMetadatum will create or update the key-value pair with the given key for the given server ID.
655func CreateMetadatum(client *gophercloud.ServiceClient, id string, opts MetadatumOptsBuilder) CreateMetadatumResult {
656 var res CreateMetadatumResult
657 metadatum, key, err := opts.ToMetadatumCreateMap()
658 if err != nil {
659 res.Err = err
660 return res
661 }
662
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100663 _, res.Err = client.Put(metadatumURL(client, id, key), metadatum, &res.Body, &gophercloud.RequestOpts{
664 OkCodes: []int{200},
Jon Perritt78c57ce2014-11-20 11:07:18 -0700665 })
666 return res
667}
668
669// Metadatum requests the key-value pair with the given key for the given server ID.
670func Metadatum(client *gophercloud.ServiceClient, id, key string) GetMetadatumResult {
671 var res GetMetadatumResult
Ash Wilson59fb6c42015-02-12 16:21:13 -0500672 _, res.Err = client.Request("GET", metadatumURL(client, id, key), gophercloud.RequestOpts{
673 JSONResponse: &res.Body,
Jon Perritt78c57ce2014-11-20 11:07:18 -0700674 })
675 return res
676}
677
678// DeleteMetadatum will delete the key-value pair with the given key for the given server ID.
679func DeleteMetadatum(client *gophercloud.ServiceClient, id, key string) DeleteMetadatumResult {
680 var res DeleteMetadatumResult
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100681 _, res.Err = client.Delete(metadatumURL(client, id, key), &gophercloud.RequestOpts{
Ash Wilson59fb6c42015-02-12 16:21:13 -0500682 JSONResponse: &res.Body,
Jon Perrittcc77da62014-11-16 13:14:21 -0700683 })
684 return res
685}
Jon Perritt5cb49482015-02-19 12:19:58 -0700686
687// ListAddresses makes a request against the API to list the servers IP addresses.
688func ListAddresses(client *gophercloud.ServiceClient, id string) pagination.Pager {
689 createPageFn := func(r pagination.PageResult) pagination.Page {
690 return AddressPage{pagination.SinglePageBase(r)}
691 }
692 return pagination.NewPager(client, listAddressesURL(client, id), createPageFn)
693}
Jon Perritt04d073c2015-02-19 21:46:23 -0700694
695// ListAddressesByNetwork makes a request against the API to list the servers IP addresses
696// for the given network.
697func ListAddressesByNetwork(client *gophercloud.ServiceClient, id, network string) pagination.Pager {
698 createPageFn := func(r pagination.PageResult) pagination.Page {
699 return NetworkAddressPage{pagination.SinglePageBase(r)}
700 }
701 return pagination.NewPager(client, listAddressesByNetworkURL(client, id, network), createPageFn)
702}
einarf2fc665e2015-04-16 20:16:21 +0000703
einarf4e5fdaf2015-04-16 23:14:59 +0000704type CreateImageOpts struct {
einarf2fc665e2015-04-16 20:16:21 +0000705 // Name [required] of the image/snapshot
706 Name string
707 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the created image.
708 Metadata map[string]string
709}
710
einarf4e5fdaf2015-04-16 23:14:59 +0000711type CreateImageOptsBuilder interface {
712 ToServerCreateImageMap() (map[string]interface{}, error)
einarf2fc665e2015-04-16 20:16:21 +0000713}
714
einarf4e5fdaf2015-04-16 23:14:59 +0000715// ToServerCreateImageMap formats a CreateImageOpts structure into a request body.
716func (opts CreateImageOpts) ToServerCreateImageMap() (map[string]interface{}, error) {
einarf2fc665e2015-04-16 20:16:21 +0000717 var err error
718 img := make(map[string]interface{})
719 if opts.Name == "" {
einarf4e5fdaf2015-04-16 23:14:59 +0000720 return nil, fmt.Errorf("Cannot create a server image without a name")
einarf2fc665e2015-04-16 20:16:21 +0000721 }
722 img["name"] = opts.Name
723 if opts.Metadata != nil {
724 img["metadata"] = opts.Metadata
725 }
726 createImage := make(map[string]interface{})
727 createImage["createImage"] = img
728 return createImage, err
729}
730
einarf4e5fdaf2015-04-16 23:14:59 +0000731// CreateImage makes a request against the nova API to schedule an image to be created of the server
732func CreateImage(client *gophercloud.ServiceClient, serverId string, opts CreateImageOptsBuilder) CreateImageResult {
733 var res CreateImageResult
734 reqBody, err := opts.ToServerCreateImageMap()
einarf2fc665e2015-04-16 20:16:21 +0000735 if err != nil {
736 res.Err = err
737 return res
738 }
739 response, err := client.Post(actionURL(client, serverId), reqBody, nil, &gophercloud.RequestOpts{
740 OkCodes: []int{202},
741 })
742 res.Err = err
einarf4e5fdaf2015-04-16 23:14:59 +0000743 res.Header = response.Header
einarf2fc665e2015-04-16 20:16:21 +0000744 return res
745}