blob: c79a6e6f6b85edebb22ee5a0d6d27676fc870a16 [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.
Gavin Williamscd65a062016-11-08 19:05:47 +0000148 // Create will base64-encode it for you, if it isn't already.
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.
Joe Topjianf464c962016-09-12 08:02:43 -0600159 Metadata map[string]string `json:"metadata,omitempty"`
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.
Bruce Martins6b3419f2017-01-18 20:06:55 -0500163 Personality Personality `json:"personality,omitempty"`
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) {
jrperritt0d7ed5d2016-08-16 11:23:26 -0500185 sc := opts.ServiceClient
186 opts.ServiceClient = nil
Jon Perrittdb0ae142016-03-13 00:33:41 -0600187 b, err := gophercloud.BuildRequestBody(opts, "")
Jon Perritt01618ee2016-03-09 03:04:06 -0600188 if err != nil {
189 return nil, err
190 }
Ash Wilson6a310e02014-09-29 08:24:02 -0400191
192 if opts.UserData != nil {
Gavin Williamscd65a062016-11-08 19:05:47 +0000193 var userData string
194 if _, err := base64.StdEncoding.DecodeString(string(opts.UserData)); err != nil {
195 userData = base64.StdEncoding.EncodeToString(opts.UserData)
196 } else {
197 userData = string(opts.UserData)
198 }
199 b["user_data"] = &userData
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
Joe Topjian50cdddf2016-09-16 10:56:09 -0600227 // If ImageRef isn't provided, check if ImageName was provided to ascertain
228 // the image ID.
jrperrittb1013232016-02-10 19:01:53 -0600229 if opts.ImageRef == "" {
Joe Topjian50cdddf2016-09-16 10:56:09 -0600230 if opts.ImageName != "" {
231 if sc == nil {
232 err := ErrNoClientProvidedForIDByName{}
233 err.Argument = "ServiceClient"
234 return nil, err
235 }
236 imageID, err := images.IDFromName(sc, opts.ImageName)
237 if err != nil {
238 return nil, err
239 }
240 b["imageRef"] = imageID
jrperrittb1013232016-02-10 19:01:53 -0600241 }
jrperrittb1013232016-02-10 19:01:53 -0600242 }
243
244 // If FlavorRef isn't provided, use FlavorName to ascertain the flavor ID.
245 if opts.FlavorRef == "" {
246 if opts.FlavorName == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600247 err := ErrNeitherFlavorIDNorFlavorNameProvided{}
Jon Perrittf094fef2016-03-07 01:41:59 -0600248 err.Argument = "FlavorRef/FlavorName"
249 return nil, err
jrperrittb1013232016-02-10 19:01:53 -0600250 }
jrperritt0d7ed5d2016-08-16 11:23:26 -0500251 if sc == nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600252 err := ErrNoClientProvidedForIDByName{}
253 err.Argument = "ServiceClient"
Jon Perrittf094fef2016-03-07 01:41:59 -0600254 return nil, err
Jon Perritt994370e2016-02-18 15:23:34 -0600255 }
jrperritt0d7ed5d2016-08-16 11:23:26 -0500256 flavorID, err := flavors.IDFromName(sc, opts.FlavorName)
jrperrittb1013232016-02-10 19:01:53 -0600257 if err != nil {
258 return nil, err
259 }
Jon Perritt01618ee2016-03-09 03:04:06 -0600260 b["flavorRef"] = flavorID
jrperrittb1013232016-02-10 19:01:53 -0600261 }
262
Jon Perritt01618ee2016-03-09 03:04:06 -0600263 return map[string]interface{}{"server": b}, nil
Ash Wilson6a310e02014-09-29 08:24:02 -0400264}
265
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800266// Create requests a server to be provisioned to the user in the current tenant.
Jon Perritt3860b512016-03-29 12:01:48 -0500267func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
Jon Perritt4149d7c2014-10-23 21:23:46 -0500268 reqBody, err := opts.ToServerCreateMap()
269 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600270 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500271 return
Jon Perritt4149d7c2014-10-23 21:23:46 -0500272 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600273 _, r.Err = client.Post(listURL(client), reqBody, &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500274 return
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800275}
276
277// Delete requests that a server previously provisioned be removed from your account.
Jon Perritt3860b512016-03-29 12:01:48 -0500278func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600279 _, r.Err = client.Delete(deleteURL(client, id), nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500280 return
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800281}
282
Jon Perritt01618ee2016-03-09 03:04:06 -0600283// ForceDelete forces the deletion of a server
Jon Perritt3860b512016-03-29 12:01:48 -0500284func ForceDelete(client *gophercloud.ServiceClient, id string) (r ActionResult) {
Jon Perritt01618ee2016-03-09 03:04:06 -0600285 _, r.Err = client.Post(actionURL(client, id), map[string]interface{}{"forceDelete": ""}, nil, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500286 return
Ian Duffy370c4302016-01-21 10:44:56 +0000287}
288
Ash Wilson7ddf0362014-09-17 10:59:09 -0400289// Get requests details on a single server, by ID.
Jon Perritt3860b512016-03-29 12:01:48 -0500290func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600291 _, r.Err = client.Get(getURL(client, id), &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100292 OkCodes: []int{200, 203},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800293 })
jrperritt29ae6b32016-04-13 12:59:37 -0500294 return
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800295}
296
Alex Gaynora6d5f9f2014-10-27 10:52:32 -0700297// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
Jon Perritt82048212014-10-13 22:33:13 -0500298type UpdateOptsBuilder interface {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600299 ToServerUpdateMap() (map[string]interface{}, error)
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400300}
301
302// UpdateOpts specifies the base attributes that may be updated on an existing server.
303type UpdateOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600304 // Name changes the displayed name of the server.
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400305 // The server host name will *not* change.
306 // Server names are not constrained to be unique, even within the same tenant.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600307 Name string `json:"name,omitempty"`
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400308
Jon Perrittdb0ae142016-03-13 00:33:41 -0600309 // AccessIPv4 provides a new IPv4 address for the instance.
310 AccessIPv4 string `json:"accessIPv4,omitempty"`
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400311
Jon Perrittdb0ae142016-03-13 00:33:41 -0600312 // AccessIPv6 provides a new IPv6 address for the instance.
313 AccessIPv6 string `json:"accessIPv6,omitempty"`
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400314}
315
Ash Wilsone45c9732014-09-29 10:54:12 -0400316// ToServerUpdateMap formats an UpdateOpts structure into a request body.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600317func (opts UpdateOpts) ToServerUpdateMap() (map[string]interface{}, error) {
318 return gophercloud.BuildRequestBody(opts, "server")
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400319}
320
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800321// Update requests that various attributes of the indicated server be changed.
Jon Perritt3860b512016-03-29 12:01:48 -0500322func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600323 b, err := opts.ToServerUpdateMap()
324 if err != nil {
325 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500326 return
Jon Perrittdb0ae142016-03-13 00:33:41 -0600327 }
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 })
jrperritt29ae6b32016-04-13 12:59:37 -0500331 return
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.
Jon Perritt3860b512016-03-29 12:01:48 -0500335func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) (r ActionResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600336 b := map[string]interface{}{
337 "changePassword": map[string]string{
338 "adminPass": newPassword,
339 },
340 }
341 _, r.Err = client.Post(actionURL(client, id), b, nil, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500342 return
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700343}
344
Ash Wilson01626a32014-09-17 10:38:07 -0400345// RebootMethod describes the mechanisms by which a server reboot can be requested.
346type RebootMethod string
347
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700348// These constants determine how a server should be rebooted.
349// See the Reboot() function for further details.
350const (
Ash Wilson01626a32014-09-17 10:38:07 -0400351 SoftReboot RebootMethod = "SOFT"
352 HardReboot RebootMethod = "HARD"
353 OSReboot = SoftReboot
354 PowerCycle = HardReboot
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700355)
356
Jon Perrittdb0ae142016-03-13 00:33:41 -0600357// RebootOptsBuilder is an interface that options must satisfy in order to be
358// used when rebooting a server instance
Jon Perrittf094fef2016-03-07 01:41:59 -0600359type RebootOptsBuilder interface {
360 ToServerRebootMap() (map[string]interface{}, error)
361}
362
Jon Perrittdb0ae142016-03-13 00:33:41 -0600363// RebootOpts satisfies the RebootOptsBuilder interface
Jon Perrittf094fef2016-03-07 01:41:59 -0600364type RebootOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600365 Type RebootMethod `json:"type" required:"true"`
Jon Perrittf094fef2016-03-07 01:41:59 -0600366}
367
Jon Perrittdb0ae142016-03-13 00:33:41 -0600368// ToServerRebootMap allows RebootOpts to satisfiy the RebootOptsBuilder
369// interface
Jon Perrittf094fef2016-03-07 01:41:59 -0600370func (opts *RebootOpts) ToServerRebootMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600371 return gophercloud.BuildRequestBody(opts, "reboot")
Jon Perrittf094fef2016-03-07 01:41:59 -0600372}
373
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700374// Reboot requests that a given server reboot.
375// Two methods exist for rebooting a server:
376//
Jon Perrittdb0ae142016-03-13 00:33:41 -0600377// 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 -0400378// terminating it at the hypervisor level.
379// It's done. Caput. Full stop.
Jon Perrittf094fef2016-03-07 01:41:59 -0600380// Then, after a brief while, power is rtored or the VM instance rtarted.
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700381//
Jon Perrittf094fef2016-03-07 01:41:59 -0600382// SoftReboot (aka OSReboot) simply tells the OS to rtart under its own procedur.
383// 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 -0500384func Reboot(client *gophercloud.ServiceClient, id string, opts RebootOptsBuilder) (r ActionResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600385 b, err := opts.ToServerRebootMap()
Jon Perrittf094fef2016-03-07 01:41:59 -0600386 if err != nil {
387 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500388 return
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700389 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600390 _, r.Err = client.Post(actionURL(client, id), b, nil, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500391 return
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700392}
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700393
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200394// RebuildOptsBuilder is an interface that allows extensions to override the
395// default behaviour of rebuild options
396type RebuildOptsBuilder interface {
397 ToServerRebuildMap() (map[string]interface{}, error)
398}
399
400// RebuildOpts represents the configuration options used in a server rebuild
401// operation
402type RebuildOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600403 // The server's admin password
404 AdminPass string `json:"adminPass" required:"true"`
405 // The ID of the image you want your server to be provisioned on
406 ImageID string `json:"imageRef"`
407 ImageName string `json:"-"`
408 //ImageName string `json:"-"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200409 // Name to set the server to
Jon Perrittdb0ae142016-03-13 00:33:41 -0600410 Name string `json:"name,omitempty"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200411 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600412 AccessIPv4 string `json:"accessIPv4,omitempty"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200413 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600414 AccessIPv6 string `json:"accessIPv6,omitempty"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200415 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600416 Metadata map[string]string `json:"metadata,omitempty"`
Kevin Pike92e10b52015-04-10 15:16:57 -0700417 // Personality [optional] includes files to inject into the server at launch.
418 // Rebuild will base64-encode file contents for you.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600419 Personality Personality `json:"personality,omitempty"`
420 ServiceClient *gophercloud.ServiceClient `json:"-"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200421}
422
423// ToServerRebuildMap formats a RebuildOpts struct into a map for use in JSON
424func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600425 b, err := gophercloud.BuildRequestBody(opts, "")
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200426 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600427 return nil, err
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200428 }
429
Joe Topjian50cdddf2016-09-16 10:56:09 -0600430 // If ImageRef isn't provided, check if ImageName was provided to ascertain
431 // the image ID.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600432 if opts.ImageID == "" {
Joe Topjian50cdddf2016-09-16 10:56:09 -0600433 if opts.ImageName != "" {
434 if opts.ServiceClient == nil {
435 err := ErrNoClientProvidedForIDByName{}
436 err.Argument = "ServiceClient"
437 return nil, err
438 }
439 imageID, err := images.IDFromName(opts.ServiceClient, opts.ImageName)
440 if err != nil {
441 return nil, err
442 }
443 b["imageRef"] = imageID
Jon Perrittdb0ae142016-03-13 00:33:41 -0600444 }
Jon Perritt12395212016-02-24 10:41:17 -0600445 }
446
Jon Perrittdb0ae142016-03-13 00:33:41 -0600447 return map[string]interface{}{"rebuild": b}, nil
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200448}
449
450// Rebuild will reprovision the server according to the configuration options
451// provided in the RebuildOpts struct.
Jon Perritt3860b512016-03-29 12:01:48 -0500452func Rebuild(client *gophercloud.ServiceClient, id string, opts RebuildOptsBuilder) (r RebuildResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600453 b, err := opts.ToServerRebuildMap()
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200454 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600455 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500456 return
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700457 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600458 _, r.Err = client.Post(actionURL(client, id), b, &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500459 return
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700460}
461
Ash Wilson5f7cf182014-10-23 08:35:24 -0400462// ResizeOptsBuilder is an interface that allows extensions to override the default structure of
463// a Resize request.
464type ResizeOptsBuilder interface {
465 ToServerResizeMap() (map[string]interface{}, error)
466}
467
468// ResizeOpts represents the configuration options used to control a Resize operation.
469type ResizeOpts struct {
470 // FlavorRef is the ID of the flavor you wish your server to become.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600471 FlavorRef string `json:"flavorRef" required:"true"`
Ash Wilson5f7cf182014-10-23 08:35:24 -0400472}
473
Alex Gaynor266e9332014-10-28 14:44:04 -0700474// 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 -0400475// Resize request.
476func (opts ResizeOpts) ToServerResizeMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600477 return gophercloud.BuildRequestBody(opts, "resize")
Ash Wilson5f7cf182014-10-23 08:35:24 -0400478}
479
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700480// Resize instructs the provider to change the flavor of the server.
Ash Wilson01626a32014-09-17 10:38:07 -0400481// Note that this implies rebuilding it.
Gleb37b56e82016-09-06 19:07:58 +0300482// Unfortunately, one cannot pass rebuild parameters to the resize function.
483// When the resize completes, the server will be in RESIZE_VERIFY state.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700484// While in this state, you can explore the use of the new server's configuration.
Gleb37b56e82016-09-06 19:07:58 +0300485// If you like it, call ConfirmResize() to commit the resize permanently.
486// Otherwise, call RevertResize() to restore the old configuration.
Jon Perritt3860b512016-03-29 12:01:48 -0500487func Resize(client *gophercloud.ServiceClient, id string, opts ResizeOptsBuilder) (r ActionResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600488 b, err := opts.ToServerResizeMap()
489 if err != nil {
490 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500491 return
Jon Perrittf094fef2016-03-07 01:41:59 -0600492 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600493 _, r.Err = client.Post(actionURL(client, id), b, nil, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500494 return
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700495}
496
Gleb37b56e82016-09-06 19:07:58 +0300497// ConfirmResize confirms a previous resize operation on a server.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700498// See Resize() for more details.
Jon Perritt3860b512016-03-29 12:01:48 -0500499func ConfirmResize(client *gophercloud.ServiceClient, id string) (r ActionResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600500 _, r.Err = client.Post(actionURL(client, id), map[string]interface{}{"confirmResize": nil}, nil, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100501 OkCodes: []int{201, 202, 204},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700502 })
jrperritt29ae6b32016-04-13 12:59:37 -0500503 return
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700504}
505
Gleb37b56e82016-09-06 19:07:58 +0300506// RevertResize cancels a previous resize operation on a server.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700507// See Resize() for more details.
Jon Perritt3860b512016-03-29 12:01:48 -0500508func RevertResize(client *gophercloud.ServiceClient, id string) (r ActionResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600509 _, r.Err = client.Post(actionURL(client, id), map[string]interface{}{"revertResize": nil}, nil, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500510 return
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700511}
Alex Gaynor39584a02014-10-28 13:59:21 -0700512
Alex Gaynor266e9332014-10-28 14:44:04 -0700513// RescueOptsBuilder is an interface that allows extensions to override the
514// default structure of a Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700515type RescueOptsBuilder interface {
516 ToServerRescueMap() (map[string]interface{}, error)
517}
518
Alex Gaynor266e9332014-10-28 14:44:04 -0700519// RescueOpts represents the configuration options used to control a Rescue
520// option.
Alex Gaynor39584a02014-10-28 13:59:21 -0700521type RescueOpts struct {
Alex Gaynor266e9332014-10-28 14:44:04 -0700522 // AdminPass is the desired administrative password for the instance in
Alex Gaynorcfec7722014-11-13 13:33:49 -0800523 // RESCUE mode. If it's left blank, the server will generate a password.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600524 AdminPass string `json:"adminPass,omitempty"`
Alex Gaynor39584a02014-10-28 13:59:21 -0700525}
526
Jon Perrittcc77da62014-11-16 13:14:21 -0700527// ToServerRescueMap formats a RescueOpts as a map that can be used as a JSON
Alex Gaynor266e9332014-10-28 14:44:04 -0700528// request body for the Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700529func (opts RescueOpts) ToServerRescueMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600530 return gophercloud.BuildRequestBody(opts, "rescue")
Alex Gaynor39584a02014-10-28 13:59:21 -0700531}
532
Alex Gaynor266e9332014-10-28 14:44:04 -0700533// Rescue instructs the provider to place the server into RESCUE mode.
Jon Perritt3860b512016-03-29 12:01:48 -0500534func Rescue(client *gophercloud.ServiceClient, id string, opts RescueOptsBuilder) (r RescueResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600535 b, err := opts.ToServerRescueMap()
536 if err != nil {
537 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500538 return
Jon Perrittf094fef2016-03-07 01:41:59 -0600539 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600540 _, r.Err = client.Post(actionURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100541 OkCodes: []int{200},
Alex Gaynor39584a02014-10-28 13:59:21 -0700542 })
jrperritt29ae6b32016-04-13 12:59:37 -0500543 return
Alex Gaynor39584a02014-10-28 13:59:21 -0700544}
Jon Perrittcc77da62014-11-16 13:14:21 -0700545
Jon Perritt789f8322014-11-21 08:20:04 -0700546// ResetMetadataOptsBuilder allows extensions to add additional parameters to the
547// Reset request.
548type ResetMetadataOptsBuilder interface {
549 ToMetadataResetMap() (map[string]interface{}, error)
Jon Perrittcc77da62014-11-16 13:14:21 -0700550}
551
Jon Perritt78c57ce2014-11-20 11:07:18 -0700552// MetadataOpts is a map that contains key-value pairs.
553type MetadataOpts map[string]string
Jon Perrittcc77da62014-11-16 13:14:21 -0700554
Jon Perritt789f8322014-11-21 08:20:04 -0700555// ToMetadataResetMap assembles a body for a Reset request based on the contents of a MetadataOpts.
556func (opts MetadataOpts) ToMetadataResetMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700557 return map[string]interface{}{"metadata": opts}, nil
558}
559
Jon Perritt78c57ce2014-11-20 11:07:18 -0700560// ToMetadataUpdateMap assembles a body for an Update request based on the contents of a MetadataOpts.
561func (opts MetadataOpts) ToMetadataUpdateMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700562 return map[string]interface{}{"metadata": opts}, nil
563}
564
Jon Perritt789f8322014-11-21 08:20:04 -0700565// ResetMetadata will create multiple new key-value pairs for the given server ID.
Jon Perrittcc77da62014-11-16 13:14:21 -0700566// Note: Using this operation will erase any already-existing metadata and create
567// the new metadata provided. To keep any already-existing metadata, use the
568// UpdateMetadatas or UpdateMetadata function.
Jon Perritt3860b512016-03-29 12:01:48 -0500569func ResetMetadata(client *gophercloud.ServiceClient, id string, opts ResetMetadataOptsBuilder) (r ResetMetadataResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600570 b, err := opts.ToMetadataResetMap()
Jon Perrittcc77da62014-11-16 13:14:21 -0700571 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600572 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500573 return
Jon Perrittcc77da62014-11-16 13:14:21 -0700574 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600575 _, r.Err = client.Put(metadataURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100576 OkCodes: []int{200},
Jon Perrittcc77da62014-11-16 13:14:21 -0700577 })
jrperritt29ae6b32016-04-13 12:59:37 -0500578 return
Jon Perrittcc77da62014-11-16 13:14:21 -0700579}
580
Jon Perritt78c57ce2014-11-20 11:07:18 -0700581// Metadata requests all the metadata for the given server ID.
Jon Perritt3860b512016-03-29 12:01:48 -0500582func Metadata(client *gophercloud.ServiceClient, id string) (r GetMetadataResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600583 _, r.Err = client.Get(metadataURL(client, id), &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500584 return
Jon Perrittcc77da62014-11-16 13:14:21 -0700585}
586
Jon Perritt78c57ce2014-11-20 11:07:18 -0700587// UpdateMetadataOptsBuilder allows extensions to add additional parameters to the
588// Create request.
589type UpdateMetadataOptsBuilder interface {
590 ToMetadataUpdateMap() (map[string]interface{}, error)
591}
592
593// UpdateMetadata updates (or creates) all the metadata specified by opts for the given server ID.
594// This operation does not affect already-existing metadata that is not specified
595// by opts.
Jon Perritt3860b512016-03-29 12:01:48 -0500596func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) (r UpdateMetadataResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600597 b, err := opts.ToMetadataUpdateMap()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700598 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600599 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500600 return
Jon Perritt78c57ce2014-11-20 11:07:18 -0700601 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600602 _, r.Err = client.Post(metadataURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100603 OkCodes: []int{200},
Jon Perritt78c57ce2014-11-20 11:07:18 -0700604 })
jrperritt29ae6b32016-04-13 12:59:37 -0500605 return
Jon Perritt78c57ce2014-11-20 11:07:18 -0700606}
607
608// MetadatumOptsBuilder allows extensions to add additional parameters to the
609// Create request.
610type MetadatumOptsBuilder interface {
611 ToMetadatumCreateMap() (map[string]interface{}, string, error)
612}
613
614// MetadatumOpts is a map of length one that contains a key-value pair.
615type MetadatumOpts map[string]string
616
617// ToMetadatumCreateMap assembles a body for a Create request based on the contents of a MetadataumOpts.
618func (opts MetadatumOpts) ToMetadatumCreateMap() (map[string]interface{}, string, error) {
619 if len(opts) != 1 {
Jon Perritt13808262016-03-09 00:50:12 -0600620 err := gophercloud.ErrInvalidInput{}
621 err.Argument = "servers.MetadatumOpts"
622 err.Info = "Must have 1 and only 1 key-value pair"
623 return nil, "", err
Jon Perritt78c57ce2014-11-20 11:07:18 -0700624 }
625 metadatum := map[string]interface{}{"meta": opts}
626 var key string
627 for k := range metadatum["meta"].(MetadatumOpts) {
628 key = k
629 }
630 return metadatum, key, nil
631}
632
633// 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 -0500634func CreateMetadatum(client *gophercloud.ServiceClient, id string, opts MetadatumOptsBuilder) (r CreateMetadatumResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600635 b, key, err := opts.ToMetadatumCreateMap()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700636 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600637 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500638 return
Jon Perritt78c57ce2014-11-20 11:07:18 -0700639 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600640 _, r.Err = client.Put(metadatumURL(client, id, key), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100641 OkCodes: []int{200},
Jon Perritt78c57ce2014-11-20 11:07:18 -0700642 })
jrperritt29ae6b32016-04-13 12:59:37 -0500643 return
Jon Perritt78c57ce2014-11-20 11:07:18 -0700644}
645
646// Metadatum requests the key-value pair with the given key for the given server ID.
Jon Perritt3860b512016-03-29 12:01:48 -0500647func Metadatum(client *gophercloud.ServiceClient, id, key string) (r GetMetadatumResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600648 _, r.Err = client.Get(metadatumURL(client, id, key), &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500649 return
Jon Perritt78c57ce2014-11-20 11:07:18 -0700650}
651
652// DeleteMetadatum will delete the key-value pair with the given key for the given server ID.
Jon Perritt3860b512016-03-29 12:01:48 -0500653func DeleteMetadatum(client *gophercloud.ServiceClient, id, key string) (r DeleteMetadatumResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600654 _, r.Err = client.Delete(metadatumURL(client, id, key), nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500655 return
Jon Perrittcc77da62014-11-16 13:14:21 -0700656}
Jon Perritt5cb49482015-02-19 12:19:58 -0700657
658// ListAddresses makes a request against the API to list the servers IP addresses.
659func ListAddresses(client *gophercloud.ServiceClient, id string) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600660 return pagination.NewPager(client, listAddressesURL(client, id), func(r pagination.PageResult) pagination.Page {
Jon Perritt5cb49482015-02-19 12:19:58 -0700661 return AddressPage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600662 })
Jon Perritt5cb49482015-02-19 12:19:58 -0700663}
Jon Perritt04d073c2015-02-19 21:46:23 -0700664
665// ListAddressesByNetwork makes a request against the API to list the servers IP addresses
666// for the given network.
667func ListAddressesByNetwork(client *gophercloud.ServiceClient, id, network string) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600668 return pagination.NewPager(client, listAddressesByNetworkURL(client, id, network), func(r pagination.PageResult) pagination.Page {
Jon Perritt04d073c2015-02-19 21:46:23 -0700669 return NetworkAddressPage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600670 })
Jon Perritt04d073c2015-02-19 21:46:23 -0700671}
einarf2fc665e2015-04-16 20:16:21 +0000672
Jon Perrittdb0ae142016-03-13 00:33:41 -0600673// CreateImageOptsBuilder is the interface types must satisfy in order to be
674// used as CreateImage options
einarf4e5fdaf2015-04-16 23:14:59 +0000675type CreateImageOptsBuilder interface {
676 ToServerCreateImageMap() (map[string]interface{}, error)
einarf2fc665e2015-04-16 20:16:21 +0000677}
678
Jon Perrittdb0ae142016-03-13 00:33:41 -0600679// CreateImageOpts satisfies the CreateImageOptsBuilder
680type CreateImageOpts struct {
681 // Name of the image/snapshot
682 Name string `json:"name" required:"true"`
683 // Metadata contains key-value pairs (up to 255 bytes each) to attach to the created image.
684 Metadata map[string]string `json:"metadata,omitempty"`
685}
686
einarf4e5fdaf2015-04-16 23:14:59 +0000687// ToServerCreateImageMap formats a CreateImageOpts structure into a request body.
688func (opts CreateImageOpts) ToServerCreateImageMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600689 return gophercloud.BuildRequestBody(opts, "createImage")
einarf2fc665e2015-04-16 20:16:21 +0000690}
691
einarf4e5fdaf2015-04-16 23:14:59 +0000692// 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 -0500693func CreateImage(client *gophercloud.ServiceClient, id string, opts CreateImageOptsBuilder) (r CreateImageResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600694 b, err := opts.ToServerCreateImageMap()
einarf2fc665e2015-04-16 20:16:21 +0000695 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600696 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500697 return
einarf2fc665e2015-04-16 20:16:21 +0000698 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600699 resp, err := client.Post(actionURL(client, id), b, nil, &gophercloud.RequestOpts{
einarf2fc665e2015-04-16 20:16:21 +0000700 OkCodes: []int{202},
701 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600702 r.Err = err
703 r.Header = resp.Header
jrperritt29ae6b32016-04-13 12:59:37 -0500704 return
einarf2fc665e2015-04-16 20:16:21 +0000705}
Jon Perritt6b0a8832015-06-04 14:32:30 -0600706
707// IDFromName is a convienience function that returns a server's ID given its name.
708func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600709 count := 0
710 id := ""
Jon Perrittf094fef2016-03-07 01:41:59 -0600711 allPages, err := List(client, nil).AllPages()
712 if err != nil {
713 return "", err
714 }
Jon Perritt6b0a8832015-06-04 14:32:30 -0600715
Jon Perrittf094fef2016-03-07 01:41:59 -0600716 all, err := ExtractServers(allPages)
717 if err != nil {
718 return "", err
719 }
720
721 for _, f := range all {
722 if f.Name == name {
723 count++
724 id = f.ID
725 }
726 }
727
728 switch count {
Jon Perritt6b0a8832015-06-04 14:32:30 -0600729 case 0:
Jon Perrittdb0ae142016-03-13 00:33:41 -0600730 return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "server"}
Jon Perritt6b0a8832015-06-04 14:32:30 -0600731 case 1:
Jon Perrittf094fef2016-03-07 01:41:59 -0600732 return id, nil
Jon Perritt6b0a8832015-06-04 14:32:30 -0600733 default:
Jon Perrittdb0ae142016-03-13 00:33:41 -0600734 return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "server"}
Jon Perritt6b0a8832015-06-04 14:32:30 -0600735 }
736}
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100737
Rickard von Essenc3d49b72016-02-16 20:59:18 +0100738// GetPassword makes a request against the nova API to get the encrypted administrative password.
jrperritt2f93a632016-04-13 15:41:20 -0500739func GetPassword(client *gophercloud.ServiceClient, serverId string) (r GetPasswordResult) {
740 _, r.Err = client.Get(passwordURL(client, serverId), &r.Body, nil)
741 return
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100742}