blob: 4c26a68e8c5ea20468676dc9e8f0bbd86ac5a691 [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"
Ash Wilson01626a32014-09-17 10:38:07 -04006
Jon Perritt27249f42016-02-18 10:35:59 -06007 "github.com/gophercloud/gophercloud"
8 "github.com/gophercloud/gophercloud/openstack/compute/v2/flavors"
Jon Perritt994370e2016-02-18 15:23:34 -06009 "github.com/gophercloud/gophercloud/openstack/compute/v2/images"
Jon Perritt27249f42016-02-18 10:35:59 -060010 "github.com/gophercloud/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}
Kevin Pike9748b7b2015-05-05 07:34:07 -070018
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020019// 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"`
Daniel Speichert9342e522015-06-05 10:31:52 -040050
51 // Bool to show all tenants
52 AllTenants bool `q:"all_tenants"`
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020053}
54
55// ToServerListQuery formats a ListOpts into a query string.
56func (opts ListOpts) ToServerListQuery() (string, error) {
57 q, err := gophercloud.BuildQueryString(opts)
58 if err != nil {
59 return "", err
60 }
61 return q.String(), nil
62}
63
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080064// List makes a request against the API to list servers accessible to you.
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020065func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
66 url := listDetailURL(client)
67
68 if opts != nil {
69 query, err := opts.ToServerListQuery()
70 if err != nil {
71 return pagination.Pager{Err: err}
72 }
73 url += query
74 }
75
Ash Wilsonb8b16f82014-10-20 10:19:49 -040076 createPageFn := func(r pagination.PageResult) pagination.Page {
77 return ServerPage{pagination.LinkedPageBase{PageResult: r}}
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080078 }
79
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020080 return pagination.NewPager(client, url, createPageFn)
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080081}
82
Ash Wilson2206a112014-10-02 10:57:38 -040083// CreateOptsBuilder describes struct types that can be accepted by the Create call.
Ash Wilson6a310e02014-09-29 08:24:02 -040084// The CreateOpts struct in this package does.
Ash Wilson2206a112014-10-02 10:57:38 -040085type CreateOptsBuilder interface {
Jon Perritt4149d7c2014-10-23 21:23:46 -050086 ToServerCreateMap() (map[string]interface{}, error)
Ash Wilson6a310e02014-09-29 08:24:02 -040087}
88
89// Network is used within CreateOpts to control a new server's network attachments.
90type Network struct {
91 // UUID of a nova-network to attach to the newly provisioned server.
92 // Required unless Port is provided.
93 UUID string
94
95 // Port of a neutron network to attach to the newly provisioned server.
96 // Required unless UUID is provided.
97 Port string
98
99 // FixedIP [optional] specifies a fixed IPv4 address to be used on this network.
100 FixedIP string
101}
102
Kevin Pike92e10b52015-04-10 15:16:57 -0700103// Personality is an array of files that are injected into the server at launch.
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700104type Personality []*File
Kevin Pike92e10b52015-04-10 15:16:57 -0700105
106// File is used within CreateOpts and RebuildOpts to inject a file into the server at launch.
Kevin Pike9748b7b2015-05-05 07:34:07 -0700107// File implements the json.Marshaler interface, so when a Create or Rebuild operation is requested,
108// json.Marshal will call File's MarshalJSON method.
Kevin Pike92e10b52015-04-10 15:16:57 -0700109type File struct {
110 // Path of the file
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700111 Path string
Kevin Pike92e10b52015-04-10 15:16:57 -0700112 // Contents of the file. Maximum content size is 255 bytes.
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700113 Contents []byte
Kevin Pike92e10b52015-04-10 15:16:57 -0700114}
115
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700116// MarshalJSON marshals the escaped file, base64 encoding the contents.
117func (f *File) MarshalJSON() ([]byte, error) {
118 file := struct {
119 Path string `json:"path"`
120 Contents string `json:"contents"`
121 }{
122 Path: f.Path,
123 Contents: base64.StdEncoding.EncodeToString(f.Contents),
Kevin Pike92e10b52015-04-10 15:16:57 -0700124 }
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700125 return json.Marshal(file)
Kevin Pike92e10b52015-04-10 15:16:57 -0700126}
127
Ash Wilson6a310e02014-09-29 08:24:02 -0400128// CreateOpts specifies server creation parameters.
129type CreateOpts struct {
130 // Name [required] is the name to assign to the newly launched server.
jrperrittb1013232016-02-10 19:01:53 -0600131 Name string `b:"name,required"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400132
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600133 // ImageRef [optional; required if ImageName is not provided] is the ID or full
134 // URL to the image that contains the server's OS and initial state.
135 // Also optional if using the boot-from-volume extension.
Ash Wilson6a310e02014-09-29 08:24:02 -0400136 ImageRef string
137
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600138 // ImageName [optional; required if ImageRef is not provided] is the name of the
139 // image that contains the server's OS and initial state.
140 // Also optional if using the boot-from-volume extension.
141 ImageName string
142
143 // FlavorRef [optional; required if FlavorName is not provided] is the ID or
144 // full URL to the flavor that describes the server's specs.
Ash Wilson6a310e02014-09-29 08:24:02 -0400145 FlavorRef string
146
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600147 // FlavorName [optional; required if FlavorRef is not provided] is the name of
148 // the flavor that describes the server's specs.
149 FlavorName string
150
Ash Wilson6a310e02014-09-29 08:24:02 -0400151 // SecurityGroups [optional] lists the names of the security groups to which this server should belong.
152 SecurityGroups []string
153
154 // UserData [optional] contains configuration information or scripts to use upon launch.
155 // Create will base64-encode it for you.
156 UserData []byte
157
158 // AvailabilityZone [optional] in which to launch the server.
159 AvailabilityZone string
160
161 // Networks [optional] dictates how this server will be attached to available networks.
162 // By default, the server will be attached to all isolated networks for the tenant.
163 Networks []Network
164
165 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
166 Metadata map[string]string
167
Kevin Pike92e10b52015-04-10 15:16:57 -0700168 // Personality [optional] includes files to inject into the server at launch.
169 // Create will base64-encode file contents for you.
170 Personality Personality
Ash Wilson6a310e02014-09-29 08:24:02 -0400171
172 // ConfigDrive [optional] enables metadata injection through a configuration drive.
173 ConfigDrive bool
Jon Perrittf3b2e142014-11-04 16:00:19 -0600174
175 // AdminPass [optional] sets the root user password. If not set, a randomly-generated
Jon Perrittf094fef2016-03-07 01:41:59 -0600176 // password will be created and returned in the rponse.
Jon Perrittf3b2e142014-11-04 16:00:19 -0600177 AdminPass string
Jon Perritt7b9671c2015-02-01 22:03:14 -0700178
179 // AccessIPv4 [optional] specifies an IPv4 address for the instance.
180 AccessIPv4 string
181
182 // AccessIPv6 [optional] specifies an IPv6 address for the instance.
183 AccessIPv6 string
Jon Perritt994370e2016-02-18 15:23:34 -0600184
185 // ServiceClient [optional] will allow calls to be made to retrieve an image or
186 // flavor ID by name.
187 ServiceClient *gophercloud.ServiceClient
Ash Wilson6a310e02014-09-29 08:24:02 -0400188}
189
Ash Wilsone45c9732014-09-29 10:54:12 -0400190// ToServerCreateMap assembles a request body based on the contents of a CreateOpts.
Jon Perritt4149d7c2014-10-23 21:23:46 -0500191func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) {
Ash Wilson6a310e02014-09-29 08:24:02 -0400192 server := make(map[string]interface{})
193
194 server["name"] = opts.Name
195 server["imageRef"] = opts.ImageRef
196 server["flavorRef"] = opts.FlavorRef
197
198 if opts.UserData != nil {
199 encoded := base64.StdEncoding.EncodeToString(opts.UserData)
200 server["user_data"] = &encoded
201 }
Ash Wilson6a310e02014-09-29 08:24:02 -0400202 if opts.ConfigDrive {
203 server["config_drive"] = "true"
204 }
205 if opts.AvailabilityZone != "" {
206 server["availability_zone"] = opts.AvailabilityZone
207 }
208 if opts.Metadata != nil {
209 server["metadata"] = opts.Metadata
210 }
Jon Perrittf3b2e142014-11-04 16:00:19 -0600211 if opts.AdminPass != "" {
212 server["adminPass"] = opts.AdminPass
213 }
Jon Perritt7b9671c2015-02-01 22:03:14 -0700214 if opts.AccessIPv4 != "" {
215 server["accessIPv4"] = opts.AccessIPv4
216 }
217 if opts.AccessIPv6 != "" {
218 server["accessIPv6"] = opts.AccessIPv6
219 }
Ash Wilson6a310e02014-09-29 08:24:02 -0400220
221 if len(opts.SecurityGroups) > 0 {
222 securityGroups := make([]map[string]interface{}, len(opts.SecurityGroups))
223 for i, groupName := range opts.SecurityGroups {
224 securityGroups[i] = map[string]interface{}{"name": groupName}
225 }
eselldf709942014-11-13 21:07:11 -0700226 server["security_groups"] = securityGroups
Ash Wilson6a310e02014-09-29 08:24:02 -0400227 }
Jon Perritt2a7797d2014-10-21 15:08:43 -0500228
Ash Wilson6a310e02014-09-29 08:24:02 -0400229 if len(opts.Networks) > 0 {
230 networks := make([]map[string]interface{}, len(opts.Networks))
231 for i, net := range opts.Networks {
232 networks[i] = make(map[string]interface{})
233 if net.UUID != "" {
234 networks[i]["uuid"] = net.UUID
235 }
236 if net.Port != "" {
237 networks[i]["port"] = net.Port
238 }
239 if net.FixedIP != "" {
240 networks[i]["fixed_ip"] = net.FixedIP
241 }
242 }
Jon Perritt2a7797d2014-10-21 15:08:43 -0500243 server["networks"] = networks
Ash Wilson6a310e02014-09-29 08:24:02 -0400244 }
245
Kevin Pike92e10b52015-04-10 15:16:57 -0700246 if len(opts.Personality) > 0 {
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700247 server["personality"] = opts.Personality
Kevin Pike92e10b52015-04-10 15:16:57 -0700248 }
249
jrperrittb1013232016-02-10 19:01:53 -0600250 // If ImageRef isn't provided, use ImageName to ascertain the image ID.
251 if opts.ImageRef == "" {
252 if opts.ImageName == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600253 err := ErrNeitherImageIDNorImageNameProvided{}
254 err.Function = "servers.CreateOpts.ToServerCreateMap"
255 err.Argument = "ImageRef/ImageName"
256 return nil, err
jrperrittb1013232016-02-10 19:01:53 -0600257 }
Jon Perritt994370e2016-02-18 15:23:34 -0600258 if opts.ServiceClient == nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600259 err := ErrNoClientProvidedForIDByName{}
260 err.Function = "servers.CreateOpts.ToServerCreateMap"
261 err.Argument = "ServiceClient"
262 return nil, err
Jon Perritt994370e2016-02-18 15:23:34 -0600263 }
264 imageID, err := images.IDFromName(opts.ServiceClient, opts.ImageName)
jrperrittb1013232016-02-10 19:01:53 -0600265 if err != nil {
266 return nil, err
267 }
268 server["imageRef"] = imageID
269 }
270
271 // If FlavorRef isn't provided, use FlavorName to ascertain the flavor ID.
272 if opts.FlavorRef == "" {
273 if opts.FlavorName == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600274 err := ErrNeitherFlavorIDNorFlavorNameProvided{}
275 err.Function = "servers.CreateOpts.ToServerCreateMap"
276 err.Argument = "FlavorRef/FlavorName"
277 return nil, err
jrperrittb1013232016-02-10 19:01:53 -0600278 }
Jon Perritt994370e2016-02-18 15:23:34 -0600279 if opts.ServiceClient == nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600280 err := ErrNoClientProvidedForIDByName{}
281 err.Argument = "ServiceClient"
282 err.Function = "servers.CreateOpts.ToServerCreateMap"
283 return nil, err
Jon Perritt994370e2016-02-18 15:23:34 -0600284 }
285 flavorID, err := flavors.IDFromName(opts.ServiceClient, opts.FlavorName)
jrperrittb1013232016-02-10 19:01:53 -0600286 if err != nil {
287 return nil, err
288 }
289 server["flavorRef"] = flavorID
290 }
291
Jon Perritt4149d7c2014-10-23 21:23:46 -0500292 return map[string]interface{}{"server": server}, nil
Ash Wilson6a310e02014-09-29 08:24:02 -0400293}
294
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800295// Create requests a server to be provisioned to the user in the current tenant.
Ash Wilson2206a112014-10-02 10:57:38 -0400296func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600297 var r CreateResult
Jon Perritt4149d7c2014-10-23 21:23:46 -0500298
299 reqBody, err := opts.ToServerCreateMap()
300 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600301 r.Err = err
302 return r
Jon Perritt4149d7c2014-10-23 21:23:46 -0500303 }
304
Jon Perrittf094fef2016-03-07 01:41:59 -0600305 _, r.Err = client.Post(listURL(client), reqBody, &r.Body, nil)
306 return r
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800307}
308
309// Delete requests that a server previously provisioned be removed from your account.
Jamie Hannaford34732fe2014-10-27 11:29:36 +0100310func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600311 var r DeleteResult
312 _, r.Err = client.Delete(deleteURL(client, id), nil)
313 return r
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800314}
315
Ian Duffy370c4302016-01-21 10:44:56 +0000316func ForceDelete(client *gophercloud.ServiceClient, id string) ActionResult {
317 var req struct {
318 ForceDelete string `json:"forceDelete"`
319 }
320
Jon Perrittf094fef2016-03-07 01:41:59 -0600321 var r ActionResult
322 _, r.Err = client.Post(actionURL(client, id), req, nil, nil)
323 return r
Ian Duffy370c4302016-01-21 10:44:56 +0000324
325}
326
Ash Wilson7ddf0362014-09-17 10:59:09 -0400327// Get requests details on a single server, by ID.
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400328func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600329 var r GetResult
330 _, r.Err = client.Get(getURL(client, id), &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100331 OkCodes: []int{200, 203},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800332 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600333 return r
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800334}
335
Alex Gaynora6d5f9f2014-10-27 10:52:32 -0700336// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
Jon Perritt82048212014-10-13 22:33:13 -0500337type UpdateOptsBuilder interface {
Ash Wilsone45c9732014-09-29 10:54:12 -0400338 ToServerUpdateMap() map[string]interface{}
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400339}
340
341// UpdateOpts specifies the base attributes that may be updated on an existing server.
342type UpdateOpts struct {
343 // Name [optional] changes the displayed name of the server.
344 // The server host name will *not* change.
345 // Server names are not constrained to be unique, even within the same tenant.
346 Name string
347
348 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
349 AccessIPv4 string
350
351 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
352 AccessIPv6 string
353}
354
Ash Wilsone45c9732014-09-29 10:54:12 -0400355// ToServerUpdateMap formats an UpdateOpts structure into a request body.
356func (opts UpdateOpts) ToServerUpdateMap() map[string]interface{} {
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400357 server := make(map[string]string)
358 if opts.Name != "" {
359 server["name"] = opts.Name
360 }
361 if opts.AccessIPv4 != "" {
362 server["accessIPv4"] = opts.AccessIPv4
363 }
364 if opts.AccessIPv6 != "" {
365 server["accessIPv6"] = opts.AccessIPv6
366 }
367 return map[string]interface{}{"server": server}
368}
369
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800370// Update requests that various attributes of the indicated server be changed.
Jon Perritt82048212014-10-13 22:33:13 -0500371func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600372 var r UpdateResult
373 b := opts.ToServerUpdateMap()
374 _, r.Err = client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100375 OkCodes: []int{200},
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800376 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600377 return r
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800378}
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700379
Ash Wilson01626a32014-09-17 10:38:07 -0400380// ChangeAdminPassword alters the administrator or root password for a specified server.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200381func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) ActionResult {
Ash Wilsondc7daa82014-09-23 16:34:42 -0400382 var req struct {
383 ChangePassword struct {
384 AdminPass string `json:"adminPass"`
385 } `json:"changePassword"`
386 }
387
388 req.ChangePassword.AdminPass = newPassword
389
Jon Perrittf094fef2016-03-07 01:41:59 -0600390 var r ActionResult
391 _, r.Err = client.Post(actionURL(client, id), req, nil, nil)
392 return r
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700393}
394
Ash Wilson01626a32014-09-17 10:38:07 -0400395// RebootMethod describes the mechanisms by which a server reboot can be requested.
396type RebootMethod string
397
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700398// These constants determine how a server should be rebooted.
399// See the Reboot() function for further details.
400const (
Ash Wilson01626a32014-09-17 10:38:07 -0400401 SoftReboot RebootMethod = "SOFT"
402 HardReboot RebootMethod = "HARD"
403 OSReboot = SoftReboot
404 PowerCycle = HardReboot
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700405)
406
Jon Perrittf094fef2016-03-07 01:41:59 -0600407type RebootOptsBuilder interface {
408 ToServerRebootMap() (map[string]interface{}, error)
409}
410
411type RebootOpts struct {
412 Type RebootMethod
413}
414
415func (opts *RebootOpts) ToServerRebootMap() (map[string]interface{}, error) {
416 if (opts.Type != SoftReboot) && (opts.Type != HardReboot) {
417 err := &gophercloud.ErrInvalidInput{}
418 err.Argument = "servers.RebootOpts.Type"
419 err.Value = opts.Type
420 err.Function = "servers.Reboot"
421 return nil, err
422 }
423
424 reqBody := map[string]interface{}{
425 "reboot": map[string]interface{}{
426 "type": string(opts.Type),
427 },
428 }
429
430 return reqBody, nil
431}
432
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700433// Reboot requests that a given server reboot.
434// Two methods exist for rebooting a server:
435//
Jon Perrittf094fef2016-03-07 01:41:59 -0600436// HardReboot (aka PowerCycle) rtarts the server instance by physically cutting power to the machine, or if a VM,
Ash Wilson01626a32014-09-17 10:38:07 -0400437// terminating it at the hypervisor level.
438// It's done. Caput. Full stop.
Jon Perrittf094fef2016-03-07 01:41:59 -0600439// Then, after a brief while, power is rtored or the VM instance rtarted.
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700440//
Jon Perrittf094fef2016-03-07 01:41:59 -0600441// SoftReboot (aka OSReboot) simply tells the OS to rtart under its own procedur.
442// E.g., in Linux, asking it to enter runlevel 6, or executing "sudo shutdown -r now", or by asking Windows to rtart the machine.
443func Reboot(client *gophercloud.ServiceClient, id string, opts RebootOptsBuilder) ActionResult {
444 var r ActionResult
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200445
Jon Perrittf094fef2016-03-07 01:41:59 -0600446 reqBody, err := opts.ToServerRebootMap()
447 if err != nil {
448 r.Err = err
449 return r
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700450 }
Jon Perritt30558642014-04-14 17:07:12 -0500451
Jon Perrittf094fef2016-03-07 01:41:59 -0600452 _, r.Err = client.Post(actionURL(client, id), reqBody, nil, nil)
453 return r
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700454}
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700455
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200456// RebuildOptsBuilder is an interface that allows extensions to override the
457// default behaviour of rebuild options
458type RebuildOptsBuilder interface {
459 ToServerRebuildMap() (map[string]interface{}, error)
460}
461
462// RebuildOpts represents the configuration options used in a server rebuild
463// operation
464type RebuildOpts struct {
465 // Required. The ID of the image you want your server to be provisioned on
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200466 ImageID string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200467
468 // Name to set the server to
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200469 Name string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200470
471 // Required. The server's admin password
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200472 AdminPass string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200473
474 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200475 AccessIPv4 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200476
477 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200478 AccessIPv6 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200479
480 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200481 Metadata map[string]string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200482
Kevin Pike92e10b52015-04-10 15:16:57 -0700483 // Personality [optional] includes files to inject into the server at launch.
484 // Rebuild will base64-encode file contents for you.
485 Personality Personality
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200486}
487
488// ToServerRebuildMap formats a RebuildOpts struct into a map for use in JSON
489func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {
490 var err error
491 server := make(map[string]interface{})
492
493 if opts.AdminPass == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600494 err := ErrNoAdminPassProvided{}
495 err.Function = "servers.ToServerRebuildMap"
496 err.Argument = "AdminPass"
497 return nil, err
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200498 }
499
500 if opts.ImageID == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600501 err := ErrNoImageIDProvided{}
502 err.Function = "servers.ToServerRebuildMap"
503 err.Argument = "ImageID"
504 return nil, err
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200505 }
506
507 if err != nil {
508 return server, err
509 }
510
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200511 server["adminPass"] = opts.AdminPass
512 server["imageRef"] = opts.ImageID
513
Jon Perritt12395212016-02-24 10:41:17 -0600514 if opts.Name != "" {
515 server["name"] = opts.Name
516 }
517
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200518 if opts.AccessIPv4 != "" {
519 server["accessIPv4"] = opts.AccessIPv4
520 }
521
522 if opts.AccessIPv6 != "" {
523 server["accessIPv6"] = opts.AccessIPv6
524 }
525
526 if opts.Metadata != nil {
527 server["metadata"] = opts.Metadata
528 }
529
Kevin Pike92e10b52015-04-10 15:16:57 -0700530 if len(opts.Personality) > 0 {
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700531 server["personality"] = opts.Personality
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200532 }
533
534 return map[string]interface{}{"rebuild": server}, nil
535}
536
537// Rebuild will reprovision the server according to the configuration options
538// provided in the RebuildOpts struct.
539func Rebuild(client *gophercloud.ServiceClient, id string, opts RebuildOptsBuilder) RebuildResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600540 var r RebuildResult
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700541
542 if id == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600543 err := ErrNoIDProvided{}
544 err.Function = "servers.Rebuild"
545 err.Argument = "id"
546 r.Err = err
547 return r
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700548 }
549
Jon Perrittf094fef2016-03-07 01:41:59 -0600550 b, err := opts.ToServerRebuildMap()
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200551 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600552 r.Err = err
553 return r
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700554 }
555
Jon Perrittf094fef2016-03-07 01:41:59 -0600556 _, r.Err = client.Post(actionURL(client, id), b, &r.Body, nil)
557 return r
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700558}
559
Ash Wilson5f7cf182014-10-23 08:35:24 -0400560// ResizeOptsBuilder is an interface that allows extensions to override the default structure of
561// a Resize request.
562type ResizeOptsBuilder interface {
563 ToServerResizeMap() (map[string]interface{}, error)
564}
565
566// ResizeOpts represents the configuration options used to control a Resize operation.
567type ResizeOpts struct {
568 // FlavorRef is the ID of the flavor you wish your server to become.
569 FlavorRef string
570}
571
Alex Gaynor266e9332014-10-28 14:44:04 -0700572// 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 -0400573// Resize request.
574func (opts ResizeOpts) ToServerResizeMap() (map[string]interface{}, error) {
575 resize := map[string]interface{}{
576 "flavorRef": opts.FlavorRef,
577 }
578
579 return map[string]interface{}{"resize": resize}, nil
580}
581
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700582// Resize instructs the provider to change the flavor of the server.
Ash Wilson01626a32014-09-17 10:38:07 -0400583// Note that this implies rebuilding it.
Jon Perrittf094fef2016-03-07 01:41:59 -0600584// Unfortunately, one cannot pass rebuild parameters to the rize function.
585// When the rize completes, the server will be in RESIZE_VERIFY state.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700586// While in this state, you can explore the use of the new server's configuration.
Jon Perrittf094fef2016-03-07 01:41:59 -0600587// If you like it, call ConfirmResize() to commit the rize permanently.
588// Otherwise, call RevertResize() to rtore the old configuration.
Ash Wilson9e87a922014-10-23 14:29:22 -0400589func Resize(client *gophercloud.ServiceClient, id string, opts ResizeOptsBuilder) ActionResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600590 var r ActionResult
591
592 if id == "" {
593 err := ErrNoIDProvided{}
594 err.Function = "servers.Resize"
595 err.Argument = "id"
596 r.Err = err
597 return r
Ash Wilson5f7cf182014-10-23 08:35:24 -0400598 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200599
Jon Perrittf094fef2016-03-07 01:41:59 -0600600 b, err := opts.ToServerResizeMap()
601 if err != nil {
602 r.Err = err
603 return r
604 }
605
606 _, r.Err = client.Post(actionURL(client, id), b, nil, nil)
607 return r
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700608}
609
Jon Perrittf094fef2016-03-07 01:41:59 -0600610// ConfirmResize confirms a previous rize operation on a server.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700611// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200612func ConfirmResize(client *gophercloud.ServiceClient, id string) ActionResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600613 var r ActionResult
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200614
Jon Perrittf094fef2016-03-07 01:41:59 -0600615 if id == "" {
616 err := ErrNoIDProvided{}
617 err.Function = "servers.ConfirmResize"
618 err.Argument = "id"
619 r.Err = err
620 return r
621 }
622
623 b := map[string]interface{}{"confirmResize": nil}
624 _, r.Err = client.Post(actionURL(client, id), b, nil, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100625 OkCodes: []int{201, 202, 204},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700626 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600627 return r
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700628}
629
Jon Perrittf094fef2016-03-07 01:41:59 -0600630// RevertResize cancels a previous rize operation on a server.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700631// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200632func RevertResize(client *gophercloud.ServiceClient, id string) ActionResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600633 var r ActionResult
634
635 if id == "" {
636 err := ErrNoIDProvided{}
637 err.Function = "servers.RevertResize"
638 err.Argument = "id"
639 r.Err = err
640 return r
641 }
642
643 b := map[string]interface{}{"revertResize": nil}
644 _, r.Err = client.Post(actionURL(client, id), b, nil, nil)
645 return r
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700646}
Alex Gaynor39584a02014-10-28 13:59:21 -0700647
Alex Gaynor266e9332014-10-28 14:44:04 -0700648// RescueOptsBuilder is an interface that allows extensions to override the
649// default structure of a Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700650type RescueOptsBuilder interface {
651 ToServerRescueMap() (map[string]interface{}, error)
652}
653
Alex Gaynor266e9332014-10-28 14:44:04 -0700654// RescueOpts represents the configuration options used to control a Rescue
655// option.
Alex Gaynor39584a02014-10-28 13:59:21 -0700656type RescueOpts struct {
Alex Gaynor266e9332014-10-28 14:44:04 -0700657 // AdminPass is the desired administrative password for the instance in
Alex Gaynorcfec7722014-11-13 13:33:49 -0800658 // RESCUE mode. If it's left blank, the server will generate a password.
Alex Gaynor39584a02014-10-28 13:59:21 -0700659 AdminPass string
660}
661
Jon Perrittcc77da62014-11-16 13:14:21 -0700662// ToServerRescueMap formats a RescueOpts as a map that can be used as a JSON
Alex Gaynor266e9332014-10-28 14:44:04 -0700663// request body for the Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700664func (opts RescueOpts) ToServerRescueMap() (map[string]interface{}, error) {
665 server := make(map[string]interface{})
666 if opts.AdminPass != "" {
667 server["adminPass"] = opts.AdminPass
668 }
669 return map[string]interface{}{"rescue": server}, nil
670}
671
Alex Gaynor266e9332014-10-28 14:44:04 -0700672// Rescue instructs the provider to place the server into RESCUE mode.
Alex Gaynorfbe61bb2014-11-12 13:35:03 -0800673func Rescue(client *gophercloud.ServiceClient, id string, opts RescueOptsBuilder) RescueResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600674 var r RescueResult
Alex Gaynor39584a02014-10-28 13:59:21 -0700675
676 if id == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600677 err := ErrNoIDProvided{}
678 err.Function = "servers.Rebuild"
679 err.Argument = "id"
680 r.Err = err
681 return r
Alex Gaynor39584a02014-10-28 13:59:21 -0700682 }
683
Jon Perrittf094fef2016-03-07 01:41:59 -0600684 b, err := opts.ToServerRescueMap()
685 if err != nil {
686 r.Err = err
687 return r
688 }
689
690 _, r.Err = client.Post(actionURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100691 OkCodes: []int{200},
Alex Gaynor39584a02014-10-28 13:59:21 -0700692 })
693
Jon Perrittf094fef2016-03-07 01:41:59 -0600694 return r
Alex Gaynor39584a02014-10-28 13:59:21 -0700695}
Jon Perrittcc77da62014-11-16 13:14:21 -0700696
Jon Perritt789f8322014-11-21 08:20:04 -0700697// ResetMetadataOptsBuilder allows extensions to add additional parameters to the
698// Reset request.
699type ResetMetadataOptsBuilder interface {
700 ToMetadataResetMap() (map[string]interface{}, error)
Jon Perrittcc77da62014-11-16 13:14:21 -0700701}
702
Jon Perritt78c57ce2014-11-20 11:07:18 -0700703// MetadataOpts is a map that contains key-value pairs.
704type MetadataOpts map[string]string
Jon Perrittcc77da62014-11-16 13:14:21 -0700705
Jon Perritt789f8322014-11-21 08:20:04 -0700706// ToMetadataResetMap assembles a body for a Reset request based on the contents of a MetadataOpts.
707func (opts MetadataOpts) ToMetadataResetMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700708 return map[string]interface{}{"metadata": opts}, nil
709}
710
Jon Perritt78c57ce2014-11-20 11:07:18 -0700711// ToMetadataUpdateMap assembles a body for an Update request based on the contents of a MetadataOpts.
712func (opts MetadataOpts) ToMetadataUpdateMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700713 return map[string]interface{}{"metadata": opts}, nil
714}
715
Jon Perritt789f8322014-11-21 08:20:04 -0700716// ResetMetadata will create multiple new key-value pairs for the given server ID.
Jon Perrittcc77da62014-11-16 13:14:21 -0700717// Note: Using this operation will erase any already-existing metadata and create
718// the new metadata provided. To keep any already-existing metadata, use the
719// UpdateMetadatas or UpdateMetadata function.
Jon Perritt789f8322014-11-21 08:20:04 -0700720func ResetMetadata(client *gophercloud.ServiceClient, id string, opts ResetMetadataOptsBuilder) ResetMetadataResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600721 var r ResetMetadataResult
Jon Perritt789f8322014-11-21 08:20:04 -0700722 metadata, err := opts.ToMetadataResetMap()
Jon Perrittcc77da62014-11-16 13:14:21 -0700723 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600724 r.Err = err
725 return r
Jon Perrittcc77da62014-11-16 13:14:21 -0700726 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600727 _, r.Err = client.Put(metadataURL(client, id), metadata, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100728 OkCodes: []int{200},
Jon Perrittcc77da62014-11-16 13:14:21 -0700729 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600730 return r
Jon Perrittcc77da62014-11-16 13:14:21 -0700731}
732
Jon Perritt78c57ce2014-11-20 11:07:18 -0700733// Metadata requests all the metadata for the given server ID.
734func Metadata(client *gophercloud.ServiceClient, id string) GetMetadataResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600735 var r GetMetadataResult
736 _, r.Err = client.Get(metadataURL(client, id), &r.Body, nil)
737 return r
Jon Perrittcc77da62014-11-16 13:14:21 -0700738}
739
Jon Perritt78c57ce2014-11-20 11:07:18 -0700740// UpdateMetadataOptsBuilder allows extensions to add additional parameters to the
741// Create request.
742type UpdateMetadataOptsBuilder interface {
743 ToMetadataUpdateMap() (map[string]interface{}, error)
744}
745
746// UpdateMetadata updates (or creates) all the metadata specified by opts for the given server ID.
747// This operation does not affect already-existing metadata that is not specified
748// by opts.
749func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) UpdateMetadataResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600750 var r UpdateMetadataResult
Jon Perritt78c57ce2014-11-20 11:07:18 -0700751 metadata, err := opts.ToMetadataUpdateMap()
752 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600753 r.Err = err
754 return r
Jon Perritt78c57ce2014-11-20 11:07:18 -0700755 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600756 _, r.Err = client.Post(metadataURL(client, id), metadata, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100757 OkCodes: []int{200},
Jon Perritt78c57ce2014-11-20 11:07:18 -0700758 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600759 return r
Jon Perritt78c57ce2014-11-20 11:07:18 -0700760}
761
762// MetadatumOptsBuilder allows extensions to add additional parameters to the
763// Create request.
764type MetadatumOptsBuilder interface {
765 ToMetadatumCreateMap() (map[string]interface{}, string, error)
766}
767
768// MetadatumOpts is a map of length one that contains a key-value pair.
769type MetadatumOpts map[string]string
770
771// ToMetadatumCreateMap assembles a body for a Create request based on the contents of a MetadataumOpts.
772func (opts MetadatumOpts) ToMetadatumCreateMap() (map[string]interface{}, string, error) {
773 if len(opts) != 1 {
Jon Perritt13808262016-03-09 00:50:12 -0600774 err := gophercloud.ErrInvalidInput{}
775 err.Argument = "servers.MetadatumOpts"
776 err.Info = "Must have 1 and only 1 key-value pair"
777 return nil, "", err
Jon Perritt78c57ce2014-11-20 11:07:18 -0700778 }
779 metadatum := map[string]interface{}{"meta": opts}
780 var key string
781 for k := range metadatum["meta"].(MetadatumOpts) {
782 key = k
783 }
784 return metadatum, key, nil
785}
786
787// CreateMetadatum will create or update the key-value pair with the given key for the given server ID.
788func CreateMetadatum(client *gophercloud.ServiceClient, id string, opts MetadatumOptsBuilder) CreateMetadatumResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600789 var r CreateMetadatumResult
Jon Perritt78c57ce2014-11-20 11:07:18 -0700790 metadatum, key, err := opts.ToMetadatumCreateMap()
791 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600792 r.Err = err
793 return r
Jon Perritt78c57ce2014-11-20 11:07:18 -0700794 }
795
Jon Perrittf094fef2016-03-07 01:41:59 -0600796 _, r.Err = client.Put(metadatumURL(client, id, key), metadatum, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100797 OkCodes: []int{200},
Jon Perritt78c57ce2014-11-20 11:07:18 -0700798 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600799 return r
Jon Perritt78c57ce2014-11-20 11:07:18 -0700800}
801
802// Metadatum requests the key-value pair with the given key for the given server ID.
803func Metadatum(client *gophercloud.ServiceClient, id, key string) GetMetadatumResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600804 var r GetMetadatumResult
805 _, r.Err = client.Request("GET", metadatumURL(client, id, key), &gophercloud.RequestOpts{
806 JSONResponse: &r.Body,
Jon Perritt78c57ce2014-11-20 11:07:18 -0700807 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600808 return r
Jon Perritt78c57ce2014-11-20 11:07:18 -0700809}
810
811// DeleteMetadatum will delete the key-value pair with the given key for the given server ID.
812func DeleteMetadatum(client *gophercloud.ServiceClient, id, key string) DeleteMetadatumResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600813 var r DeleteMetadatumResult
814 _, r.Err = client.Delete(metadatumURL(client, id, key), nil)
815 return r
Jon Perrittcc77da62014-11-16 13:14:21 -0700816}
Jon Perritt5cb49482015-02-19 12:19:58 -0700817
818// ListAddresses makes a request against the API to list the servers IP addresses.
819func ListAddresses(client *gophercloud.ServiceClient, id string) pagination.Pager {
820 createPageFn := func(r pagination.PageResult) pagination.Page {
821 return AddressPage{pagination.SinglePageBase(r)}
822 }
823 return pagination.NewPager(client, listAddressesURL(client, id), createPageFn)
824}
Jon Perritt04d073c2015-02-19 21:46:23 -0700825
826// ListAddressesByNetwork makes a request against the API to list the servers IP addresses
827// for the given network.
828func ListAddressesByNetwork(client *gophercloud.ServiceClient, id, network string) pagination.Pager {
829 createPageFn := func(r pagination.PageResult) pagination.Page {
830 return NetworkAddressPage{pagination.SinglePageBase(r)}
831 }
832 return pagination.NewPager(client, listAddressesByNetworkURL(client, id, network), createPageFn)
833}
einarf2fc665e2015-04-16 20:16:21 +0000834
einarf4e5fdaf2015-04-16 23:14:59 +0000835type CreateImageOpts struct {
einarf2fc665e2015-04-16 20:16:21 +0000836 // Name [required] of the image/snapshot
837 Name string
838 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the created image.
839 Metadata map[string]string
840}
841
einarf4e5fdaf2015-04-16 23:14:59 +0000842type CreateImageOptsBuilder interface {
843 ToServerCreateImageMap() (map[string]interface{}, error)
einarf2fc665e2015-04-16 20:16:21 +0000844}
845
einarf4e5fdaf2015-04-16 23:14:59 +0000846// ToServerCreateImageMap formats a CreateImageOpts structure into a request body.
847func (opts CreateImageOpts) ToServerCreateImageMap() (map[string]interface{}, error) {
einarf2fc665e2015-04-16 20:16:21 +0000848 var err error
849 img := make(map[string]interface{})
850 if opts.Name == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600851 err := gophercloud.ErrMissingInput{}
852 err.Function = "servers.CreateImageOpts.ToServerCreateImageMap"
853 err.Argument = "CreateImageOpts.Name"
854 return nil, err
einarf2fc665e2015-04-16 20:16:21 +0000855 }
856 img["name"] = opts.Name
857 if opts.Metadata != nil {
858 img["metadata"] = opts.Metadata
859 }
860 createImage := make(map[string]interface{})
861 createImage["createImage"] = img
862 return createImage, err
863}
864
einarf4e5fdaf2015-04-16 23:14:59 +0000865// CreateImage makes a request against the nova API to schedule an image to be created of the server
jrperrittb1013232016-02-10 19:01:53 -0600866func CreateImage(client *gophercloud.ServiceClient, serverID string, opts CreateImageOptsBuilder) CreateImageResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600867 var r CreateImageResult
868 b, err := opts.ToServerCreateImageMap()
einarf2fc665e2015-04-16 20:16:21 +0000869 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600870 r.Err = err
871 return r
einarf2fc665e2015-04-16 20:16:21 +0000872 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600873 resp, err := client.Post(actionURL(client, serverID), b, nil, &gophercloud.RequestOpts{
einarf2fc665e2015-04-16 20:16:21 +0000874 OkCodes: []int{202},
875 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600876 r.Err = err
877 r.Header = resp.Header
878 return r
einarf2fc665e2015-04-16 20:16:21 +0000879}
Jon Perritt6b0a8832015-06-04 14:32:30 -0600880
881// IDFromName is a convienience function that returns a server's ID given its name.
882func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600883 count := 0
884 id := ""
Jon Perritt6b0a8832015-06-04 14:32:30 -0600885 if name == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600886 err := &gophercloud.ErrMissingInput{}
887 err.Function = "servers.IDFromName"
888 err.Argument = "name"
889 return "", err
Jon Perritt6b0a8832015-06-04 14:32:30 -0600890 }
Jon Perritt6b0a8832015-06-04 14:32:30 -0600891
Jon Perrittf094fef2016-03-07 01:41:59 -0600892 allPages, err := List(client, nil).AllPages()
893 if err != nil {
894 return "", err
895 }
Jon Perritt6b0a8832015-06-04 14:32:30 -0600896
Jon Perrittf094fef2016-03-07 01:41:59 -0600897 all, err := ExtractServers(allPages)
898 if err != nil {
899 return "", err
900 }
901
902 for _, f := range all {
903 if f.Name == name {
904 count++
905 id = f.ID
906 }
907 }
908
909 switch count {
Jon Perritt6b0a8832015-06-04 14:32:30 -0600910 case 0:
Jon Perrittf094fef2016-03-07 01:41:59 -0600911 err := &gophercloud.ErrResourceNotFound{}
912 err.Function = "servers.IDFromName"
913 err.ResourceType = "server"
914 err.Name = name
915 return "", err
Jon Perritt6b0a8832015-06-04 14:32:30 -0600916 case 1:
Jon Perrittf094fef2016-03-07 01:41:59 -0600917 return id, nil
Jon Perritt6b0a8832015-06-04 14:32:30 -0600918 default:
Jon Perrittf094fef2016-03-07 01:41:59 -0600919 err := &gophercloud.ErrMultipleResourcesFound{}
920 err.Function = "servers.IDFromName"
921 err.ResourceType = "server"
922 err.Name = name
923 err.Count = count
924 return "", err
Jon Perritt6b0a8832015-06-04 14:32:30 -0600925 }
926}