blob: ba30a799f1e969eb9debfee235f20b19d12984c2 [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)
Jon Perrittdb0ae142016-03-13 00:33:41 -060058 return q.String(), err
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020059}
60
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080061// List makes a request against the API to list servers accessible to you.
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020062func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
63 url := listDetailURL(client)
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020064 if opts != nil {
65 query, err := opts.ToServerListQuery()
66 if err != nil {
67 return pagination.Pager{Err: err}
68 }
69 url += query
70 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060071 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
Ash Wilsonb8b16f82014-10-20 10:19:49 -040072 return ServerPage{pagination.LinkedPageBase{PageResult: r}}
Jon Perrittdb0ae142016-03-13 00:33:41 -060073 })
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080074}
75
Ash Wilson2206a112014-10-02 10:57:38 -040076// CreateOptsBuilder describes struct types that can be accepted by the Create call.
Ash Wilson6a310e02014-09-29 08:24:02 -040077// The CreateOpts struct in this package does.
Ash Wilson2206a112014-10-02 10:57:38 -040078type CreateOptsBuilder interface {
Jon Perritt4149d7c2014-10-23 21:23:46 -050079 ToServerCreateMap() (map[string]interface{}, error)
Ash Wilson6a310e02014-09-29 08:24:02 -040080}
81
82// Network is used within CreateOpts to control a new server's network attachments.
83type Network struct {
84 // UUID of a nova-network to attach to the newly provisioned server.
85 // Required unless Port is provided.
86 UUID string
87
88 // Port of a neutron network to attach to the newly provisioned server.
89 // Required unless UUID is provided.
90 Port string
91
92 // FixedIP [optional] specifies a fixed IPv4 address to be used on this network.
93 FixedIP string
94}
95
Kevin Pike92e10b52015-04-10 15:16:57 -070096// Personality is an array of files that are injected into the server at launch.
Kevin Pikea2bfaea2015-04-21 11:45:59 -070097type Personality []*File
Kevin Pike92e10b52015-04-10 15:16:57 -070098
99// File is used within CreateOpts and RebuildOpts to inject a file into the server at launch.
Kevin Pike9748b7b2015-05-05 07:34:07 -0700100// File implements the json.Marshaler interface, so when a Create or Rebuild operation is requested,
101// json.Marshal will call File's MarshalJSON method.
Kevin Pike92e10b52015-04-10 15:16:57 -0700102type File struct {
103 // Path of the file
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700104 Path string
Kevin Pike92e10b52015-04-10 15:16:57 -0700105 // Contents of the file. Maximum content size is 255 bytes.
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700106 Contents []byte
Kevin Pike92e10b52015-04-10 15:16:57 -0700107}
108
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700109// MarshalJSON marshals the escaped file, base64 encoding the contents.
110func (f *File) MarshalJSON() ([]byte, error) {
111 file := struct {
112 Path string `json:"path"`
113 Contents string `json:"contents"`
114 }{
115 Path: f.Path,
116 Contents: base64.StdEncoding.EncodeToString(f.Contents),
Kevin Pike92e10b52015-04-10 15:16:57 -0700117 }
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700118 return json.Marshal(file)
Kevin Pike92e10b52015-04-10 15:16:57 -0700119}
120
Ash Wilson6a310e02014-09-29 08:24:02 -0400121// CreateOpts specifies server creation parameters.
122type CreateOpts struct {
Jon Perritt01618ee2016-03-09 03:04:06 -0600123 // Name is the name to assign to the newly launched server.
124 Name string `json:"name" required:"true"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400125
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600126 // ImageRef [optional; required if ImageName is not provided] is the ID or full
127 // URL to the image that contains the server's OS and initial state.
128 // Also optional if using the boot-from-volume extension.
Jon Perritt01618ee2016-03-09 03:04:06 -0600129 ImageRef string `json:"imageRef"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400130
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600131 // ImageName [optional; required if ImageRef is not provided] is the name of the
132 // image that contains the server's OS and initial state.
133 // Also optional if using the boot-from-volume extension.
Jon Perritt01618ee2016-03-09 03:04:06 -0600134 ImageName string `json:"-"`
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600135
136 // FlavorRef [optional; required if FlavorName is not provided] is the ID or
137 // full URL to the flavor that describes the server's specs.
Jon Perritt01618ee2016-03-09 03:04:06 -0600138 FlavorRef string `json:"flavorRef"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400139
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600140 // FlavorName [optional; required if FlavorRef is not provided] is the name of
141 // the flavor that describes the server's specs.
Jon Perritt01618ee2016-03-09 03:04:06 -0600142 FlavorName string `json:"-"`
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600143
Jon Perritt01618ee2016-03-09 03:04:06 -0600144 // SecurityGroups lists the names of the security groups to which this server should belong.
145 SecurityGroups []string `json:"-"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400146
Jon Perritt01618ee2016-03-09 03:04:06 -0600147 // UserData contains configuration information or scripts to use upon launch.
Ash Wilson6a310e02014-09-29 08:24:02 -0400148 // Create will base64-encode it for you.
Jon Perritt01618ee2016-03-09 03:04:06 -0600149 UserData []byte `json:"-"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400150
Jon Perritt01618ee2016-03-09 03:04:06 -0600151 // AvailabilityZone in which to launch the server.
152 AvailabilityZone string `json:"availability_zone,omitempty"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400153
Jon Perritt01618ee2016-03-09 03:04:06 -0600154 // Networks dictates how this server will be attached to available networks.
Ash Wilson6a310e02014-09-29 08:24:02 -0400155 // By default, the server will be attached to all isolated networks for the tenant.
Jon Perritt01618ee2016-03-09 03:04:06 -0600156 Networks []Network `json:"-"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400157
Jon Perritt01618ee2016-03-09 03:04:06 -0600158 // Metadata contains key-value pairs (up to 255 bytes each) to attach to the server.
159 Metadata map[string]string `json:"-"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400160
Jon Perritt01618ee2016-03-09 03:04:06 -0600161 // Personality includes files to inject into the server at launch.
Kevin Pike92e10b52015-04-10 15:16:57 -0700162 // Create will base64-encode file contents for you.
Jon Perritt01618ee2016-03-09 03:04:06 -0600163 Personality Personality `json:"-"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400164
Jon Perritt01618ee2016-03-09 03:04:06 -0600165 // ConfigDrive enables metadata injection through a configuration drive.
166 ConfigDrive *bool `json:"config_drive,omitempty"`
Jon Perrittf3b2e142014-11-04 16:00:19 -0600167
Jon Perritt01618ee2016-03-09 03:04:06 -0600168 // AdminPass sets the root user password. If not set, a randomly-generated
Jon Perrittf094fef2016-03-07 01:41:59 -0600169 // password will be created and returned in the rponse.
Jon Perritt01618ee2016-03-09 03:04:06 -0600170 AdminPass string `json:"adminPass,omitempty"`
Jon Perritt7b9671c2015-02-01 22:03:14 -0700171
Jon Perritt01618ee2016-03-09 03:04:06 -0600172 // AccessIPv4 specifies an IPv4 address for the instance.
173 AccessIPv4 string `json:"accessIPv4,omitempty"`
Jon Perritt7b9671c2015-02-01 22:03:14 -0700174
Jon Perritt01618ee2016-03-09 03:04:06 -0600175 // AccessIPv6 pecifies an IPv6 address for the instance.
176 AccessIPv6 string `json:"accessIPv6,omitempty"`
Jon Perritt994370e2016-02-18 15:23:34 -0600177
Jon Perritt01618ee2016-03-09 03:04:06 -0600178 // ServiceClient will allow calls to be made to retrieve an image or
Jon Perritt994370e2016-02-18 15:23:34 -0600179 // flavor ID by name.
Jon Perritt01618ee2016-03-09 03:04:06 -0600180 ServiceClient *gophercloud.ServiceClient `json:"-"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400181}
182
Ash Wilsone45c9732014-09-29 10:54:12 -0400183// ToServerCreateMap assembles a request body based on the contents of a CreateOpts.
Jon Perritt4149d7c2014-10-23 21:23:46 -0500184func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600185 b, err := gophercloud.BuildRequestBody(opts, "")
Jon Perritt01618ee2016-03-09 03:04:06 -0600186 if err != nil {
187 return nil, err
188 }
Ash Wilson6a310e02014-09-29 08:24:02 -0400189
190 if opts.UserData != nil {
191 encoded := base64.StdEncoding.EncodeToString(opts.UserData)
Jon Perritt01618ee2016-03-09 03:04:06 -0600192 b["user_data"] = &encoded
Jon Perritt7b9671c2015-02-01 22:03:14 -0700193 }
Ash Wilson6a310e02014-09-29 08:24:02 -0400194
195 if len(opts.SecurityGroups) > 0 {
196 securityGroups := make([]map[string]interface{}, len(opts.SecurityGroups))
197 for i, groupName := range opts.SecurityGroups {
198 securityGroups[i] = map[string]interface{}{"name": groupName}
199 }
Jon Perritt01618ee2016-03-09 03:04:06 -0600200 b["security_groups"] = securityGroups
Ash Wilson6a310e02014-09-29 08:24:02 -0400201 }
Jon Perritt2a7797d2014-10-21 15:08:43 -0500202
Ash Wilson6a310e02014-09-29 08:24:02 -0400203 if len(opts.Networks) > 0 {
204 networks := make([]map[string]interface{}, len(opts.Networks))
205 for i, net := range opts.Networks {
206 networks[i] = make(map[string]interface{})
207 if net.UUID != "" {
208 networks[i]["uuid"] = net.UUID
209 }
210 if net.Port != "" {
211 networks[i]["port"] = net.Port
212 }
213 if net.FixedIP != "" {
214 networks[i]["fixed_ip"] = net.FixedIP
215 }
216 }
Jon Perritt01618ee2016-03-09 03:04:06 -0600217 b["networks"] = networks
Kevin Pike92e10b52015-04-10 15:16:57 -0700218 }
219
jrperrittb1013232016-02-10 19:01:53 -0600220 // If ImageRef isn't provided, use ImageName to ascertain the image ID.
221 if opts.ImageRef == "" {
222 if opts.ImageName == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600223 err := ErrNeitherImageIDNorImageNameProvided{}
Jon Perrittf094fef2016-03-07 01:41:59 -0600224 err.Argument = "ImageRef/ImageName"
225 return nil, err
jrperrittb1013232016-02-10 19:01:53 -0600226 }
Jon Perritt994370e2016-02-18 15:23:34 -0600227 if opts.ServiceClient == nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600228 err := ErrNoClientProvidedForIDByName{}
Jon Perrittf094fef2016-03-07 01:41:59 -0600229 err.Argument = "ServiceClient"
230 return nil, err
Jon Perritt994370e2016-02-18 15:23:34 -0600231 }
232 imageID, err := images.IDFromName(opts.ServiceClient, opts.ImageName)
jrperrittb1013232016-02-10 19:01:53 -0600233 if err != nil {
234 return nil, err
235 }
Jon Perritt01618ee2016-03-09 03:04:06 -0600236 b["imageRef"] = imageID
jrperrittb1013232016-02-10 19:01:53 -0600237 }
238
239 // If FlavorRef isn't provided, use FlavorName to ascertain the flavor ID.
240 if opts.FlavorRef == "" {
241 if opts.FlavorName == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600242 err := ErrNeitherFlavorIDNorFlavorNameProvided{}
Jon Perrittf094fef2016-03-07 01:41:59 -0600243 err.Argument = "FlavorRef/FlavorName"
244 return nil, err
jrperrittb1013232016-02-10 19:01:53 -0600245 }
Jon Perritt994370e2016-02-18 15:23:34 -0600246 if opts.ServiceClient == nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600247 err := ErrNoClientProvidedForIDByName{}
248 err.Argument = "ServiceClient"
Jon Perrittf094fef2016-03-07 01:41:59 -0600249 return nil, err
Jon Perritt994370e2016-02-18 15:23:34 -0600250 }
251 flavorID, err := flavors.IDFromName(opts.ServiceClient, opts.FlavorName)
jrperrittb1013232016-02-10 19:01:53 -0600252 if err != nil {
253 return nil, err
254 }
Jon Perritt01618ee2016-03-09 03:04:06 -0600255 b["flavorRef"] = flavorID
jrperrittb1013232016-02-10 19:01:53 -0600256 }
257
Jon Perritt01618ee2016-03-09 03:04:06 -0600258 return map[string]interface{}{"server": b}, nil
Ash Wilson6a310e02014-09-29 08:24:02 -0400259}
260
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800261// Create requests a server to be provisioned to the user in the current tenant.
Jon Perritt3860b512016-03-29 12:01:48 -0500262func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
Jon Perritt4149d7c2014-10-23 21:23:46 -0500263 reqBody, err := opts.ToServerCreateMap()
264 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600265 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500266 return
Jon Perritt4149d7c2014-10-23 21:23:46 -0500267 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600268 _, r.Err = client.Post(listURL(client), reqBody, &r.Body, nil)
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800269}
270
271// Delete requests that a server previously provisioned be removed from your account.
Jon Perritt3860b512016-03-29 12:01:48 -0500272func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600273 _, r.Err = client.Delete(deleteURL(client, id), nil)
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800274}
275
Jon Perritt01618ee2016-03-09 03:04:06 -0600276// ForceDelete forces the deletion of a server
Jon Perritt3860b512016-03-29 12:01:48 -0500277func ForceDelete(client *gophercloud.ServiceClient, id string) (r ActionResult) {
Jon Perritt01618ee2016-03-09 03:04:06 -0600278 _, r.Err = client.Post(actionURL(client, id), map[string]interface{}{"forceDelete": ""}, nil, nil)
Ian Duffy370c4302016-01-21 10:44:56 +0000279}
280
Ash Wilson7ddf0362014-09-17 10:59:09 -0400281// Get requests details on a single server, by ID.
Jon Perritt3860b512016-03-29 12:01:48 -0500282func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600283 _, r.Err = client.Get(getURL(client, id), &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100284 OkCodes: []int{200, 203},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800285 })
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800286}
287
Alex Gaynora6d5f9f2014-10-27 10:52:32 -0700288// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
Jon Perritt82048212014-10-13 22:33:13 -0500289type UpdateOptsBuilder interface {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600290 ToServerUpdateMap() (map[string]interface{}, error)
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400291}
292
293// UpdateOpts specifies the base attributes that may be updated on an existing server.
294type UpdateOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600295 // Name changes the displayed name of the server.
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400296 // The server host name will *not* change.
297 // Server names are not constrained to be unique, even within the same tenant.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600298 Name string `json:"name,omitempty"`
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400299
Jon Perrittdb0ae142016-03-13 00:33:41 -0600300 // AccessIPv4 provides a new IPv4 address for the instance.
301 AccessIPv4 string `json:"accessIPv4,omitempty"`
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400302
Jon Perrittdb0ae142016-03-13 00:33:41 -0600303 // AccessIPv6 provides a new IPv6 address for the instance.
304 AccessIPv6 string `json:"accessIPv6,omitempty"`
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400305}
306
Ash Wilsone45c9732014-09-29 10:54:12 -0400307// ToServerUpdateMap formats an UpdateOpts structure into a request body.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600308func (opts UpdateOpts) ToServerUpdateMap() (map[string]interface{}, error) {
309 return gophercloud.BuildRequestBody(opts, "server")
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400310}
311
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800312// Update requests that various attributes of the indicated server be changed.
Jon Perritt3860b512016-03-29 12:01:48 -0500313func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600314 b, err := opts.ToServerUpdateMap()
315 if err != nil {
316 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500317 return
Jon Perrittdb0ae142016-03-13 00:33:41 -0600318 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600319 _, r.Err = client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100320 OkCodes: []int{200},
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800321 })
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800322}
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700323
Ash Wilson01626a32014-09-17 10:38:07 -0400324// ChangeAdminPassword alters the administrator or root password for a specified server.
Jon Perritt3860b512016-03-29 12:01:48 -0500325func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) (r ActionResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600326 b := map[string]interface{}{
327 "changePassword": map[string]string{
328 "adminPass": newPassword,
329 },
330 }
331 _, r.Err = client.Post(actionURL(client, id), b, nil, nil)
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700332}
333
Ash Wilson01626a32014-09-17 10:38:07 -0400334// RebootMethod describes the mechanisms by which a server reboot can be requested.
335type RebootMethod string
336
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700337// These constants determine how a server should be rebooted.
338// See the Reboot() function for further details.
339const (
Ash Wilson01626a32014-09-17 10:38:07 -0400340 SoftReboot RebootMethod = "SOFT"
341 HardReboot RebootMethod = "HARD"
342 OSReboot = SoftReboot
343 PowerCycle = HardReboot
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700344)
345
Jon Perrittdb0ae142016-03-13 00:33:41 -0600346// RebootOptsBuilder is an interface that options must satisfy in order to be
347// used when rebooting a server instance
Jon Perrittf094fef2016-03-07 01:41:59 -0600348type RebootOptsBuilder interface {
349 ToServerRebootMap() (map[string]interface{}, error)
350}
351
Jon Perrittdb0ae142016-03-13 00:33:41 -0600352// RebootOpts satisfies the RebootOptsBuilder interface
Jon Perrittf094fef2016-03-07 01:41:59 -0600353type RebootOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600354 Type RebootMethod `json:"type" required:"true"`
Jon Perrittf094fef2016-03-07 01:41:59 -0600355}
356
Jon Perrittdb0ae142016-03-13 00:33:41 -0600357// ToServerRebootMap allows RebootOpts to satisfiy the RebootOptsBuilder
358// interface
Jon Perrittf094fef2016-03-07 01:41:59 -0600359func (opts *RebootOpts) ToServerRebootMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600360 return gophercloud.BuildRequestBody(opts, "reboot")
Jon Perrittf094fef2016-03-07 01:41:59 -0600361}
362
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700363// Reboot requests that a given server reboot.
364// Two methods exist for rebooting a server:
365//
Jon Perrittdb0ae142016-03-13 00:33:41 -0600366// HardReboot (aka PowerCycle) starts the server instance by physically cutting power to the machine, or if a VM,
Ash Wilson01626a32014-09-17 10:38:07 -0400367// terminating it at the hypervisor level.
368// It's done. Caput. Full stop.
Jon Perrittf094fef2016-03-07 01:41:59 -0600369// Then, after a brief while, power is rtored or the VM instance rtarted.
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700370//
Jon Perrittf094fef2016-03-07 01:41:59 -0600371// SoftReboot (aka OSReboot) simply tells the OS to rtart under its own procedur.
372// E.g., in Linux, asking it to enter runlevel 6, or executing "sudo shutdown -r now", or by asking Windows to rtart the machine.
Jon Perritt3860b512016-03-29 12:01:48 -0500373func Reboot(client *gophercloud.ServiceClient, id string, opts RebootOptsBuilder) (r ActionResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600374 b, err := opts.ToServerRebootMap()
Jon Perrittf094fef2016-03-07 01:41:59 -0600375 if err != nil {
376 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500377 return
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700378 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600379 _, r.Err = client.Post(actionURL(client, id), b, nil, nil)
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700380}
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700381
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200382// RebuildOptsBuilder is an interface that allows extensions to override the
383// default behaviour of rebuild options
384type RebuildOptsBuilder interface {
385 ToServerRebuildMap() (map[string]interface{}, error)
386}
387
388// RebuildOpts represents the configuration options used in a server rebuild
389// operation
390type RebuildOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600391 // The server's admin password
392 AdminPass string `json:"adminPass" required:"true"`
393 // The ID of the image you want your server to be provisioned on
394 ImageID string `json:"imageRef"`
395 ImageName string `json:"-"`
396 //ImageName string `json:"-"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200397 // Name to set the server to
Jon Perrittdb0ae142016-03-13 00:33:41 -0600398 Name string `json:"name,omitempty"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200399 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600400 AccessIPv4 string `json:"accessIPv4,omitempty"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200401 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600402 AccessIPv6 string `json:"accessIPv6,omitempty"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200403 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600404 Metadata map[string]string `json:"metadata,omitempty"`
Kevin Pike92e10b52015-04-10 15:16:57 -0700405 // Personality [optional] includes files to inject into the server at launch.
406 // Rebuild will base64-encode file contents for you.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600407 Personality Personality `json:"personality,omitempty"`
408 ServiceClient *gophercloud.ServiceClient `json:"-"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200409}
410
411// ToServerRebuildMap formats a RebuildOpts struct into a map for use in JSON
412func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600413 b, err := gophercloud.BuildRequestBody(opts, "")
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200414 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600415 return nil, err
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200416 }
417
Jon Perrittdb0ae142016-03-13 00:33:41 -0600418 // If ImageRef isn't provided, use ImageName to ascertain the image ID.
419 if opts.ImageID == "" {
420 if opts.ImageName == "" {
421 err := ErrNeitherImageIDNorImageNameProvided{}
422 err.Argument = "ImageRef/ImageName"
423 return nil, err
424 }
425 if opts.ServiceClient == nil {
426 err := ErrNoClientProvidedForIDByName{}
427 err.Argument = "ServiceClient"
428 return nil, err
429 }
430 imageID, err := images.IDFromName(opts.ServiceClient, opts.ImageName)
431 if err != nil {
432 return nil, err
433 }
434 b["imageRef"] = imageID
Jon Perritt12395212016-02-24 10:41:17 -0600435 }
436
Jon Perrittdb0ae142016-03-13 00:33:41 -0600437 return map[string]interface{}{"rebuild": b}, nil
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200438}
439
440// Rebuild will reprovision the server according to the configuration options
441// provided in the RebuildOpts struct.
Jon Perritt3860b512016-03-29 12:01:48 -0500442func Rebuild(client *gophercloud.ServiceClient, id string, opts RebuildOptsBuilder) (r RebuildResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600443 b, err := opts.ToServerRebuildMap()
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200444 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600445 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500446 return
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700447 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600448 _, r.Err = client.Post(actionURL(client, id), b, &r.Body, nil)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700449}
450
Ash Wilson5f7cf182014-10-23 08:35:24 -0400451// ResizeOptsBuilder is an interface that allows extensions to override the default structure of
452// a Resize request.
453type ResizeOptsBuilder interface {
454 ToServerResizeMap() (map[string]interface{}, error)
455}
456
457// ResizeOpts represents the configuration options used to control a Resize operation.
458type ResizeOpts struct {
459 // FlavorRef is the ID of the flavor you wish your server to become.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600460 FlavorRef string `json:"flavorRef" required:"true"`
Ash Wilson5f7cf182014-10-23 08:35:24 -0400461}
462
Alex Gaynor266e9332014-10-28 14:44:04 -0700463// 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 -0400464// Resize request.
465func (opts ResizeOpts) ToServerResizeMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600466 return gophercloud.BuildRequestBody(opts, "resize")
Ash Wilson5f7cf182014-10-23 08:35:24 -0400467}
468
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700469// Resize instructs the provider to change the flavor of the server.
Ash Wilson01626a32014-09-17 10:38:07 -0400470// Note that this implies rebuilding it.
Jon Perrittf094fef2016-03-07 01:41:59 -0600471// Unfortunately, one cannot pass rebuild parameters to the rize function.
472// When the rize completes, the server will be in RESIZE_VERIFY state.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700473// While in this state, you can explore the use of the new server's configuration.
Jon Perrittf094fef2016-03-07 01:41:59 -0600474// If you like it, call ConfirmResize() to commit the rize permanently.
475// Otherwise, call RevertResize() to rtore the old configuration.
Jon Perritt3860b512016-03-29 12:01:48 -0500476func Resize(client *gophercloud.ServiceClient, id string, opts ResizeOptsBuilder) (r ActionResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600477 b, err := opts.ToServerResizeMap()
478 if err != nil {
479 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500480 return
Jon Perrittf094fef2016-03-07 01:41:59 -0600481 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600482 _, r.Err = client.Post(actionURL(client, id), b, nil, nil)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700483}
484
Jon Perrittf094fef2016-03-07 01:41:59 -0600485// ConfirmResize confirms a previous rize operation on a server.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700486// See Resize() for more details.
Jon Perritt3860b512016-03-29 12:01:48 -0500487func ConfirmResize(client *gophercloud.ServiceClient, id string) (r ActionResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600488 _, r.Err = client.Post(actionURL(client, id), map[string]interface{}{"confirmResize": nil}, nil, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100489 OkCodes: []int{201, 202, 204},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700490 })
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700491}
492
Jon Perrittf094fef2016-03-07 01:41:59 -0600493// RevertResize cancels a previous rize operation on a server.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700494// See Resize() for more details.
Jon Perritt3860b512016-03-29 12:01:48 -0500495func RevertResize(client *gophercloud.ServiceClient, id string) (r ActionResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600496 _, r.Err = client.Post(actionURL(client, id), map[string]interface{}{"revertResize": nil}, nil, nil)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700497}
Alex Gaynor39584a02014-10-28 13:59:21 -0700498
Alex Gaynor266e9332014-10-28 14:44:04 -0700499// RescueOptsBuilder is an interface that allows extensions to override the
500// default structure of a Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700501type RescueOptsBuilder interface {
502 ToServerRescueMap() (map[string]interface{}, error)
503}
504
Alex Gaynor266e9332014-10-28 14:44:04 -0700505// RescueOpts represents the configuration options used to control a Rescue
506// option.
Alex Gaynor39584a02014-10-28 13:59:21 -0700507type RescueOpts struct {
Alex Gaynor266e9332014-10-28 14:44:04 -0700508 // AdminPass is the desired administrative password for the instance in
Alex Gaynorcfec7722014-11-13 13:33:49 -0800509 // RESCUE mode. If it's left blank, the server will generate a password.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600510 AdminPass string `json:"adminPass,omitempty"`
Alex Gaynor39584a02014-10-28 13:59:21 -0700511}
512
Jon Perrittcc77da62014-11-16 13:14:21 -0700513// ToServerRescueMap formats a RescueOpts as a map that can be used as a JSON
Alex Gaynor266e9332014-10-28 14:44:04 -0700514// request body for the Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700515func (opts RescueOpts) ToServerRescueMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600516 return gophercloud.BuildRequestBody(opts, "rescue")
Alex Gaynor39584a02014-10-28 13:59:21 -0700517}
518
Alex Gaynor266e9332014-10-28 14:44:04 -0700519// Rescue instructs the provider to place the server into RESCUE mode.
Jon Perritt3860b512016-03-29 12:01:48 -0500520func Rescue(client *gophercloud.ServiceClient, id string, opts RescueOptsBuilder) (r RescueResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600521 b, err := opts.ToServerRescueMap()
522 if err != nil {
523 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500524 return
Jon Perrittf094fef2016-03-07 01:41:59 -0600525 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600526 _, r.Err = client.Post(actionURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100527 OkCodes: []int{200},
Alex Gaynor39584a02014-10-28 13:59:21 -0700528 })
Alex Gaynor39584a02014-10-28 13:59:21 -0700529}
Jon Perrittcc77da62014-11-16 13:14:21 -0700530
Jon Perritt789f8322014-11-21 08:20:04 -0700531// ResetMetadataOptsBuilder allows extensions to add additional parameters to the
532// Reset request.
533type ResetMetadataOptsBuilder interface {
534 ToMetadataResetMap() (map[string]interface{}, error)
Jon Perrittcc77da62014-11-16 13:14:21 -0700535}
536
Jon Perritt78c57ce2014-11-20 11:07:18 -0700537// MetadataOpts is a map that contains key-value pairs.
538type MetadataOpts map[string]string
Jon Perrittcc77da62014-11-16 13:14:21 -0700539
Jon Perritt789f8322014-11-21 08:20:04 -0700540// ToMetadataResetMap assembles a body for a Reset request based on the contents of a MetadataOpts.
541func (opts MetadataOpts) ToMetadataResetMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700542 return map[string]interface{}{"metadata": opts}, nil
543}
544
Jon Perritt78c57ce2014-11-20 11:07:18 -0700545// ToMetadataUpdateMap assembles a body for an Update request based on the contents of a MetadataOpts.
546func (opts MetadataOpts) ToMetadataUpdateMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700547 return map[string]interface{}{"metadata": opts}, nil
548}
549
Jon Perritt789f8322014-11-21 08:20:04 -0700550// ResetMetadata will create multiple new key-value pairs for the given server ID.
Jon Perrittcc77da62014-11-16 13:14:21 -0700551// Note: Using this operation will erase any already-existing metadata and create
552// the new metadata provided. To keep any already-existing metadata, use the
553// UpdateMetadatas or UpdateMetadata function.
Jon Perritt3860b512016-03-29 12:01:48 -0500554func ResetMetadata(client *gophercloud.ServiceClient, id string, opts ResetMetadataOptsBuilder) (r ResetMetadataResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600555 b, err := opts.ToMetadataResetMap()
Jon Perrittcc77da62014-11-16 13:14:21 -0700556 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600557 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500558 return
Jon Perrittcc77da62014-11-16 13:14:21 -0700559 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600560 _, r.Err = client.Put(metadataURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100561 OkCodes: []int{200},
Jon Perrittcc77da62014-11-16 13:14:21 -0700562 })
Jon Perrittcc77da62014-11-16 13:14:21 -0700563}
564
Jon Perritt78c57ce2014-11-20 11:07:18 -0700565// Metadata requests all the metadata for the given server ID.
Jon Perritt3860b512016-03-29 12:01:48 -0500566func Metadata(client *gophercloud.ServiceClient, id string) (r GetMetadataResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600567 _, r.Err = client.Get(metadataURL(client, id), &r.Body, nil)
Jon Perrittcc77da62014-11-16 13:14:21 -0700568}
569
Jon Perritt78c57ce2014-11-20 11:07:18 -0700570// UpdateMetadataOptsBuilder allows extensions to add additional parameters to the
571// Create request.
572type UpdateMetadataOptsBuilder interface {
573 ToMetadataUpdateMap() (map[string]interface{}, error)
574}
575
576// UpdateMetadata updates (or creates) all the metadata specified by opts for the given server ID.
577// This operation does not affect already-existing metadata that is not specified
578// by opts.
Jon Perritt3860b512016-03-29 12:01:48 -0500579func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) (r UpdateMetadataResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600580 b, err := opts.ToMetadataUpdateMap()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700581 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600582 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500583 return
Jon Perritt78c57ce2014-11-20 11:07:18 -0700584 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600585 _, r.Err = client.Post(metadataURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100586 OkCodes: []int{200},
Jon Perritt78c57ce2014-11-20 11:07:18 -0700587 })
Jon Perritt78c57ce2014-11-20 11:07:18 -0700588}
589
590// MetadatumOptsBuilder allows extensions to add additional parameters to the
591// Create request.
592type MetadatumOptsBuilder interface {
593 ToMetadatumCreateMap() (map[string]interface{}, string, error)
594}
595
596// MetadatumOpts is a map of length one that contains a key-value pair.
597type MetadatumOpts map[string]string
598
599// ToMetadatumCreateMap assembles a body for a Create request based on the contents of a MetadataumOpts.
600func (opts MetadatumOpts) ToMetadatumCreateMap() (map[string]interface{}, string, error) {
601 if len(opts) != 1 {
Jon Perritt13808262016-03-09 00:50:12 -0600602 err := gophercloud.ErrInvalidInput{}
603 err.Argument = "servers.MetadatumOpts"
604 err.Info = "Must have 1 and only 1 key-value pair"
605 return nil, "", err
Jon Perritt78c57ce2014-11-20 11:07:18 -0700606 }
607 metadatum := map[string]interface{}{"meta": opts}
608 var key string
609 for k := range metadatum["meta"].(MetadatumOpts) {
610 key = k
611 }
612 return metadatum, key, nil
613}
614
615// CreateMetadatum will create or update the key-value pair with the given key for the given server ID.
Jon Perritt3860b512016-03-29 12:01:48 -0500616func CreateMetadatum(client *gophercloud.ServiceClient, id string, opts MetadatumOptsBuilder) (r CreateMetadatumResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600617 b, key, err := opts.ToMetadatumCreateMap()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700618 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600619 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500620 return
Jon Perritt78c57ce2014-11-20 11:07:18 -0700621 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600622 _, r.Err = client.Put(metadatumURL(client, id, key), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100623 OkCodes: []int{200},
Jon Perritt78c57ce2014-11-20 11:07:18 -0700624 })
Jon Perritt78c57ce2014-11-20 11:07:18 -0700625}
626
627// Metadatum requests the key-value pair with the given key for the given server ID.
Jon Perritt3860b512016-03-29 12:01:48 -0500628func Metadatum(client *gophercloud.ServiceClient, id, key string) (r GetMetadatumResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600629 _, r.Err = client.Get(metadatumURL(client, id, key), &r.Body, nil)
Jon Perritt78c57ce2014-11-20 11:07:18 -0700630}
631
632// DeleteMetadatum will delete the key-value pair with the given key for the given server ID.
Jon Perritt3860b512016-03-29 12:01:48 -0500633func DeleteMetadatum(client *gophercloud.ServiceClient, id, key string) (r DeleteMetadatumResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600634 _, r.Err = client.Delete(metadatumURL(client, id, key), nil)
Jon Perrittcc77da62014-11-16 13:14:21 -0700635}
Jon Perritt5cb49482015-02-19 12:19:58 -0700636
637// ListAddresses makes a request against the API to list the servers IP addresses.
638func ListAddresses(client *gophercloud.ServiceClient, id string) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600639 return pagination.NewPager(client, listAddressesURL(client, id), func(r pagination.PageResult) pagination.Page {
Jon Perritt5cb49482015-02-19 12:19:58 -0700640 return AddressPage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600641 })
Jon Perritt5cb49482015-02-19 12:19:58 -0700642}
Jon Perritt04d073c2015-02-19 21:46:23 -0700643
644// ListAddressesByNetwork makes a request against the API to list the servers IP addresses
645// for the given network.
646func ListAddressesByNetwork(client *gophercloud.ServiceClient, id, network string) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600647 return pagination.NewPager(client, listAddressesByNetworkURL(client, id, network), func(r pagination.PageResult) pagination.Page {
Jon Perritt04d073c2015-02-19 21:46:23 -0700648 return NetworkAddressPage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600649 })
Jon Perritt04d073c2015-02-19 21:46:23 -0700650}
einarf2fc665e2015-04-16 20:16:21 +0000651
Jon Perrittdb0ae142016-03-13 00:33:41 -0600652// CreateImageOptsBuilder is the interface types must satisfy in order to be
653// used as CreateImage options
einarf4e5fdaf2015-04-16 23:14:59 +0000654type CreateImageOptsBuilder interface {
655 ToServerCreateImageMap() (map[string]interface{}, error)
einarf2fc665e2015-04-16 20:16:21 +0000656}
657
Jon Perrittdb0ae142016-03-13 00:33:41 -0600658// CreateImageOpts satisfies the CreateImageOptsBuilder
659type CreateImageOpts struct {
660 // Name of the image/snapshot
661 Name string `json:"name" required:"true"`
662 // Metadata contains key-value pairs (up to 255 bytes each) to attach to the created image.
663 Metadata map[string]string `json:"metadata,omitempty"`
664}
665
einarf4e5fdaf2015-04-16 23:14:59 +0000666// ToServerCreateImageMap formats a CreateImageOpts structure into a request body.
667func (opts CreateImageOpts) ToServerCreateImageMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600668 return gophercloud.BuildRequestBody(opts, "createImage")
einarf2fc665e2015-04-16 20:16:21 +0000669}
670
einarf4e5fdaf2015-04-16 23:14:59 +0000671// CreateImage makes a request against the nova API to schedule an image to be created of the server
Jon Perritt3860b512016-03-29 12:01:48 -0500672func CreateImage(client *gophercloud.ServiceClient, id string, opts CreateImageOptsBuilder) (r CreateImageResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600673 b, err := opts.ToServerCreateImageMap()
einarf2fc665e2015-04-16 20:16:21 +0000674 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600675 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500676 return
einarf2fc665e2015-04-16 20:16:21 +0000677 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600678 resp, err := client.Post(actionURL(client, id), b, nil, &gophercloud.RequestOpts{
einarf2fc665e2015-04-16 20:16:21 +0000679 OkCodes: []int{202},
680 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600681 r.Err = err
682 r.Header = resp.Header
einarf2fc665e2015-04-16 20:16:21 +0000683}
Jon Perritt6b0a8832015-06-04 14:32:30 -0600684
685// IDFromName is a convienience function that returns a server's ID given its name.
686func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600687 count := 0
688 id := ""
Jon Perrittf094fef2016-03-07 01:41:59 -0600689 allPages, err := List(client, nil).AllPages()
690 if err != nil {
691 return "", err
692 }
Jon Perritt6b0a8832015-06-04 14:32:30 -0600693
Jon Perrittf094fef2016-03-07 01:41:59 -0600694 all, err := ExtractServers(allPages)
695 if err != nil {
696 return "", err
697 }
698
699 for _, f := range all {
700 if f.Name == name {
701 count++
702 id = f.ID
703 }
704 }
705
706 switch count {
Jon Perritt6b0a8832015-06-04 14:32:30 -0600707 case 0:
Jon Perrittdb0ae142016-03-13 00:33:41 -0600708 return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "server"}
Jon Perritt6b0a8832015-06-04 14:32:30 -0600709 case 1:
Jon Perrittf094fef2016-03-07 01:41:59 -0600710 return id, nil
Jon Perritt6b0a8832015-06-04 14:32:30 -0600711 default:
Jon Perrittdb0ae142016-03-13 00:33:41 -0600712 return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "server"}
Jon Perritt6b0a8832015-06-04 14:32:30 -0600713 }
714}