blob: 709f8390ae58c0c8ec11a4b2ab4479b628d6edea [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
Krzysztof Szukiełojć3f41d082017-05-07 14:43:06 +02007 "gerrit.mcp.mirantis.net/debian/gophercloud.git"
Krzysztof Szukiełojć24a29ce2017-05-07 14:24:02 +02008 "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack/compute/v2/flavors"
9 "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack/compute/v2/images"
10 "gerrit.mcp.mirantis.net/debian/gophercloud.git/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
Ildar Svetlov6c31b972018-03-12 17:01:52 +040076// ListServers makes a request against the API to list IDs, names, and links for all servers.
77func ListServers(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
78 url := listURL(client)
79 if opts != nil {
80 query, err := opts.ToServerListQuery()
81 if err != nil {
82 return pagination.Pager{Err: err}
83 }
84 url += query
85 }
86 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
87 return ShortServerPage{pagination.SinglePageBase(r)}
88 })
89}
90
Ash Wilson2206a112014-10-02 10:57:38 -040091// CreateOptsBuilder describes struct types that can be accepted by the Create call.
Ash Wilson6a310e02014-09-29 08:24:02 -040092// The CreateOpts struct in this package does.
Ash Wilson2206a112014-10-02 10:57:38 -040093type CreateOptsBuilder interface {
Jon Perritt4149d7c2014-10-23 21:23:46 -050094 ToServerCreateMap() (map[string]interface{}, error)
Ash Wilson6a310e02014-09-29 08:24:02 -040095}
96
97// Network is used within CreateOpts to control a new server's network attachments.
98type Network struct {
99 // UUID of a nova-network to attach to the newly provisioned server.
100 // Required unless Port is provided.
101 UUID string
102
103 // Port of a neutron network to attach to the newly provisioned server.
104 // Required unless UUID is provided.
105 Port string
106
107 // FixedIP [optional] specifies a fixed IPv4 address to be used on this network.
108 FixedIP string
109}
110
Kevin Pike92e10b52015-04-10 15:16:57 -0700111// Personality is an array of files that are injected into the server at launch.
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700112type Personality []*File
Kevin Pike92e10b52015-04-10 15:16:57 -0700113
114// File is used within CreateOpts and RebuildOpts to inject a file into the server at launch.
Kevin Pike9748b7b2015-05-05 07:34:07 -0700115// File implements the json.Marshaler interface, so when a Create or Rebuild operation is requested,
116// json.Marshal will call File's MarshalJSON method.
Kevin Pike92e10b52015-04-10 15:16:57 -0700117type File struct {
118 // Path of the file
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700119 Path string
Kevin Pike92e10b52015-04-10 15:16:57 -0700120 // Contents of the file. Maximum content size is 255 bytes.
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700121 Contents []byte
Kevin Pike92e10b52015-04-10 15:16:57 -0700122}
123
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700124// MarshalJSON marshals the escaped file, base64 encoding the contents.
125func (f *File) MarshalJSON() ([]byte, error) {
126 file := struct {
127 Path string `json:"path"`
128 Contents string `json:"contents"`
129 }{
130 Path: f.Path,
131 Contents: base64.StdEncoding.EncodeToString(f.Contents),
Kevin Pike92e10b52015-04-10 15:16:57 -0700132 }
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700133 return json.Marshal(file)
Kevin Pike92e10b52015-04-10 15:16:57 -0700134}
135
Ash Wilson6a310e02014-09-29 08:24:02 -0400136// CreateOpts specifies server creation parameters.
137type CreateOpts struct {
Jon Perritt01618ee2016-03-09 03:04:06 -0600138 // Name is the name to assign to the newly launched server.
139 Name string `json:"name" required:"true"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400140
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600141 // ImageRef [optional; required if ImageName is not provided] is the ID or full
142 // URL to the image that contains the server's OS and initial state.
143 // Also optional if using the boot-from-volume extension.
Jon Perritt01618ee2016-03-09 03:04:06 -0600144 ImageRef string `json:"imageRef"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400145
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600146 // ImageName [optional; required if ImageRef is not provided] is the name of the
147 // image that contains the server's OS and initial state.
148 // Also optional if using the boot-from-volume extension.
Jon Perritt01618ee2016-03-09 03:04:06 -0600149 ImageName string `json:"-"`
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600150
151 // FlavorRef [optional; required if FlavorName is not provided] is the ID or
152 // full URL to the flavor that describes the server's specs.
Jon Perritt01618ee2016-03-09 03:04:06 -0600153 FlavorRef string `json:"flavorRef"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400154
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600155 // FlavorName [optional; required if FlavorRef is not provided] is the name of
156 // the flavor that describes the server's specs.
Jon Perritt01618ee2016-03-09 03:04:06 -0600157 FlavorName string `json:"-"`
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600158
Jon Perritt01618ee2016-03-09 03:04:06 -0600159 // SecurityGroups lists the names of the security groups to which this server should belong.
160 SecurityGroups []string `json:"-"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400161
Jon Perritt01618ee2016-03-09 03:04:06 -0600162 // UserData contains configuration information or scripts to use upon launch.
Gavin Williamscd65a062016-11-08 19:05:47 +0000163 // Create will base64-encode it for you, if it isn't already.
Jon Perritt01618ee2016-03-09 03:04:06 -0600164 UserData []byte `json:"-"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400165
Jon Perritt01618ee2016-03-09 03:04:06 -0600166 // AvailabilityZone in which to launch the server.
167 AvailabilityZone string `json:"availability_zone,omitempty"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400168
Jon Perritt01618ee2016-03-09 03:04:06 -0600169 // Networks dictates how this server will be attached to available networks.
Ash Wilson6a310e02014-09-29 08:24:02 -0400170 // By default, the server will be attached to all isolated networks for the tenant.
Jon Perritt01618ee2016-03-09 03:04:06 -0600171 Networks []Network `json:"-"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400172
Jon Perritt01618ee2016-03-09 03:04:06 -0600173 // Metadata contains key-value pairs (up to 255 bytes each) to attach to the server.
Joe Topjianf464c962016-09-12 08:02:43 -0600174 Metadata map[string]string `json:"metadata,omitempty"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400175
Jon Perritt01618ee2016-03-09 03:04:06 -0600176 // Personality includes files to inject into the server at launch.
Kevin Pike92e10b52015-04-10 15:16:57 -0700177 // Create will base64-encode file contents for you.
Bruce Martins6b3419f2017-01-18 20:06:55 -0500178 Personality Personality `json:"personality,omitempty"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400179
Jon Perritt01618ee2016-03-09 03:04:06 -0600180 // ConfigDrive enables metadata injection through a configuration drive.
181 ConfigDrive *bool `json:"config_drive,omitempty"`
Jon Perrittf3b2e142014-11-04 16:00:19 -0600182
Jon Perritt01618ee2016-03-09 03:04:06 -0600183 // AdminPass sets the root user password. If not set, a randomly-generated
Jon Perrittf094fef2016-03-07 01:41:59 -0600184 // password will be created and returned in the rponse.
Jon Perritt01618ee2016-03-09 03:04:06 -0600185 AdminPass string `json:"adminPass,omitempty"`
Jon Perritt7b9671c2015-02-01 22:03:14 -0700186
Jon Perritt01618ee2016-03-09 03:04:06 -0600187 // AccessIPv4 specifies an IPv4 address for the instance.
188 AccessIPv4 string `json:"accessIPv4,omitempty"`
Jon Perritt7b9671c2015-02-01 22:03:14 -0700189
Jon Perritt01618ee2016-03-09 03:04:06 -0600190 // AccessIPv6 pecifies an IPv6 address for the instance.
191 AccessIPv6 string `json:"accessIPv6,omitempty"`
Jon Perritt994370e2016-02-18 15:23:34 -0600192
Jon Perritt01618ee2016-03-09 03:04:06 -0600193 // ServiceClient will allow calls to be made to retrieve an image or
Jon Perritt994370e2016-02-18 15:23:34 -0600194 // flavor ID by name.
Jon Perritt01618ee2016-03-09 03:04:06 -0600195 ServiceClient *gophercloud.ServiceClient `json:"-"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400196}
197
Ash Wilsone45c9732014-09-29 10:54:12 -0400198// ToServerCreateMap assembles a request body based on the contents of a CreateOpts.
Jon Perritt4149d7c2014-10-23 21:23:46 -0500199func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) {
jrperritt0d7ed5d2016-08-16 11:23:26 -0500200 sc := opts.ServiceClient
201 opts.ServiceClient = nil
Jon Perrittdb0ae142016-03-13 00:33:41 -0600202 b, err := gophercloud.BuildRequestBody(opts, "")
Jon Perritt01618ee2016-03-09 03:04:06 -0600203 if err != nil {
204 return nil, err
205 }
Ash Wilson6a310e02014-09-29 08:24:02 -0400206
207 if opts.UserData != nil {
Gavin Williamscd65a062016-11-08 19:05:47 +0000208 var userData string
209 if _, err := base64.StdEncoding.DecodeString(string(opts.UserData)); err != nil {
210 userData = base64.StdEncoding.EncodeToString(opts.UserData)
211 } else {
212 userData = string(opts.UserData)
213 }
214 b["user_data"] = &userData
Jon Perritt7b9671c2015-02-01 22:03:14 -0700215 }
Ash Wilson6a310e02014-09-29 08:24:02 -0400216
217 if len(opts.SecurityGroups) > 0 {
218 securityGroups := make([]map[string]interface{}, len(opts.SecurityGroups))
219 for i, groupName := range opts.SecurityGroups {
220 securityGroups[i] = map[string]interface{}{"name": groupName}
221 }
Jon Perritt01618ee2016-03-09 03:04:06 -0600222 b["security_groups"] = securityGroups
Ash Wilson6a310e02014-09-29 08:24:02 -0400223 }
Jon Perritt2a7797d2014-10-21 15:08:43 -0500224
Ash Wilson6a310e02014-09-29 08:24:02 -0400225 if len(opts.Networks) > 0 {
226 networks := make([]map[string]interface{}, len(opts.Networks))
227 for i, net := range opts.Networks {
228 networks[i] = make(map[string]interface{})
229 if net.UUID != "" {
230 networks[i]["uuid"] = net.UUID
231 }
232 if net.Port != "" {
233 networks[i]["port"] = net.Port
234 }
235 if net.FixedIP != "" {
236 networks[i]["fixed_ip"] = net.FixedIP
237 }
238 }
Jon Perritt01618ee2016-03-09 03:04:06 -0600239 b["networks"] = networks
Kevin Pike92e10b52015-04-10 15:16:57 -0700240 }
241
Joe Topjian50cdddf2016-09-16 10:56:09 -0600242 // If ImageRef isn't provided, check if ImageName was provided to ascertain
243 // the image ID.
jrperrittb1013232016-02-10 19:01:53 -0600244 if opts.ImageRef == "" {
Joe Topjian50cdddf2016-09-16 10:56:09 -0600245 if opts.ImageName != "" {
246 if sc == nil {
247 err := ErrNoClientProvidedForIDByName{}
248 err.Argument = "ServiceClient"
249 return nil, err
250 }
251 imageID, err := images.IDFromName(sc, opts.ImageName)
252 if err != nil {
253 return nil, err
254 }
255 b["imageRef"] = imageID
jrperrittb1013232016-02-10 19:01:53 -0600256 }
jrperrittb1013232016-02-10 19:01:53 -0600257 }
258
259 // If FlavorRef isn't provided, use FlavorName to ascertain the flavor ID.
260 if opts.FlavorRef == "" {
261 if opts.FlavorName == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600262 err := ErrNeitherFlavorIDNorFlavorNameProvided{}
Jon Perrittf094fef2016-03-07 01:41:59 -0600263 err.Argument = "FlavorRef/FlavorName"
264 return nil, err
jrperrittb1013232016-02-10 19:01:53 -0600265 }
jrperritt0d7ed5d2016-08-16 11:23:26 -0500266 if sc == nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600267 err := ErrNoClientProvidedForIDByName{}
268 err.Argument = "ServiceClient"
Jon Perrittf094fef2016-03-07 01:41:59 -0600269 return nil, err
Jon Perritt994370e2016-02-18 15:23:34 -0600270 }
jrperritt0d7ed5d2016-08-16 11:23:26 -0500271 flavorID, err := flavors.IDFromName(sc, opts.FlavorName)
jrperrittb1013232016-02-10 19:01:53 -0600272 if err != nil {
273 return nil, err
274 }
Jon Perritt01618ee2016-03-09 03:04:06 -0600275 b["flavorRef"] = flavorID
jrperrittb1013232016-02-10 19:01:53 -0600276 }
277
Jon Perritt01618ee2016-03-09 03:04:06 -0600278 return map[string]interface{}{"server": b}, nil
Ash Wilson6a310e02014-09-29 08:24:02 -0400279}
280
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800281// Create requests a server to be provisioned to the user in the current tenant.
Jon Perritt3860b512016-03-29 12:01:48 -0500282func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
Jon Perritt4149d7c2014-10-23 21:23:46 -0500283 reqBody, err := opts.ToServerCreateMap()
284 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600285 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500286 return
Jon Perritt4149d7c2014-10-23 21:23:46 -0500287 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600288 _, r.Err = client.Post(listURL(client), reqBody, &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500289 return
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800290}
291
292// Delete requests that a server previously provisioned be removed from your account.
Jon Perritt3860b512016-03-29 12:01:48 -0500293func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600294 _, r.Err = client.Delete(deleteURL(client, id), nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500295 return
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800296}
297
Jon Perritt01618ee2016-03-09 03:04:06 -0600298// ForceDelete forces the deletion of a server
Jon Perritt3860b512016-03-29 12:01:48 -0500299func ForceDelete(client *gophercloud.ServiceClient, id string) (r ActionResult) {
Jon Perritt01618ee2016-03-09 03:04:06 -0600300 _, r.Err = client.Post(actionURL(client, id), map[string]interface{}{"forceDelete": ""}, nil, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500301 return
Ian Duffy370c4302016-01-21 10:44:56 +0000302}
303
Ash Wilson7ddf0362014-09-17 10:59:09 -0400304// Get requests details on a single server, by ID.
Jon Perritt3860b512016-03-29 12:01:48 -0500305func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600306 _, r.Err = client.Get(getURL(client, id), &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100307 OkCodes: []int{200, 203},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800308 })
jrperritt29ae6b32016-04-13 12:59:37 -0500309 return
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800310}
311
Alex Gaynora6d5f9f2014-10-27 10:52:32 -0700312// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
Jon Perritt82048212014-10-13 22:33:13 -0500313type UpdateOptsBuilder interface {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600314 ToServerUpdateMap() (map[string]interface{}, error)
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400315}
316
317// UpdateOpts specifies the base attributes that may be updated on an existing server.
318type UpdateOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600319 // Name changes the displayed name of the server.
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400320 // The server host name will *not* change.
321 // Server names are not constrained to be unique, even within the same tenant.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600322 Name string `json:"name,omitempty"`
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400323
Jon Perrittdb0ae142016-03-13 00:33:41 -0600324 // AccessIPv4 provides a new IPv4 address for the instance.
325 AccessIPv4 string `json:"accessIPv4,omitempty"`
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400326
Jon Perrittdb0ae142016-03-13 00:33:41 -0600327 // AccessIPv6 provides a new IPv6 address for the instance.
328 AccessIPv6 string `json:"accessIPv6,omitempty"`
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400329}
330
Ash Wilsone45c9732014-09-29 10:54:12 -0400331// ToServerUpdateMap formats an UpdateOpts structure into a request body.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600332func (opts UpdateOpts) ToServerUpdateMap() (map[string]interface{}, error) {
333 return gophercloud.BuildRequestBody(opts, "server")
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400334}
335
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800336// Update requests that various attributes of the indicated server be changed.
Jon Perritt3860b512016-03-29 12:01:48 -0500337func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600338 b, err := opts.ToServerUpdateMap()
339 if err != nil {
340 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500341 return
Jon Perrittdb0ae142016-03-13 00:33:41 -0600342 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600343 _, r.Err = client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100344 OkCodes: []int{200},
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800345 })
jrperritt29ae6b32016-04-13 12:59:37 -0500346 return
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800347}
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700348
Ash Wilson01626a32014-09-17 10:38:07 -0400349// ChangeAdminPassword alters the administrator or root password for a specified server.
Jon Perritt3860b512016-03-29 12:01:48 -0500350func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) (r ActionResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600351 b := map[string]interface{}{
352 "changePassword": map[string]string{
353 "adminPass": newPassword,
354 },
355 }
356 _, r.Err = client.Post(actionURL(client, id), b, nil, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500357 return
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700358}
359
Ash Wilson01626a32014-09-17 10:38:07 -0400360// RebootMethod describes the mechanisms by which a server reboot can be requested.
361type RebootMethod string
362
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700363// These constants determine how a server should be rebooted.
364// See the Reboot() function for further details.
365const (
Ash Wilson01626a32014-09-17 10:38:07 -0400366 SoftReboot RebootMethod = "SOFT"
367 HardReboot RebootMethod = "HARD"
368 OSReboot = SoftReboot
369 PowerCycle = HardReboot
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700370)
371
Jon Perrittdb0ae142016-03-13 00:33:41 -0600372// RebootOptsBuilder is an interface that options must satisfy in order to be
373// used when rebooting a server instance
Jon Perrittf094fef2016-03-07 01:41:59 -0600374type RebootOptsBuilder interface {
375 ToServerRebootMap() (map[string]interface{}, error)
376}
377
Jon Perrittdb0ae142016-03-13 00:33:41 -0600378// RebootOpts satisfies the RebootOptsBuilder interface
Jon Perrittf094fef2016-03-07 01:41:59 -0600379type RebootOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600380 Type RebootMethod `json:"type" required:"true"`
Jon Perrittf094fef2016-03-07 01:41:59 -0600381}
382
Jon Perrittdb0ae142016-03-13 00:33:41 -0600383// ToServerRebootMap allows RebootOpts to satisfiy the RebootOptsBuilder
384// interface
Jon Perrittf094fef2016-03-07 01:41:59 -0600385func (opts *RebootOpts) ToServerRebootMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600386 return gophercloud.BuildRequestBody(opts, "reboot")
Jon Perrittf094fef2016-03-07 01:41:59 -0600387}
388
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700389// Reboot requests that a given server reboot.
390// Two methods exist for rebooting a server:
391//
Jon Perrittdb0ae142016-03-13 00:33:41 -0600392// 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 -0400393// terminating it at the hypervisor level.
394// It's done. Caput. Full stop.
Jon Perrittf094fef2016-03-07 01:41:59 -0600395// Then, after a brief while, power is rtored or the VM instance rtarted.
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700396//
Jon Perrittf094fef2016-03-07 01:41:59 -0600397// SoftReboot (aka OSReboot) simply tells the OS to rtart under its own procedur.
398// 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 -0500399func Reboot(client *gophercloud.ServiceClient, id string, opts RebootOptsBuilder) (r ActionResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600400 b, err := opts.ToServerRebootMap()
Jon Perrittf094fef2016-03-07 01:41:59 -0600401 if err != nil {
402 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500403 return
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700404 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600405 _, r.Err = client.Post(actionURL(client, id), b, nil, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500406 return
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700407}
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700408
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200409// RebuildOptsBuilder is an interface that allows extensions to override the
410// default behaviour of rebuild options
411type RebuildOptsBuilder interface {
412 ToServerRebuildMap() (map[string]interface{}, error)
413}
414
415// RebuildOpts represents the configuration options used in a server rebuild
416// operation
417type RebuildOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600418 // The server's admin password
jrperritt6df190c2017-02-20 16:35:10 -0600419 AdminPass string `json:"adminPass,omitempty"`
Jon Perrittdb0ae142016-03-13 00:33:41 -0600420 // The ID of the image you want your server to be provisioned on
421 ImageID string `json:"imageRef"`
422 ImageName string `json:"-"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200423 // Name to set the server to
Jon Perrittdb0ae142016-03-13 00:33:41 -0600424 Name string `json:"name,omitempty"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200425 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600426 AccessIPv4 string `json:"accessIPv4,omitempty"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200427 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600428 AccessIPv6 string `json:"accessIPv6,omitempty"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200429 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600430 Metadata map[string]string `json:"metadata,omitempty"`
Kevin Pike92e10b52015-04-10 15:16:57 -0700431 // Personality [optional] includes files to inject into the server at launch.
432 // Rebuild will base64-encode file contents for you.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600433 Personality Personality `json:"personality,omitempty"`
434 ServiceClient *gophercloud.ServiceClient `json:"-"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200435}
436
437// ToServerRebuildMap formats a RebuildOpts struct into a map for use in JSON
438func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600439 b, err := gophercloud.BuildRequestBody(opts, "")
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200440 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600441 return nil, err
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200442 }
443
Joe Topjian50cdddf2016-09-16 10:56:09 -0600444 // If ImageRef isn't provided, check if ImageName was provided to ascertain
445 // the image ID.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600446 if opts.ImageID == "" {
Joe Topjian50cdddf2016-09-16 10:56:09 -0600447 if opts.ImageName != "" {
448 if opts.ServiceClient == nil {
449 err := ErrNoClientProvidedForIDByName{}
450 err.Argument = "ServiceClient"
451 return nil, err
452 }
453 imageID, err := images.IDFromName(opts.ServiceClient, opts.ImageName)
454 if err != nil {
455 return nil, err
456 }
457 b["imageRef"] = imageID
Jon Perrittdb0ae142016-03-13 00:33:41 -0600458 }
Jon Perritt12395212016-02-24 10:41:17 -0600459 }
460
Jon Perrittdb0ae142016-03-13 00:33:41 -0600461 return map[string]interface{}{"rebuild": b}, nil
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200462}
463
464// Rebuild will reprovision the server according to the configuration options
465// provided in the RebuildOpts struct.
Jon Perritt3860b512016-03-29 12:01:48 -0500466func Rebuild(client *gophercloud.ServiceClient, id string, opts RebuildOptsBuilder) (r RebuildResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600467 b, err := opts.ToServerRebuildMap()
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200468 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600469 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500470 return
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700471 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600472 _, r.Err = client.Post(actionURL(client, id), b, &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500473 return
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700474}
475
Ash Wilson5f7cf182014-10-23 08:35:24 -0400476// ResizeOptsBuilder is an interface that allows extensions to override the default structure of
477// a Resize request.
478type ResizeOptsBuilder interface {
479 ToServerResizeMap() (map[string]interface{}, error)
480}
481
482// ResizeOpts represents the configuration options used to control a Resize operation.
483type ResizeOpts struct {
484 // FlavorRef is the ID of the flavor you wish your server to become.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600485 FlavorRef string `json:"flavorRef" required:"true"`
Ash Wilson5f7cf182014-10-23 08:35:24 -0400486}
487
Alex Gaynor266e9332014-10-28 14:44:04 -0700488// 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 -0400489// Resize request.
490func (opts ResizeOpts) ToServerResizeMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600491 return gophercloud.BuildRequestBody(opts, "resize")
Ash Wilson5f7cf182014-10-23 08:35:24 -0400492}
493
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700494// Resize instructs the provider to change the flavor of the server.
Ash Wilson01626a32014-09-17 10:38:07 -0400495// Note that this implies rebuilding it.
Gleb37b56e82016-09-06 19:07:58 +0300496// Unfortunately, one cannot pass rebuild parameters to the resize function.
497// When the resize completes, the server will be in RESIZE_VERIFY state.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700498// While in this state, you can explore the use of the new server's configuration.
Gleb37b56e82016-09-06 19:07:58 +0300499// If you like it, call ConfirmResize() to commit the resize permanently.
500// Otherwise, call RevertResize() to restore the old configuration.
Jon Perritt3860b512016-03-29 12:01:48 -0500501func Resize(client *gophercloud.ServiceClient, id string, opts ResizeOptsBuilder) (r ActionResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600502 b, err := opts.ToServerResizeMap()
503 if err != nil {
504 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500505 return
Jon Perrittf094fef2016-03-07 01:41:59 -0600506 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600507 _, r.Err = client.Post(actionURL(client, id), b, nil, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500508 return
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700509}
510
Gleb37b56e82016-09-06 19:07:58 +0300511// ConfirmResize confirms a previous resize operation on a server.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700512// See Resize() for more details.
Jon Perritt3860b512016-03-29 12:01:48 -0500513func ConfirmResize(client *gophercloud.ServiceClient, id string) (r ActionResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600514 _, r.Err = client.Post(actionURL(client, id), map[string]interface{}{"confirmResize": nil}, nil, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100515 OkCodes: []int{201, 202, 204},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700516 })
jrperritt29ae6b32016-04-13 12:59:37 -0500517 return
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700518}
519
Gleb37b56e82016-09-06 19:07:58 +0300520// RevertResize cancels a previous resize operation on a server.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700521// See Resize() for more details.
Jon Perritt3860b512016-03-29 12:01:48 -0500522func RevertResize(client *gophercloud.ServiceClient, id string) (r ActionResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600523 _, r.Err = client.Post(actionURL(client, id), map[string]interface{}{"revertResize": nil}, nil, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500524 return
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700525}
Alex Gaynor39584a02014-10-28 13:59:21 -0700526
Alex Gaynor266e9332014-10-28 14:44:04 -0700527// RescueOptsBuilder is an interface that allows extensions to override the
528// default structure of a Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700529type RescueOptsBuilder interface {
530 ToServerRescueMap() (map[string]interface{}, error)
531}
532
Alex Gaynor266e9332014-10-28 14:44:04 -0700533// RescueOpts represents the configuration options used to control a Rescue
534// option.
Alex Gaynor39584a02014-10-28 13:59:21 -0700535type RescueOpts struct {
Alex Gaynor266e9332014-10-28 14:44:04 -0700536 // AdminPass is the desired administrative password for the instance in
Alex Gaynorcfec7722014-11-13 13:33:49 -0800537 // RESCUE mode. If it's left blank, the server will generate a password.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600538 AdminPass string `json:"adminPass,omitempty"`
Alex Gaynor39584a02014-10-28 13:59:21 -0700539}
540
Jon Perrittcc77da62014-11-16 13:14:21 -0700541// ToServerRescueMap formats a RescueOpts as a map that can be used as a JSON
Alex Gaynor266e9332014-10-28 14:44:04 -0700542// request body for the Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700543func (opts RescueOpts) ToServerRescueMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600544 return gophercloud.BuildRequestBody(opts, "rescue")
Alex Gaynor39584a02014-10-28 13:59:21 -0700545}
546
Alex Gaynor266e9332014-10-28 14:44:04 -0700547// Rescue instructs the provider to place the server into RESCUE mode.
Jon Perritt3860b512016-03-29 12:01:48 -0500548func Rescue(client *gophercloud.ServiceClient, id string, opts RescueOptsBuilder) (r RescueResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600549 b, err := opts.ToServerRescueMap()
550 if err != nil {
551 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500552 return
Jon Perrittf094fef2016-03-07 01:41:59 -0600553 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600554 _, r.Err = client.Post(actionURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100555 OkCodes: []int{200},
Alex Gaynor39584a02014-10-28 13:59:21 -0700556 })
jrperritt29ae6b32016-04-13 12:59:37 -0500557 return
Alex Gaynor39584a02014-10-28 13:59:21 -0700558}
Jon Perrittcc77da62014-11-16 13:14:21 -0700559
Jon Perritt789f8322014-11-21 08:20:04 -0700560// ResetMetadataOptsBuilder allows extensions to add additional parameters to the
561// Reset request.
562type ResetMetadataOptsBuilder interface {
563 ToMetadataResetMap() (map[string]interface{}, error)
Jon Perrittcc77da62014-11-16 13:14:21 -0700564}
565
Jon Perritt78c57ce2014-11-20 11:07:18 -0700566// MetadataOpts is a map that contains key-value pairs.
567type MetadataOpts map[string]string
Jon Perrittcc77da62014-11-16 13:14:21 -0700568
Jon Perritt789f8322014-11-21 08:20:04 -0700569// ToMetadataResetMap assembles a body for a Reset request based on the contents of a MetadataOpts.
570func (opts MetadataOpts) ToMetadataResetMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700571 return map[string]interface{}{"metadata": opts}, nil
572}
573
Jon Perritt78c57ce2014-11-20 11:07:18 -0700574// ToMetadataUpdateMap assembles a body for an Update request based on the contents of a MetadataOpts.
575func (opts MetadataOpts) ToMetadataUpdateMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700576 return map[string]interface{}{"metadata": opts}, nil
577}
578
Jon Perritt789f8322014-11-21 08:20:04 -0700579// ResetMetadata will create multiple new key-value pairs for the given server ID.
Jon Perrittcc77da62014-11-16 13:14:21 -0700580// Note: Using this operation will erase any already-existing metadata and create
581// the new metadata provided. To keep any already-existing metadata, use the
582// UpdateMetadatas or UpdateMetadata function.
Jon Perritt3860b512016-03-29 12:01:48 -0500583func ResetMetadata(client *gophercloud.ServiceClient, id string, opts ResetMetadataOptsBuilder) (r ResetMetadataResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600584 b, err := opts.ToMetadataResetMap()
Jon Perrittcc77da62014-11-16 13:14:21 -0700585 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600586 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500587 return
Jon Perrittcc77da62014-11-16 13:14:21 -0700588 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600589 _, r.Err = client.Put(metadataURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100590 OkCodes: []int{200},
Jon Perrittcc77da62014-11-16 13:14:21 -0700591 })
jrperritt29ae6b32016-04-13 12:59:37 -0500592 return
Jon Perrittcc77da62014-11-16 13:14:21 -0700593}
594
Jon Perritt78c57ce2014-11-20 11:07:18 -0700595// Metadata requests all the metadata for the given server ID.
Jon Perritt3860b512016-03-29 12:01:48 -0500596func Metadata(client *gophercloud.ServiceClient, id string) (r GetMetadataResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600597 _, r.Err = client.Get(metadataURL(client, id), &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500598 return
Jon Perrittcc77da62014-11-16 13:14:21 -0700599}
600
Jon Perritt78c57ce2014-11-20 11:07:18 -0700601// UpdateMetadataOptsBuilder allows extensions to add additional parameters to the
602// Create request.
603type UpdateMetadataOptsBuilder interface {
604 ToMetadataUpdateMap() (map[string]interface{}, error)
605}
606
607// UpdateMetadata updates (or creates) all the metadata specified by opts for the given server ID.
608// This operation does not affect already-existing metadata that is not specified
609// by opts.
Jon Perritt3860b512016-03-29 12:01:48 -0500610func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) (r UpdateMetadataResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600611 b, err := opts.ToMetadataUpdateMap()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700612 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600613 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500614 return
Jon Perritt78c57ce2014-11-20 11:07:18 -0700615 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600616 _, r.Err = client.Post(metadataURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100617 OkCodes: []int{200},
Jon Perritt78c57ce2014-11-20 11:07:18 -0700618 })
jrperritt29ae6b32016-04-13 12:59:37 -0500619 return
Jon Perritt78c57ce2014-11-20 11:07:18 -0700620}
621
622// MetadatumOptsBuilder allows extensions to add additional parameters to the
623// Create request.
624type MetadatumOptsBuilder interface {
625 ToMetadatumCreateMap() (map[string]interface{}, string, error)
626}
627
628// MetadatumOpts is a map of length one that contains a key-value pair.
629type MetadatumOpts map[string]string
630
631// ToMetadatumCreateMap assembles a body for a Create request based on the contents of a MetadataumOpts.
632func (opts MetadatumOpts) ToMetadatumCreateMap() (map[string]interface{}, string, error) {
633 if len(opts) != 1 {
Jon Perritt13808262016-03-09 00:50:12 -0600634 err := gophercloud.ErrInvalidInput{}
635 err.Argument = "servers.MetadatumOpts"
636 err.Info = "Must have 1 and only 1 key-value pair"
637 return nil, "", err
Jon Perritt78c57ce2014-11-20 11:07:18 -0700638 }
639 metadatum := map[string]interface{}{"meta": opts}
640 var key string
641 for k := range metadatum["meta"].(MetadatumOpts) {
642 key = k
643 }
644 return metadatum, key, nil
645}
646
647// 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 -0500648func CreateMetadatum(client *gophercloud.ServiceClient, id string, opts MetadatumOptsBuilder) (r CreateMetadatumResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600649 b, key, err := opts.ToMetadatumCreateMap()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700650 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600651 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500652 return
Jon Perritt78c57ce2014-11-20 11:07:18 -0700653 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600654 _, r.Err = client.Put(metadatumURL(client, id, key), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100655 OkCodes: []int{200},
Jon Perritt78c57ce2014-11-20 11:07:18 -0700656 })
jrperritt29ae6b32016-04-13 12:59:37 -0500657 return
Jon Perritt78c57ce2014-11-20 11:07:18 -0700658}
659
660// Metadatum requests the key-value pair with the given key for the given server ID.
Jon Perritt3860b512016-03-29 12:01:48 -0500661func Metadatum(client *gophercloud.ServiceClient, id, key string) (r GetMetadatumResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600662 _, r.Err = client.Get(metadatumURL(client, id, key), &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500663 return
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.
Jon Perritt3860b512016-03-29 12:01:48 -0500667func DeleteMetadatum(client *gophercloud.ServiceClient, id, key string) (r DeleteMetadatumResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600668 _, r.Err = client.Delete(metadatumURL(client, id, key), nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500669 return
Jon Perrittcc77da62014-11-16 13:14:21 -0700670}
Jon Perritt5cb49482015-02-19 12:19:58 -0700671
672// ListAddresses makes a request against the API to list the servers IP addresses.
673func ListAddresses(client *gophercloud.ServiceClient, id string) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600674 return pagination.NewPager(client, listAddressesURL(client, id), func(r pagination.PageResult) pagination.Page {
Jon Perritt5cb49482015-02-19 12:19:58 -0700675 return AddressPage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600676 })
Jon Perritt5cb49482015-02-19 12:19:58 -0700677}
Jon Perritt04d073c2015-02-19 21:46:23 -0700678
679// ListAddressesByNetwork makes a request against the API to list the servers IP addresses
680// for the given network.
681func ListAddressesByNetwork(client *gophercloud.ServiceClient, id, network string) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600682 return pagination.NewPager(client, listAddressesByNetworkURL(client, id, network), func(r pagination.PageResult) pagination.Page {
Jon Perritt04d073c2015-02-19 21:46:23 -0700683 return NetworkAddressPage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600684 })
Jon Perritt04d073c2015-02-19 21:46:23 -0700685}
einarf2fc665e2015-04-16 20:16:21 +0000686
Jon Perrittdb0ae142016-03-13 00:33:41 -0600687// CreateImageOptsBuilder is the interface types must satisfy in order to be
688// used as CreateImage options
einarf4e5fdaf2015-04-16 23:14:59 +0000689type CreateImageOptsBuilder interface {
690 ToServerCreateImageMap() (map[string]interface{}, error)
einarf2fc665e2015-04-16 20:16:21 +0000691}
692
Jon Perrittdb0ae142016-03-13 00:33:41 -0600693// CreateImageOpts satisfies the CreateImageOptsBuilder
694type CreateImageOpts struct {
695 // Name of the image/snapshot
696 Name string `json:"name" required:"true"`
697 // Metadata contains key-value pairs (up to 255 bytes each) to attach to the created image.
698 Metadata map[string]string `json:"metadata,omitempty"`
699}
700
einarf4e5fdaf2015-04-16 23:14:59 +0000701// ToServerCreateImageMap formats a CreateImageOpts structure into a request body.
702func (opts CreateImageOpts) ToServerCreateImageMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600703 return gophercloud.BuildRequestBody(opts, "createImage")
einarf2fc665e2015-04-16 20:16:21 +0000704}
705
einarf4e5fdaf2015-04-16 23:14:59 +0000706// 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 -0500707func CreateImage(client *gophercloud.ServiceClient, id string, opts CreateImageOptsBuilder) (r CreateImageResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600708 b, err := opts.ToServerCreateImageMap()
einarf2fc665e2015-04-16 20:16:21 +0000709 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600710 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500711 return
einarf2fc665e2015-04-16 20:16:21 +0000712 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600713 resp, err := client.Post(actionURL(client, id), b, nil, &gophercloud.RequestOpts{
einarf2fc665e2015-04-16 20:16:21 +0000714 OkCodes: []int{202},
715 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600716 r.Err = err
717 r.Header = resp.Header
jrperritt29ae6b32016-04-13 12:59:37 -0500718 return
einarf2fc665e2015-04-16 20:16:21 +0000719}
Jon Perritt6b0a8832015-06-04 14:32:30 -0600720
721// IDFromName is a convienience function that returns a server's ID given its name.
722func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600723 count := 0
724 id := ""
Jon Perrittf094fef2016-03-07 01:41:59 -0600725 allPages, err := List(client, nil).AllPages()
726 if err != nil {
727 return "", err
728 }
Jon Perritt6b0a8832015-06-04 14:32:30 -0600729
Jon Perrittf094fef2016-03-07 01:41:59 -0600730 all, err := ExtractServers(allPages)
731 if err != nil {
732 return "", err
733 }
734
735 for _, f := range all {
736 if f.Name == name {
737 count++
738 id = f.ID
739 }
740 }
741
742 switch count {
Jon Perritt6b0a8832015-06-04 14:32:30 -0600743 case 0:
Jon Perrittdb0ae142016-03-13 00:33:41 -0600744 return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "server"}
Jon Perritt6b0a8832015-06-04 14:32:30 -0600745 case 1:
Jon Perrittf094fef2016-03-07 01:41:59 -0600746 return id, nil
Jon Perritt6b0a8832015-06-04 14:32:30 -0600747 default:
Jon Perrittdb0ae142016-03-13 00:33:41 -0600748 return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "server"}
Jon Perritt6b0a8832015-06-04 14:32:30 -0600749 }
750}
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100751
Rickard von Essenc3d49b72016-02-16 20:59:18 +0100752// GetPassword makes a request against the nova API to get the encrypted administrative password.
jrperritt2f93a632016-04-13 15:41:20 -0500753func GetPassword(client *gophercloud.ServiceClient, serverId string) (r GetPasswordResult) {
754 _, r.Err = client.Get(passwordURL(client, serverId), &r.Body, nil)
755 return
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100756}