blob: 4257a4ab2d14ab0156486601fd3438059c95b55c [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.
Ash Wilson2206a112014-10-02 10:57:38 -0400262func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600263 var r CreateResult
Jon Perritt4149d7c2014-10-23 21:23:46 -0500264 reqBody, err := opts.ToServerCreateMap()
265 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600266 r.Err = err
267 return r
Jon Perritt4149d7c2014-10-23 21:23:46 -0500268 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600269 _, r.Err = client.Post(listURL(client), reqBody, &r.Body, nil)
270 return r
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800271}
272
273// Delete requests that a server previously provisioned be removed from your account.
Jamie Hannaford34732fe2014-10-27 11:29:36 +0100274func Delete(client *gophercloud.ServiceClient, id string) DeleteResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600275 var r DeleteResult
276 _, r.Err = client.Delete(deleteURL(client, id), nil)
277 return r
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800278}
279
Jon Perritt01618ee2016-03-09 03:04:06 -0600280// ForceDelete forces the deletion of a server
Ian Duffy370c4302016-01-21 10:44:56 +0000281func ForceDelete(client *gophercloud.ServiceClient, id string) ActionResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600282 var r ActionResult
Jon Perritt01618ee2016-03-09 03:04:06 -0600283 _, r.Err = client.Post(actionURL(client, id), map[string]interface{}{"forceDelete": ""}, nil, nil)
Jon Perrittf094fef2016-03-07 01:41:59 -0600284 return r
Ian Duffy370c4302016-01-21 10:44:56 +0000285}
286
Ash Wilson7ddf0362014-09-17 10:59:09 -0400287// Get requests details on a single server, by ID.
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400288func Get(client *gophercloud.ServiceClient, id string) GetResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600289 var r GetResult
290 _, r.Err = client.Get(getURL(client, id), &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100291 OkCodes: []int{200, 203},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800292 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600293 return r
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800294}
295
Alex Gaynora6d5f9f2014-10-27 10:52:32 -0700296// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
Jon Perritt82048212014-10-13 22:33:13 -0500297type UpdateOptsBuilder interface {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600298 ToServerUpdateMap() (map[string]interface{}, error)
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400299}
300
301// UpdateOpts specifies the base attributes that may be updated on an existing server.
302type UpdateOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600303 // Name changes the displayed name of the server.
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400304 // The server host name will *not* change.
305 // Server names are not constrained to be unique, even within the same tenant.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600306 Name string `json:"name,omitempty"`
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400307
Jon Perrittdb0ae142016-03-13 00:33:41 -0600308 // AccessIPv4 provides a new IPv4 address for the instance.
309 AccessIPv4 string `json:"accessIPv4,omitempty"`
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400310
Jon Perrittdb0ae142016-03-13 00:33:41 -0600311 // AccessIPv6 provides a new IPv6 address for the instance.
312 AccessIPv6 string `json:"accessIPv6,omitempty"`
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400313}
314
Ash Wilsone45c9732014-09-29 10:54:12 -0400315// ToServerUpdateMap formats an UpdateOpts structure into a request body.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600316func (opts UpdateOpts) ToServerUpdateMap() (map[string]interface{}, error) {
317 return gophercloud.BuildRequestBody(opts, "server")
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400318}
319
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800320// Update requests that various attributes of the indicated server be changed.
Jon Perritt82048212014-10-13 22:33:13 -0500321func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600322 var r UpdateResult
Jon Perrittdb0ae142016-03-13 00:33:41 -0600323 b, err := opts.ToServerUpdateMap()
324 if err != nil {
325 r.Err = err
326 return r
327 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600328 _, r.Err = client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100329 OkCodes: []int{200},
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800330 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600331 return r
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800332}
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700333
Ash Wilson01626a32014-09-17 10:38:07 -0400334// ChangeAdminPassword alters the administrator or root password for a specified server.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200335func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) ActionResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600336 var r ActionResult
Jon Perrittdb0ae142016-03-13 00:33:41 -0600337 b := map[string]interface{}{
338 "changePassword": map[string]string{
339 "adminPass": newPassword,
340 },
341 }
342 _, r.Err = client.Post(actionURL(client, id), b, nil, nil)
Jon Perrittf094fef2016-03-07 01:41:59 -0600343 return r
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700344}
345
Ash Wilson01626a32014-09-17 10:38:07 -0400346// RebootMethod describes the mechanisms by which a server reboot can be requested.
347type RebootMethod string
348
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700349// These constants determine how a server should be rebooted.
350// See the Reboot() function for further details.
351const (
Ash Wilson01626a32014-09-17 10:38:07 -0400352 SoftReboot RebootMethod = "SOFT"
353 HardReboot RebootMethod = "HARD"
354 OSReboot = SoftReboot
355 PowerCycle = HardReboot
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700356)
357
Jon Perrittdb0ae142016-03-13 00:33:41 -0600358// RebootOptsBuilder is an interface that options must satisfy in order to be
359// used when rebooting a server instance
Jon Perrittf094fef2016-03-07 01:41:59 -0600360type RebootOptsBuilder interface {
361 ToServerRebootMap() (map[string]interface{}, error)
362}
363
Jon Perrittdb0ae142016-03-13 00:33:41 -0600364// RebootOpts satisfies the RebootOptsBuilder interface
Jon Perrittf094fef2016-03-07 01:41:59 -0600365type RebootOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600366 Type RebootMethod `json:"type" required:"true"`
Jon Perrittf094fef2016-03-07 01:41:59 -0600367}
368
Jon Perrittdb0ae142016-03-13 00:33:41 -0600369// ToServerRebootMap allows RebootOpts to satisfiy the RebootOptsBuilder
370// interface
Jon Perrittf094fef2016-03-07 01:41:59 -0600371func (opts *RebootOpts) ToServerRebootMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600372 return gophercloud.BuildRequestBody(opts, "reboot")
Jon Perrittf094fef2016-03-07 01:41:59 -0600373}
374
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700375// Reboot requests that a given server reboot.
376// Two methods exist for rebooting a server:
377//
Jon Perrittdb0ae142016-03-13 00:33:41 -0600378// 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 -0400379// terminating it at the hypervisor level.
380// It's done. Caput. Full stop.
Jon Perrittf094fef2016-03-07 01:41:59 -0600381// Then, after a brief while, power is rtored or the VM instance rtarted.
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700382//
Jon Perrittf094fef2016-03-07 01:41:59 -0600383// SoftReboot (aka OSReboot) simply tells the OS to rtart under its own procedur.
384// E.g., in Linux, asking it to enter runlevel 6, or executing "sudo shutdown -r now", or by asking Windows to rtart the machine.
385func Reboot(client *gophercloud.ServiceClient, id string, opts RebootOptsBuilder) ActionResult {
386 var r ActionResult
Jon Perrittdb0ae142016-03-13 00:33:41 -0600387 b, err := opts.ToServerRebootMap()
Jon Perrittf094fef2016-03-07 01:41:59 -0600388 if err != nil {
389 r.Err = err
390 return r
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700391 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600392 _, r.Err = client.Post(actionURL(client, id), b, nil, nil)
Jon Perrittf094fef2016-03-07 01:41:59 -0600393 return r
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700394}
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700395
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200396// RebuildOptsBuilder is an interface that allows extensions to override the
397// default behaviour of rebuild options
398type RebuildOptsBuilder interface {
399 ToServerRebuildMap() (map[string]interface{}, error)
400}
401
402// RebuildOpts represents the configuration options used in a server rebuild
403// operation
404type RebuildOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600405 // The server's admin password
406 AdminPass string `json:"adminPass" required:"true"`
407 // The ID of the image you want your server to be provisioned on
408 ImageID string `json:"imageRef"`
409 ImageName string `json:"-"`
410 //ImageName string `json:"-"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200411 // Name to set the server to
Jon Perrittdb0ae142016-03-13 00:33:41 -0600412 Name string `json:"name,omitempty"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200413 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600414 AccessIPv4 string `json:"accessIPv4,omitempty"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200415 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600416 AccessIPv6 string `json:"accessIPv6,omitempty"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200417 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600418 Metadata map[string]string `json:"metadata,omitempty"`
Kevin Pike92e10b52015-04-10 15:16:57 -0700419 // Personality [optional] includes files to inject into the server at launch.
420 // Rebuild will base64-encode file contents for you.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600421 Personality Personality `json:"personality,omitempty"`
422 ServiceClient *gophercloud.ServiceClient `json:"-"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200423}
424
425// ToServerRebuildMap formats a RebuildOpts struct into a map for use in JSON
426func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600427 b, err := gophercloud.BuildRequestBody(opts, "")
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200428 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600429 return nil, err
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200430 }
431
Jon Perrittdb0ae142016-03-13 00:33:41 -0600432 // If ImageRef isn't provided, use ImageName to ascertain the image ID.
433 if opts.ImageID == "" {
434 if opts.ImageName == "" {
435 err := ErrNeitherImageIDNorImageNameProvided{}
436 err.Argument = "ImageRef/ImageName"
437 return nil, err
438 }
439 if opts.ServiceClient == nil {
440 err := ErrNoClientProvidedForIDByName{}
441 err.Argument = "ServiceClient"
442 return nil, err
443 }
444 imageID, err := images.IDFromName(opts.ServiceClient, opts.ImageName)
445 if err != nil {
446 return nil, err
447 }
448 b["imageRef"] = imageID
Jon Perritt12395212016-02-24 10:41:17 -0600449 }
450
Jon Perrittdb0ae142016-03-13 00:33:41 -0600451 return map[string]interface{}{"rebuild": b}, nil
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200452}
453
454// Rebuild will reprovision the server according to the configuration options
455// provided in the RebuildOpts struct.
456func Rebuild(client *gophercloud.ServiceClient, id string, opts RebuildOptsBuilder) RebuildResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600457 var r RebuildResult
Jon Perrittf094fef2016-03-07 01:41:59 -0600458 b, err := opts.ToServerRebuildMap()
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200459 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600460 r.Err = err
461 return r
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700462 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600463 _, r.Err = client.Post(actionURL(client, id), b, &r.Body, nil)
464 return r
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700465}
466
Ash Wilson5f7cf182014-10-23 08:35:24 -0400467// ResizeOptsBuilder is an interface that allows extensions to override the default structure of
468// a Resize request.
469type ResizeOptsBuilder interface {
470 ToServerResizeMap() (map[string]interface{}, error)
471}
472
473// ResizeOpts represents the configuration options used to control a Resize operation.
474type ResizeOpts struct {
475 // FlavorRef is the ID of the flavor you wish your server to become.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600476 FlavorRef string `json:"flavorRef" required:"true"`
Ash Wilson5f7cf182014-10-23 08:35:24 -0400477}
478
Alex Gaynor266e9332014-10-28 14:44:04 -0700479// 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 -0400480// Resize request.
481func (opts ResizeOpts) ToServerResizeMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600482 return gophercloud.BuildRequestBody(opts, "resize")
Ash Wilson5f7cf182014-10-23 08:35:24 -0400483}
484
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700485// Resize instructs the provider to change the flavor of the server.
Ash Wilson01626a32014-09-17 10:38:07 -0400486// Note that this implies rebuilding it.
Jon Perrittf094fef2016-03-07 01:41:59 -0600487// Unfortunately, one cannot pass rebuild parameters to the rize function.
488// When the rize completes, the server will be in RESIZE_VERIFY state.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700489// While in this state, you can explore the use of the new server's configuration.
Jon Perrittf094fef2016-03-07 01:41:59 -0600490// If you like it, call ConfirmResize() to commit the rize permanently.
491// Otherwise, call RevertResize() to rtore the old configuration.
Ash Wilson9e87a922014-10-23 14:29:22 -0400492func Resize(client *gophercloud.ServiceClient, id string, opts ResizeOptsBuilder) ActionResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600493 var r ActionResult
Jon Perrittf094fef2016-03-07 01:41:59 -0600494 b, err := opts.ToServerResizeMap()
495 if err != nil {
496 r.Err = err
497 return r
498 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600499 _, r.Err = client.Post(actionURL(client, id), b, nil, nil)
500 return r
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700501}
502
Jon Perrittf094fef2016-03-07 01:41:59 -0600503// ConfirmResize confirms a previous rize operation on a server.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700504// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200505func ConfirmResize(client *gophercloud.ServiceClient, id string) ActionResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600506 var r ActionResult
Jon Perrittdb0ae142016-03-13 00:33:41 -0600507 _, r.Err = client.Post(actionURL(client, id), map[string]interface{}{"confirmResize": nil}, nil, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100508 OkCodes: []int{201, 202, 204},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700509 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600510 return r
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700511}
512
Jon Perrittf094fef2016-03-07 01:41:59 -0600513// RevertResize cancels a previous rize operation on a server.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700514// See Resize() for more details.
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200515func RevertResize(client *gophercloud.ServiceClient, id string) ActionResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600516 var r ActionResult
Jon Perrittdb0ae142016-03-13 00:33:41 -0600517 _, r.Err = client.Post(actionURL(client, id), map[string]interface{}{"revertResize": nil}, nil, nil)
Jon Perrittf094fef2016-03-07 01:41:59 -0600518 return r
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700519}
Alex Gaynor39584a02014-10-28 13:59:21 -0700520
Alex Gaynor266e9332014-10-28 14:44:04 -0700521// RescueOptsBuilder is an interface that allows extensions to override the
522// default structure of a Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700523type RescueOptsBuilder interface {
524 ToServerRescueMap() (map[string]interface{}, error)
525}
526
Alex Gaynor266e9332014-10-28 14:44:04 -0700527// RescueOpts represents the configuration options used to control a Rescue
528// option.
Alex Gaynor39584a02014-10-28 13:59:21 -0700529type RescueOpts struct {
Alex Gaynor266e9332014-10-28 14:44:04 -0700530 // AdminPass is the desired administrative password for the instance in
Alex Gaynorcfec7722014-11-13 13:33:49 -0800531 // RESCUE mode. If it's left blank, the server will generate a password.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600532 AdminPass string `json:"adminPass,omitempty"`
Alex Gaynor39584a02014-10-28 13:59:21 -0700533}
534
Jon Perrittcc77da62014-11-16 13:14:21 -0700535// ToServerRescueMap formats a RescueOpts as a map that can be used as a JSON
Alex Gaynor266e9332014-10-28 14:44:04 -0700536// request body for the Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700537func (opts RescueOpts) ToServerRescueMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600538 return gophercloud.BuildRequestBody(opts, "rescue")
Alex Gaynor39584a02014-10-28 13:59:21 -0700539}
540
Alex Gaynor266e9332014-10-28 14:44:04 -0700541// Rescue instructs the provider to place the server into RESCUE mode.
Alex Gaynorfbe61bb2014-11-12 13:35:03 -0800542func Rescue(client *gophercloud.ServiceClient, id string, opts RescueOptsBuilder) RescueResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600543 var r RescueResult
Jon Perrittf094fef2016-03-07 01:41:59 -0600544 b, err := opts.ToServerRescueMap()
545 if err != nil {
546 r.Err = err
547 return r
548 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600549 _, r.Err = client.Post(actionURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100550 OkCodes: []int{200},
Alex Gaynor39584a02014-10-28 13:59:21 -0700551 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600552 return r
Alex Gaynor39584a02014-10-28 13:59:21 -0700553}
Jon Perrittcc77da62014-11-16 13:14:21 -0700554
Jon Perritt789f8322014-11-21 08:20:04 -0700555// ResetMetadataOptsBuilder allows extensions to add additional parameters to the
556// Reset request.
557type ResetMetadataOptsBuilder interface {
558 ToMetadataResetMap() (map[string]interface{}, error)
Jon Perrittcc77da62014-11-16 13:14:21 -0700559}
560
Jon Perritt78c57ce2014-11-20 11:07:18 -0700561// MetadataOpts is a map that contains key-value pairs.
562type MetadataOpts map[string]string
Jon Perrittcc77da62014-11-16 13:14:21 -0700563
Jon Perritt789f8322014-11-21 08:20:04 -0700564// ToMetadataResetMap assembles a body for a Reset request based on the contents of a MetadataOpts.
565func (opts MetadataOpts) ToMetadataResetMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700566 return map[string]interface{}{"metadata": opts}, nil
567}
568
Jon Perritt78c57ce2014-11-20 11:07:18 -0700569// ToMetadataUpdateMap assembles a body for an Update request based on the contents of a MetadataOpts.
570func (opts MetadataOpts) ToMetadataUpdateMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700571 return map[string]interface{}{"metadata": opts}, nil
572}
573
Jon Perritt789f8322014-11-21 08:20:04 -0700574// ResetMetadata will create multiple new key-value pairs for the given server ID.
Jon Perrittcc77da62014-11-16 13:14:21 -0700575// Note: Using this operation will erase any already-existing metadata and create
576// the new metadata provided. To keep any already-existing metadata, use the
577// UpdateMetadatas or UpdateMetadata function.
Jon Perritt789f8322014-11-21 08:20:04 -0700578func ResetMetadata(client *gophercloud.ServiceClient, id string, opts ResetMetadataOptsBuilder) ResetMetadataResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600579 var r ResetMetadataResult
Jon Perrittdb0ae142016-03-13 00:33:41 -0600580 b, err := opts.ToMetadataResetMap()
Jon Perrittcc77da62014-11-16 13:14:21 -0700581 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600582 r.Err = err
583 return r
Jon Perrittcc77da62014-11-16 13:14:21 -0700584 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600585 _, r.Err = client.Put(metadataURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100586 OkCodes: []int{200},
Jon Perrittcc77da62014-11-16 13:14:21 -0700587 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600588 return r
Jon Perrittcc77da62014-11-16 13:14:21 -0700589}
590
Jon Perritt78c57ce2014-11-20 11:07:18 -0700591// Metadata requests all the metadata for the given server ID.
592func Metadata(client *gophercloud.ServiceClient, id string) GetMetadataResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600593 var r GetMetadataResult
594 _, r.Err = client.Get(metadataURL(client, id), &r.Body, nil)
595 return r
Jon Perrittcc77da62014-11-16 13:14:21 -0700596}
597
Jon Perritt78c57ce2014-11-20 11:07:18 -0700598// UpdateMetadataOptsBuilder allows extensions to add additional parameters to the
599// Create request.
600type UpdateMetadataOptsBuilder interface {
601 ToMetadataUpdateMap() (map[string]interface{}, error)
602}
603
604// UpdateMetadata updates (or creates) all the metadata specified by opts for the given server ID.
605// This operation does not affect already-existing metadata that is not specified
606// by opts.
607func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) UpdateMetadataResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600608 var r UpdateMetadataResult
Jon Perrittdb0ae142016-03-13 00:33:41 -0600609 b, err := opts.ToMetadataUpdateMap()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700610 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600611 r.Err = err
612 return r
Jon Perritt78c57ce2014-11-20 11:07:18 -0700613 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600614 _, r.Err = client.Post(metadataURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100615 OkCodes: []int{200},
Jon Perritt78c57ce2014-11-20 11:07:18 -0700616 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600617 return r
Jon Perritt78c57ce2014-11-20 11:07:18 -0700618}
619
620// MetadatumOptsBuilder allows extensions to add additional parameters to the
621// Create request.
622type MetadatumOptsBuilder interface {
623 ToMetadatumCreateMap() (map[string]interface{}, string, error)
624}
625
626// MetadatumOpts is a map of length one that contains a key-value pair.
627type MetadatumOpts map[string]string
628
629// ToMetadatumCreateMap assembles a body for a Create request based on the contents of a MetadataumOpts.
630func (opts MetadatumOpts) ToMetadatumCreateMap() (map[string]interface{}, string, error) {
631 if len(opts) != 1 {
Jon Perritt13808262016-03-09 00:50:12 -0600632 err := gophercloud.ErrInvalidInput{}
633 err.Argument = "servers.MetadatumOpts"
634 err.Info = "Must have 1 and only 1 key-value pair"
635 return nil, "", err
Jon Perritt78c57ce2014-11-20 11:07:18 -0700636 }
637 metadatum := map[string]interface{}{"meta": opts}
638 var key string
639 for k := range metadatum["meta"].(MetadatumOpts) {
640 key = k
641 }
642 return metadatum, key, nil
643}
644
645// CreateMetadatum will create or update the key-value pair with the given key for the given server ID.
646func CreateMetadatum(client *gophercloud.ServiceClient, id string, opts MetadatumOptsBuilder) CreateMetadatumResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600647 var r CreateMetadatumResult
Jon Perrittdb0ae142016-03-13 00:33:41 -0600648 b, key, err := opts.ToMetadatumCreateMap()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700649 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600650 r.Err = err
651 return r
Jon Perritt78c57ce2014-11-20 11:07:18 -0700652 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600653 _, r.Err = client.Put(metadatumURL(client, id, key), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100654 OkCodes: []int{200},
Jon Perritt78c57ce2014-11-20 11:07:18 -0700655 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600656 return r
Jon Perritt78c57ce2014-11-20 11:07:18 -0700657}
658
659// Metadatum requests the key-value pair with the given key for the given server ID.
660func Metadatum(client *gophercloud.ServiceClient, id, key string) GetMetadatumResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600661 var r GetMetadatumResult
Jon Perrittdb0ae142016-03-13 00:33:41 -0600662 _, r.Err = client.Get(metadatumURL(client, id, key), &r.Body, nil)
Jon Perrittf094fef2016-03-07 01:41:59 -0600663 return r
Jon Perritt78c57ce2014-11-20 11:07:18 -0700664}
665
666// DeleteMetadatum will delete the key-value pair with the given key for the given server ID.
667func DeleteMetadatum(client *gophercloud.ServiceClient, id, key string) DeleteMetadatumResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600668 var r DeleteMetadatumResult
669 _, r.Err = client.Delete(metadatumURL(client, id, key), nil)
670 return r
Jon Perrittcc77da62014-11-16 13:14:21 -0700671}
Jon Perritt5cb49482015-02-19 12:19:58 -0700672
673// ListAddresses makes a request against the API to list the servers IP addresses.
674func ListAddresses(client *gophercloud.ServiceClient, id string) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600675 return pagination.NewPager(client, listAddressesURL(client, id), func(r pagination.PageResult) pagination.Page {
Jon Perritt5cb49482015-02-19 12:19:58 -0700676 return AddressPage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600677 })
Jon Perritt5cb49482015-02-19 12:19:58 -0700678}
Jon Perritt04d073c2015-02-19 21:46:23 -0700679
680// ListAddressesByNetwork makes a request against the API to list the servers IP addresses
681// for the given network.
682func ListAddressesByNetwork(client *gophercloud.ServiceClient, id, network string) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600683 return pagination.NewPager(client, listAddressesByNetworkURL(client, id, network), func(r pagination.PageResult) pagination.Page {
Jon Perritt04d073c2015-02-19 21:46:23 -0700684 return NetworkAddressPage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600685 })
Jon Perritt04d073c2015-02-19 21:46:23 -0700686}
einarf2fc665e2015-04-16 20:16:21 +0000687
Jon Perrittdb0ae142016-03-13 00:33:41 -0600688// CreateImageOptsBuilder is the interface types must satisfy in order to be
689// used as CreateImage options
einarf4e5fdaf2015-04-16 23:14:59 +0000690type CreateImageOptsBuilder interface {
691 ToServerCreateImageMap() (map[string]interface{}, error)
einarf2fc665e2015-04-16 20:16:21 +0000692}
693
Jon Perrittdb0ae142016-03-13 00:33:41 -0600694// CreateImageOpts satisfies the CreateImageOptsBuilder
695type CreateImageOpts struct {
696 // Name of the image/snapshot
697 Name string `json:"name" required:"true"`
698 // Metadata contains key-value pairs (up to 255 bytes each) to attach to the created image.
699 Metadata map[string]string `json:"metadata,omitempty"`
700}
701
einarf4e5fdaf2015-04-16 23:14:59 +0000702// ToServerCreateImageMap formats a CreateImageOpts structure into a request body.
703func (opts CreateImageOpts) ToServerCreateImageMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600704 return gophercloud.BuildRequestBody(opts, "createImage")
einarf2fc665e2015-04-16 20:16:21 +0000705}
706
einarf4e5fdaf2015-04-16 23:14:59 +0000707// CreateImage makes a request against the nova API to schedule an image to be created of the server
Jon Perrittdb0ae142016-03-13 00:33:41 -0600708func CreateImage(client *gophercloud.ServiceClient, id string, opts CreateImageOptsBuilder) CreateImageResult {
Jon Perrittf094fef2016-03-07 01:41:59 -0600709 var r CreateImageResult
710 b, err := opts.ToServerCreateImageMap()
einarf2fc665e2015-04-16 20:16:21 +0000711 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600712 r.Err = err
713 return r
einarf2fc665e2015-04-16 20:16:21 +0000714 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600715 resp, err := client.Post(actionURL(client, id), b, nil, &gophercloud.RequestOpts{
einarf2fc665e2015-04-16 20:16:21 +0000716 OkCodes: []int{202},
717 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600718 r.Err = err
719 r.Header = resp.Header
720 return r
einarf2fc665e2015-04-16 20:16:21 +0000721}
Jon Perritt6b0a8832015-06-04 14:32:30 -0600722
723// IDFromName is a convienience function that returns a server's ID given its name.
724func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600725 count := 0
726 id := ""
Jon Perrittf094fef2016-03-07 01:41:59 -0600727 allPages, err := List(client, nil).AllPages()
728 if err != nil {
729 return "", err
730 }
Jon Perritt6b0a8832015-06-04 14:32:30 -0600731
Jon Perrittf094fef2016-03-07 01:41:59 -0600732 all, err := ExtractServers(allPages)
733 if err != nil {
734 return "", err
735 }
736
737 for _, f := range all {
738 if f.Name == name {
739 count++
740 id = f.ID
741 }
742 }
743
744 switch count {
Jon Perritt6b0a8832015-06-04 14:32:30 -0600745 case 0:
Jon Perrittdb0ae142016-03-13 00:33:41 -0600746 return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "server"}
Jon Perritt6b0a8832015-06-04 14:32:30 -0600747 case 1:
Jon Perrittf094fef2016-03-07 01:41:59 -0600748 return id, nil
Jon Perritt6b0a8832015-06-04 14:32:30 -0600749 default:
Jon Perrittdb0ae142016-03-13 00:33:41 -0600750 return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "server"}
Jon Perritt6b0a8832015-06-04 14:32:30 -0600751 }
752}