blob: 488829e63db4bbd62101b4aba385174f18afe2e1 [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"
Ash Wilson01626a32014-09-17 10:38:07 -04007
Jon Perritt27249f42016-02-18 10:35:59 -06008 "github.com/gophercloud/gophercloud"
9 "github.com/gophercloud/gophercloud/openstack/compute/v2/flavors"
Jon Perritt994370e2016-02-18 15:23:34 -060010 "github.com/gophercloud/gophercloud/openstack/compute/v2/images"
Jon Perritt27249f42016-02-18 10:35:59 -060011 "github.com/gophercloud/gophercloud/pagination"
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080012)
13
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020014// ListOptsBuilder allows extensions to add additional parameters to the
15// List request.
16type ListOptsBuilder interface {
17 ToServerListQuery() (string, error)
18}
Kevin Pike9748b7b2015-05-05 07:34:07 -070019
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020020// ListOpts allows the filtering and sorting of paginated collections through
21// the API. Filtering is achieved by passing in struct field values that map to
22// the server attributes you want to see returned. Marker and Limit are used
23// for pagination.
24type ListOpts struct {
25 // A time/date stamp for when the server last changed status.
26 ChangesSince string `q:"changes-since"`
27
28 // Name of the image in URL format.
29 Image string `q:"image"`
30
31 // Name of the flavor in URL format.
32 Flavor string `q:"flavor"`
33
34 // Name of the server as a string; can be queried with regular expressions.
35 // Realize that ?name=bob returns both bob and bobb. If you need to match bob
36 // only, you can use a regular expression matching the syntax of the
37 // underlying database server implemented for Compute.
38 Name string `q:"name"`
39
40 // Value of the status of the server so that you can filter on "ACTIVE" for example.
41 Status string `q:"status"`
42
43 // Name of the host as a string.
44 Host string `q:"host"`
45
46 // UUID of the server at which you want to set a marker.
47 Marker string `q:"marker"`
48
49 // Integer value for the limit of values to return.
50 Limit int `q:"limit"`
Daniel Speichert9342e522015-06-05 10:31:52 -040051
52 // Bool to show all tenants
53 AllTenants bool `q:"all_tenants"`
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020054}
55
56// ToServerListQuery formats a ListOpts into a query string.
57func (opts ListOpts) ToServerListQuery() (string, error) {
58 q, err := gophercloud.BuildQueryString(opts)
59 if err != nil {
60 return "", err
61 }
62 return q.String(), nil
63}
64
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080065// List makes a request against the API to list servers accessible to you.
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020066func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
67 url := listDetailURL(client)
68
69 if opts != nil {
70 query, err := opts.ToServerListQuery()
71 if err != nil {
72 return pagination.Pager{Err: err}
73 }
74 url += query
75 }
76
Ash Wilsonb8b16f82014-10-20 10:19:49 -040077 createPageFn := func(r pagination.PageResult) pagination.Page {
78 return ServerPage{pagination.LinkedPageBase{PageResult: r}}
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080079 }
80
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020081 return pagination.NewPager(client, url, createPageFn)
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080082}
83
Ash Wilson2206a112014-10-02 10:57:38 -040084// CreateOptsBuilder describes struct types that can be accepted by the Create call.
Ash Wilson6a310e02014-09-29 08:24:02 -040085// The CreateOpts struct in this package does.
Ash Wilson2206a112014-10-02 10:57:38 -040086type CreateOptsBuilder interface {
Jon Perritt4149d7c2014-10-23 21:23:46 -050087 ToServerCreateMap() (map[string]interface{}, error)
Ash Wilson6a310e02014-09-29 08:24:02 -040088}
89
90// Network is used within CreateOpts to control a new server's network attachments.
91type Network struct {
92 // UUID of a nova-network to attach to the newly provisioned server.
93 // Required unless Port is provided.
94 UUID string
95
96 // Port of a neutron network to attach to the newly provisioned server.
97 // Required unless UUID is provided.
98 Port string
99
100 // FixedIP [optional] specifies a fixed IPv4 address to be used on this network.
101 FixedIP string
102}
103
Kevin Pike92e10b52015-04-10 15:16:57 -0700104// Personality is an array of files that are injected into the server at launch.
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700105type Personality []*File
Kevin Pike92e10b52015-04-10 15:16:57 -0700106
107// File is used within CreateOpts and RebuildOpts to inject a file into the server at launch.
Kevin Pike9748b7b2015-05-05 07:34:07 -0700108// File implements the json.Marshaler interface, so when a Create or Rebuild operation is requested,
109// json.Marshal will call File's MarshalJSON method.
Kevin Pike92e10b52015-04-10 15:16:57 -0700110type File struct {
111 // Path of the file
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700112 Path string
Kevin Pike92e10b52015-04-10 15:16:57 -0700113 // Contents of the file. Maximum content size is 255 bytes.
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700114 Contents []byte
Kevin Pike92e10b52015-04-10 15:16:57 -0700115}
116
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700117// MarshalJSON marshals the escaped file, base64 encoding the contents.
118func (f *File) MarshalJSON() ([]byte, error) {
119 file := struct {
120 Path string `json:"path"`
121 Contents string `json:"contents"`
122 }{
123 Path: f.Path,
124 Contents: base64.StdEncoding.EncodeToString(f.Contents),
Kevin Pike92e10b52015-04-10 15:16:57 -0700125 }
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700126 return json.Marshal(file)
Kevin Pike92e10b52015-04-10 15:16:57 -0700127}
128
Ash Wilson6a310e02014-09-29 08:24:02 -0400129// CreateOpts specifies server creation parameters.
130type CreateOpts struct {
131 // Name [required] is the name to assign to the newly launched server.
jrperrittb1013232016-02-10 19:01:53 -0600132 Name string `b:"name,required"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400133
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600134 // ImageRef [optional; required if ImageName is not provided] is the ID or full
135 // URL to the image that contains the server's OS and initial state.
136 // Also optional if using the boot-from-volume extension.
Ash Wilson6a310e02014-09-29 08:24:02 -0400137 ImageRef string
138
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600139 // ImageName [optional; required if ImageRef is not provided] is the name of the
140 // image that contains the server's OS and initial state.
141 // Also optional if using the boot-from-volume extension.
142 ImageName string
143
144 // FlavorRef [optional; required if FlavorName is not provided] is the ID or
145 // full URL to the flavor that describes the server's specs.
Ash Wilson6a310e02014-09-29 08:24:02 -0400146 FlavorRef string
147
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600148 // FlavorName [optional; required if FlavorRef is not provided] is the name of
149 // the flavor that describes the server's specs.
150 FlavorName string
151
Ash Wilson6a310e02014-09-29 08:24:02 -0400152 // SecurityGroups [optional] lists the names of the security groups to which this server should belong.
153 SecurityGroups []string
154
155 // UserData [optional] contains configuration information or scripts to use upon launch.
156 // Create will base64-encode it for you.
157 UserData []byte
158
159 // AvailabilityZone [optional] in which to launch the server.
160 AvailabilityZone string
161
162 // Networks [optional] dictates how this server will be attached to available networks.
163 // By default, the server will be attached to all isolated networks for the tenant.
164 Networks []Network
165
166 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
167 Metadata map[string]string
168
Kevin Pike92e10b52015-04-10 15:16:57 -0700169 // Personality [optional] includes files to inject into the server at launch.
170 // Create will base64-encode file contents for you.
171 Personality Personality
Ash Wilson6a310e02014-09-29 08:24:02 -0400172
173 // ConfigDrive [optional] enables metadata injection through a configuration drive.
174 ConfigDrive bool
Jon Perrittf3b2e142014-11-04 16:00:19 -0600175
176 // AdminPass [optional] sets the root user password. If not set, a randomly-generated
Jon Perrittf094fef2016-03-07 01:41:59 -0600177 // password will be created and returned in the rponse.
Jon Perrittf3b2e142014-11-04 16:00:19 -0600178 AdminPass string
Jon Perritt7b9671c2015-02-01 22:03:14 -0700179
180 // AccessIPv4 [optional] specifies an IPv4 address for the instance.
181 AccessIPv4 string
182
183 // AccessIPv6 [optional] specifies an IPv6 address for the instance.
184 AccessIPv6 string
Jon Perritt994370e2016-02-18 15:23:34 -0600185
186 // ServiceClient [optional] will allow calls to be made to retrieve an image or
187 // flavor ID by name.
188 ServiceClient *gophercloud.ServiceClient
Ash Wilson6a310e02014-09-29 08:24:02 -0400189}
190
Ash Wilsone45c9732014-09-29 10:54:12 -0400191// ToServerCreateMap assembles a request body based on the contents of a CreateOpts.
Jon Perritt4149d7c2014-10-23 21:23:46 -0500192func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) {
Ash Wilson6a310e02014-09-29 08:24:02 -0400193 server := make(map[string]interface{})
194
195 server["name"] = opts.Name
196 server["imageRef"] = opts.ImageRef
197 server["flavorRef"] = opts.FlavorRef
198
199 if opts.UserData != nil {
200 encoded := base64.StdEncoding.EncodeToString(opts.UserData)
201 server["user_data"] = &encoded
202 }
Ash Wilson6a310e02014-09-29 08:24:02 -0400203 if opts.ConfigDrive {
204 server["config_drive"] = "true"
205 }
206 if opts.AvailabilityZone != "" {
207 server["availability_zone"] = opts.AvailabilityZone
208 }
209 if opts.Metadata != nil {
210 server["metadata"] = opts.Metadata
211 }
Jon Perrittf3b2e142014-11-04 16:00:19 -0600212 if opts.AdminPass != "" {
213 server["adminPass"] = opts.AdminPass
214 }
Jon Perritt7b9671c2015-02-01 22:03:14 -0700215 if opts.AccessIPv4 != "" {
216 server["accessIPv4"] = opts.AccessIPv4
217 }
218 if opts.AccessIPv6 != "" {
219 server["accessIPv6"] = opts.AccessIPv6
220 }
Ash Wilson6a310e02014-09-29 08:24:02 -0400221
222 if len(opts.SecurityGroups) > 0 {
223 securityGroups := make([]map[string]interface{}, len(opts.SecurityGroups))
224 for i, groupName := range opts.SecurityGroups {
225 securityGroups[i] = map[string]interface{}{"name": groupName}
226 }
eselldf709942014-11-13 21:07:11 -0700227 server["security_groups"] = securityGroups
Ash Wilson6a310e02014-09-29 08:24:02 -0400228 }
Jon Perritt2a7797d2014-10-21 15:08:43 -0500229
Ash Wilson6a310e02014-09-29 08:24:02 -0400230 if len(opts.Networks) > 0 {
231 networks := make([]map[string]interface{}, len(opts.Networks))
232 for i, net := range opts.Networks {
233 networks[i] = make(map[string]interface{})
234 if net.UUID != "" {
235 networks[i]["uuid"] = net.UUID
236 }
237 if net.Port != "" {
238 networks[i]["port"] = net.Port
239 }
240 if net.FixedIP != "" {
241 networks[i]["fixed_ip"] = net.FixedIP
242 }
243 }
Jon Perritt2a7797d2014-10-21 15:08:43 -0500244 server["networks"] = networks
Ash Wilson6a310e02014-09-29 08:24:02 -0400245 }
246
Kevin Pike92e10b52015-04-10 15:16:57 -0700247 if len(opts.Personality) > 0 {
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700248 server["personality"] = opts.Personality
Kevin Pike92e10b52015-04-10 15:16:57 -0700249 }
250
jrperrittb1013232016-02-10 19:01:53 -0600251 // If ImageRef isn't provided, use ImageName to ascertain the image ID.
252 if opts.ImageRef == "" {
253 if opts.ImageName == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600254 err := ErrNeitherImageIDNorImageNameProvided{}
255 err.Function = "servers.CreateOpts.ToServerCreateMap"
256 err.Argument = "ImageRef/ImageName"
257 return nil, err
jrperrittb1013232016-02-10 19:01:53 -0600258 }
Jon Perritt994370e2016-02-18 15:23:34 -0600259 if opts.ServiceClient == nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600260 err := ErrNoClientProvidedForIDByName{}
261 err.Function = "servers.CreateOpts.ToServerCreateMap"
262 err.Argument = "ServiceClient"
263 return nil, err
Jon Perritt994370e2016-02-18 15:23:34 -0600264 }
265 imageID, err := images.IDFromName(opts.ServiceClient, opts.ImageName)
jrperrittb1013232016-02-10 19:01:53 -0600266 if err != nil {
267 return nil, err
268 }
269 server["imageRef"] = imageID
270 }
271
272 // If FlavorRef isn't provided, use FlavorName to ascertain the flavor ID.
273 if opts.FlavorRef == "" {
274 if opts.FlavorName == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600275 err := ErrNeitherFlavorIDNorFlavorNameProvided{}
276 err.Function = "servers.CreateOpts.ToServerCreateMap"
277 err.Argument = "FlavorRef/FlavorName"
278 return nil, err
jrperrittb1013232016-02-10 19:01:53 -0600279 }
Jon Perritt994370e2016-02-18 15:23:34 -0600280 if opts.ServiceClient == nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600281 err := ErrNoClientProvidedForIDByName{}
282 err.Argument = "ServiceClient"
283 err.Function = "servers.CreateOpts.ToServerCreateMap"
284 return nil, err
Jon Perritt994370e2016-02-18 15:23:34 -0600285 }
286 flavorID, err := flavors.IDFromName(opts.ServiceClient, opts.FlavorName)
jrperrittb1013232016-02-10 19:01:53 -0600287 if err != nil {
288 return nil, err
289 }
290 server["flavorRef"] = flavorID
291 }
292
Jon Perritt4149d7c2014-10-23 21:23:46 -0500293 return map[string]interface{}{"server": server}, nil
Ash Wilson6a310e02014-09-29 08:24:02 -0400294}
295
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800296// Create requests a server to be provisioned to the user in the current tenant.
Ash Wilson2206a112014-10-02 10:57:38 -0400297func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600298 var r CreateResult
Jon Perritt4149d7c2014-10-23 21:23:46 -0500299
300 reqBody, err := opts.ToServerCreateMap()
301 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600302 r.Err = err
303 return r
Jon Perritt4149d7c2014-10-23 21:23:46 -0500304 }
305
Jon Perrittf094fef2016-03-07 01:41:59 -0600306 _, r.Err = client.Post(listURL(client), reqBody, &r.Body, nil)
307 return r
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800308}
309
310// Delete requests that a server previously provisioned be removed from your account.
Jamie Hannaford34732fe2014-10-27 11:29:36 +0100311func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600312 var r DeleteResult
313 _, r.Err = client.Delete(deleteURL(client, id), nil)
314 return r
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800315}
316
Ian Duffy370c4302016-01-21 10:44:56 +0000317func ForceDelete(client *gophercloud.ServiceClient, id string) ActionResult {
318 var req struct {
319 ForceDelete string `json:"forceDelete"`
320 }
321
Jon Perrittf094fef2016-03-07 01:41:59 -0600322 var r ActionResult
323 _, r.Err = client.Post(actionURL(client, id), req, nil, nil)
324 return r
Ian Duffy370c4302016-01-21 10:44:56 +0000325
326}
327
Ash Wilson7ddf0362014-09-17 10:59:09 -0400328// Get requests details on a single server, by ID.
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400329func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600330 var r GetResult
331 _, r.Err = client.Get(getURL(client, id), &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100332 OkCodes: []int{200, 203},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800333 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600334 return r
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800335}
336
Alex Gaynora6d5f9f2014-10-27 10:52:32 -0700337// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
Jon Perritt82048212014-10-13 22:33:13 -0500338type UpdateOptsBuilder interface {
Ash Wilsone45c9732014-09-29 10:54:12 -0400339 ToServerUpdateMap() map[string]interface{}
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400340}
341
342// UpdateOpts specifies the base attributes that may be updated on an existing server.
343type UpdateOpts struct {
344 // Name [optional] changes the displayed name of the server.
345 // The server host name will *not* change.
346 // Server names are not constrained to be unique, even within the same tenant.
347 Name string
348
349 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
350 AccessIPv4 string
351
352 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
353 AccessIPv6 string
354}
355
Ash Wilsone45c9732014-09-29 10:54:12 -0400356// ToServerUpdateMap formats an UpdateOpts structure into a request body.
357func (opts UpdateOpts) ToServerUpdateMap() map[string]interface{} {
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400358 server := make(map[string]string)
359 if opts.Name != "" {
360 server["name"] = opts.Name
361 }
362 if opts.AccessIPv4 != "" {
363 server["accessIPv4"] = opts.AccessIPv4
364 }
365 if opts.AccessIPv6 != "" {
366 server["accessIPv6"] = opts.AccessIPv6
367 }
368 return map[string]interface{}{"server": server}
369}
370
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800371// Update requests that various attributes of the indicated server be changed.
Jon Perritt82048212014-10-13 22:33:13 -0500372func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600373 var r UpdateResult
374 b := opts.ToServerUpdateMap()
375 _, r.Err = client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100376 OkCodes: []int{200},
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800377 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600378 return r
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800379}
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700380
Ash Wilson01626a32014-09-17 10:38:07 -0400381// ChangeAdminPassword alters the administrator or root password for a specified server.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200382func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) ActionResult {
Ash Wilsondc7daa82014-09-23 16:34:42 -0400383 var req struct {
384 ChangePassword struct {
385 AdminPass string `json:"adminPass"`
386 } `json:"changePassword"`
387 }
388
389 req.ChangePassword.AdminPass = newPassword
390
Jon Perrittf094fef2016-03-07 01:41:59 -0600391 var r ActionResult
392 _, r.Err = client.Post(actionURL(client, id), req, nil, nil)
393 return r
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700394}
395
Ash Wilson01626a32014-09-17 10:38:07 -0400396// RebootMethod describes the mechanisms by which a server reboot can be requested.
397type RebootMethod string
398
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700399// These constants determine how a server should be rebooted.
400// See the Reboot() function for further details.
401const (
Ash Wilson01626a32014-09-17 10:38:07 -0400402 SoftReboot RebootMethod = "SOFT"
403 HardReboot RebootMethod = "HARD"
404 OSReboot = SoftReboot
405 PowerCycle = HardReboot
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700406)
407
Jon Perrittf094fef2016-03-07 01:41:59 -0600408type RebootOptsBuilder interface {
409 ToServerRebootMap() (map[string]interface{}, error)
410}
411
412type RebootOpts struct {
413 Type RebootMethod
414}
415
416func (opts *RebootOpts) ToServerRebootMap() (map[string]interface{}, error) {
417 if (opts.Type != SoftReboot) && (opts.Type != HardReboot) {
418 err := &gophercloud.ErrInvalidInput{}
419 err.Argument = "servers.RebootOpts.Type"
420 err.Value = opts.Type
421 err.Function = "servers.Reboot"
422 return nil, err
423 }
424
425 reqBody := map[string]interface{}{
426 "reboot": map[string]interface{}{
427 "type": string(opts.Type),
428 },
429 }
430
431 return reqBody, nil
432}
433
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700434// Reboot requests that a given server reboot.
435// Two methods exist for rebooting a server:
436//
Jon Perrittf094fef2016-03-07 01:41:59 -0600437// 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 -0400438// terminating it at the hypervisor level.
439// It's done. Caput. Full stop.
Jon Perrittf094fef2016-03-07 01:41:59 -0600440// Then, after a brief while, power is rtored or the VM instance rtarted.
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700441//
Jon Perrittf094fef2016-03-07 01:41:59 -0600442// SoftReboot (aka OSReboot) simply tells the OS to rtart under its own procedur.
443// E.g., in Linux, asking it to enter runlevel 6, or executing "sudo shutdown -r now", or by asking Windows to rtart the machine.
444func Reboot(client *gophercloud.ServiceClient, id string, opts RebootOptsBuilder) ActionResult {
445 var r ActionResult
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200446
Jon Perrittf094fef2016-03-07 01:41:59 -0600447 reqBody, err := opts.ToServerRebootMap()
448 if err != nil {
449 r.Err = err
450 return r
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700451 }
Jon Perritt30558642014-04-14 17:07:12 -0500452
Jon Perrittf094fef2016-03-07 01:41:59 -0600453 _, r.Err = client.Post(actionURL(client, id), reqBody, nil, nil)
454 return r
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700455}
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700456
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200457// RebuildOptsBuilder is an interface that allows extensions to override the
458// default behaviour of rebuild options
459type RebuildOptsBuilder interface {
460 ToServerRebuildMap() (map[string]interface{}, error)
461}
462
463// RebuildOpts represents the configuration options used in a server rebuild
464// operation
465type RebuildOpts struct {
466 // Required. The ID of the image you want your server to be provisioned on
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200467 ImageID string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200468
469 // Name to set the server to
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200470 Name string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200471
472 // Required. The server's admin password
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200473 AdminPass string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200474
475 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200476 AccessIPv4 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200477
478 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200479 AccessIPv6 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200480
481 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200482 Metadata map[string]string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200483
Kevin Pike92e10b52015-04-10 15:16:57 -0700484 // Personality [optional] includes files to inject into the server at launch.
485 // Rebuild will base64-encode file contents for you.
486 Personality Personality
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200487}
488
489// ToServerRebuildMap formats a RebuildOpts struct into a map for use in JSON
490func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {
491 var err error
492 server := make(map[string]interface{})
493
494 if opts.AdminPass == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600495 err := ErrNoAdminPassProvided{}
496 err.Function = "servers.ToServerRebuildMap"
497 err.Argument = "AdminPass"
498 return nil, err
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200499 }
500
501 if opts.ImageID == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600502 err := ErrNoImageIDProvided{}
503 err.Function = "servers.ToServerRebuildMap"
504 err.Argument = "ImageID"
505 return nil, err
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200506 }
507
508 if err != nil {
509 return server, err
510 }
511
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200512 server["adminPass"] = opts.AdminPass
513 server["imageRef"] = opts.ImageID
514
Jon Perritt12395212016-02-24 10:41:17 -0600515 if opts.Name != "" {
516 server["name"] = opts.Name
517 }
518
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200519 if opts.AccessIPv4 != "" {
520 server["accessIPv4"] = opts.AccessIPv4
521 }
522
523 if opts.AccessIPv6 != "" {
524 server["accessIPv6"] = opts.AccessIPv6
525 }
526
527 if opts.Metadata != nil {
528 server["metadata"] = opts.Metadata
529 }
530
Kevin Pike92e10b52015-04-10 15:16:57 -0700531 if len(opts.Personality) > 0 {
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700532 server["personality"] = opts.Personality
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200533 }
534
535 return map[string]interface{}{"rebuild": server}, nil
536}
537
538// Rebuild will reprovision the server according to the configuration options
539// provided in the RebuildOpts struct.
540func Rebuild(client *gophercloud.ServiceClient, id string, opts RebuildOptsBuilder) RebuildResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600541 var r RebuildResult
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700542
543 if id == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600544 err := ErrNoIDProvided{}
545 err.Function = "servers.Rebuild"
546 err.Argument = "id"
547 r.Err = err
548 return r
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700549 }
550
Jon Perrittf094fef2016-03-07 01:41:59 -0600551 b, err := opts.ToServerRebuildMap()
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200552 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600553 r.Err = err
554 return r
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700555 }
556
Jon Perrittf094fef2016-03-07 01:41:59 -0600557 _, r.Err = client.Post(actionURL(client, id), b, &r.Body, nil)
558 return r
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700559}
560
Ash Wilson5f7cf182014-10-23 08:35:24 -0400561// ResizeOptsBuilder is an interface that allows extensions to override the default structure of
562// a Resize request.
563type ResizeOptsBuilder interface {
564 ToServerResizeMap() (map[string]interface{}, error)
565}
566
567// ResizeOpts represents the configuration options used to control a Resize operation.
568type ResizeOpts struct {
569 // FlavorRef is the ID of the flavor you wish your server to become.
570 FlavorRef string
571}
572
Alex Gaynor266e9332014-10-28 14:44:04 -0700573// 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 -0400574// Resize request.
575func (opts ResizeOpts) ToServerResizeMap() (map[string]interface{}, error) {
576 resize := map[string]interface{}{
577 "flavorRef": opts.FlavorRef,
578 }
579
580 return map[string]interface{}{"resize": resize}, nil
581}
582
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700583// Resize instructs the provider to change the flavor of the server.
Ash Wilson01626a32014-09-17 10:38:07 -0400584// Note that this implies rebuilding it.
Jon Perrittf094fef2016-03-07 01:41:59 -0600585// Unfortunately, one cannot pass rebuild parameters to the rize function.
586// When the rize completes, the server will be in RESIZE_VERIFY state.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700587// While in this state, you can explore the use of the new server's configuration.
Jon Perrittf094fef2016-03-07 01:41:59 -0600588// If you like it, call ConfirmResize() to commit the rize permanently.
589// Otherwise, call RevertResize() to rtore the old configuration.
Ash Wilson9e87a922014-10-23 14:29:22 -0400590func Resize(client *gophercloud.ServiceClient, id string, opts ResizeOptsBuilder) ActionResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600591 var r ActionResult
592
593 if id == "" {
594 err := ErrNoIDProvided{}
595 err.Function = "servers.Resize"
596 err.Argument = "id"
597 r.Err = err
598 return r
Ash Wilson5f7cf182014-10-23 08:35:24 -0400599 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200600
Jon Perrittf094fef2016-03-07 01:41:59 -0600601 b, err := opts.ToServerResizeMap()
602 if err != nil {
603 r.Err = err
604 return r
605 }
606
607 _, r.Err = client.Post(actionURL(client, id), b, nil, nil)
608 return r
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700609}
610
Jon Perrittf094fef2016-03-07 01:41:59 -0600611// ConfirmResize confirms a previous rize operation on a server.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700612// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200613func ConfirmResize(client *gophercloud.ServiceClient, id string) ActionResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600614 var r ActionResult
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200615
Jon Perrittf094fef2016-03-07 01:41:59 -0600616 if id == "" {
617 err := ErrNoIDProvided{}
618 err.Function = "servers.ConfirmResize"
619 err.Argument = "id"
620 r.Err = err
621 return r
622 }
623
624 b := map[string]interface{}{"confirmResize": nil}
625 _, r.Err = client.Post(actionURL(client, id), b, nil, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100626 OkCodes: []int{201, 202, 204},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700627 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600628 return r
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700629}
630
Jon Perrittf094fef2016-03-07 01:41:59 -0600631// RevertResize cancels a previous rize operation on a server.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700632// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200633func RevertResize(client *gophercloud.ServiceClient, id string) ActionResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600634 var r ActionResult
635
636 if id == "" {
637 err := ErrNoIDProvided{}
638 err.Function = "servers.RevertResize"
639 err.Argument = "id"
640 r.Err = err
641 return r
642 }
643
644 b := map[string]interface{}{"revertResize": nil}
645 _, r.Err = client.Post(actionURL(client, id), b, nil, nil)
646 return r
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700647}
Alex Gaynor39584a02014-10-28 13:59:21 -0700648
Alex Gaynor266e9332014-10-28 14:44:04 -0700649// RescueOptsBuilder is an interface that allows extensions to override the
650// default structure of a Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700651type RescueOptsBuilder interface {
652 ToServerRescueMap() (map[string]interface{}, error)
653}
654
Alex Gaynor266e9332014-10-28 14:44:04 -0700655// RescueOpts represents the configuration options used to control a Rescue
656// option.
Alex Gaynor39584a02014-10-28 13:59:21 -0700657type RescueOpts struct {
Alex Gaynor266e9332014-10-28 14:44:04 -0700658 // AdminPass is the desired administrative password for the instance in
Alex Gaynorcfec7722014-11-13 13:33:49 -0800659 // RESCUE mode. If it's left blank, the server will generate a password.
Alex Gaynor39584a02014-10-28 13:59:21 -0700660 AdminPass string
661}
662
Jon Perrittcc77da62014-11-16 13:14:21 -0700663// ToServerRescueMap formats a RescueOpts as a map that can be used as a JSON
Alex Gaynor266e9332014-10-28 14:44:04 -0700664// request body for the Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700665func (opts RescueOpts) ToServerRescueMap() (map[string]interface{}, error) {
666 server := make(map[string]interface{})
667 if opts.AdminPass != "" {
668 server["adminPass"] = opts.AdminPass
669 }
670 return map[string]interface{}{"rescue": server}, nil
671}
672
Alex Gaynor266e9332014-10-28 14:44:04 -0700673// Rescue instructs the provider to place the server into RESCUE mode.
Alex Gaynorfbe61bb2014-11-12 13:35:03 -0800674func Rescue(client *gophercloud.ServiceClient, id string, opts RescueOptsBuilder) RescueResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600675 var r RescueResult
Alex Gaynor39584a02014-10-28 13:59:21 -0700676
677 if id == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600678 err := ErrNoIDProvided{}
679 err.Function = "servers.Rebuild"
680 err.Argument = "id"
681 r.Err = err
682 return r
Alex Gaynor39584a02014-10-28 13:59:21 -0700683 }
684
Jon Perrittf094fef2016-03-07 01:41:59 -0600685 b, err := opts.ToServerRescueMap()
686 if err != nil {
687 r.Err = err
688 return r
689 }
690
691 _, r.Err = client.Post(actionURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100692 OkCodes: []int{200},
Alex Gaynor39584a02014-10-28 13:59:21 -0700693 })
694
Jon Perrittf094fef2016-03-07 01:41:59 -0600695 return r
Alex Gaynor39584a02014-10-28 13:59:21 -0700696}
Jon Perrittcc77da62014-11-16 13:14:21 -0700697
Jon Perritt789f8322014-11-21 08:20:04 -0700698// ResetMetadataOptsBuilder allows extensions to add additional parameters to the
699// Reset request.
700type ResetMetadataOptsBuilder interface {
701 ToMetadataResetMap() (map[string]interface{}, error)
Jon Perrittcc77da62014-11-16 13:14:21 -0700702}
703
Jon Perritt78c57ce2014-11-20 11:07:18 -0700704// MetadataOpts is a map that contains key-value pairs.
705type MetadataOpts map[string]string
Jon Perrittcc77da62014-11-16 13:14:21 -0700706
Jon Perritt789f8322014-11-21 08:20:04 -0700707// ToMetadataResetMap assembles a body for a Reset request based on the contents of a MetadataOpts.
708func (opts MetadataOpts) ToMetadataResetMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700709 return map[string]interface{}{"metadata": opts}, nil
710}
711
Jon Perritt78c57ce2014-11-20 11:07:18 -0700712// ToMetadataUpdateMap assembles a body for an Update request based on the contents of a MetadataOpts.
713func (opts MetadataOpts) ToMetadataUpdateMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700714 return map[string]interface{}{"metadata": opts}, nil
715}
716
Jon Perritt789f8322014-11-21 08:20:04 -0700717// ResetMetadata will create multiple new key-value pairs for the given server ID.
Jon Perrittcc77da62014-11-16 13:14:21 -0700718// Note: Using this operation will erase any already-existing metadata and create
719// the new metadata provided. To keep any already-existing metadata, use the
720// UpdateMetadatas or UpdateMetadata function.
Jon Perritt789f8322014-11-21 08:20:04 -0700721func ResetMetadata(client *gophercloud.ServiceClient, id string, opts ResetMetadataOptsBuilder) ResetMetadataResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600722 var r ResetMetadataResult
Jon Perritt789f8322014-11-21 08:20:04 -0700723 metadata, err := opts.ToMetadataResetMap()
Jon Perrittcc77da62014-11-16 13:14:21 -0700724 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600725 r.Err = err
726 return r
Jon Perrittcc77da62014-11-16 13:14:21 -0700727 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600728 _, r.Err = client.Put(metadataURL(client, id), metadata, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100729 OkCodes: []int{200},
Jon Perrittcc77da62014-11-16 13:14:21 -0700730 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600731 return r
Jon Perrittcc77da62014-11-16 13:14:21 -0700732}
733
Jon Perritt78c57ce2014-11-20 11:07:18 -0700734// Metadata requests all the metadata for the given server ID.
735func Metadata(client *gophercloud.ServiceClient, id string) GetMetadataResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600736 var r GetMetadataResult
737 _, r.Err = client.Get(metadataURL(client, id), &r.Body, nil)
738 return r
Jon Perrittcc77da62014-11-16 13:14:21 -0700739}
740
Jon Perritt78c57ce2014-11-20 11:07:18 -0700741// UpdateMetadataOptsBuilder allows extensions to add additional parameters to the
742// Create request.
743type UpdateMetadataOptsBuilder interface {
744 ToMetadataUpdateMap() (map[string]interface{}, error)
745}
746
747// UpdateMetadata updates (or creates) all the metadata specified by opts for the given server ID.
748// This operation does not affect already-existing metadata that is not specified
749// by opts.
750func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) UpdateMetadataResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600751 var r UpdateMetadataResult
Jon Perritt78c57ce2014-11-20 11:07:18 -0700752 metadata, err := opts.ToMetadataUpdateMap()
753 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600754 r.Err = err
755 return r
Jon Perritt78c57ce2014-11-20 11:07:18 -0700756 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600757 _, r.Err = client.Post(metadataURL(client, id), metadata, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100758 OkCodes: []int{200},
Jon Perritt78c57ce2014-11-20 11:07:18 -0700759 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600760 return r
Jon Perritt78c57ce2014-11-20 11:07:18 -0700761}
762
763// MetadatumOptsBuilder allows extensions to add additional parameters to the
764// Create request.
765type MetadatumOptsBuilder interface {
766 ToMetadatumCreateMap() (map[string]interface{}, string, error)
767}
768
769// MetadatumOpts is a map of length one that contains a key-value pair.
770type MetadatumOpts map[string]string
771
772// ToMetadatumCreateMap assembles a body for a Create request based on the contents of a MetadataumOpts.
773func (opts MetadatumOpts) ToMetadatumCreateMap() (map[string]interface{}, string, error) {
774 if len(opts) != 1 {
775 return nil, "", errors.New("CreateMetadatum operation must have 1 and only 1 key-value pair.")
776 }
777 metadatum := map[string]interface{}{"meta": opts}
778 var key string
779 for k := range metadatum["meta"].(MetadatumOpts) {
780 key = k
781 }
782 return metadatum, key, nil
783}
784
785// CreateMetadatum will create or update the key-value pair with the given key for the given server ID.
786func CreateMetadatum(client *gophercloud.ServiceClient, id string, opts MetadatumOptsBuilder) CreateMetadatumResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600787 var r CreateMetadatumResult
Jon Perritt78c57ce2014-11-20 11:07:18 -0700788 metadatum, key, err := opts.ToMetadatumCreateMap()
789 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600790 r.Err = err
791 return r
Jon Perritt78c57ce2014-11-20 11:07:18 -0700792 }
793
Jon Perrittf094fef2016-03-07 01:41:59 -0600794 _, r.Err = client.Put(metadatumURL(client, id, key), metadatum, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100795 OkCodes: []int{200},
Jon Perritt78c57ce2014-11-20 11:07:18 -0700796 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600797 return r
Jon Perritt78c57ce2014-11-20 11:07:18 -0700798}
799
800// Metadatum requests the key-value pair with the given key for the given server ID.
801func Metadatum(client *gophercloud.ServiceClient, id, key string) GetMetadatumResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600802 var r GetMetadatumResult
803 _, r.Err = client.Request("GET", metadatumURL(client, id, key), &gophercloud.RequestOpts{
804 JSONResponse: &r.Body,
Jon Perritt78c57ce2014-11-20 11:07:18 -0700805 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600806 return r
Jon Perritt78c57ce2014-11-20 11:07:18 -0700807}
808
809// DeleteMetadatum will delete the key-value pair with the given key for the given server ID.
810func DeleteMetadatum(client *gophercloud.ServiceClient, id, key string) DeleteMetadatumResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600811 var r DeleteMetadatumResult
812 _, r.Err = client.Delete(metadatumURL(client, id, key), nil)
813 return r
Jon Perrittcc77da62014-11-16 13:14:21 -0700814}
Jon Perritt5cb49482015-02-19 12:19:58 -0700815
816// ListAddresses makes a request against the API to list the servers IP addresses.
817func ListAddresses(client *gophercloud.ServiceClient, id string) pagination.Pager {
818 createPageFn := func(r pagination.PageResult) pagination.Page {
819 return AddressPage{pagination.SinglePageBase(r)}
820 }
821 return pagination.NewPager(client, listAddressesURL(client, id), createPageFn)
822}
Jon Perritt04d073c2015-02-19 21:46:23 -0700823
824// ListAddressesByNetwork makes a request against the API to list the servers IP addresses
825// for the given network.
826func ListAddressesByNetwork(client *gophercloud.ServiceClient, id, network string) pagination.Pager {
827 createPageFn := func(r pagination.PageResult) pagination.Page {
828 return NetworkAddressPage{pagination.SinglePageBase(r)}
829 }
830 return pagination.NewPager(client, listAddressesByNetworkURL(client, id, network), createPageFn)
831}
einarf2fc665e2015-04-16 20:16:21 +0000832
einarf4e5fdaf2015-04-16 23:14:59 +0000833type CreateImageOpts struct {
einarf2fc665e2015-04-16 20:16:21 +0000834 // Name [required] of the image/snapshot
835 Name string
836 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the created image.
837 Metadata map[string]string
838}
839
einarf4e5fdaf2015-04-16 23:14:59 +0000840type CreateImageOptsBuilder interface {
841 ToServerCreateImageMap() (map[string]interface{}, error)
einarf2fc665e2015-04-16 20:16:21 +0000842}
843
einarf4e5fdaf2015-04-16 23:14:59 +0000844// ToServerCreateImageMap formats a CreateImageOpts structure into a request body.
845func (opts CreateImageOpts) ToServerCreateImageMap() (map[string]interface{}, error) {
einarf2fc665e2015-04-16 20:16:21 +0000846 var err error
847 img := make(map[string]interface{})
848 if opts.Name == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600849 err := gophercloud.ErrMissingInput{}
850 err.Function = "servers.CreateImageOpts.ToServerCreateImageMap"
851 err.Argument = "CreateImageOpts.Name"
852 return nil, err
einarf2fc665e2015-04-16 20:16:21 +0000853 }
854 img["name"] = opts.Name
855 if opts.Metadata != nil {
856 img["metadata"] = opts.Metadata
857 }
858 createImage := make(map[string]interface{})
859 createImage["createImage"] = img
860 return createImage, err
861}
862
einarf4e5fdaf2015-04-16 23:14:59 +0000863// CreateImage makes a request against the nova API to schedule an image to be created of the server
jrperrittb1013232016-02-10 19:01:53 -0600864func CreateImage(client *gophercloud.ServiceClient, serverID string, opts CreateImageOptsBuilder) CreateImageResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600865 var r CreateImageResult
866 b, err := opts.ToServerCreateImageMap()
einarf2fc665e2015-04-16 20:16:21 +0000867 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600868 r.Err = err
869 return r
einarf2fc665e2015-04-16 20:16:21 +0000870 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600871 resp, err := client.Post(actionURL(client, serverID), b, nil, &gophercloud.RequestOpts{
einarf2fc665e2015-04-16 20:16:21 +0000872 OkCodes: []int{202},
873 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600874 r.Err = err
875 r.Header = resp.Header
876 return r
einarf2fc665e2015-04-16 20:16:21 +0000877}
Jon Perritt6b0a8832015-06-04 14:32:30 -0600878
879// IDFromName is a convienience function that returns a server's ID given its name.
880func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600881 count := 0
882 id := ""
Jon Perritt6b0a8832015-06-04 14:32:30 -0600883 if name == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600884 err := &gophercloud.ErrMissingInput{}
885 err.Function = "servers.IDFromName"
886 err.Argument = "name"
887 return "", err
Jon Perritt6b0a8832015-06-04 14:32:30 -0600888 }
Jon Perritt6b0a8832015-06-04 14:32:30 -0600889
Jon Perrittf094fef2016-03-07 01:41:59 -0600890 allPages, err := List(client, nil).AllPages()
891 if err != nil {
892 return "", err
893 }
Jon Perritt6b0a8832015-06-04 14:32:30 -0600894
Jon Perrittf094fef2016-03-07 01:41:59 -0600895 all, err := ExtractServers(allPages)
896 if err != nil {
897 return "", err
898 }
899
900 for _, f := range all {
901 if f.Name == name {
902 count++
903 id = f.ID
904 }
905 }
906
907 switch count {
Jon Perritt6b0a8832015-06-04 14:32:30 -0600908 case 0:
Jon Perrittf094fef2016-03-07 01:41:59 -0600909 err := &gophercloud.ErrResourceNotFound{}
910 err.Function = "servers.IDFromName"
911 err.ResourceType = "server"
912 err.Name = name
913 return "", err
Jon Perritt6b0a8832015-06-04 14:32:30 -0600914 case 1:
Jon Perrittf094fef2016-03-07 01:41:59 -0600915 return id, nil
Jon Perritt6b0a8832015-06-04 14:32:30 -0600916 default:
Jon Perrittf094fef2016-03-07 01:41:59 -0600917 err := &gophercloud.ErrMultipleResourcesFound{}
918 err.Function = "servers.IDFromName"
919 err.ResourceType = "server"
920 err.Name = name
921 err.Count = count
922 return "", err
Jon Perritt6b0a8832015-06-04 14:32:30 -0600923 }
924}