blob: 0210c2ac565c7e19e42ad649be1c293b33462bc6 [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) {
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 {
193 encoded := base64.StdEncoding.EncodeToString(opts.UserData)
Jon Perritt01618ee2016-03-09 03:04:06 -0600194 b["user_data"] = &encoded
Jon Perritt7b9671c2015-02-01 22:03:14 -0700195 }
Ash Wilson6a310e02014-09-29 08:24:02 -0400196
197 if len(opts.SecurityGroups) > 0 {
198 securityGroups := make([]map[string]interface{}, len(opts.SecurityGroups))
199 for i, groupName := range opts.SecurityGroups {
200 securityGroups[i] = map[string]interface{}{"name": groupName}
201 }
Jon Perritt01618ee2016-03-09 03:04:06 -0600202 b["security_groups"] = securityGroups
Ash Wilson6a310e02014-09-29 08:24:02 -0400203 }
Jon Perritt2a7797d2014-10-21 15:08:43 -0500204
Ash Wilson6a310e02014-09-29 08:24:02 -0400205 if len(opts.Networks) > 0 {
206 networks := make([]map[string]interface{}, len(opts.Networks))
207 for i, net := range opts.Networks {
208 networks[i] = make(map[string]interface{})
209 if net.UUID != "" {
210 networks[i]["uuid"] = net.UUID
211 }
212 if net.Port != "" {
213 networks[i]["port"] = net.Port
214 }
215 if net.FixedIP != "" {
216 networks[i]["fixed_ip"] = net.FixedIP
217 }
218 }
Jon Perritt01618ee2016-03-09 03:04:06 -0600219 b["networks"] = networks
Kevin Pike92e10b52015-04-10 15:16:57 -0700220 }
221
jrperrittb1013232016-02-10 19:01:53 -0600222 // If ImageRef isn't provided, use ImageName to ascertain the image ID.
223 if opts.ImageRef == "" {
224 if opts.ImageName == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600225 err := ErrNeitherImageIDNorImageNameProvided{}
Jon Perrittf094fef2016-03-07 01:41:59 -0600226 err.Argument = "ImageRef/ImageName"
227 return nil, err
jrperrittb1013232016-02-10 19:01:53 -0600228 }
jrperritt0d7ed5d2016-08-16 11:23:26 -0500229 if sc == nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600230 err := ErrNoClientProvidedForIDByName{}
Jon Perrittf094fef2016-03-07 01:41:59 -0600231 err.Argument = "ServiceClient"
232 return nil, err
Jon Perritt994370e2016-02-18 15:23:34 -0600233 }
jrperritt0d7ed5d2016-08-16 11:23:26 -0500234 imageID, err := images.IDFromName(sc, opts.ImageName)
jrperrittb1013232016-02-10 19:01:53 -0600235 if err != nil {
236 return nil, err
237 }
Jon Perritt01618ee2016-03-09 03:04:06 -0600238 b["imageRef"] = imageID
jrperrittb1013232016-02-10 19:01:53 -0600239 }
240
241 // If FlavorRef isn't provided, use FlavorName to ascertain the flavor ID.
242 if opts.FlavorRef == "" {
243 if opts.FlavorName == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600244 err := ErrNeitherFlavorIDNorFlavorNameProvided{}
Jon Perrittf094fef2016-03-07 01:41:59 -0600245 err.Argument = "FlavorRef/FlavorName"
246 return nil, err
jrperrittb1013232016-02-10 19:01:53 -0600247 }
jrperritt0d7ed5d2016-08-16 11:23:26 -0500248 if sc == nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600249 err := ErrNoClientProvidedForIDByName{}
250 err.Argument = "ServiceClient"
Jon Perrittf094fef2016-03-07 01:41:59 -0600251 return nil, err
Jon Perritt994370e2016-02-18 15:23:34 -0600252 }
jrperritt0d7ed5d2016-08-16 11:23:26 -0500253 flavorID, err := flavors.IDFromName(sc, opts.FlavorName)
jrperrittb1013232016-02-10 19:01:53 -0600254 if err != nil {
255 return nil, err
256 }
Jon Perritt01618ee2016-03-09 03:04:06 -0600257 b["flavorRef"] = flavorID
jrperrittb1013232016-02-10 19:01:53 -0600258 }
259
Jon Perritt01618ee2016-03-09 03:04:06 -0600260 return map[string]interface{}{"server": b}, nil
Ash Wilson6a310e02014-09-29 08:24:02 -0400261}
262
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800263// Create requests a server to be provisioned to the user in the current tenant.
Jon Perritt3860b512016-03-29 12:01:48 -0500264func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
Jon Perritt4149d7c2014-10-23 21:23:46 -0500265 reqBody, err := opts.ToServerCreateMap()
266 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600267 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500268 return
Jon Perritt4149d7c2014-10-23 21:23:46 -0500269 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600270 _, r.Err = client.Post(listURL(client), reqBody, &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500271 return
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800272}
273
274// Delete requests that a server previously provisioned be removed from your account.
Jon Perritt3860b512016-03-29 12:01:48 -0500275func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600276 _, r.Err = client.Delete(deleteURL(client, id), nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500277 return
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
Jon Perritt3860b512016-03-29 12:01:48 -0500281func ForceDelete(client *gophercloud.ServiceClient, id string) (r ActionResult) {
Jon Perritt01618ee2016-03-09 03:04:06 -0600282 _, r.Err = client.Post(actionURL(client, id), map[string]interface{}{"forceDelete": ""}, nil, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500283 return
Ian Duffy370c4302016-01-21 10:44:56 +0000284}
285
Ash Wilson7ddf0362014-09-17 10:59:09 -0400286// Get requests details on a single server, by ID.
Jon Perritt3860b512016-03-29 12:01:48 -0500287func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600288 _, r.Err = client.Get(getURL(client, id), &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100289 OkCodes: []int{200, 203},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800290 })
jrperritt29ae6b32016-04-13 12:59:37 -0500291 return
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800292}
293
Alex Gaynora6d5f9f2014-10-27 10:52:32 -0700294// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
Jon Perritt82048212014-10-13 22:33:13 -0500295type UpdateOptsBuilder interface {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600296 ToServerUpdateMap() (map[string]interface{}, error)
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400297}
298
299// UpdateOpts specifies the base attributes that may be updated on an existing server.
300type UpdateOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600301 // Name changes the displayed name of the server.
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400302 // The server host name will *not* change.
303 // Server names are not constrained to be unique, even within the same tenant.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600304 Name string `json:"name,omitempty"`
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400305
Jon Perrittdb0ae142016-03-13 00:33:41 -0600306 // AccessIPv4 provides a new IPv4 address for the instance.
307 AccessIPv4 string `json:"accessIPv4,omitempty"`
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400308
Jon Perrittdb0ae142016-03-13 00:33:41 -0600309 // AccessIPv6 provides a new IPv6 address for the instance.
310 AccessIPv6 string `json:"accessIPv6,omitempty"`
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400311}
312
Ash Wilsone45c9732014-09-29 10:54:12 -0400313// ToServerUpdateMap formats an UpdateOpts structure into a request body.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600314func (opts UpdateOpts) ToServerUpdateMap() (map[string]interface{}, error) {
315 return gophercloud.BuildRequestBody(opts, "server")
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400316}
317
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800318// Update requests that various attributes of the indicated server be changed.
Jon Perritt3860b512016-03-29 12:01:48 -0500319func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600320 b, err := opts.ToServerUpdateMap()
321 if err != nil {
322 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500323 return
Jon Perrittdb0ae142016-03-13 00:33:41 -0600324 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600325 _, r.Err = client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100326 OkCodes: []int{200},
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800327 })
jrperritt29ae6b32016-04-13 12:59:37 -0500328 return
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800329}
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700330
Ash Wilson01626a32014-09-17 10:38:07 -0400331// ChangeAdminPassword alters the administrator or root password for a specified server.
Jon Perritt3860b512016-03-29 12:01:48 -0500332func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) (r ActionResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600333 b := map[string]interface{}{
334 "changePassword": map[string]string{
335 "adminPass": newPassword,
336 },
337 }
338 _, r.Err = client.Post(actionURL(client, id), b, nil, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500339 return
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700340}
341
Ash Wilson01626a32014-09-17 10:38:07 -0400342// RebootMethod describes the mechanisms by which a server reboot can be requested.
343type RebootMethod string
344
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700345// These constants determine how a server should be rebooted.
346// See the Reboot() function for further details.
347const (
Ash Wilson01626a32014-09-17 10:38:07 -0400348 SoftReboot RebootMethod = "SOFT"
349 HardReboot RebootMethod = "HARD"
350 OSReboot = SoftReboot
351 PowerCycle = HardReboot
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700352)
353
Jon Perrittdb0ae142016-03-13 00:33:41 -0600354// RebootOptsBuilder is an interface that options must satisfy in order to be
355// used when rebooting a server instance
Jon Perrittf094fef2016-03-07 01:41:59 -0600356type RebootOptsBuilder interface {
357 ToServerRebootMap() (map[string]interface{}, error)
358}
359
Jon Perrittdb0ae142016-03-13 00:33:41 -0600360// RebootOpts satisfies the RebootOptsBuilder interface
Jon Perrittf094fef2016-03-07 01:41:59 -0600361type RebootOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600362 Type RebootMethod `json:"type" required:"true"`
Jon Perrittf094fef2016-03-07 01:41:59 -0600363}
364
Jon Perrittdb0ae142016-03-13 00:33:41 -0600365// ToServerRebootMap allows RebootOpts to satisfiy the RebootOptsBuilder
366// interface
Jon Perrittf094fef2016-03-07 01:41:59 -0600367func (opts *RebootOpts) ToServerRebootMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600368 return gophercloud.BuildRequestBody(opts, "reboot")
Jon Perrittf094fef2016-03-07 01:41:59 -0600369}
370
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700371// Reboot requests that a given server reboot.
372// Two methods exist for rebooting a server:
373//
Jon Perrittdb0ae142016-03-13 00:33:41 -0600374// 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 -0400375// terminating it at the hypervisor level.
376// It's done. Caput. Full stop.
Jon Perrittf094fef2016-03-07 01:41:59 -0600377// Then, after a brief while, power is rtored or the VM instance rtarted.
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700378//
Jon Perrittf094fef2016-03-07 01:41:59 -0600379// SoftReboot (aka OSReboot) simply tells the OS to rtart under its own procedur.
380// 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 -0500381func Reboot(client *gophercloud.ServiceClient, id string, opts RebootOptsBuilder) (r ActionResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600382 b, err := opts.ToServerRebootMap()
Jon Perrittf094fef2016-03-07 01:41:59 -0600383 if err != nil {
384 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500385 return
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700386 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600387 _, r.Err = client.Post(actionURL(client, id), b, nil, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500388 return
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700389}
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700390
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200391// RebuildOptsBuilder is an interface that allows extensions to override the
392// default behaviour of rebuild options
393type RebuildOptsBuilder interface {
394 ToServerRebuildMap() (map[string]interface{}, error)
395}
396
397// RebuildOpts represents the configuration options used in a server rebuild
398// operation
399type RebuildOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600400 // The server's admin password
401 AdminPass string `json:"adminPass" required:"true"`
402 // The ID of the image you want your server to be provisioned on
403 ImageID string `json:"imageRef"`
404 ImageName string `json:"-"`
405 //ImageName string `json:"-"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200406 // Name to set the server to
Jon Perrittdb0ae142016-03-13 00:33:41 -0600407 Name string `json:"name,omitempty"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200408 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600409 AccessIPv4 string `json:"accessIPv4,omitempty"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200410 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600411 AccessIPv6 string `json:"accessIPv6,omitempty"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200412 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600413 Metadata map[string]string `json:"metadata,omitempty"`
Kevin Pike92e10b52015-04-10 15:16:57 -0700414 // Personality [optional] includes files to inject into the server at launch.
415 // Rebuild will base64-encode file contents for you.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600416 Personality Personality `json:"personality,omitempty"`
417 ServiceClient *gophercloud.ServiceClient `json:"-"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200418}
419
420// ToServerRebuildMap formats a RebuildOpts struct into a map for use in JSON
421func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600422 b, err := gophercloud.BuildRequestBody(opts, "")
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200423 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600424 return nil, err
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200425 }
426
Jon Perrittdb0ae142016-03-13 00:33:41 -0600427 // If ImageRef isn't provided, use ImageName to ascertain the image ID.
428 if opts.ImageID == "" {
429 if opts.ImageName == "" {
430 err := ErrNeitherImageIDNorImageNameProvided{}
431 err.Argument = "ImageRef/ImageName"
432 return nil, err
433 }
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 Perritt12395212016-02-24 10:41:17 -0600444 }
445
Jon Perrittdb0ae142016-03-13 00:33:41 -0600446 return map[string]interface{}{"rebuild": b}, nil
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200447}
448
449// Rebuild will reprovision the server according to the configuration options
450// provided in the RebuildOpts struct.
Jon Perritt3860b512016-03-29 12:01:48 -0500451func Rebuild(client *gophercloud.ServiceClient, id string, opts RebuildOptsBuilder) (r RebuildResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600452 b, err := opts.ToServerRebuildMap()
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200453 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600454 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500455 return
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700456 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600457 _, r.Err = client.Post(actionURL(client, id), b, &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500458 return
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700459}
460
Ash Wilson5f7cf182014-10-23 08:35:24 -0400461// ResizeOptsBuilder is an interface that allows extensions to override the default structure of
462// a Resize request.
463type ResizeOptsBuilder interface {
464 ToServerResizeMap() (map[string]interface{}, error)
465}
466
467// ResizeOpts represents the configuration options used to control a Resize operation.
468type ResizeOpts struct {
469 // FlavorRef is the ID of the flavor you wish your server to become.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600470 FlavorRef string `json:"flavorRef" required:"true"`
Ash Wilson5f7cf182014-10-23 08:35:24 -0400471}
472
Alex Gaynor266e9332014-10-28 14:44:04 -0700473// 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 -0400474// Resize request.
475func (opts ResizeOpts) ToServerResizeMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600476 return gophercloud.BuildRequestBody(opts, "resize")
Ash Wilson5f7cf182014-10-23 08:35:24 -0400477}
478
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700479// Resize instructs the provider to change the flavor of the server.
Ash Wilson01626a32014-09-17 10:38:07 -0400480// Note that this implies rebuilding it.
Gleb37b56e82016-09-06 19:07:58 +0300481// Unfortunately, one cannot pass rebuild parameters to the resize function.
482// When the resize completes, the server will be in RESIZE_VERIFY state.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700483// While in this state, you can explore the use of the new server's configuration.
Gleb37b56e82016-09-06 19:07:58 +0300484// If you like it, call ConfirmResize() to commit the resize permanently.
485// Otherwise, call RevertResize() to restore the old configuration.
Jon Perritt3860b512016-03-29 12:01:48 -0500486func Resize(client *gophercloud.ServiceClient, id string, opts ResizeOptsBuilder) (r ActionResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600487 b, err := opts.ToServerResizeMap()
488 if err != nil {
489 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500490 return
Jon Perrittf094fef2016-03-07 01:41:59 -0600491 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600492 _, r.Err = client.Post(actionURL(client, id), b, nil, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500493 return
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700494}
495
Gleb37b56e82016-09-06 19:07:58 +0300496// ConfirmResize confirms a previous resize operation on a server.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700497// See Resize() for more details.
Jon Perritt3860b512016-03-29 12:01:48 -0500498func ConfirmResize(client *gophercloud.ServiceClient, id string) (r ActionResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600499 _, r.Err = client.Post(actionURL(client, id), map[string]interface{}{"confirmResize": nil}, nil, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100500 OkCodes: []int{201, 202, 204},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700501 })
jrperritt29ae6b32016-04-13 12:59:37 -0500502 return
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700503}
504
Gleb37b56e82016-09-06 19:07:58 +0300505// RevertResize cancels a previous resize operation on a server.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700506// See Resize() for more details.
Jon Perritt3860b512016-03-29 12:01:48 -0500507func RevertResize(client *gophercloud.ServiceClient, id string) (r ActionResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600508 _, r.Err = client.Post(actionURL(client, id), map[string]interface{}{"revertResize": nil}, nil, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500509 return
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700510}
Alex Gaynor39584a02014-10-28 13:59:21 -0700511
Alex Gaynor266e9332014-10-28 14:44:04 -0700512// RescueOptsBuilder is an interface that allows extensions to override the
513// default structure of a Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700514type RescueOptsBuilder interface {
515 ToServerRescueMap() (map[string]interface{}, error)
516}
517
Alex Gaynor266e9332014-10-28 14:44:04 -0700518// RescueOpts represents the configuration options used to control a Rescue
519// option.
Alex Gaynor39584a02014-10-28 13:59:21 -0700520type RescueOpts struct {
Alex Gaynor266e9332014-10-28 14:44:04 -0700521 // AdminPass is the desired administrative password for the instance in
Alex Gaynorcfec7722014-11-13 13:33:49 -0800522 // RESCUE mode. If it's left blank, the server will generate a password.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600523 AdminPass string `json:"adminPass,omitempty"`
Alex Gaynor39584a02014-10-28 13:59:21 -0700524}
525
Jon Perrittcc77da62014-11-16 13:14:21 -0700526// ToServerRescueMap formats a RescueOpts as a map that can be used as a JSON
Alex Gaynor266e9332014-10-28 14:44:04 -0700527// request body for the Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700528func (opts RescueOpts) ToServerRescueMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600529 return gophercloud.BuildRequestBody(opts, "rescue")
Alex Gaynor39584a02014-10-28 13:59:21 -0700530}
531
Alex Gaynor266e9332014-10-28 14:44:04 -0700532// Rescue instructs the provider to place the server into RESCUE mode.
Jon Perritt3860b512016-03-29 12:01:48 -0500533func Rescue(client *gophercloud.ServiceClient, id string, opts RescueOptsBuilder) (r RescueResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600534 b, err := opts.ToServerRescueMap()
535 if err != nil {
536 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500537 return
Jon Perrittf094fef2016-03-07 01:41:59 -0600538 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600539 _, r.Err = client.Post(actionURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100540 OkCodes: []int{200},
Alex Gaynor39584a02014-10-28 13:59:21 -0700541 })
jrperritt29ae6b32016-04-13 12:59:37 -0500542 return
Alex Gaynor39584a02014-10-28 13:59:21 -0700543}
Jon Perrittcc77da62014-11-16 13:14:21 -0700544
Jon Perritt789f8322014-11-21 08:20:04 -0700545// ResetMetadataOptsBuilder allows extensions to add additional parameters to the
546// Reset request.
547type ResetMetadataOptsBuilder interface {
548 ToMetadataResetMap() (map[string]interface{}, error)
Jon Perrittcc77da62014-11-16 13:14:21 -0700549}
550
Jon Perritt78c57ce2014-11-20 11:07:18 -0700551// MetadataOpts is a map that contains key-value pairs.
552type MetadataOpts map[string]string
Jon Perrittcc77da62014-11-16 13:14:21 -0700553
Jon Perritt789f8322014-11-21 08:20:04 -0700554// ToMetadataResetMap assembles a body for a Reset request based on the contents of a MetadataOpts.
555func (opts MetadataOpts) ToMetadataResetMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700556 return map[string]interface{}{"metadata": opts}, nil
557}
558
Jon Perritt78c57ce2014-11-20 11:07:18 -0700559// ToMetadataUpdateMap assembles a body for an Update request based on the contents of a MetadataOpts.
560func (opts MetadataOpts) ToMetadataUpdateMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700561 return map[string]interface{}{"metadata": opts}, nil
562}
563
Jon Perritt789f8322014-11-21 08:20:04 -0700564// ResetMetadata will create multiple new key-value pairs for the given server ID.
Jon Perrittcc77da62014-11-16 13:14:21 -0700565// Note: Using this operation will erase any already-existing metadata and create
566// the new metadata provided. To keep any already-existing metadata, use the
567// UpdateMetadatas or UpdateMetadata function.
Jon Perritt3860b512016-03-29 12:01:48 -0500568func ResetMetadata(client *gophercloud.ServiceClient, id string, opts ResetMetadataOptsBuilder) (r ResetMetadataResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600569 b, err := opts.ToMetadataResetMap()
Jon Perrittcc77da62014-11-16 13:14:21 -0700570 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600571 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500572 return
Jon Perrittcc77da62014-11-16 13:14:21 -0700573 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600574 _, r.Err = client.Put(metadataURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100575 OkCodes: []int{200},
Jon Perrittcc77da62014-11-16 13:14:21 -0700576 })
jrperritt29ae6b32016-04-13 12:59:37 -0500577 return
Jon Perrittcc77da62014-11-16 13:14:21 -0700578}
579
Jon Perritt78c57ce2014-11-20 11:07:18 -0700580// Metadata requests all the metadata for the given server ID.
Jon Perritt3860b512016-03-29 12:01:48 -0500581func Metadata(client *gophercloud.ServiceClient, id string) (r GetMetadataResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600582 _, r.Err = client.Get(metadataURL(client, id), &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500583 return
Jon Perrittcc77da62014-11-16 13:14:21 -0700584}
585
Jon Perritt78c57ce2014-11-20 11:07:18 -0700586// UpdateMetadataOptsBuilder allows extensions to add additional parameters to the
587// Create request.
588type UpdateMetadataOptsBuilder interface {
589 ToMetadataUpdateMap() (map[string]interface{}, error)
590}
591
592// UpdateMetadata updates (or creates) all the metadata specified by opts for the given server ID.
593// This operation does not affect already-existing metadata that is not specified
594// by opts.
Jon Perritt3860b512016-03-29 12:01:48 -0500595func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) (r UpdateMetadataResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600596 b, err := opts.ToMetadataUpdateMap()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700597 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600598 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500599 return
Jon Perritt78c57ce2014-11-20 11:07:18 -0700600 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600601 _, r.Err = client.Post(metadataURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100602 OkCodes: []int{200},
Jon Perritt78c57ce2014-11-20 11:07:18 -0700603 })
jrperritt29ae6b32016-04-13 12:59:37 -0500604 return
Jon Perritt78c57ce2014-11-20 11:07:18 -0700605}
606
607// MetadatumOptsBuilder allows extensions to add additional parameters to the
608// Create request.
609type MetadatumOptsBuilder interface {
610 ToMetadatumCreateMap() (map[string]interface{}, string, error)
611}
612
613// MetadatumOpts is a map of length one that contains a key-value pair.
614type MetadatumOpts map[string]string
615
616// ToMetadatumCreateMap assembles a body for a Create request based on the contents of a MetadataumOpts.
617func (opts MetadatumOpts) ToMetadatumCreateMap() (map[string]interface{}, string, error) {
618 if len(opts) != 1 {
Jon Perritt13808262016-03-09 00:50:12 -0600619 err := gophercloud.ErrInvalidInput{}
620 err.Argument = "servers.MetadatumOpts"
621 err.Info = "Must have 1 and only 1 key-value pair"
622 return nil, "", err
Jon Perritt78c57ce2014-11-20 11:07:18 -0700623 }
624 metadatum := map[string]interface{}{"meta": opts}
625 var key string
626 for k := range metadatum["meta"].(MetadatumOpts) {
627 key = k
628 }
629 return metadatum, key, nil
630}
631
632// 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 -0500633func CreateMetadatum(client *gophercloud.ServiceClient, id string, opts MetadatumOptsBuilder) (r CreateMetadatumResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600634 b, key, err := opts.ToMetadatumCreateMap()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700635 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600636 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500637 return
Jon Perritt78c57ce2014-11-20 11:07:18 -0700638 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600639 _, r.Err = client.Put(metadatumURL(client, id, key), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100640 OkCodes: []int{200},
Jon Perritt78c57ce2014-11-20 11:07:18 -0700641 })
jrperritt29ae6b32016-04-13 12:59:37 -0500642 return
Jon Perritt78c57ce2014-11-20 11:07:18 -0700643}
644
645// Metadatum requests the key-value pair with the given key for the given server ID.
Jon Perritt3860b512016-03-29 12:01:48 -0500646func Metadatum(client *gophercloud.ServiceClient, id, key string) (r GetMetadatumResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600647 _, r.Err = client.Get(metadatumURL(client, id, key), &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500648 return
Jon Perritt78c57ce2014-11-20 11:07:18 -0700649}
650
651// DeleteMetadatum will delete the key-value pair with the given key for the given server ID.
Jon Perritt3860b512016-03-29 12:01:48 -0500652func DeleteMetadatum(client *gophercloud.ServiceClient, id, key string) (r DeleteMetadatumResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600653 _, r.Err = client.Delete(metadatumURL(client, id, key), nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500654 return
Jon Perrittcc77da62014-11-16 13:14:21 -0700655}
Jon Perritt5cb49482015-02-19 12:19:58 -0700656
657// ListAddresses makes a request against the API to list the servers IP addresses.
658func ListAddresses(client *gophercloud.ServiceClient, id string) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600659 return pagination.NewPager(client, listAddressesURL(client, id), func(r pagination.PageResult) pagination.Page {
Jon Perritt5cb49482015-02-19 12:19:58 -0700660 return AddressPage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600661 })
Jon Perritt5cb49482015-02-19 12:19:58 -0700662}
Jon Perritt04d073c2015-02-19 21:46:23 -0700663
664// ListAddressesByNetwork makes a request against the API to list the servers IP addresses
665// for the given network.
666func ListAddressesByNetwork(client *gophercloud.ServiceClient, id, network string) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600667 return pagination.NewPager(client, listAddressesByNetworkURL(client, id, network), func(r pagination.PageResult) pagination.Page {
Jon Perritt04d073c2015-02-19 21:46:23 -0700668 return NetworkAddressPage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600669 })
Jon Perritt04d073c2015-02-19 21:46:23 -0700670}
einarf2fc665e2015-04-16 20:16:21 +0000671
Jon Perrittdb0ae142016-03-13 00:33:41 -0600672// CreateImageOptsBuilder is the interface types must satisfy in order to be
673// used as CreateImage options
einarf4e5fdaf2015-04-16 23:14:59 +0000674type CreateImageOptsBuilder interface {
675 ToServerCreateImageMap() (map[string]interface{}, error)
einarf2fc665e2015-04-16 20:16:21 +0000676}
677
Jon Perrittdb0ae142016-03-13 00:33:41 -0600678// CreateImageOpts satisfies the CreateImageOptsBuilder
679type CreateImageOpts struct {
680 // Name of the image/snapshot
681 Name string `json:"name" required:"true"`
682 // Metadata contains key-value pairs (up to 255 bytes each) to attach to the created image.
683 Metadata map[string]string `json:"metadata,omitempty"`
684}
685
einarf4e5fdaf2015-04-16 23:14:59 +0000686// ToServerCreateImageMap formats a CreateImageOpts structure into a request body.
687func (opts CreateImageOpts) ToServerCreateImageMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600688 return gophercloud.BuildRequestBody(opts, "createImage")
einarf2fc665e2015-04-16 20:16:21 +0000689}
690
einarf4e5fdaf2015-04-16 23:14:59 +0000691// 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 -0500692func CreateImage(client *gophercloud.ServiceClient, id string, opts CreateImageOptsBuilder) (r CreateImageResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600693 b, err := opts.ToServerCreateImageMap()
einarf2fc665e2015-04-16 20:16:21 +0000694 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600695 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500696 return
einarf2fc665e2015-04-16 20:16:21 +0000697 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600698 resp, err := client.Post(actionURL(client, id), b, nil, &gophercloud.RequestOpts{
einarf2fc665e2015-04-16 20:16:21 +0000699 OkCodes: []int{202},
700 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600701 r.Err = err
702 r.Header = resp.Header
jrperritt29ae6b32016-04-13 12:59:37 -0500703 return
einarf2fc665e2015-04-16 20:16:21 +0000704}
Jon Perritt6b0a8832015-06-04 14:32:30 -0600705
706// IDFromName is a convienience function that returns a server's ID given its name.
707func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600708 count := 0
709 id := ""
Jon Perrittf094fef2016-03-07 01:41:59 -0600710 allPages, err := List(client, nil).AllPages()
711 if err != nil {
712 return "", err
713 }
Jon Perritt6b0a8832015-06-04 14:32:30 -0600714
Jon Perrittf094fef2016-03-07 01:41:59 -0600715 all, err := ExtractServers(allPages)
716 if err != nil {
717 return "", err
718 }
719
720 for _, f := range all {
721 if f.Name == name {
722 count++
723 id = f.ID
724 }
725 }
726
727 switch count {
Jon Perritt6b0a8832015-06-04 14:32:30 -0600728 case 0:
Jon Perrittdb0ae142016-03-13 00:33:41 -0600729 return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "server"}
Jon Perritt6b0a8832015-06-04 14:32:30 -0600730 case 1:
Jon Perrittf094fef2016-03-07 01:41:59 -0600731 return id, nil
Jon Perritt6b0a8832015-06-04 14:32:30 -0600732 default:
Jon Perrittdb0ae142016-03-13 00:33:41 -0600733 return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "server"}
Jon Perritt6b0a8832015-06-04 14:32:30 -0600734 }
735}
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100736
Rickard von Essenc3d49b72016-02-16 20:59:18 +0100737// GetPassword makes a request against the nova API to get the encrypted administrative password.
jrperritt2f93a632016-04-13 15:41:20 -0500738func GetPassword(client *gophercloud.ServiceClient, serverId string) (r GetPasswordResult) {
739 _, r.Err = client.Get(passwordURL(client, serverId), &r.Body, nil)
740 return
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100741}