blob: 6e23ada3086aacf17575d2ecbef83efa30c46e18 [file] [log] [blame]
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -08001package servers
2
3import (
Ash Wilson6a310e02014-09-29 08:24:02 -04004 "encoding/base64"
Kevin Pikea2bfaea2015-04-21 11:45:59 -07005 "encoding/json"
Ash Wilson01626a32014-09-17 10:38:07 -04006
Jon Perritt27249f42016-02-18 10:35:59 -06007 "github.com/gophercloud/gophercloud"
8 "github.com/gophercloud/gophercloud/openstack/compute/v2/flavors"
Jon Perritt994370e2016-02-18 15:23:34 -06009 "github.com/gophercloud/gophercloud/openstack/compute/v2/images"
Jon Perritt27249f42016-02-18 10:35:59 -060010 "github.com/gophercloud/gophercloud/pagination"
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080011)
12
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020013// ListOptsBuilder allows extensions to add additional parameters to the
14// List request.
15type ListOptsBuilder interface {
16 ToServerListQuery() (string, error)
17}
Kevin Pike9748b7b2015-05-05 07:34:07 -070018
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020019// ListOpts allows the filtering and sorting of paginated collections through
20// the API. Filtering is achieved by passing in struct field values that map to
21// the server attributes you want to see returned. Marker and Limit are used
22// for pagination.
23type ListOpts struct {
24 // A time/date stamp for when the server last changed status.
25 ChangesSince string `q:"changes-since"`
26
27 // Name of the image in URL format.
28 Image string `q:"image"`
29
30 // Name of the flavor in URL format.
31 Flavor string `q:"flavor"`
32
33 // Name of the server as a string; can be queried with regular expressions.
34 // Realize that ?name=bob returns both bob and bobb. If you need to match bob
35 // only, you can use a regular expression matching the syntax of the
36 // underlying database server implemented for Compute.
37 Name string `q:"name"`
38
39 // Value of the status of the server so that you can filter on "ACTIVE" for example.
40 Status string `q:"status"`
41
42 // Name of the host as a string.
43 Host string `q:"host"`
44
45 // UUID of the server at which you want to set a marker.
46 Marker string `q:"marker"`
47
48 // Integer value for the limit of values to return.
49 Limit int `q:"limit"`
Daniel Speichert9342e522015-06-05 10:31:52 -040050
51 // Bool to show all tenants
52 AllTenants bool `q:"all_tenants"`
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020053}
54
55// ToServerListQuery formats a ListOpts into a query string.
56func (opts ListOpts) ToServerListQuery() (string, error) {
57 q, err := gophercloud.BuildQueryString(opts)
Jon Perrittdb0ae142016-03-13 00:33:41 -060058 return q.String(), err
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020059}
60
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080061// List makes a request against the API to list servers accessible to you.
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020062func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
63 url := listDetailURL(client)
Jamie Hannafordbfe33b22014-10-16 12:45:40 +020064 if opts != nil {
65 query, err := opts.ToServerListQuery()
66 if err != nil {
67 return pagination.Pager{Err: err}
68 }
69 url += query
70 }
Jon Perrittdb0ae142016-03-13 00:33:41 -060071 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
Ash Wilsonb8b16f82014-10-20 10:19:49 -040072 return ServerPage{pagination.LinkedPageBase{PageResult: r}}
Jon Perrittdb0ae142016-03-13 00:33:41 -060073 })
Samuel A. Falvo IIc007c272014-02-10 20:49:26 -080074}
75
Ash Wilson2206a112014-10-02 10:57:38 -040076// CreateOptsBuilder describes struct types that can be accepted by the Create call.
Ash Wilson6a310e02014-09-29 08:24:02 -040077// The CreateOpts struct in this package does.
Ash Wilson2206a112014-10-02 10:57:38 -040078type CreateOptsBuilder interface {
Jon Perritt4149d7c2014-10-23 21:23:46 -050079 ToServerCreateMap() (map[string]interface{}, error)
Ash Wilson6a310e02014-09-29 08:24:02 -040080}
81
82// Network is used within CreateOpts to control a new server's network attachments.
83type Network struct {
84 // UUID of a nova-network to attach to the newly provisioned server.
85 // Required unless Port is provided.
86 UUID string
87
88 // Port of a neutron network to attach to the newly provisioned server.
89 // Required unless UUID is provided.
90 Port string
91
92 // FixedIP [optional] specifies a fixed IPv4 address to be used on this network.
93 FixedIP string
94}
95
Kevin Pike92e10b52015-04-10 15:16:57 -070096// Personality is an array of files that are injected into the server at launch.
Kevin Pikea2bfaea2015-04-21 11:45:59 -070097type Personality []*File
Kevin Pike92e10b52015-04-10 15:16:57 -070098
99// File is used within CreateOpts and RebuildOpts to inject a file into the server at launch.
Kevin Pike9748b7b2015-05-05 07:34:07 -0700100// File implements the json.Marshaler interface, so when a Create or Rebuild operation is requested,
101// json.Marshal will call File's MarshalJSON method.
Kevin Pike92e10b52015-04-10 15:16:57 -0700102type File struct {
103 // Path of the file
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700104 Path string
Kevin Pike92e10b52015-04-10 15:16:57 -0700105 // Contents of the file. Maximum content size is 255 bytes.
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700106 Contents []byte
Kevin Pike92e10b52015-04-10 15:16:57 -0700107}
108
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700109// MarshalJSON marshals the escaped file, base64 encoding the contents.
110func (f *File) MarshalJSON() ([]byte, error) {
111 file := struct {
112 Path string `json:"path"`
113 Contents string `json:"contents"`
114 }{
115 Path: f.Path,
116 Contents: base64.StdEncoding.EncodeToString(f.Contents),
Kevin Pike92e10b52015-04-10 15:16:57 -0700117 }
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700118 return json.Marshal(file)
Kevin Pike92e10b52015-04-10 15:16:57 -0700119}
120
Ash Wilson6a310e02014-09-29 08:24:02 -0400121// CreateOpts specifies server creation parameters.
122type CreateOpts struct {
Jon Perritt01618ee2016-03-09 03:04:06 -0600123 // Name is the name to assign to the newly launched server.
124 Name string `json:"name" required:"true"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400125
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600126 // ImageRef [optional; required if ImageName is not provided] is the ID or full
127 // URL to the image that contains the server's OS and initial state.
128 // Also optional if using the boot-from-volume extension.
Jon Perritt01618ee2016-03-09 03:04:06 -0600129 ImageRef string `json:"imageRef"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400130
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600131 // ImageName [optional; required if ImageRef is not provided] is the name of the
132 // image that contains the server's OS and initial state.
133 // Also optional if using the boot-from-volume extension.
Jon Perritt01618ee2016-03-09 03:04:06 -0600134 ImageName string `json:"-"`
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600135
136 // FlavorRef [optional; required if FlavorName is not provided] is the ID or
137 // full URL to the flavor that describes the server's specs.
Jon Perritt01618ee2016-03-09 03:04:06 -0600138 FlavorRef string `json:"flavorRef"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400139
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600140 // FlavorName [optional; required if FlavorRef is not provided] is the name of
141 // the flavor that describes the server's specs.
Jon Perritt01618ee2016-03-09 03:04:06 -0600142 FlavorName string `json:"-"`
Jon Perrittad5f1cb2015-05-20 10:38:13 -0600143
Jon Perritt01618ee2016-03-09 03:04:06 -0600144 // SecurityGroups lists the names of the security groups to which this server should belong.
145 SecurityGroups []string `json:"-"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400146
Jon Perritt01618ee2016-03-09 03:04:06 -0600147 // UserData contains configuration information or scripts to use upon launch.
Ash Wilson6a310e02014-09-29 08:24:02 -0400148 // Create will base64-encode it for you.
Jon Perritt01618ee2016-03-09 03:04:06 -0600149 UserData []byte `json:"-"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400150
Jon Perritt01618ee2016-03-09 03:04:06 -0600151 // AvailabilityZone in which to launch the server.
152 AvailabilityZone string `json:"availability_zone,omitempty"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400153
Jon Perritt01618ee2016-03-09 03:04:06 -0600154 // Networks dictates how this server will be attached to available networks.
Ash Wilson6a310e02014-09-29 08:24:02 -0400155 // By default, the server will be attached to all isolated networks for the tenant.
Jon Perritt01618ee2016-03-09 03:04:06 -0600156 Networks []Network `json:"-"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400157
Jon Perritt01618ee2016-03-09 03:04:06 -0600158 // Metadata contains key-value pairs (up to 255 bytes each) to attach to the server.
159 Metadata map[string]string `json:"-"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400160
Jon Perritt01618ee2016-03-09 03:04:06 -0600161 // Personality includes files to inject into the server at launch.
Kevin Pike92e10b52015-04-10 15:16:57 -0700162 // Create will base64-encode file contents for you.
Jon Perritt01618ee2016-03-09 03:04:06 -0600163 Personality Personality `json:"-"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400164
Jon Perritt01618ee2016-03-09 03:04:06 -0600165 // ConfigDrive enables metadata injection through a configuration drive.
166 ConfigDrive *bool `json:"config_drive,omitempty"`
Jon Perrittf3b2e142014-11-04 16:00:19 -0600167
Jon Perritt01618ee2016-03-09 03:04:06 -0600168 // AdminPass sets the root user password. If not set, a randomly-generated
Jon Perrittf094fef2016-03-07 01:41:59 -0600169 // password will be created and returned in the rponse.
Jon Perritt01618ee2016-03-09 03:04:06 -0600170 AdminPass string `json:"adminPass,omitempty"`
Jon Perritt7b9671c2015-02-01 22:03:14 -0700171
Jon Perritt01618ee2016-03-09 03:04:06 -0600172 // AccessIPv4 specifies an IPv4 address for the instance.
173 AccessIPv4 string `json:"accessIPv4,omitempty"`
Jon Perritt7b9671c2015-02-01 22:03:14 -0700174
Jon Perritt01618ee2016-03-09 03:04:06 -0600175 // AccessIPv6 pecifies an IPv6 address for the instance.
176 AccessIPv6 string `json:"accessIPv6,omitempty"`
Jon Perritt994370e2016-02-18 15:23:34 -0600177
Jon Perritt01618ee2016-03-09 03:04:06 -0600178 // ServiceClient will allow calls to be made to retrieve an image or
Jon Perritt994370e2016-02-18 15:23:34 -0600179 // flavor ID by name.
Jon Perritt01618ee2016-03-09 03:04:06 -0600180 ServiceClient *gophercloud.ServiceClient `json:"-"`
Ash Wilson6a310e02014-09-29 08:24:02 -0400181}
182
Ash Wilsone45c9732014-09-29 10:54:12 -0400183// ToServerCreateMap assembles a request body based on the contents of a CreateOpts.
Jon Perritt4149d7c2014-10-23 21:23:46 -0500184func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600185 b, err := gophercloud.BuildRequestBody(opts, "")
Jon Perritt01618ee2016-03-09 03:04:06 -0600186 if err != nil {
187 return nil, err
188 }
Ash Wilson6a310e02014-09-29 08:24:02 -0400189
190 if opts.UserData != nil {
191 encoded := base64.StdEncoding.EncodeToString(opts.UserData)
Jon Perritt01618ee2016-03-09 03:04:06 -0600192 b["user_data"] = &encoded
Jon Perritt7b9671c2015-02-01 22:03:14 -0700193 }
Ash Wilson6a310e02014-09-29 08:24:02 -0400194
195 if len(opts.SecurityGroups) > 0 {
196 securityGroups := make([]map[string]interface{}, len(opts.SecurityGroups))
197 for i, groupName := range opts.SecurityGroups {
198 securityGroups[i] = map[string]interface{}{"name": groupName}
199 }
Jon Perritt01618ee2016-03-09 03:04:06 -0600200 b["security_groups"] = securityGroups
Ash Wilson6a310e02014-09-29 08:24:02 -0400201 }
Jon Perritt2a7797d2014-10-21 15:08:43 -0500202
Ash Wilson6a310e02014-09-29 08:24:02 -0400203 if len(opts.Networks) > 0 {
204 networks := make([]map[string]interface{}, len(opts.Networks))
205 for i, net := range opts.Networks {
206 networks[i] = make(map[string]interface{})
207 if net.UUID != "" {
208 networks[i]["uuid"] = net.UUID
209 }
210 if net.Port != "" {
211 networks[i]["port"] = net.Port
212 }
213 if net.FixedIP != "" {
214 networks[i]["fixed_ip"] = net.FixedIP
215 }
216 }
Jon Perritt01618ee2016-03-09 03:04:06 -0600217 b["networks"] = networks
Kevin Pike92e10b52015-04-10 15:16:57 -0700218 }
219
jrperrittb1013232016-02-10 19:01:53 -0600220 // If ImageRef isn't provided, use ImageName to ascertain the image ID.
221 if opts.ImageRef == "" {
222 if opts.ImageName == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600223 err := ErrNeitherImageIDNorImageNameProvided{}
Jon Perrittf094fef2016-03-07 01:41:59 -0600224 err.Argument = "ImageRef/ImageName"
225 return nil, err
jrperrittb1013232016-02-10 19:01:53 -0600226 }
Jon Perritt994370e2016-02-18 15:23:34 -0600227 if opts.ServiceClient == nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600228 err := ErrNoClientProvidedForIDByName{}
Jon Perrittf094fef2016-03-07 01:41:59 -0600229 err.Argument = "ServiceClient"
230 return nil, err
Jon Perritt994370e2016-02-18 15:23:34 -0600231 }
232 imageID, err := images.IDFromName(opts.ServiceClient, opts.ImageName)
jrperrittb1013232016-02-10 19:01:53 -0600233 if err != nil {
234 return nil, err
235 }
Jon Perritt01618ee2016-03-09 03:04:06 -0600236 b["imageRef"] = imageID
jrperrittb1013232016-02-10 19:01:53 -0600237 }
238
239 // If FlavorRef isn't provided, use FlavorName to ascertain the flavor ID.
240 if opts.FlavorRef == "" {
241 if opts.FlavorName == "" {
Jon Perrittf094fef2016-03-07 01:41:59 -0600242 err := ErrNeitherFlavorIDNorFlavorNameProvided{}
Jon Perrittf094fef2016-03-07 01:41:59 -0600243 err.Argument = "FlavorRef/FlavorName"
244 return nil, err
jrperrittb1013232016-02-10 19:01:53 -0600245 }
Jon Perritt994370e2016-02-18 15:23:34 -0600246 if opts.ServiceClient == nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600247 err := ErrNoClientProvidedForIDByName{}
248 err.Argument = "ServiceClient"
Jon Perrittf094fef2016-03-07 01:41:59 -0600249 return nil, err
Jon Perritt994370e2016-02-18 15:23:34 -0600250 }
251 flavorID, err := flavors.IDFromName(opts.ServiceClient, opts.FlavorName)
jrperrittb1013232016-02-10 19:01:53 -0600252 if err != nil {
253 return nil, err
254 }
Jon Perritt01618ee2016-03-09 03:04:06 -0600255 b["flavorRef"] = flavorID
jrperrittb1013232016-02-10 19:01:53 -0600256 }
257
Jon Perritt01618ee2016-03-09 03:04:06 -0600258 return map[string]interface{}{"server": b}, nil
Ash Wilson6a310e02014-09-29 08:24:02 -0400259}
260
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800261// Create requests a server to be provisioned to the user in the current tenant.
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
Jon Perrittdb0ae142016-03-13 00:33:41 -0600425 // If ImageRef isn't provided, use ImageName to ascertain the image ID.
426 if opts.ImageID == "" {
427 if opts.ImageName == "" {
428 err := ErrNeitherImageIDNorImageNameProvided{}
429 err.Argument = "ImageRef/ImageName"
430 return nil, err
431 }
432 if opts.ServiceClient == nil {
433 err := ErrNoClientProvidedForIDByName{}
434 err.Argument = "ServiceClient"
435 return nil, err
436 }
437 imageID, err := images.IDFromName(opts.ServiceClient, opts.ImageName)
438 if err != nil {
439 return nil, err
440 }
441 b["imageRef"] = imageID
Jon Perritt12395212016-02-24 10:41:17 -0600442 }
443
Jon Perrittdb0ae142016-03-13 00:33:41 -0600444 return map[string]interface{}{"rebuild": b}, nil
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200445}
446
447// Rebuild will reprovision the server according to the configuration options
448// provided in the RebuildOpts struct.
Jon Perritt3860b512016-03-29 12:01:48 -0500449func Rebuild(client *gophercloud.ServiceClient, id string, opts RebuildOptsBuilder) (r RebuildResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600450 b, err := opts.ToServerRebuildMap()
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200451 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600452 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500453 return
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700454 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600455 _, r.Err = client.Post(actionURL(client, id), b, &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500456 return
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700457}
458
Ash Wilson5f7cf182014-10-23 08:35:24 -0400459// ResizeOptsBuilder is an interface that allows extensions to override the default structure of
460// a Resize request.
461type ResizeOptsBuilder interface {
462 ToServerResizeMap() (map[string]interface{}, error)
463}
464
465// ResizeOpts represents the configuration options used to control a Resize operation.
466type ResizeOpts struct {
467 // FlavorRef is the ID of the flavor you wish your server to become.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600468 FlavorRef string `json:"flavorRef" required:"true"`
Ash Wilson5f7cf182014-10-23 08:35:24 -0400469}
470
Alex Gaynor266e9332014-10-28 14:44:04 -0700471// 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 -0400472// Resize request.
473func (opts ResizeOpts) ToServerResizeMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600474 return gophercloud.BuildRequestBody(opts, "resize")
Ash Wilson5f7cf182014-10-23 08:35:24 -0400475}
476
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700477// Resize instructs the provider to change the flavor of the server.
Ash Wilson01626a32014-09-17 10:38:07 -0400478// Note that this implies rebuilding it.
Jon Perrittf094fef2016-03-07 01:41:59 -0600479// Unfortunately, one cannot pass rebuild parameters to the rize function.
480// When the rize completes, the server will be in RESIZE_VERIFY state.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700481// While in this state, you can explore the use of the new server's configuration.
Jon Perrittf094fef2016-03-07 01:41:59 -0600482// If you like it, call ConfirmResize() to commit the rize permanently.
483// Otherwise, call RevertResize() to rtore the old configuration.
Jon Perritt3860b512016-03-29 12:01:48 -0500484func Resize(client *gophercloud.ServiceClient, id string, opts ResizeOptsBuilder) (r ActionResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600485 b, err := opts.ToServerResizeMap()
486 if err != nil {
487 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500488 return
Jon Perrittf094fef2016-03-07 01:41:59 -0600489 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600490 _, r.Err = client.Post(actionURL(client, id), b, nil, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500491 return
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700492}
493
Jon Perrittf094fef2016-03-07 01:41:59 -0600494// ConfirmResize confirms a previous rize operation on a server.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700495// See Resize() for more details.
Jon Perritt3860b512016-03-29 12:01:48 -0500496func ConfirmResize(client *gophercloud.ServiceClient, id string) (r ActionResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600497 _, r.Err = client.Post(actionURL(client, id), map[string]interface{}{"confirmResize": nil}, nil, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100498 OkCodes: []int{201, 202, 204},
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700499 })
jrperritt29ae6b32016-04-13 12:59:37 -0500500 return
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700501}
502
Jon Perrittf094fef2016-03-07 01:41:59 -0600503// RevertResize cancels a previous rize operation on a server.
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700504// See Resize() for more details.
Jon Perritt3860b512016-03-29 12:01:48 -0500505func RevertResize(client *gophercloud.ServiceClient, id string) (r ActionResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600506 _, r.Err = client.Post(actionURL(client, id), map[string]interface{}{"revertResize": nil}, nil, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500507 return
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700508}
Alex Gaynor39584a02014-10-28 13:59:21 -0700509
Alex Gaynor266e9332014-10-28 14:44:04 -0700510// RescueOptsBuilder is an interface that allows extensions to override the
511// default structure of a Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700512type RescueOptsBuilder interface {
513 ToServerRescueMap() (map[string]interface{}, error)
514}
515
Alex Gaynor266e9332014-10-28 14:44:04 -0700516// RescueOpts represents the configuration options used to control a Rescue
517// option.
Alex Gaynor39584a02014-10-28 13:59:21 -0700518type RescueOpts struct {
Alex Gaynor266e9332014-10-28 14:44:04 -0700519 // AdminPass is the desired administrative password for the instance in
Alex Gaynorcfec7722014-11-13 13:33:49 -0800520 // RESCUE mode. If it's left blank, the server will generate a password.
Jon Perrittdb0ae142016-03-13 00:33:41 -0600521 AdminPass string `json:"adminPass,omitempty"`
Alex Gaynor39584a02014-10-28 13:59:21 -0700522}
523
Jon Perrittcc77da62014-11-16 13:14:21 -0700524// ToServerRescueMap formats a RescueOpts as a map that can be used as a JSON
Alex Gaynor266e9332014-10-28 14:44:04 -0700525// request body for the Rescue request.
Alex Gaynor39584a02014-10-28 13:59:21 -0700526func (opts RescueOpts) ToServerRescueMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600527 return gophercloud.BuildRequestBody(opts, "rescue")
Alex Gaynor39584a02014-10-28 13:59:21 -0700528}
529
Alex Gaynor266e9332014-10-28 14:44:04 -0700530// Rescue instructs the provider to place the server into RESCUE mode.
Jon Perritt3860b512016-03-29 12:01:48 -0500531func Rescue(client *gophercloud.ServiceClient, id string, opts RescueOptsBuilder) (r RescueResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600532 b, err := opts.ToServerRescueMap()
533 if err != nil {
534 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500535 return
Jon Perrittf094fef2016-03-07 01:41:59 -0600536 }
Jon Perrittf094fef2016-03-07 01:41:59 -0600537 _, r.Err = client.Post(actionURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100538 OkCodes: []int{200},
Alex Gaynor39584a02014-10-28 13:59:21 -0700539 })
jrperritt29ae6b32016-04-13 12:59:37 -0500540 return
Alex Gaynor39584a02014-10-28 13:59:21 -0700541}
Jon Perrittcc77da62014-11-16 13:14:21 -0700542
Jon Perritt789f8322014-11-21 08:20:04 -0700543// ResetMetadataOptsBuilder allows extensions to add additional parameters to the
544// Reset request.
545type ResetMetadataOptsBuilder interface {
546 ToMetadataResetMap() (map[string]interface{}, error)
Jon Perrittcc77da62014-11-16 13:14:21 -0700547}
548
Jon Perritt78c57ce2014-11-20 11:07:18 -0700549// MetadataOpts is a map that contains key-value pairs.
550type MetadataOpts map[string]string
Jon Perrittcc77da62014-11-16 13:14:21 -0700551
Jon Perritt789f8322014-11-21 08:20:04 -0700552// ToMetadataResetMap assembles a body for a Reset request based on the contents of a MetadataOpts.
553func (opts MetadataOpts) ToMetadataResetMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700554 return map[string]interface{}{"metadata": opts}, nil
555}
556
Jon Perritt78c57ce2014-11-20 11:07:18 -0700557// ToMetadataUpdateMap assembles a body for an Update request based on the contents of a MetadataOpts.
558func (opts MetadataOpts) ToMetadataUpdateMap() (map[string]interface{}, error) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700559 return map[string]interface{}{"metadata": opts}, nil
560}
561
Jon Perritt789f8322014-11-21 08:20:04 -0700562// ResetMetadata will create multiple new key-value pairs for the given server ID.
Jon Perrittcc77da62014-11-16 13:14:21 -0700563// Note: Using this operation will erase any already-existing metadata and create
564// the new metadata provided. To keep any already-existing metadata, use the
565// UpdateMetadatas or UpdateMetadata function.
Jon Perritt3860b512016-03-29 12:01:48 -0500566func ResetMetadata(client *gophercloud.ServiceClient, id string, opts ResetMetadataOptsBuilder) (r ResetMetadataResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600567 b, err := opts.ToMetadataResetMap()
Jon Perrittcc77da62014-11-16 13:14:21 -0700568 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600569 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500570 return
Jon Perrittcc77da62014-11-16 13:14:21 -0700571 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600572 _, r.Err = client.Put(metadataURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100573 OkCodes: []int{200},
Jon Perrittcc77da62014-11-16 13:14:21 -0700574 })
jrperritt29ae6b32016-04-13 12:59:37 -0500575 return
Jon Perrittcc77da62014-11-16 13:14:21 -0700576}
577
Jon Perritt78c57ce2014-11-20 11:07:18 -0700578// Metadata requests all the metadata for the given server ID.
Jon Perritt3860b512016-03-29 12:01:48 -0500579func Metadata(client *gophercloud.ServiceClient, id string) (r GetMetadataResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600580 _, r.Err = client.Get(metadataURL(client, id), &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500581 return
Jon Perrittcc77da62014-11-16 13:14:21 -0700582}
583
Jon Perritt78c57ce2014-11-20 11:07:18 -0700584// UpdateMetadataOptsBuilder allows extensions to add additional parameters to the
585// Create request.
586type UpdateMetadataOptsBuilder interface {
587 ToMetadataUpdateMap() (map[string]interface{}, error)
588}
589
590// UpdateMetadata updates (or creates) all the metadata specified by opts for the given server ID.
591// This operation does not affect already-existing metadata that is not specified
592// by opts.
Jon Perritt3860b512016-03-29 12:01:48 -0500593func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) (r UpdateMetadataResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600594 b, err := opts.ToMetadataUpdateMap()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700595 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600596 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500597 return
Jon Perritt78c57ce2014-11-20 11:07:18 -0700598 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600599 _, r.Err = client.Post(metadataURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100600 OkCodes: []int{200},
Jon Perritt78c57ce2014-11-20 11:07:18 -0700601 })
jrperritt29ae6b32016-04-13 12:59:37 -0500602 return
Jon Perritt78c57ce2014-11-20 11:07:18 -0700603}
604
605// MetadatumOptsBuilder allows extensions to add additional parameters to the
606// Create request.
607type MetadatumOptsBuilder interface {
608 ToMetadatumCreateMap() (map[string]interface{}, string, error)
609}
610
611// MetadatumOpts is a map of length one that contains a key-value pair.
612type MetadatumOpts map[string]string
613
614// ToMetadatumCreateMap assembles a body for a Create request based on the contents of a MetadataumOpts.
615func (opts MetadatumOpts) ToMetadatumCreateMap() (map[string]interface{}, string, error) {
616 if len(opts) != 1 {
Jon Perritt13808262016-03-09 00:50:12 -0600617 err := gophercloud.ErrInvalidInput{}
618 err.Argument = "servers.MetadatumOpts"
619 err.Info = "Must have 1 and only 1 key-value pair"
620 return nil, "", err
Jon Perritt78c57ce2014-11-20 11:07:18 -0700621 }
622 metadatum := map[string]interface{}{"meta": opts}
623 var key string
624 for k := range metadatum["meta"].(MetadatumOpts) {
625 key = k
626 }
627 return metadatum, key, nil
628}
629
630// 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 -0500631func CreateMetadatum(client *gophercloud.ServiceClient, id string, opts MetadatumOptsBuilder) (r CreateMetadatumResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600632 b, key, err := opts.ToMetadatumCreateMap()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700633 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600634 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500635 return
Jon Perritt78c57ce2014-11-20 11:07:18 -0700636 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600637 _, r.Err = client.Put(metadatumURL(client, id, key), b, &r.Body, &gophercloud.RequestOpts{
Jamie Hannaford6a3a78f2015-03-24 14:56:12 +0100638 OkCodes: []int{200},
Jon Perritt78c57ce2014-11-20 11:07:18 -0700639 })
jrperritt29ae6b32016-04-13 12:59:37 -0500640 return
Jon Perritt78c57ce2014-11-20 11:07:18 -0700641}
642
643// Metadatum requests the key-value pair with the given key for the given server ID.
Jon Perritt3860b512016-03-29 12:01:48 -0500644func Metadatum(client *gophercloud.ServiceClient, id, key string) (r GetMetadatumResult) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600645 _, r.Err = client.Get(metadatumURL(client, id, key), &r.Body, nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500646 return
Jon Perritt78c57ce2014-11-20 11:07:18 -0700647}
648
649// DeleteMetadatum will delete the key-value pair with the given key for the given server ID.
Jon Perritt3860b512016-03-29 12:01:48 -0500650func DeleteMetadatum(client *gophercloud.ServiceClient, id, key string) (r DeleteMetadatumResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600651 _, r.Err = client.Delete(metadatumURL(client, id, key), nil)
jrperritt29ae6b32016-04-13 12:59:37 -0500652 return
Jon Perrittcc77da62014-11-16 13:14:21 -0700653}
Jon Perritt5cb49482015-02-19 12:19:58 -0700654
655// ListAddresses makes a request against the API to list the servers IP addresses.
656func ListAddresses(client *gophercloud.ServiceClient, id string) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600657 return pagination.NewPager(client, listAddressesURL(client, id), func(r pagination.PageResult) pagination.Page {
Jon Perritt5cb49482015-02-19 12:19:58 -0700658 return AddressPage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600659 })
Jon Perritt5cb49482015-02-19 12:19:58 -0700660}
Jon Perritt04d073c2015-02-19 21:46:23 -0700661
662// ListAddressesByNetwork makes a request against the API to list the servers IP addresses
663// for the given network.
664func ListAddressesByNetwork(client *gophercloud.ServiceClient, id, network string) pagination.Pager {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600665 return pagination.NewPager(client, listAddressesByNetworkURL(client, id, network), func(r pagination.PageResult) pagination.Page {
Jon Perritt04d073c2015-02-19 21:46:23 -0700666 return NetworkAddressPage{pagination.SinglePageBase(r)}
Jon Perrittdb0ae142016-03-13 00:33:41 -0600667 })
Jon Perritt04d073c2015-02-19 21:46:23 -0700668}
einarf2fc665e2015-04-16 20:16:21 +0000669
Jon Perrittdb0ae142016-03-13 00:33:41 -0600670// CreateImageOptsBuilder is the interface types must satisfy in order to be
671// used as CreateImage options
einarf4e5fdaf2015-04-16 23:14:59 +0000672type CreateImageOptsBuilder interface {
673 ToServerCreateImageMap() (map[string]interface{}, error)
einarf2fc665e2015-04-16 20:16:21 +0000674}
675
Jon Perrittdb0ae142016-03-13 00:33:41 -0600676// CreateImageOpts satisfies the CreateImageOptsBuilder
677type CreateImageOpts struct {
678 // Name of the image/snapshot
679 Name string `json:"name" required:"true"`
680 // Metadata contains key-value pairs (up to 255 bytes each) to attach to the created image.
681 Metadata map[string]string `json:"metadata,omitempty"`
682}
683
einarf4e5fdaf2015-04-16 23:14:59 +0000684// ToServerCreateImageMap formats a CreateImageOpts structure into a request body.
685func (opts CreateImageOpts) ToServerCreateImageMap() (map[string]interface{}, error) {
Jon Perrittdb0ae142016-03-13 00:33:41 -0600686 return gophercloud.BuildRequestBody(opts, "createImage")
einarf2fc665e2015-04-16 20:16:21 +0000687}
688
einarf4e5fdaf2015-04-16 23:14:59 +0000689// 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 -0500690func CreateImage(client *gophercloud.ServiceClient, id string, opts CreateImageOptsBuilder) (r CreateImageResult) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600691 b, err := opts.ToServerCreateImageMap()
einarf2fc665e2015-04-16 20:16:21 +0000692 if err != nil {
Jon Perrittf094fef2016-03-07 01:41:59 -0600693 r.Err = err
Jon Perritt3860b512016-03-29 12:01:48 -0500694 return
einarf2fc665e2015-04-16 20:16:21 +0000695 }
Jon Perrittdb0ae142016-03-13 00:33:41 -0600696 resp, err := client.Post(actionURL(client, id), b, nil, &gophercloud.RequestOpts{
einarf2fc665e2015-04-16 20:16:21 +0000697 OkCodes: []int{202},
698 })
Jon Perrittf094fef2016-03-07 01:41:59 -0600699 r.Err = err
700 r.Header = resp.Header
jrperritt29ae6b32016-04-13 12:59:37 -0500701 return
einarf2fc665e2015-04-16 20:16:21 +0000702}
Jon Perritt6b0a8832015-06-04 14:32:30 -0600703
704// IDFromName is a convienience function that returns a server's ID given its name.
705func IDFromName(client *gophercloud.ServiceClient, name string) (string, error) {
Jon Perrittf094fef2016-03-07 01:41:59 -0600706 count := 0
707 id := ""
Jon Perrittf094fef2016-03-07 01:41:59 -0600708 allPages, err := List(client, nil).AllPages()
709 if err != nil {
710 return "", err
711 }
Jon Perritt6b0a8832015-06-04 14:32:30 -0600712
Jon Perrittf094fef2016-03-07 01:41:59 -0600713 all, err := ExtractServers(allPages)
714 if err != nil {
715 return "", err
716 }
717
718 for _, f := range all {
719 if f.Name == name {
720 count++
721 id = f.ID
722 }
723 }
724
725 switch count {
Jon Perritt6b0a8832015-06-04 14:32:30 -0600726 case 0:
Jon Perrittdb0ae142016-03-13 00:33:41 -0600727 return "", gophercloud.ErrResourceNotFound{Name: name, ResourceType: "server"}
Jon Perritt6b0a8832015-06-04 14:32:30 -0600728 case 1:
Jon Perrittf094fef2016-03-07 01:41:59 -0600729 return id, nil
Jon Perritt6b0a8832015-06-04 14:32:30 -0600730 default:
Jon Perrittdb0ae142016-03-13 00:33:41 -0600731 return "", gophercloud.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "server"}
Jon Perritt6b0a8832015-06-04 14:32:30 -0600732 }
733}
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100734
Rickard von Essenc3d49b72016-02-16 20:59:18 +0100735// GetPassword makes a request against the nova API to get the encrypted administrative password.
jrperritt2f93a632016-04-13 15:41:20 -0500736func GetPassword(client *gophercloud.ServiceClient, serverId string) (r GetPasswordResult) {
737 _, r.Err = client.Get(passwordURL(client, serverId), &r.Body, nil)
738 return
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100739}