blob: 4129471c1f06fa8de921d00ccc0204710ee532b0 [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 {
Jon Perritt01618ee2016-03-09 03:04:06 -0600130 // Name is the name to assign to the newly launched server.
131 Name string `json:"name" required:"true"`
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.
Jon Perritt01618ee2016-03-09 03:04:06 -0600136 ImageRef string `json:"imageRef"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400137
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.
Jon Perritt01618ee2016-03-09 03:04:06 -0600141 ImageName string `json:"-"`
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600142
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.
Jon Perritt01618ee2016-03-09 03:04:06 -0600145 FlavorRef string `json:"flavorRef"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400146
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.
Jon Perritt01618ee2016-03-09 03:04:06 -0600149 FlavorName string `json:"-"`
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600150
Jon Perritt01618ee2016-03-09 03:04:06 -0600151 // SecurityGroups lists the names of the security groups to which this server should belong.
152 SecurityGroups []string `json:"-"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400153
Jon Perritt01618ee2016-03-09 03:04:06 -0600154 // UserData contains configuration information or scripts to use upon launch.
Ash Wilson6a310e02014-09-29 08:24:02 -0400155 // Create will base64-encode it for you.
Jon Perritt01618ee2016-03-09 03:04:06 -0600156 UserData []byte `json:"-"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400157
Jon Perritt01618ee2016-03-09 03:04:06 -0600158 // AvailabilityZone in which to launch the server.
159 AvailabilityZone string `json:"availability_zone,omitempty"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400160
Jon Perritt01618ee2016-03-09 03:04:06 -0600161 // Networks dictates how this server will be attached to available networks.
Ash Wilson6a310e02014-09-29 08:24:02 -0400162 // By default, the server will be attached to all isolated networks for the tenant.
Jon Perritt01618ee2016-03-09 03:04:06 -0600163 Networks []Network `json:"-"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400164
Jon Perritt01618ee2016-03-09 03:04:06 -0600165 // Metadata contains key-value pairs (up to 255 bytes each) to attach to the server.
166 Metadata map[string]string `json:"-"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400167
Jon Perritt01618ee2016-03-09 03:04:06 -0600168 // Personality includes files to inject into the server at launch.
Kevin Pike92e10b52015-04-10 15:16:57 -0700169 // Create will base64-encode file contents for you.
Jon Perritt01618ee2016-03-09 03:04:06 -0600170 Personality Personality `json:"-"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400171
Jon Perritt01618ee2016-03-09 03:04:06 -0600172 // ConfigDrive enables metadata injection through a configuration drive.
173 ConfigDrive *bool `json:"config_drive,omitempty"`
Jon Perrittf3b2e142014-11-04 16:00:19 -0600174
Jon Perritt01618ee2016-03-09 03:04:06 -0600175 // AdminPass 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 Perritt01618ee2016-03-09 03:04:06 -0600177 AdminPass string `json:"adminPass,omitempty"`
Jon Perritt7b9671c2015-02-01 22:03:14 -0700178
Jon Perritt01618ee2016-03-09 03:04:06 -0600179 // AccessIPv4 specifies an IPv4 address for the instance.
180 AccessIPv4 string `json:"accessIPv4,omitempty"`
Jon Perritt7b9671c2015-02-01 22:03:14 -0700181
Jon Perritt01618ee2016-03-09 03:04:06 -0600182 // AccessIPv6 pecifies an IPv6 address for the instance.
183 AccessIPv6 string `json:"accessIPv6,omitempty"`
Jon Perritt994370e2016-02-18 15:23:34 -0600184
Jon Perritt01618ee2016-03-09 03:04:06 -0600185 // ServiceClient will allow calls to be made to retrieve an image or
Jon Perritt994370e2016-02-18 15:23:34 -0600186 // flavor ID by name.
Jon Perritt01618ee2016-03-09 03:04:06 -0600187 ServiceClient *gophercloud.ServiceClient `json:"-"`
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) {
Jon Perritt01618ee2016-03-09 03:04:06 -0600192 b, err := gophercloud.BuildRequestBody(opts)
193 if err != nil {
194 return nil, err
195 }
Ash Wilson6a310e02014-09-29 08:24:02 -0400196
197 if opts.UserData != nil {
198 encoded := base64.StdEncoding.EncodeToString(opts.UserData)
Jon Perritt01618ee2016-03-09 03:04:06 -0600199 b["user_data"] = &encoded
Jon Perritt7b9671c2015-02-01 22:03:14 -0700200 }
Ash Wilson6a310e02014-09-29 08:24:02 -0400201
202 if len(opts.SecurityGroups) > 0 {
203 securityGroups := make([]map[string]interface{}, len(opts.SecurityGroups))
204 for i, groupName := range opts.SecurityGroups {
205 securityGroups[i] = map[string]interface{}{"name": groupName}
206 }
Jon Perritt01618ee2016-03-09 03:04:06 -0600207 b["security_groups"] = securityGroups
Ash Wilson6a310e02014-09-29 08:24:02 -0400208 }
Jon Perritt2a7797d2014-10-21 15:08:43 -0500209
Ash Wilson6a310e02014-09-29 08:24:02 -0400210 if len(opts.Networks) > 0 {
211 networks := make([]map[string]interface{}, len(opts.Networks))
212 for i, net := range opts.Networks {
213 networks[i] = make(map[string]interface{})
214 if net.UUID != "" {
215 networks[i]["uuid"] = net.UUID
216 }
217 if net.Port != "" {
218 networks[i]["port"] = net.Port
219 }
220 if net.FixedIP != "" {
221 networks[i]["fixed_ip"] = net.FixedIP
222 }
223 }
Jon Perritt01618ee2016-03-09 03:04:06 -0600224 b["networks"] = networks
Kevin Pike92e10b52015-04-10 15:16:57 -0700225 }
226
jrperrittb1013232016-02-10 19:01:53 -0600227 // If ImageRef isn't provided, use ImageName to ascertain the image ID.
228 if opts.ImageRef == "" {
229 if opts.ImageName == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600230 err := ErrNeitherImageIDNorImageNameProvided{}
231 err.Function = "servers.CreateOpts.ToServerCreateMap"
232 err.Argument = "ImageRef/ImageName"
233 return nil, err
jrperrittb1013232016-02-10 19:01:53 -0600234 }
Jon Perritt994370e2016-02-18 15:23:34 -0600235 if opts.ServiceClient == nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600236 err := ErrNoClientProvidedForIDByName{}
237 err.Function = "servers.CreateOpts.ToServerCreateMap"
238 err.Argument = "ServiceClient"
239 return nil, err
Jon Perritt994370e2016-02-18 15:23:34 -0600240 }
241 imageID, err := images.IDFromName(opts.ServiceClient, opts.ImageName)
jrperrittb1013232016-02-10 19:01:53 -0600242 if err != nil {
243 return nil, err
244 }
Jon Perritt01618ee2016-03-09 03:04:06 -0600245 b["imageRef"] = imageID
jrperrittb1013232016-02-10 19:01:53 -0600246 }
247
248 // If FlavorRef isn't provided, use FlavorName to ascertain the flavor ID.
249 if opts.FlavorRef == "" {
250 if opts.FlavorName == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600251 err := ErrNeitherFlavorIDNorFlavorNameProvided{}
252 err.Function = "servers.CreateOpts.ToServerCreateMap"
253 err.Argument = "FlavorRef/FlavorName"
254 return nil, err
jrperrittb1013232016-02-10 19:01:53 -0600255 }
Jon Perritt994370e2016-02-18 15:23:34 -0600256 if opts.ServiceClient == nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600257 err := ErrNoClientProvidedForIDByName{}
258 err.Argument = "ServiceClient"
259 err.Function = "servers.CreateOpts.ToServerCreateMap"
260 return nil, err
Jon Perritt994370e2016-02-18 15:23:34 -0600261 }
262 flavorID, err := flavors.IDFromName(opts.ServiceClient, opts.FlavorName)
jrperrittb1013232016-02-10 19:01:53 -0600263 if err != nil {
264 return nil, err
265 }
Jon Perritt01618ee2016-03-09 03:04:06 -0600266 b["flavorRef"] = flavorID
jrperrittb1013232016-02-10 19:01:53 -0600267 }
268
Jon Perritt01618ee2016-03-09 03:04:06 -0600269 return map[string]interface{}{"server": b}, nil
Ash Wilson6a310e02014-09-29 08:24:02 -0400270}
271
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800272// Create requests a server to be provisioned to the user in the current tenant.
Ash Wilson2206a112014-10-02 10:57:38 -0400273func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600274 var r CreateResult
Jon Perritt4149d7c2014-10-23 21:23:46 -0500275
276 reqBody, err := opts.ToServerCreateMap()
277 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600278 r.Err = err
279 return r
Jon Perritt4149d7c2014-10-23 21:23:46 -0500280 }
281
Jon Perrittf094fef2016-03-07 01:41:59 -0600282 _, r.Err = client.Post(listURL(client), reqBody, &r.Body, nil)
283 return r
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800284}
285
286// Delete requests that a server previously provisioned be removed from your account.
Jamie Hannaford34732fe2014-10-27 11:29:36 +0100287func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600288 var r DeleteResult
289 _, r.Err = client.Delete(deleteURL(client, id), nil)
290 return r
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800291}
292
Jon Perritt01618ee2016-03-09 03:04:06 -0600293// ForceDelete forces the deletion of a server
Ian Duffy370c4302016-01-21 10:44:56 +0000294func ForceDelete(client *gophercloud.ServiceClient, id string) ActionResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600295 var r ActionResult
Jon Perritt01618ee2016-03-09 03:04:06 -0600296 _, r.Err = client.Post(actionURL(client, id), map[string]interface{}{"forceDelete": ""}, nil, nil)
Jon Perrittf094fef2016-03-07 01:41:59 -0600297 return r
Ian Duffy370c4302016-01-21 10:44:56 +0000298}
299
Ash Wilson7ddf0362014-09-17 10:59:09 -0400300// Get requests details on a single server, by ID.
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400301func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600302 var r GetResult
303 _, r.Err = client.Get(getURL(client, id), &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100304 OkCodes: []int{200, 203},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800305 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600306 return r
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800307}
308
Alex Gaynora6d5f9f2014-10-27 10:52:32 -0700309// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
Jon Perritt82048212014-10-13 22:33:13 -0500310type UpdateOptsBuilder interface {
Ash Wilsone45c9732014-09-29 10:54:12 -0400311 ToServerUpdateMap() map[string]interface{}
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400312}
313
314// UpdateOpts specifies the base attributes that may be updated on an existing server.
315type UpdateOpts struct {
316 // Name [optional] changes the displayed name of the server.
317 // The server host name will *not* change.
318 // Server names are not constrained to be unique, even within the same tenant.
319 Name string
320
321 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
322 AccessIPv4 string
323
324 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
325 AccessIPv6 string
326}
327
Ash Wilsone45c9732014-09-29 10:54:12 -0400328// ToServerUpdateMap formats an UpdateOpts structure into a request body.
329func (opts UpdateOpts) ToServerUpdateMap() map[string]interface{} {
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400330 server := make(map[string]string)
331 if opts.Name != "" {
332 server["name"] = opts.Name
333 }
334 if opts.AccessIPv4 != "" {
335 server["accessIPv4"] = opts.AccessIPv4
336 }
337 if opts.AccessIPv6 != "" {
338 server["accessIPv6"] = opts.AccessIPv6
339 }
340 return map[string]interface{}{"server": server}
341}
342
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800343// Update requests that various attributes of the indicated server be changed.
Jon Perritt82048212014-10-13 22:33:13 -0500344func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600345 var r UpdateResult
346 b := opts.ToServerUpdateMap()
347 _, r.Err = client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100348 OkCodes: []int{200},
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800349 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600350 return r
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800351}
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700352
Ash Wilson01626a32014-09-17 10:38:07 -0400353// ChangeAdminPassword alters the administrator or root password for a specified server.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200354func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) ActionResult {
Ash Wilsondc7daa82014-09-23 16:34:42 -0400355 var req struct {
356 ChangePassword struct {
357 AdminPass string `json:"adminPass"`
358 } `json:"changePassword"`
359 }
360
361 req.ChangePassword.AdminPass = newPassword
362
Jon Perrittf094fef2016-03-07 01:41:59 -0600363 var r ActionResult
364 _, r.Err = client.Post(actionURL(client, id), req, nil, nil)
365 return r
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700366}
367
Ash Wilson01626a32014-09-17 10:38:07 -0400368// RebootMethod describes the mechanisms by which a server reboot can be requested.
369type RebootMethod string
370
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700371// These constants determine how a server should be rebooted.
372// See the Reboot() function for further details.
373const (
Ash Wilson01626a32014-09-17 10:38:07 -0400374 SoftReboot RebootMethod = "SOFT"
375 HardReboot RebootMethod = "HARD"
376 OSReboot = SoftReboot
377 PowerCycle = HardReboot
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700378)
379
Jon Perrittf094fef2016-03-07 01:41:59 -0600380type RebootOptsBuilder interface {
381 ToServerRebootMap() (map[string]interface{}, error)
382}
383
384type RebootOpts struct {
385 Type RebootMethod
386}
387
388func (opts *RebootOpts) ToServerRebootMap() (map[string]interface{}, error) {
389 if (opts.Type != SoftReboot) && (opts.Type != HardReboot) {
390 err := &gophercloud.ErrInvalidInput{}
391 err.Argument = "servers.RebootOpts.Type"
392 err.Value = opts.Type
393 err.Function = "servers.Reboot"
394 return nil, err
395 }
396
397 reqBody := map[string]interface{}{
398 "reboot": map[string]interface{}{
399 "type": string(opts.Type),
400 },
401 }
402
403 return reqBody, nil
404}
405
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700406// Reboot requests that a given server reboot.
407// Two methods exist for rebooting a server:
408//
Jon Perrittf094fef2016-03-07 01:41:59 -0600409// 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 -0400410// terminating it at the hypervisor level.
411// It's done. Caput. Full stop.
Jon Perrittf094fef2016-03-07 01:41:59 -0600412// Then, after a brief while, power is rtored or the VM instance rtarted.
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700413//
Jon Perrittf094fef2016-03-07 01:41:59 -0600414// SoftReboot (aka OSReboot) simply tells the OS to rtart under its own procedur.
415// E.g., in Linux, asking it to enter runlevel 6, or executing "sudo shutdown -r now", or by asking Windows to rtart the machine.
416func Reboot(client *gophercloud.ServiceClient, id string, opts RebootOptsBuilder) ActionResult {
417 var r ActionResult
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200418
Jon Perrittf094fef2016-03-07 01:41:59 -0600419 reqBody, err := opts.ToServerRebootMap()
420 if err != nil {
421 r.Err = err
422 return r
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700423 }
Jon Perritt30558642014-04-14 17:07:12 -0500424
Jon Perrittf094fef2016-03-07 01:41:59 -0600425 _, r.Err = client.Post(actionURL(client, id), reqBody, nil, nil)
426 return r
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700427}
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700428
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200429// RebuildOptsBuilder is an interface that allows extensions to override the
430// default behaviour of rebuild options
431type RebuildOptsBuilder interface {
432 ToServerRebuildMap() (map[string]interface{}, error)
433}
434
435// RebuildOpts represents the configuration options used in a server rebuild
436// operation
437type RebuildOpts struct {
438 // Required. The ID of the image you want your server to be provisioned on
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200439 ImageID string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200440
441 // Name to set the server to
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200442 Name string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200443
444 // Required. The server's admin password
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200445 AdminPass string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200446
447 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200448 AccessIPv4 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200449
450 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200451 AccessIPv6 string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200452
453 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
Jamie Hannaforddcb8c272014-10-16 16:34:41 +0200454 Metadata map[string]string
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200455
Kevin Pike92e10b52015-04-10 15:16:57 -0700456 // Personality [optional] includes files to inject into the server at launch.
457 // Rebuild will base64-encode file contents for you.
458 Personality Personality
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200459}
460
461// ToServerRebuildMap formats a RebuildOpts struct into a map for use in JSON
462func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {
463 var err error
464 server := make(map[string]interface{})
465
466 if opts.AdminPass == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600467 err := ErrNoAdminPassProvided{}
468 err.Function = "servers.ToServerRebuildMap"
469 err.Argument = "AdminPass"
470 return nil, err
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200471 }
472
473 if opts.ImageID == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600474 err := ErrNoImageIDProvided{}
475 err.Function = "servers.ToServerRebuildMap"
476 err.Argument = "ImageID"
477 return nil, err
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200478 }
479
480 if err != nil {
481 return server, err
482 }
483
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200484 server["adminPass"] = opts.AdminPass
485 server["imageRef"] = opts.ImageID
486
Jon Perritt12395212016-02-24 10:41:17 -0600487 if opts.Name != "" {
488 server["name"] = opts.Name
489 }
490
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200491 if opts.AccessIPv4 != "" {
492 server["accessIPv4"] = opts.AccessIPv4
493 }
494
495 if opts.AccessIPv6 != "" {
496 server["accessIPv6"] = opts.AccessIPv6
497 }
498
499 if opts.Metadata != nil {
500 server["metadata"] = opts.Metadata
501 }
502
Kevin Pike92e10b52015-04-10 15:16:57 -0700503 if len(opts.Personality) > 0 {
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700504 server["personality"] = opts.Personality
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200505 }
506
507 return map[string]interface{}{"rebuild": server}, nil
508}
509
510// Rebuild will reprovision the server according to the configuration options
511// provided in the RebuildOpts struct.
512func Rebuild(client *gophercloud.ServiceClient, id string, opts RebuildOptsBuilder) RebuildResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600513 var r RebuildResult
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700514
515 if id == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600516 err := ErrNoIDProvided{}
517 err.Function = "servers.Rebuild"
518 err.Argument = "id"
519 r.Err = err
520 return r
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700521 }
522
Jon Perrittf094fef2016-03-07 01:41:59 -0600523 b, err := opts.ToServerRebuildMap()
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200524 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600525 r.Err = err
526 return r
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700527 }
528
Jon Perrittf094fef2016-03-07 01:41:59 -0600529 _, r.Err = client.Post(actionURL(client, id), b, &r.Body, nil)
530 return r
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700531}
532
Ash Wilson5f7cf182014-10-23 08:35:24 -0400533// ResizeOptsBuilder is an interface that allows extensions to override the default structure of
534// a Resize request.
535type ResizeOptsBuilder interface {
536 ToServerResizeMap() (map[string]interface{}, error)
537}
538
539// ResizeOpts represents the configuration options used to control a Resize operation.
540type ResizeOpts struct {
541 // FlavorRef is the ID of the flavor you wish your server to become.
542 FlavorRef string
543}
544
Alex Gaynor266e9332014-10-28 14:44:04 -0700545// 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 -0400546// Resize request.
547func (opts ResizeOpts) ToServerResizeMap() (map[string]interface{}, error) {
548 resize := map[string]interface{}{
549 "flavorRef": opts.FlavorRef,
550 }
551
552 return map[string]interface{}{"resize": resize}, nil
553}
554
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700555// Resize instructs the provider to change the flavor of the server.
Ash Wilson01626a32014-09-17 10:38:07 -0400556// Note that this implies rebuilding it.
Jon Perrittf094fef2016-03-07 01:41:59 -0600557// Unfortunately, one cannot pass rebuild parameters to the rize function.
558// When the rize completes, the server will be in RESIZE_VERIFY state.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700559// While in this state, you can explore the use of the new server's configuration.
Jon Perrittf094fef2016-03-07 01:41:59 -0600560// If you like it, call ConfirmResize() to commit the rize permanently.
561// Otherwise, call RevertResize() to rtore the old configuration.
Ash Wilson9e87a922014-10-23 14:29:22 -0400562func Resize(client *gophercloud.ServiceClient, id string, opts ResizeOptsBuilder) ActionResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600563 var r ActionResult
564
565 if id == "" {
566 err := ErrNoIDProvided{}
567 err.Function = "servers.Resize"
568 err.Argument = "id"
569 r.Err = err
570 return r
Ash Wilson5f7cf182014-10-23 08:35:24 -0400571 }
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200572
Jon Perrittf094fef2016-03-07 01:41:59 -0600573 b, err := opts.ToServerResizeMap()
574 if err != nil {
575 r.Err = err
576 return r
577 }
578
579 _, r.Err = client.Post(actionURL(client, id), b, nil, nil)
580 return r
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700581}
582
Jon Perrittf094fef2016-03-07 01:41:59 -0600583// ConfirmResize confirms a previous rize operation on a server.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700584// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200585func ConfirmResize(client *gophercloud.ServiceClient, id string) ActionResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600586 var r ActionResult
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200587
Jon Perrittf094fef2016-03-07 01:41:59 -0600588 if id == "" {
589 err := ErrNoIDProvided{}
590 err.Function = "servers.ConfirmResize"
591 err.Argument = "id"
592 r.Err = err
593 return r
594 }
595
596 b := map[string]interface{}{"confirmResize": nil}
597 _, r.Err = client.Post(actionURL(client, id), b, nil, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100598 OkCodes: []int{201, 202, 204},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700599 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600600 return r
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700601}
602
Jon Perrittf094fef2016-03-07 01:41:59 -0600603// RevertResize cancels a previous rize operation on a server.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700604// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200605func RevertResize(client *gophercloud.ServiceClient, id string) ActionResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600606 var r ActionResult
607
608 if id == "" {
609 err := ErrNoIDProvided{}
610 err.Function = "servers.RevertResize"
611 err.Argument = "id"
612 r.Err = err
613 return r
614 }
615
616 b := map[string]interface{}{"revertResize": nil}
617 _, r.Err = client.Post(actionURL(client, id), b, nil, nil)
618 return r
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700619}
Alex Gaynor39584a02014-10-28 13:59:21 -0700620
Alex Gaynor266e9332014-10-28 14:44:04 -0700621// RescueOptsBuilder is an interface that allows extensions to override the
622// default structure of a Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700623type RescueOptsBuilder interface {
624 ToServerRescueMap() (map[string]interface{}, error)
625}
626
Alex Gaynor266e9332014-10-28 14:44:04 -0700627// RescueOpts represents the configuration options used to control a Rescue
628// option.
Alex Gaynor39584a02014-10-28 13:59:21 -0700629type RescueOpts struct {
Alex Gaynor266e9332014-10-28 14:44:04 -0700630 // AdminPass is the desired administrative password for the instance in
Alex Gaynorcfec7722014-11-13 13:33:49 -0800631 // RESCUE mode. If it's left blank, the server will generate a password.
Alex Gaynor39584a02014-10-28 13:59:21 -0700632 AdminPass string
633}
634
Jon Perrittcc77da62014-11-16 13:14:21 -0700635// ToServerRescueMap formats a RescueOpts as a map that can be used as a JSON
Alex Gaynor266e9332014-10-28 14:44:04 -0700636// request body for the Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700637func (opts RescueOpts) ToServerRescueMap() (map[string]interface{}, error) {
638 server := make(map[string]interface{})
639 if opts.AdminPass != "" {
640 server["adminPass"] = opts.AdminPass
641 }
642 return map[string]interface{}{"rescue": server}, nil
643}
644
Alex Gaynor266e9332014-10-28 14:44:04 -0700645// Rescue instructs the provider to place the server into RESCUE mode.
Alex Gaynorfbe61bb2014-11-12 13:35:03 -0800646func Rescue(client *gophercloud.ServiceClient, id string, opts RescueOptsBuilder) RescueResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600647 var r RescueResult
Alex Gaynor39584a02014-10-28 13:59:21 -0700648
649 if id == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600650 err := ErrNoIDProvided{}
651 err.Function = "servers.Rebuild"
652 err.Argument = "id"
653 r.Err = err
654 return r
Alex Gaynor39584a02014-10-28 13:59:21 -0700655 }
656
Jon Perrittf094fef2016-03-07 01:41:59 -0600657 b, err := opts.ToServerRescueMap()
658 if err != nil {
659 r.Err = err
660 return r
661 }
662
663 _, r.Err = client.Post(actionURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100664 OkCodes: []int{200},
Alex Gaynor39584a02014-10-28 13:59:21 -0700665 })
666
Jon Perrittf094fef2016-03-07 01:41:59 -0600667 return r
Alex Gaynor39584a02014-10-28 13:59:21 -0700668}
Jon Perrittcc77da62014-11-16 13:14:21 -0700669
Jon Perritt789f8322014-11-21 08:20:04 -0700670// ResetMetadataOptsBuilder allows extensions to add additional parameters to the
671// Reset request.
672type ResetMetadataOptsBuilder interface {
673 ToMetadataResetMap() (map[string]interface{}, error)
Jon Perrittcc77da62014-11-16 13:14:21 -0700674}
675
Jon Perritt78c57ce2014-11-20 11:07:18 -0700676// MetadataOpts is a map that contains key-value pairs.
677type MetadataOpts map[string]string
Jon Perrittcc77da62014-11-16 13:14:21 -0700678
Jon Perritt789f8322014-11-21 08:20:04 -0700679// ToMetadataResetMap assembles a body for a Reset request based on the contents of a MetadataOpts.
680func (opts MetadataOpts) ToMetadataResetMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700681 return map[string]interface{}{"metadata": opts}, nil
682}
683
Jon Perritt78c57ce2014-11-20 11:07:18 -0700684// ToMetadataUpdateMap assembles a body for an Update request based on the contents of a MetadataOpts.
685func (opts MetadataOpts) ToMetadataUpdateMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700686 return map[string]interface{}{"metadata": opts}, nil
687}
688
Jon Perritt789f8322014-11-21 08:20:04 -0700689// ResetMetadata will create multiple new key-value pairs for the given server ID.
Jon Perrittcc77da62014-11-16 13:14:21 -0700690// Note: Using this operation will erase any already-existing metadata and create
691// the new metadata provided. To keep any already-existing metadata, use the
692// UpdateMetadatas or UpdateMetadata function.
Jon Perritt789f8322014-11-21 08:20:04 -0700693func ResetMetadata(client *gophercloud.ServiceClient, id string, opts ResetMetadataOptsBuilder) ResetMetadataResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600694 var r ResetMetadataResult
Jon Perritt789f8322014-11-21 08:20:04 -0700695 metadata, err := opts.ToMetadataResetMap()
Jon Perrittcc77da62014-11-16 13:14:21 -0700696 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600697 r.Err = err
698 return r
Jon Perrittcc77da62014-11-16 13:14:21 -0700699 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600700 _, r.Err = client.Put(metadataURL(client, id), metadata, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100701 OkCodes: []int{200},
Jon Perrittcc77da62014-11-16 13:14:21 -0700702 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600703 return r
Jon Perrittcc77da62014-11-16 13:14:21 -0700704}
705
Jon Perritt78c57ce2014-11-20 11:07:18 -0700706// Metadata requests all the metadata for the given server ID.
707func Metadata(client *gophercloud.ServiceClient, id string) GetMetadataResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600708 var r GetMetadataResult
709 _, r.Err = client.Get(metadataURL(client, id), &r.Body, nil)
710 return r
Jon Perrittcc77da62014-11-16 13:14:21 -0700711}
712
Jon Perritt78c57ce2014-11-20 11:07:18 -0700713// UpdateMetadataOptsBuilder allows extensions to add additional parameters to the
714// Create request.
715type UpdateMetadataOptsBuilder interface {
716 ToMetadataUpdateMap() (map[string]interface{}, error)
717}
718
719// UpdateMetadata updates (or creates) all the metadata specified by opts for the given server ID.
720// This operation does not affect already-existing metadata that is not specified
721// by opts.
722func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) UpdateMetadataResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600723 var r UpdateMetadataResult
Jon Perritt78c57ce2014-11-20 11:07:18 -0700724 metadata, err := opts.ToMetadataUpdateMap()
725 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600726 r.Err = err
727 return r
Jon Perritt78c57ce2014-11-20 11:07:18 -0700728 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600729 _, r.Err = client.Post(metadataURL(client, id), metadata, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100730 OkCodes: []int{200},
Jon Perritt78c57ce2014-11-20 11:07:18 -0700731 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600732 return r
Jon Perritt78c57ce2014-11-20 11:07:18 -0700733}
734
735// MetadatumOptsBuilder allows extensions to add additional parameters to the
736// Create request.
737type MetadatumOptsBuilder interface {
738 ToMetadatumCreateMap() (map[string]interface{}, string, error)
739}
740
741// MetadatumOpts is a map of length one that contains a key-value pair.
742type MetadatumOpts map[string]string
743
744// ToMetadatumCreateMap assembles a body for a Create request based on the contents of a MetadataumOpts.
745func (opts MetadatumOpts) ToMetadatumCreateMap() (map[string]interface{}, string, error) {
746 if len(opts) != 1 {
Jon Perritt13808262016-03-09 00:50:12 -0600747 err := gophercloud.ErrInvalidInput{}
748 err.Argument = "servers.MetadatumOpts"
749 err.Info = "Must have 1 and only 1 key-value pair"
750 return nil, "", err
Jon Perritt78c57ce2014-11-20 11:07:18 -0700751 }
752 metadatum := map[string]interface{}{"meta": opts}
753 var key string
754 for k := range metadatum["meta"].(MetadatumOpts) {
755 key = k
756 }
757 return metadatum, key, nil
758}
759
760// CreateMetadatum will create or update the key-value pair with the given key for the given server ID.
761func CreateMetadatum(client *gophercloud.ServiceClient, id string, opts MetadatumOptsBuilder) CreateMetadatumResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600762 var r CreateMetadatumResult
Jon Perritt78c57ce2014-11-20 11:07:18 -0700763 metadatum, key, err := opts.ToMetadatumCreateMap()
764 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600765 r.Err = err
766 return r
Jon Perritt78c57ce2014-11-20 11:07:18 -0700767 }
768
Jon Perrittf094fef2016-03-07 01:41:59 -0600769 _, r.Err = client.Put(metadatumURL(client, id, key), metadatum, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100770 OkCodes: []int{200},
Jon Perritt78c57ce2014-11-20 11:07:18 -0700771 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600772 return r
Jon Perritt78c57ce2014-11-20 11:07:18 -0700773}
774
775// Metadatum requests the key-value pair with the given key for the given server ID.
776func Metadatum(client *gophercloud.ServiceClient, id, key string) GetMetadatumResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600777 var r GetMetadatumResult
778 _, r.Err = client.Request("GET", metadatumURL(client, id, key), &gophercloud.RequestOpts{
779 JSONResponse: &r.Body,
Jon Perritt78c57ce2014-11-20 11:07:18 -0700780 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600781 return r
Jon Perritt78c57ce2014-11-20 11:07:18 -0700782}
783
784// DeleteMetadatum will delete the key-value pair with the given key for the given server ID.
785func DeleteMetadatum(client *gophercloud.ServiceClient, id, key string) DeleteMetadatumResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600786 var r DeleteMetadatumResult
787 _, r.Err = client.Delete(metadatumURL(client, id, key), nil)
788 return r
Jon Perrittcc77da62014-11-16 13:14:21 -0700789}
Jon Perritt5cb49482015-02-19 12:19:58 -0700790
791// ListAddresses makes a request against the API to list the servers IP addresses.
792func ListAddresses(client *gophercloud.ServiceClient, id string) pagination.Pager {
793 createPageFn := func(r pagination.PageResult) pagination.Page {
794 return AddressPage{pagination.SinglePageBase(r)}
795 }
796 return pagination.NewPager(client, listAddressesURL(client, id), createPageFn)
797}
Jon Perritt04d073c2015-02-19 21:46:23 -0700798
799// ListAddressesByNetwork makes a request against the API to list the servers IP addresses
800// for the given network.
801func ListAddressesByNetwork(client *gophercloud.ServiceClient, id, network string) pagination.Pager {
802 createPageFn := func(r pagination.PageResult) pagination.Page {
803 return NetworkAddressPage{pagination.SinglePageBase(r)}
804 }
805 return pagination.NewPager(client, listAddressesByNetworkURL(client, id, network), createPageFn)
806}
einarf2fc665e2015-04-16 20:16:21 +0000807
einarf4e5fdaf2015-04-16 23:14:59 +0000808type CreateImageOpts struct {
einarf2fc665e2015-04-16 20:16:21 +0000809 // Name [required] of the image/snapshot
810 Name string
811 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the created image.
812 Metadata map[string]string
813}
814
einarf4e5fdaf2015-04-16 23:14:59 +0000815type CreateImageOptsBuilder interface {
816 ToServerCreateImageMap() (map[string]interface{}, error)
einarf2fc665e2015-04-16 20:16:21 +0000817}
818
einarf4e5fdaf2015-04-16 23:14:59 +0000819// ToServerCreateImageMap formats a CreateImageOpts structure into a request body.
820func (opts CreateImageOpts) ToServerCreateImageMap() (map[string]interface{}, error) {
einarf2fc665e2015-04-16 20:16:21 +0000821 var err error
822 img := make(map[string]interface{})
823 if opts.Name == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600824 err := gophercloud.ErrMissingInput{}
825 err.Function = "servers.CreateImageOpts.ToServerCreateImageMap"
826 err.Argument = "CreateImageOpts.Name"
827 return nil, err
einarf2fc665e2015-04-16 20:16:21 +0000828 }
829 img["name"] = opts.Name
830 if opts.Metadata != nil {
831 img["metadata"] = opts.Metadata
832 }
833 createImage := make(map[string]interface{})
834 createImage["createImage"] = img
835 return createImage, err
836}
837
einarf4e5fdaf2015-04-16 23:14:59 +0000838// CreateImage makes a request against the nova API to schedule an image to be created of the server
jrperrittb1013232016-02-10 19:01:53 -0600839func CreateImage(client *gophercloud.ServiceClient, serverID string, opts CreateImageOptsBuilder) CreateImageResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600840 var r CreateImageResult
841 b, err := opts.ToServerCreateImageMap()
einarf2fc665e2015-04-16 20:16:21 +0000842 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600843 r.Err = err
844 return r
einarf2fc665e2015-04-16 20:16:21 +0000845 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600846 resp, err := client.Post(actionURL(client, serverID), b, nil, &gophercloud.RequestOpts{
einarf2fc665e2015-04-16 20:16:21 +0000847 OkCodes: []int{202},
848 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600849 r.Err = err
850 r.Header = resp.Header
851 return r
einarf2fc665e2015-04-16 20:16:21 +0000852}
Jon Perritt6b0a8832015-06-04 14:32:30 -0600853
854// IDFromName is a convienience function that returns a server's ID given its name.
855func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600856 count := 0
857 id := ""
Jon Perritt6b0a8832015-06-04 14:32:30 -0600858 if name == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600859 err := &gophercloud.ErrMissingInput{}
860 err.Function = "servers.IDFromName"
861 err.Argument = "name"
862 return "", err
Jon Perritt6b0a8832015-06-04 14:32:30 -0600863 }
Jon Perritt6b0a8832015-06-04 14:32:30 -0600864
Jon Perrittf094fef2016-03-07 01:41:59 -0600865 allPages, err := List(client, nil).AllPages()
866 if err != nil {
867 return "", err
868 }
Jon Perritt6b0a8832015-06-04 14:32:30 -0600869
Jon Perrittf094fef2016-03-07 01:41:59 -0600870 all, err := ExtractServers(allPages)
871 if err != nil {
872 return "", err
873 }
874
875 for _, f := range all {
876 if f.Name == name {
877 count++
878 id = f.ID
879 }
880 }
881
882 switch count {
Jon Perritt6b0a8832015-06-04 14:32:30 -0600883 case 0:
Jon Perrittf094fef2016-03-07 01:41:59 -0600884 err := &gophercloud.ErrResourceNotFound{}
885 err.Function = "servers.IDFromName"
886 err.ResourceType = "server"
887 err.Name = name
888 return "", err
Jon Perritt6b0a8832015-06-04 14:32:30 -0600889 case 1:
Jon Perrittf094fef2016-03-07 01:41:59 -0600890 return id, nil
Jon Perritt6b0a8832015-06-04 14:32:30 -0600891 default:
Jon Perrittf094fef2016-03-07 01:41:59 -0600892 err := &gophercloud.ErrMultipleResourcesFound{}
893 err.Function = "servers.IDFromName"
894 err.ResourceType = "server"
895 err.Name = name
896 err.Count = count
897 return "", err
Jon Perritt6b0a8832015-06-04 14:32:30 -0600898 }
899}