blob: 4ec2cf078dca83e33e1e181127937de42a366993 [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.
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.
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
Joe Topjian50cdddf2016-09-16 10:56:09 -0600222 // If ImageRef isn't provided, check if ImageName was provided to ascertain
223 // the image ID.
jrperrittb1013232016-02-10 19:01:53 -0600224 if opts.ImageRef == "" {
Joe Topjian50cdddf2016-09-16 10:56:09 -0600225 if opts.ImageName != "" {
226 if sc == nil {
227 err := ErrNoClientProvidedForIDByName{}
228 err.Argument = "ServiceClient"
229 return nil, err
230 }
231 imageID, err := images.IDFromName(sc, opts.ImageName)
232 if err != nil {
233 return nil, err
234 }
235 b["imageRef"] = imageID
jrperrittb1013232016-02-10 19:01:53 -0600236 }
jrperrittb1013232016-02-10 19:01:53 -0600237 }
238
239 // If FlavorRef isn't provided, use FlavorName to ascertain the flavor ID.
240 if opts.FlavorRef == "" {
241 if opts.FlavorName == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600242 err := ErrNeitherFlavorIDNorFlavorNameProvided{}
Jon Perrittf094fef2016-03-07 01:41:59 -0600243 err.Argument = "FlavorRef/FlavorName"
244 return nil, err
jrperrittb1013232016-02-10 19:01:53 -0600245 }
jrperritt0d7ed5d2016-08-16 11:23:26 -0500246 if sc == nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600247 err := ErrNoClientProvidedForIDByName{}
248 err.Argument = "ServiceClient"
Jon Perrittf094fef2016-03-07 01:41:59 -0600249 return nil, err
Jon Perritt994370e2016-02-18 15:23:34 -0600250 }
jrperritt0d7ed5d2016-08-16 11:23:26 -0500251 flavorID, err := flavors.IDFromName(sc, opts.FlavorName)
jrperrittb1013232016-02-10 19:01:53 -0600252 if err != nil {
253 return nil, err
254 }
Jon Perritt01618ee2016-03-09 03:04:06 -0600255 b["flavorRef"] = flavorID
jrperrittb1013232016-02-10 19:01:53 -0600256 }
257
Jon Perritt01618ee2016-03-09 03:04:06 -0600258 return map[string]interface{}{"server": b}, nil
Ash Wilson6a310e02014-09-29 08:24:02 -0400259}
260
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800261// Create requests a server to be provisioned to the user in the current tenant.
Jon Perritt3860b512016-03-29 12:01:48 -0500262func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
Jon Perritt4149d7c2014-10-23 21:23:46 -0500263 reqBody, err := opts.ToServerCreateMap()
264 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600265 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500266 return
Jon Perritt4149d7c2014-10-23 21:23:46 -0500267 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600268 _, r.Err = client.Post(listURL(client), reqBody, &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500269 return
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800270}
271
272// Delete requests that a server previously provisioned be removed from your account.
Jon Perritt3860b512016-03-29 12:01:48 -0500273func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600274 _, r.Err = client.Delete(deleteURL(client, id), nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500275 return
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800276}
277
Jon Perritt01618ee2016-03-09 03:04:06 -0600278// ForceDelete forces the deletion of a server
Jon Perritt3860b512016-03-29 12:01:48 -0500279func ForceDelete(client *gophercloud.ServiceClient, id string) (r ActionResult) {
Jon Perritt01618ee2016-03-09 03:04:06 -0600280 _, r.Err = client.Post(actionURL(client, id), map[string]interface{}{"forceDelete": ""}, nil, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500281 return
Ian Duffy370c4302016-01-21 10:44:56 +0000282}
283
Ash Wilson7ddf0362014-09-17 10:59:09 -0400284// Get requests details on a single server, by ID.
Jon Perritt3860b512016-03-29 12:01:48 -0500285func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600286 _, r.Err = client.Get(getURL(client, id), &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100287 OkCodes: []int{200, 203},
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800288 })
jrperritt29ae6b32016-04-13 12:59:37 -0500289 return
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800290}
291
Alex Gaynora6d5f9f2014-10-27 10:52:32 -0700292// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
Jon Perritt82048212014-10-13 22:33:13 -0500293type UpdateOptsBuilder interface {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600294 ToServerUpdateMap() (map[string]interface{}, error)
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400295}
296
297// UpdateOpts specifies the base attributes that may be updated on an existing server.
298type UpdateOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600299 // Name changes the displayed name of the server.
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400300 // The server host name will *not* change.
301 // Server names are not constrained to be unique, even within the same tenant.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600302 Name string `json:"name,omitempty"`
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400303
Jon Perrittdb0ae142016-03-13 00:33:41 -0600304 // AccessIPv4 provides a new IPv4 address for the instance.
305 AccessIPv4 string `json:"accessIPv4,omitempty"`
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400306
Jon Perrittdb0ae142016-03-13 00:33:41 -0600307 // AccessIPv6 provides a new IPv6 address for the instance.
308 AccessIPv6 string `json:"accessIPv6,omitempty"`
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400309}
310
Ash Wilsone45c9732014-09-29 10:54:12 -0400311// ToServerUpdateMap formats an UpdateOpts structure into a request body.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600312func (opts UpdateOpts) ToServerUpdateMap() (map[string]interface{}, error) {
313 return gophercloud.BuildRequestBody(opts, "server")
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400314}
315
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800316// Update requests that various attributes of the indicated server be changed.
Jon Perritt3860b512016-03-29 12:01:48 -0500317func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600318 b, err := opts.ToServerUpdateMap()
319 if err != nil {
320 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500321 return
Jon Perrittdb0ae142016-03-13 00:33:41 -0600322 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600323 _, r.Err = client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100324 OkCodes: []int{200},
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800325 })
jrperritt29ae6b32016-04-13 12:59:37 -0500326 return
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800327}
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700328
Ash Wilson01626a32014-09-17 10:38:07 -0400329// ChangeAdminPassword alters the administrator or root password for a specified server.
Jon Perritt3860b512016-03-29 12:01:48 -0500330func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) (r ActionResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600331 b := map[string]interface{}{
332 "changePassword": map[string]string{
333 "adminPass": newPassword,
334 },
335 }
336 _, r.Err = client.Post(actionURL(client, id), b, nil, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500337 return
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700338}
339
Ash Wilson01626a32014-09-17 10:38:07 -0400340// RebootMethod describes the mechanisms by which a server reboot can be requested.
341type RebootMethod string
342
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700343// These constants determine how a server should be rebooted.
344// See the Reboot() function for further details.
345const (
Ash Wilson01626a32014-09-17 10:38:07 -0400346 SoftReboot RebootMethod = "SOFT"
347 HardReboot RebootMethod = "HARD"
348 OSReboot = SoftReboot
349 PowerCycle = HardReboot
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700350)
351
Jon Perrittdb0ae142016-03-13 00:33:41 -0600352// RebootOptsBuilder is an interface that options must satisfy in order to be
353// used when rebooting a server instance
Jon Perrittf094fef2016-03-07 01:41:59 -0600354type RebootOptsBuilder interface {
355 ToServerRebootMap() (map[string]interface{}, error)
356}
357
Jon Perrittdb0ae142016-03-13 00:33:41 -0600358// RebootOpts satisfies the RebootOptsBuilder interface
Jon Perrittf094fef2016-03-07 01:41:59 -0600359type RebootOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600360 Type RebootMethod `json:"type" required:"true"`
Jon Perrittf094fef2016-03-07 01:41:59 -0600361}
362
Jon Perrittdb0ae142016-03-13 00:33:41 -0600363// ToServerRebootMap allows RebootOpts to satisfiy the RebootOptsBuilder
364// interface
Jon Perrittf094fef2016-03-07 01:41:59 -0600365func (opts *RebootOpts) ToServerRebootMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600366 return gophercloud.BuildRequestBody(opts, "reboot")
Jon Perrittf094fef2016-03-07 01:41:59 -0600367}
368
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700369// Reboot requests that a given server reboot.
370// Two methods exist for rebooting a server:
371//
Jon Perrittdb0ae142016-03-13 00:33:41 -0600372// 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 -0400373// terminating it at the hypervisor level.
374// It's done. Caput. Full stop.
Jon Perrittf094fef2016-03-07 01:41:59 -0600375// Then, after a brief while, power is rtored or the VM instance rtarted.
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700376//
Jon Perrittf094fef2016-03-07 01:41:59 -0600377// SoftReboot (aka OSReboot) simply tells the OS to rtart under its own procedur.
378// 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 -0500379func Reboot(client *gophercloud.ServiceClient, id string, opts RebootOptsBuilder) (r ActionResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600380 b, err := opts.ToServerRebootMap()
Jon Perrittf094fef2016-03-07 01:41:59 -0600381 if err != nil {
382 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500383 return
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700384 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600385 _, r.Err = client.Post(actionURL(client, id), b, nil, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500386 return
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700387}
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700388
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200389// RebuildOptsBuilder is an interface that allows extensions to override the
390// default behaviour of rebuild options
391type RebuildOptsBuilder interface {
392 ToServerRebuildMap() (map[string]interface{}, error)
393}
394
395// RebuildOpts represents the configuration options used in a server rebuild
396// operation
397type RebuildOpts struct {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600398 // The server's admin password
399 AdminPass string `json:"adminPass" required:"true"`
400 // The ID of the image you want your server to be provisioned on
401 ImageID string `json:"imageRef"`
402 ImageName string `json:"-"`
403 //ImageName string `json:"-"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200404 // Name to set the server to
Jon Perrittdb0ae142016-03-13 00:33:41 -0600405 Name string `json:"name,omitempty"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200406 // AccessIPv4 [optional] provides a new IPv4 address for the instance.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600407 AccessIPv4 string `json:"accessIPv4,omitempty"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200408 // AccessIPv6 [optional] provides a new IPv6 address for the instance.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600409 AccessIPv6 string `json:"accessIPv6,omitempty"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200410 // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600411 Metadata map[string]string `json:"metadata,omitempty"`
Kevin Pike92e10b52015-04-10 15:16:57 -0700412 // Personality [optional] includes files to inject into the server at launch.
413 // Rebuild will base64-encode file contents for you.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600414 Personality Personality `json:"personality,omitempty"`
415 ServiceClient *gophercloud.ServiceClient `json:"-"`
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200416}
417
418// ToServerRebuildMap formats a RebuildOpts struct into a map for use in JSON
419func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600420 b, err := gophercloud.BuildRequestBody(opts, "")
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200421 if err != nil {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600422 return nil, err
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200423 }
424
Joe Topjian50cdddf2016-09-16 10:56:09 -0600425 // If ImageRef isn't provided, check if ImageName was provided to ascertain
426 // the image ID.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600427 if opts.ImageID == "" {
Joe Topjian50cdddf2016-09-16 10:56:09 -0600428 if opts.ImageName != "" {
429 if opts.ServiceClient == nil {
430 err := ErrNoClientProvidedForIDByName{}
431 err.Argument = "ServiceClient"
432 return nil, err
433 }
434 imageID, err := images.IDFromName(opts.ServiceClient, opts.ImageName)
435 if err != nil {
436 return nil, err
437 }
438 b["imageRef"] = imageID
Jon Perrittdb0ae142016-03-13 00:33:41 -0600439 }
Jon Perritt12395212016-02-24 10:41:17 -0600440 }
441
Jon Perrittdb0ae142016-03-13 00:33:41 -0600442 return map[string]interface{}{"rebuild": b}, nil
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200443}
444
445// Rebuild will reprovision the server according to the configuration options
446// provided in the RebuildOpts struct.
Jon Perritt3860b512016-03-29 12:01:48 -0500447func Rebuild(client *gophercloud.ServiceClient, id string, opts RebuildOptsBuilder) (r RebuildResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600448 b, err := opts.ToServerRebuildMap()
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200449 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600450 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500451 return
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700452 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600453 _, r.Err = client.Post(actionURL(client, id), b, &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500454 return
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700455}
456
Ash Wilson5f7cf182014-10-23 08:35:24 -0400457// ResizeOptsBuilder is an interface that allows extensions to override the default structure of
458// a Resize request.
459type ResizeOptsBuilder interface {
460 ToServerResizeMap() (map[string]interface{}, error)
461}
462
463// ResizeOpts represents the configuration options used to control a Resize operation.
464type ResizeOpts struct {
465 // FlavorRef is the ID of the flavor you wish your server to become.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600466 FlavorRef string `json:"flavorRef" required:"true"`
Ash Wilson5f7cf182014-10-23 08:35:24 -0400467}
468
Alex Gaynor266e9332014-10-28 14:44:04 -0700469// 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 -0400470// Resize request.
471func (opts ResizeOpts) ToServerResizeMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600472 return gophercloud.BuildRequestBody(opts, "resize")
Ash Wilson5f7cf182014-10-23 08:35:24 -0400473}
474
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700475// Resize instructs the provider to change the flavor of the server.
Ash Wilson01626a32014-09-17 10:38:07 -0400476// Note that this implies rebuilding it.
Gleb37b56e82016-09-06 19:07:58 +0300477// Unfortunately, one cannot pass rebuild parameters to the resize function.
478// When the resize completes, the server will be in RESIZE_VERIFY state.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700479// While in this state, you can explore the use of the new server's configuration.
Gleb37b56e82016-09-06 19:07:58 +0300480// If you like it, call ConfirmResize() to commit the resize permanently.
481// Otherwise, call RevertResize() to restore the old configuration.
Jon Perritt3860b512016-03-29 12:01:48 -0500482func Resize(client *gophercloud.ServiceClient, id string, opts ResizeOptsBuilder) (r ActionResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600483 b, err := opts.ToServerResizeMap()
484 if err != nil {
485 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500486 return
Jon Perrittf094fef2016-03-07 01:41:59 -0600487 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600488 _, r.Err = client.Post(actionURL(client, id), b, nil, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500489 return
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700490}
491
Gleb37b56e82016-09-06 19:07:58 +0300492// ConfirmResize confirms a previous resize operation on a server.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700493// See Resize() for more details.
Jon Perritt3860b512016-03-29 12:01:48 -0500494func ConfirmResize(client *gophercloud.ServiceClient, id string) (r ActionResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600495 _, r.Err = client.Post(actionURL(client, id), map[string]interface{}{"confirmResize": nil}, nil, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100496 OkCodes: []int{201, 202, 204},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700497 })
jrperritt29ae6b32016-04-13 12:59:37 -0500498 return
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700499}
500
Gleb37b56e82016-09-06 19:07:58 +0300501// RevertResize cancels a previous resize operation on a server.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700502// See Resize() for more details.
Jon Perritt3860b512016-03-29 12:01:48 -0500503func RevertResize(client *gophercloud.ServiceClient, id string) (r ActionResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600504 _, r.Err = client.Post(actionURL(client, id), map[string]interface{}{"revertResize": nil}, nil, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500505 return
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700506}
Alex Gaynor39584a02014-10-28 13:59:21 -0700507
Alex Gaynor266e9332014-10-28 14:44:04 -0700508// RescueOptsBuilder is an interface that allows extensions to override the
509// default structure of a Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700510type RescueOptsBuilder interface {
511 ToServerRescueMap() (map[string]interface{}, error)
512}
513
Alex Gaynor266e9332014-10-28 14:44:04 -0700514// RescueOpts represents the configuration options used to control a Rescue
515// option.
Alex Gaynor39584a02014-10-28 13:59:21 -0700516type RescueOpts struct {
Alex Gaynor266e9332014-10-28 14:44:04 -0700517 // AdminPass is the desired administrative password for the instance in
Alex Gaynorcfec7722014-11-13 13:33:49 -0800518 // RESCUE mode. If it's left blank, the server will generate a password.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600519 AdminPass string `json:"adminPass,omitempty"`
Alex Gaynor39584a02014-10-28 13:59:21 -0700520}
521
Jon Perrittcc77da62014-11-16 13:14:21 -0700522// ToServerRescueMap formats a RescueOpts as a map that can be used as a JSON
Alex Gaynor266e9332014-10-28 14:44:04 -0700523// request body for the Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700524func (opts RescueOpts) ToServerRescueMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600525 return gophercloud.BuildRequestBody(opts, "rescue")
Alex Gaynor39584a02014-10-28 13:59:21 -0700526}
527
Alex Gaynor266e9332014-10-28 14:44:04 -0700528// Rescue instructs the provider to place the server into RESCUE mode.
Jon Perritt3860b512016-03-29 12:01:48 -0500529func Rescue(client *gophercloud.ServiceClient, id string, opts RescueOptsBuilder) (r RescueResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600530 b, err := opts.ToServerRescueMap()
531 if err != nil {
532 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500533 return
Jon Perrittf094fef2016-03-07 01:41:59 -0600534 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600535 _, r.Err = client.Post(actionURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100536 OkCodes: []int{200},
Alex Gaynor39584a02014-10-28 13:59:21 -0700537 })
jrperritt29ae6b32016-04-13 12:59:37 -0500538 return
Alex Gaynor39584a02014-10-28 13:59:21 -0700539}
Jon Perrittcc77da62014-11-16 13:14:21 -0700540
Jon Perritt789f8322014-11-21 08:20:04 -0700541// ResetMetadataOptsBuilder allows extensions to add additional parameters to the
542// Reset request.
543type ResetMetadataOptsBuilder interface {
544 ToMetadataResetMap() (map[string]interface{}, error)
Jon Perrittcc77da62014-11-16 13:14:21 -0700545}
546
Jon Perritt78c57ce2014-11-20 11:07:18 -0700547// MetadataOpts is a map that contains key-value pairs.
548type MetadataOpts map[string]string
Jon Perrittcc77da62014-11-16 13:14:21 -0700549
Jon Perritt789f8322014-11-21 08:20:04 -0700550// ToMetadataResetMap assembles a body for a Reset request based on the contents of a MetadataOpts.
551func (opts MetadataOpts) ToMetadataResetMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700552 return map[string]interface{}{"metadata": opts}, nil
553}
554
Jon Perritt78c57ce2014-11-20 11:07:18 -0700555// ToMetadataUpdateMap assembles a body for an Update request based on the contents of a MetadataOpts.
556func (opts MetadataOpts) ToMetadataUpdateMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700557 return map[string]interface{}{"metadata": opts}, nil
558}
559
Jon Perritt789f8322014-11-21 08:20:04 -0700560// ResetMetadata will create multiple new key-value pairs for the given server ID.
Jon Perrittcc77da62014-11-16 13:14:21 -0700561// Note: Using this operation will erase any already-existing metadata and create
562// the new metadata provided. To keep any already-existing metadata, use the
563// UpdateMetadatas or UpdateMetadata function.
Jon Perritt3860b512016-03-29 12:01:48 -0500564func ResetMetadata(client *gophercloud.ServiceClient, id string, opts ResetMetadataOptsBuilder) (r ResetMetadataResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600565 b, err := opts.ToMetadataResetMap()
Jon Perrittcc77da62014-11-16 13:14:21 -0700566 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600567 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500568 return
Jon Perrittcc77da62014-11-16 13:14:21 -0700569 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600570 _, r.Err = client.Put(metadataURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100571 OkCodes: []int{200},
Jon Perrittcc77da62014-11-16 13:14:21 -0700572 })
jrperritt29ae6b32016-04-13 12:59:37 -0500573 return
Jon Perrittcc77da62014-11-16 13:14:21 -0700574}
575
Jon Perritt78c57ce2014-11-20 11:07:18 -0700576// Metadata requests all the metadata for the given server ID.
Jon Perritt3860b512016-03-29 12:01:48 -0500577func Metadata(client *gophercloud.ServiceClient, id string) (r GetMetadataResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600578 _, r.Err = client.Get(metadataURL(client, id), &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500579 return
Jon Perrittcc77da62014-11-16 13:14:21 -0700580}
581
Jon Perritt78c57ce2014-11-20 11:07:18 -0700582// UpdateMetadataOptsBuilder allows extensions to add additional parameters to the
583// Create request.
584type UpdateMetadataOptsBuilder interface {
585 ToMetadataUpdateMap() (map[string]interface{}, error)
586}
587
588// UpdateMetadata updates (or creates) all the metadata specified by opts for the given server ID.
589// This operation does not affect already-existing metadata that is not specified
590// by opts.
Jon Perritt3860b512016-03-29 12:01:48 -0500591func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) (r UpdateMetadataResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600592 b, err := opts.ToMetadataUpdateMap()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700593 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600594 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500595 return
Jon Perritt78c57ce2014-11-20 11:07:18 -0700596 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600597 _, r.Err = client.Post(metadataURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100598 OkCodes: []int{200},
Jon Perritt78c57ce2014-11-20 11:07:18 -0700599 })
jrperritt29ae6b32016-04-13 12:59:37 -0500600 return
Jon Perritt78c57ce2014-11-20 11:07:18 -0700601}
602
603// MetadatumOptsBuilder allows extensions to add additional parameters to the
604// Create request.
605type MetadatumOptsBuilder interface {
606 ToMetadatumCreateMap() (map[string]interface{}, string, error)
607}
608
609// MetadatumOpts is a map of length one that contains a key-value pair.
610type MetadatumOpts map[string]string
611
612// ToMetadatumCreateMap assembles a body for a Create request based on the contents of a MetadataumOpts.
613func (opts MetadatumOpts) ToMetadatumCreateMap() (map[string]interface{}, string, error) {
614 if len(opts) != 1 {
Jon Perritt13808262016-03-09 00:50:12 -0600615 err := gophercloud.ErrInvalidInput{}
616 err.Argument = "servers.MetadatumOpts"
617 err.Info = "Must have 1 and only 1 key-value pair"
618 return nil, "", err
Jon Perritt78c57ce2014-11-20 11:07:18 -0700619 }
620 metadatum := map[string]interface{}{"meta": opts}
621 var key string
622 for k := range metadatum["meta"].(MetadatumOpts) {
623 key = k
624 }
625 return metadatum, key, nil
626}
627
628// 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 -0500629func CreateMetadatum(client *gophercloud.ServiceClient, id string, opts MetadatumOptsBuilder) (r CreateMetadatumResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600630 b, key, err := opts.ToMetadatumCreateMap()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700631 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600632 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500633 return
Jon Perritt78c57ce2014-11-20 11:07:18 -0700634 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600635 _, r.Err = client.Put(metadatumURL(client, id, key), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100636 OkCodes: []int{200},
Jon Perritt78c57ce2014-11-20 11:07:18 -0700637 })
jrperritt29ae6b32016-04-13 12:59:37 -0500638 return
Jon Perritt78c57ce2014-11-20 11:07:18 -0700639}
640
641// Metadatum requests the key-value pair with the given key for the given server ID.
Jon Perritt3860b512016-03-29 12:01:48 -0500642func Metadatum(client *gophercloud.ServiceClient, id, key string) (r GetMetadatumResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600643 _, r.Err = client.Get(metadatumURL(client, id, key), &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500644 return
Jon Perritt78c57ce2014-11-20 11:07:18 -0700645}
646
647// DeleteMetadatum will delete the key-value pair with the given key for the given server ID.
Jon Perritt3860b512016-03-29 12:01:48 -0500648func DeleteMetadatum(client *gophercloud.ServiceClient, id, key string) (r DeleteMetadatumResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600649 _, r.Err = client.Delete(metadatumURL(client, id, key), nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500650 return
Jon Perrittcc77da62014-11-16 13:14:21 -0700651}
Jon Perritt5cb49482015-02-19 12:19:58 -0700652
653// ListAddresses makes a request against the API to list the servers IP addresses.
654func ListAddresses(client *gophercloud.ServiceClient, id string) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600655 return pagination.NewPager(client, listAddressesURL(client, id), func(r pagination.PageResult) pagination.Page {
Jon Perritt5cb49482015-02-19 12:19:58 -0700656 return AddressPage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600657 })
Jon Perritt5cb49482015-02-19 12:19:58 -0700658}
Jon Perritt04d073c2015-02-19 21:46:23 -0700659
660// ListAddressesByNetwork makes a request against the API to list the servers IP addresses
661// for the given network.
662func ListAddressesByNetwork(client *gophercloud.ServiceClient, id, network string) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600663 return pagination.NewPager(client, listAddressesByNetworkURL(client, id, network), func(r pagination.PageResult) pagination.Page {
Jon Perritt04d073c2015-02-19 21:46:23 -0700664 return NetworkAddressPage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600665 })
Jon Perritt04d073c2015-02-19 21:46:23 -0700666}
einarf2fc665e2015-04-16 20:16:21 +0000667
Jon Perrittdb0ae142016-03-13 00:33:41 -0600668// CreateImageOptsBuilder is the interface types must satisfy in order to be
669// used as CreateImage options
einarf4e5fdaf2015-04-16 23:14:59 +0000670type CreateImageOptsBuilder interface {
671 ToServerCreateImageMap() (map[string]interface{}, error)
einarf2fc665e2015-04-16 20:16:21 +0000672}
673
Jon Perrittdb0ae142016-03-13 00:33:41 -0600674// CreateImageOpts satisfies the CreateImageOptsBuilder
675type CreateImageOpts struct {
676 // Name of the image/snapshot
677 Name string `json:"name" required:"true"`
678 // Metadata contains key-value pairs (up to 255 bytes each) to attach to the created image.
679 Metadata map[string]string `json:"metadata,omitempty"`
680}
681
einarf4e5fdaf2015-04-16 23:14:59 +0000682// ToServerCreateImageMap formats a CreateImageOpts structure into a request body.
683func (opts CreateImageOpts) ToServerCreateImageMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600684 return gophercloud.BuildRequestBody(opts, "createImage")
einarf2fc665e2015-04-16 20:16:21 +0000685}
686
einarf4e5fdaf2015-04-16 23:14:59 +0000687// 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 -0500688func CreateImage(client *gophercloud.ServiceClient, id string, opts CreateImageOptsBuilder) (r CreateImageResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600689 b, err := opts.ToServerCreateImageMap()
einarf2fc665e2015-04-16 20:16:21 +0000690 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600691 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500692 return
einarf2fc665e2015-04-16 20:16:21 +0000693 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600694 resp, err := client.Post(actionURL(client, id), b, nil, &gophercloud.RequestOpts{
einarf2fc665e2015-04-16 20:16:21 +0000695 OkCodes: []int{202},
696 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600697 r.Err = err
698 r.Header = resp.Header
jrperritt29ae6b32016-04-13 12:59:37 -0500699 return
einarf2fc665e2015-04-16 20:16:21 +0000700}
Jon Perritt6b0a8832015-06-04 14:32:30 -0600701
702// IDFromName is a convienience function that returns a server's ID given its name.
703func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600704 count := 0
705 id := ""
Jon Perrittf094fef2016-03-07 01:41:59 -0600706 allPages, err := List(client, nil).AllPages()
707 if err != nil {
708 return "", err
709 }
Jon Perritt6b0a8832015-06-04 14:32:30 -0600710
Jon Perrittf094fef2016-03-07 01:41:59 -0600711 all, err := ExtractServers(allPages)
712 if err != nil {
713 return "", err
714 }
715
716 for _, f := range all {
717 if f.Name == name {
718 count++
719 id = f.ID
720 }
721 }
722
723 switch count {
Jon Perritt6b0a8832015-06-04 14:32:30 -0600724 case 0:
Jon Perrittdb0ae142016-03-13 00:33:41 -0600725 return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "server"}
Jon Perritt6b0a8832015-06-04 14:32:30 -0600726 case 1:
Jon Perrittf094fef2016-03-07 01:41:59 -0600727 return id, nil
Jon Perritt6b0a8832015-06-04 14:32:30 -0600728 default:
Jon Perrittdb0ae142016-03-13 00:33:41 -0600729 return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "server"}
Jon Perritt6b0a8832015-06-04 14:32:30 -0600730 }
731}
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100732
Rickard von Essenc3d49b72016-02-16 20:59:18 +0100733// GetPassword makes a request against the nova API to get the encrypted administrative password.
jrperritt2f93a632016-04-13 15:41:20 -0500734func GetPassword(client *gophercloud.ServiceClient, serverId string) (r GetPasswordResult) {
735 _, r.Err = client.Get(passwordURL(client, serverId), &r.Body, nil)
736 return
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100737}