Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 1 | package servers |
| 2 | |
| 3 | import ( |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 4 | "encoding/base64" |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 5 | "fmt" |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 6 | |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 7 | "github.com/racker/perigee" |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud" |
| 9 | "github.com/rackspace/gophercloud/pagination" |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 10 | ) |
| 11 | |
Jamie Hannaford | bfe33b2 | 2014-10-16 12:45:40 +0200 | [diff] [blame] | 12 | // ListOptsBuilder allows extensions to add additional parameters to the |
| 13 | // List request. |
| 14 | type ListOptsBuilder interface { |
| 15 | ToServerListQuery() (string, error) |
| 16 | } |
| 17 | |
| 18 | // ListOpts allows the filtering and sorting of paginated collections through |
| 19 | // the API. Filtering is achieved by passing in struct field values that map to |
| 20 | // the server attributes you want to see returned. Marker and Limit are used |
| 21 | // for pagination. |
| 22 | type ListOpts struct { |
| 23 | // A time/date stamp for when the server last changed status. |
| 24 | ChangesSince string `q:"changes-since"` |
| 25 | |
| 26 | // Name of the image in URL format. |
| 27 | Image string `q:"image"` |
| 28 | |
| 29 | // Name of the flavor in URL format. |
| 30 | Flavor string `q:"flavor"` |
| 31 | |
| 32 | // Name of the server as a string; can be queried with regular expressions. |
| 33 | // Realize that ?name=bob returns both bob and bobb. If you need to match bob |
| 34 | // only, you can use a regular expression matching the syntax of the |
| 35 | // underlying database server implemented for Compute. |
| 36 | Name string `q:"name"` |
| 37 | |
| 38 | // Value of the status of the server so that you can filter on "ACTIVE" for example. |
| 39 | Status string `q:"status"` |
| 40 | |
| 41 | // Name of the host as a string. |
| 42 | Host string `q:"host"` |
| 43 | |
| 44 | // UUID of the server at which you want to set a marker. |
| 45 | Marker string `q:"marker"` |
| 46 | |
| 47 | // Integer value for the limit of values to return. |
| 48 | Limit int `q:"limit"` |
| 49 | } |
| 50 | |
| 51 | // ToServerListQuery formats a ListOpts into a query string. |
| 52 | func (opts ListOpts) ToServerListQuery() (string, error) { |
| 53 | q, err := gophercloud.BuildQueryString(opts) |
| 54 | if err != nil { |
| 55 | return "", err |
| 56 | } |
| 57 | return q.String(), nil |
| 58 | } |
| 59 | |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 60 | // List makes a request against the API to list servers accessible to you. |
Jamie Hannaford | bfe33b2 | 2014-10-16 12:45:40 +0200 | [diff] [blame] | 61 | func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
| 62 | url := listDetailURL(client) |
| 63 | |
| 64 | if opts != nil { |
| 65 | query, err := opts.ToServerListQuery() |
| 66 | if err != nil { |
| 67 | return pagination.Pager{Err: err} |
| 68 | } |
| 69 | url += query |
| 70 | } |
| 71 | |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 72 | createPageFn := func(r pagination.PageResult) pagination.Page { |
| 73 | return ServerPage{pagination.LinkedPageBase{PageResult: r}} |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Jamie Hannaford | bfe33b2 | 2014-10-16 12:45:40 +0200 | [diff] [blame] | 76 | return pagination.NewPager(client, url, createPageFn) |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Ash Wilson | 2206a11 | 2014-10-02 10:57:38 -0400 | [diff] [blame] | 79 | // CreateOptsBuilder describes struct types that can be accepted by the Create call. |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 80 | // The CreateOpts struct in this package does. |
Ash Wilson | 2206a11 | 2014-10-02 10:57:38 -0400 | [diff] [blame] | 81 | type CreateOptsBuilder interface { |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 82 | ToServerCreateMap() (map[string]interface{}, error) |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // Network is used within CreateOpts to control a new server's network attachments. |
| 86 | type Network struct { |
| 87 | // UUID of a nova-network to attach to the newly provisioned server. |
| 88 | // Required unless Port is provided. |
| 89 | UUID string |
| 90 | |
| 91 | // Port of a neutron network to attach to the newly provisioned server. |
| 92 | // Required unless UUID is provided. |
| 93 | Port string |
| 94 | |
| 95 | // FixedIP [optional] specifies a fixed IPv4 address to be used on this network. |
| 96 | FixedIP string |
| 97 | } |
| 98 | |
| 99 | // CreateOpts specifies server creation parameters. |
| 100 | type CreateOpts struct { |
| 101 | // Name [required] is the name to assign to the newly launched server. |
| 102 | Name string |
| 103 | |
| 104 | // ImageRef [required] is the ID or full URL to the image that contains the server's OS and initial state. |
| 105 | // Optional if using the boot-from-volume extension. |
| 106 | ImageRef string |
| 107 | |
| 108 | // FlavorRef [required] is the ID or full URL to the flavor that describes the server's specs. |
| 109 | FlavorRef string |
| 110 | |
| 111 | // SecurityGroups [optional] lists the names of the security groups to which this server should belong. |
| 112 | SecurityGroups []string |
| 113 | |
| 114 | // UserData [optional] contains configuration information or scripts to use upon launch. |
| 115 | // Create will base64-encode it for you. |
| 116 | UserData []byte |
| 117 | |
| 118 | // AvailabilityZone [optional] in which to launch the server. |
| 119 | AvailabilityZone string |
| 120 | |
| 121 | // Networks [optional] dictates how this server will be attached to available networks. |
| 122 | // By default, the server will be attached to all isolated networks for the tenant. |
| 123 | Networks []Network |
| 124 | |
| 125 | // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server. |
| 126 | Metadata map[string]string |
| 127 | |
| 128 | // Personality [optional] includes the path and contents of a file to inject into the server at launch. |
| 129 | // The maximum size of the file is 255 bytes (decoded). |
| 130 | Personality []byte |
| 131 | |
| 132 | // ConfigDrive [optional] enables metadata injection through a configuration drive. |
| 133 | ConfigDrive bool |
Jon Perritt | f3b2e14 | 2014-11-04 16:00:19 -0600 | [diff] [blame^] | 134 | |
| 135 | // AdminPass [optional] sets the root user password. If not set, a randomly-generated |
| 136 | // password will be created and returned in the response. |
| 137 | AdminPass string |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 138 | } |
| 139 | |
Ash Wilson | e45c973 | 2014-09-29 10:54:12 -0400 | [diff] [blame] | 140 | // ToServerCreateMap assembles a request body based on the contents of a CreateOpts. |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 141 | func (opts CreateOpts) ToServerCreateMap() (map[string]interface{}, error) { |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 142 | server := make(map[string]interface{}) |
| 143 | |
| 144 | server["name"] = opts.Name |
| 145 | server["imageRef"] = opts.ImageRef |
| 146 | server["flavorRef"] = opts.FlavorRef |
| 147 | |
| 148 | if opts.UserData != nil { |
| 149 | encoded := base64.StdEncoding.EncodeToString(opts.UserData) |
| 150 | server["user_data"] = &encoded |
| 151 | } |
| 152 | if opts.Personality != nil { |
| 153 | encoded := base64.StdEncoding.EncodeToString(opts.Personality) |
| 154 | server["personality"] = &encoded |
| 155 | } |
| 156 | if opts.ConfigDrive { |
| 157 | server["config_drive"] = "true" |
| 158 | } |
| 159 | if opts.AvailabilityZone != "" { |
| 160 | server["availability_zone"] = opts.AvailabilityZone |
| 161 | } |
| 162 | if opts.Metadata != nil { |
| 163 | server["metadata"] = opts.Metadata |
| 164 | } |
Jon Perritt | f3b2e14 | 2014-11-04 16:00:19 -0600 | [diff] [blame^] | 165 | if opts.AdminPass != "" { |
| 166 | server["adminPass"] = opts.AdminPass |
| 167 | } |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 168 | |
| 169 | if len(opts.SecurityGroups) > 0 { |
| 170 | securityGroups := make([]map[string]interface{}, len(opts.SecurityGroups)) |
| 171 | for i, groupName := range opts.SecurityGroups { |
| 172 | securityGroups[i] = map[string]interface{}{"name": groupName} |
| 173 | } |
| 174 | } |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 175 | |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 176 | if len(opts.Networks) > 0 { |
| 177 | networks := make([]map[string]interface{}, len(opts.Networks)) |
| 178 | for i, net := range opts.Networks { |
| 179 | networks[i] = make(map[string]interface{}) |
| 180 | if net.UUID != "" { |
| 181 | networks[i]["uuid"] = net.UUID |
| 182 | } |
| 183 | if net.Port != "" { |
| 184 | networks[i]["port"] = net.Port |
| 185 | } |
| 186 | if net.FixedIP != "" { |
| 187 | networks[i]["fixed_ip"] = net.FixedIP |
| 188 | } |
| 189 | } |
Jon Perritt | 2a7797d | 2014-10-21 15:08:43 -0500 | [diff] [blame] | 190 | server["networks"] = networks |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 191 | } |
| 192 | |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 193 | return map[string]interface{}{"server": server}, nil |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 194 | } |
| 195 | |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 196 | // Create requests a server to be provisioned to the user in the current tenant. |
Ash Wilson | 2206a11 | 2014-10-02 10:57:38 -0400 | [diff] [blame] | 197 | func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) CreateResult { |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 198 | var res CreateResult |
| 199 | |
| 200 | reqBody, err := opts.ToServerCreateMap() |
| 201 | if err != nil { |
| 202 | res.Err = err |
| 203 | return res |
| 204 | } |
| 205 | |
| 206 | _, res.Err = perigee.Request("POST", listURL(client), perigee.Options{ |
| 207 | Results: &res.Body, |
| 208 | ReqBody: reqBody, |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 209 | MoreHeaders: client.AuthenticatedHeaders(), |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 210 | OkCodes: []int{202}, |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 211 | }) |
Jon Perritt | 4149d7c | 2014-10-23 21:23:46 -0500 | [diff] [blame] | 212 | return res |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | // Delete requests that a server previously provisioned be removed from your account. |
Jamie Hannaford | 34732fe | 2014-10-27 11:29:36 +0100 | [diff] [blame] | 216 | func Delete(client *gophercloud.ServiceClient, id string) DeleteResult { |
| 217 | var res DeleteResult |
| 218 | _, res.Err = perigee.Request("DELETE", deleteURL(client, id), perigee.Options{ |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 219 | MoreHeaders: client.AuthenticatedHeaders(), |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 220 | OkCodes: []int{204}, |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 221 | }) |
Jamie Hannaford | 34732fe | 2014-10-27 11:29:36 +0100 | [diff] [blame] | 222 | return res |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Ash Wilson | 7ddf036 | 2014-09-17 10:59:09 -0400 | [diff] [blame] | 225 | // Get requests details on a single server, by ID. |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 226 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
| 227 | var result GetResult |
Jon Perritt | 703bfc0 | 2014-10-08 14:35:00 -0500 | [diff] [blame] | 228 | _, result.Err = perigee.Request("GET", getURL(client, id), perigee.Options{ |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 229 | Results: &result.Body, |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 230 | MoreHeaders: client.AuthenticatedHeaders(), |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 231 | }) |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 232 | return result |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Alex Gaynor | a6d5f9f | 2014-10-27 10:52:32 -0700 | [diff] [blame] | 235 | // UpdateOptsBuilder allows extensions to add additional attributes to the Update request. |
Jon Perritt | 8204821 | 2014-10-13 22:33:13 -0500 | [diff] [blame] | 236 | type UpdateOptsBuilder interface { |
Ash Wilson | e45c973 | 2014-09-29 10:54:12 -0400 | [diff] [blame] | 237 | ToServerUpdateMap() map[string]interface{} |
Ash Wilson | dcbc8fb | 2014-09-29 09:05:44 -0400 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | // UpdateOpts specifies the base attributes that may be updated on an existing server. |
| 241 | type UpdateOpts struct { |
| 242 | // Name [optional] changes the displayed name of the server. |
| 243 | // The server host name will *not* change. |
| 244 | // Server names are not constrained to be unique, even within the same tenant. |
| 245 | Name string |
| 246 | |
| 247 | // AccessIPv4 [optional] provides a new IPv4 address for the instance. |
| 248 | AccessIPv4 string |
| 249 | |
| 250 | // AccessIPv6 [optional] provides a new IPv6 address for the instance. |
| 251 | AccessIPv6 string |
| 252 | } |
| 253 | |
Ash Wilson | e45c973 | 2014-09-29 10:54:12 -0400 | [diff] [blame] | 254 | // ToServerUpdateMap formats an UpdateOpts structure into a request body. |
| 255 | func (opts UpdateOpts) ToServerUpdateMap() map[string]interface{} { |
Ash Wilson | dcbc8fb | 2014-09-29 09:05:44 -0400 | [diff] [blame] | 256 | server := make(map[string]string) |
| 257 | if opts.Name != "" { |
| 258 | server["name"] = opts.Name |
| 259 | } |
| 260 | if opts.AccessIPv4 != "" { |
| 261 | server["accessIPv4"] = opts.AccessIPv4 |
| 262 | } |
| 263 | if opts.AccessIPv6 != "" { |
| 264 | server["accessIPv6"] = opts.AccessIPv6 |
| 265 | } |
| 266 | return map[string]interface{}{"server": server} |
| 267 | } |
| 268 | |
Samuel A. Falvo II | 22d3b77 | 2014-02-13 19:27:05 -0800 | [diff] [blame] | 269 | // Update requests that various attributes of the indicated server be changed. |
Jon Perritt | 8204821 | 2014-10-13 22:33:13 -0500 | [diff] [blame] | 270 | func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) UpdateResult { |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 271 | var result UpdateResult |
Jon Perritt | 703bfc0 | 2014-10-08 14:35:00 -0500 | [diff] [blame] | 272 | _, result.Err = perigee.Request("PUT", updateURL(client, id), perigee.Options{ |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 273 | Results: &result.Body, |
Ash Wilson | e45c973 | 2014-09-29 10:54:12 -0400 | [diff] [blame] | 274 | ReqBody: opts.ToServerUpdateMap(), |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 275 | MoreHeaders: client.AuthenticatedHeaders(), |
Samuel A. Falvo II | 22d3b77 | 2014-02-13 19:27:05 -0800 | [diff] [blame] | 276 | }) |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 277 | return result |
Samuel A. Falvo II | 22d3b77 | 2014-02-13 19:27:05 -0800 | [diff] [blame] | 278 | } |
Samuel A. Falvo II | ca5f9a3 | 2014-03-11 17:52:58 -0700 | [diff] [blame] | 279 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 280 | // ChangeAdminPassword alters the administrator or root password for a specified server. |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 281 | func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) ActionResult { |
Ash Wilson | dc7daa8 | 2014-09-23 16:34:42 -0400 | [diff] [blame] | 282 | var req struct { |
| 283 | ChangePassword struct { |
| 284 | AdminPass string `json:"adminPass"` |
| 285 | } `json:"changePassword"` |
| 286 | } |
| 287 | |
| 288 | req.ChangePassword.AdminPass = newPassword |
| 289 | |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 290 | var res ActionResult |
| 291 | |
| 292 | _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{ |
Ash Wilson | dc7daa8 | 2014-09-23 16:34:42 -0400 | [diff] [blame] | 293 | ReqBody: req, |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 294 | MoreHeaders: client.AuthenticatedHeaders(), |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 295 | OkCodes: []int{202}, |
Samuel A. Falvo II | ca5f9a3 | 2014-03-11 17:52:58 -0700 | [diff] [blame] | 296 | }) |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 297 | |
| 298 | return res |
Samuel A. Falvo II | ca5f9a3 | 2014-03-11 17:52:58 -0700 | [diff] [blame] | 299 | } |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 300 | |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 301 | // ErrArgument errors occur when an argument supplied to a package function |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 302 | // fails to fall within acceptable values. For example, the Reboot() function |
| 303 | // expects the "how" parameter to be one of HardReboot or SoftReboot. These |
| 304 | // constants are (currently) strings, leading someone to wonder if they can pass |
| 305 | // other string values instead, perhaps in an effort to break the API of their |
| 306 | // provider. Reboot() returns this error in this situation. |
| 307 | // |
| 308 | // Function identifies which function was called/which function is generating |
| 309 | // the error. |
| 310 | // Argument identifies which formal argument was responsible for producing the |
| 311 | // error. |
| 312 | // Value provides the value as it was passed into the function. |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 313 | type ErrArgument struct { |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 314 | Function, Argument string |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 315 | Value interface{} |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | // Error yields a useful diagnostic for debugging purposes. |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 319 | func (e *ErrArgument) Error() string { |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 320 | return fmt.Sprintf("Bad argument in call to %s, formal parameter %s, value %#v", e.Function, e.Argument, e.Value) |
| 321 | } |
| 322 | |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 323 | func (e *ErrArgument) String() string { |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 324 | return e.Error() |
| 325 | } |
| 326 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 327 | // RebootMethod describes the mechanisms by which a server reboot can be requested. |
| 328 | type RebootMethod string |
| 329 | |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 330 | // These constants determine how a server should be rebooted. |
| 331 | // See the Reboot() function for further details. |
| 332 | const ( |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 333 | SoftReboot RebootMethod = "SOFT" |
| 334 | HardReboot RebootMethod = "HARD" |
| 335 | OSReboot = SoftReboot |
| 336 | PowerCycle = HardReboot |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 337 | ) |
| 338 | |
| 339 | // Reboot requests that a given server reboot. |
| 340 | // Two methods exist for rebooting a server: |
| 341 | // |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 342 | // HardReboot (aka PowerCycle) restarts the server instance by physically cutting power to the machine, or if a VM, |
| 343 | // terminating it at the hypervisor level. |
| 344 | // It's done. Caput. Full stop. |
| 345 | // Then, after a brief while, power is restored or the VM instance restarted. |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 346 | // |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 347 | // SoftReboot (aka OSReboot) simply tells the OS to restart under its own procedures. |
| 348 | // E.g., in Linux, asking it to enter runlevel 6, or executing "sudo shutdown -r now", or by asking Windows to restart the machine. |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 349 | func Reboot(client *gophercloud.ServiceClient, id string, how RebootMethod) ActionResult { |
| 350 | var res ActionResult |
| 351 | |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 352 | if (how != SoftReboot) && (how != HardReboot) { |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 353 | res.Err = &ErrArgument{ |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 354 | Function: "Reboot", |
| 355 | Argument: "how", |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 356 | Value: how, |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 357 | } |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 358 | return res |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 359 | } |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 360 | |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 361 | _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{ |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 362 | ReqBody: struct { |
| 363 | C map[string]string `json:"reboot"` |
| 364 | }{ |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 365 | map[string]string{"type": string(how)}, |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 366 | }, |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 367 | MoreHeaders: client.AuthenticatedHeaders(), |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 368 | OkCodes: []int{202}, |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 369 | }) |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 370 | |
| 371 | return res |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 372 | } |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 373 | |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 374 | // RebuildOptsBuilder is an interface that allows extensions to override the |
| 375 | // default behaviour of rebuild options |
| 376 | type RebuildOptsBuilder interface { |
| 377 | ToServerRebuildMap() (map[string]interface{}, error) |
| 378 | } |
| 379 | |
| 380 | // RebuildOpts represents the configuration options used in a server rebuild |
| 381 | // operation |
| 382 | type RebuildOpts struct { |
| 383 | // Required. The ID of the image you want your server to be provisioned on |
Jamie Hannaford | dcb8c27 | 2014-10-16 16:34:41 +0200 | [diff] [blame] | 384 | ImageID string |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 385 | |
| 386 | // Name to set the server to |
Jamie Hannaford | dcb8c27 | 2014-10-16 16:34:41 +0200 | [diff] [blame] | 387 | Name string |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 388 | |
| 389 | // Required. The server's admin password |
Jamie Hannaford | dcb8c27 | 2014-10-16 16:34:41 +0200 | [diff] [blame] | 390 | AdminPass string |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 391 | |
| 392 | // AccessIPv4 [optional] provides a new IPv4 address for the instance. |
Jamie Hannaford | dcb8c27 | 2014-10-16 16:34:41 +0200 | [diff] [blame] | 393 | AccessIPv4 string |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 394 | |
| 395 | // AccessIPv6 [optional] provides a new IPv6 address for the instance. |
Jamie Hannaford | dcb8c27 | 2014-10-16 16:34:41 +0200 | [diff] [blame] | 396 | AccessIPv6 string |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 397 | |
| 398 | // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server. |
Jamie Hannaford | dcb8c27 | 2014-10-16 16:34:41 +0200 | [diff] [blame] | 399 | Metadata map[string]string |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 400 | |
| 401 | // Personality [optional] includes the path and contents of a file to inject into the server at launch. |
| 402 | // The maximum size of the file is 255 bytes (decoded). |
Jamie Hannaford | dcb8c27 | 2014-10-16 16:34:41 +0200 | [diff] [blame] | 403 | Personality []byte |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | // ToServerRebuildMap formats a RebuildOpts struct into a map for use in JSON |
| 407 | func (opts RebuildOpts) ToServerRebuildMap() (map[string]interface{}, error) { |
| 408 | var err error |
| 409 | server := make(map[string]interface{}) |
| 410 | |
| 411 | if opts.AdminPass == "" { |
| 412 | err = fmt.Errorf("AdminPass is required") |
| 413 | } |
| 414 | |
| 415 | if opts.ImageID == "" { |
| 416 | err = fmt.Errorf("ImageID is required") |
| 417 | } |
| 418 | |
| 419 | if err != nil { |
| 420 | return server, err |
| 421 | } |
| 422 | |
| 423 | server["name"] = opts.Name |
| 424 | server["adminPass"] = opts.AdminPass |
| 425 | server["imageRef"] = opts.ImageID |
| 426 | |
| 427 | if opts.AccessIPv4 != "" { |
| 428 | server["accessIPv4"] = opts.AccessIPv4 |
| 429 | } |
| 430 | |
| 431 | if opts.AccessIPv6 != "" { |
| 432 | server["accessIPv6"] = opts.AccessIPv6 |
| 433 | } |
| 434 | |
| 435 | if opts.Metadata != nil { |
| 436 | server["metadata"] = opts.Metadata |
| 437 | } |
| 438 | |
| 439 | if opts.Personality != nil { |
| 440 | encoded := base64.StdEncoding.EncodeToString(opts.Personality) |
| 441 | server["personality"] = &encoded |
| 442 | } |
| 443 | |
| 444 | return map[string]interface{}{"rebuild": server}, nil |
| 445 | } |
| 446 | |
| 447 | // Rebuild will reprovision the server according to the configuration options |
| 448 | // provided in the RebuildOpts struct. |
| 449 | func Rebuild(client *gophercloud.ServiceClient, id string, opts RebuildOptsBuilder) RebuildResult { |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 450 | var result RebuildResult |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 451 | |
| 452 | if id == "" { |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 453 | result.Err = fmt.Errorf("ID is required") |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 454 | return result |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 457 | reqBody, err := opts.ToServerRebuildMap() |
| 458 | if err != nil { |
| 459 | result.Err = err |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 460 | return result |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Ash Wilson | 31f6bde | 2014-09-25 14:52:12 -0400 | [diff] [blame] | 463 | _, result.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{ |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 464 | ReqBody: &reqBody, |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 465 | Results: &result.Body, |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 466 | MoreHeaders: client.AuthenticatedHeaders(), |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 467 | OkCodes: []int{202}, |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 468 | }) |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 469 | |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 470 | return result |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Ash Wilson | 5f7cf18 | 2014-10-23 08:35:24 -0400 | [diff] [blame] | 473 | // ResizeOptsBuilder is an interface that allows extensions to override the default structure of |
| 474 | // a Resize request. |
| 475 | type ResizeOptsBuilder interface { |
| 476 | ToServerResizeMap() (map[string]interface{}, error) |
| 477 | } |
| 478 | |
| 479 | // ResizeOpts represents the configuration options used to control a Resize operation. |
| 480 | type ResizeOpts struct { |
| 481 | // FlavorRef is the ID of the flavor you wish your server to become. |
| 482 | FlavorRef string |
| 483 | } |
| 484 | |
| 485 | // ToServerResizeMap formats a ResizeOpts as a map that can be used as a JSON request body to the |
| 486 | // Resize request. |
| 487 | func (opts ResizeOpts) ToServerResizeMap() (map[string]interface{}, error) { |
| 488 | resize := map[string]interface{}{ |
| 489 | "flavorRef": opts.FlavorRef, |
| 490 | } |
| 491 | |
| 492 | return map[string]interface{}{"resize": resize}, nil |
| 493 | } |
| 494 | |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 495 | // Resize instructs the provider to change the flavor of the server. |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 496 | // Note that this implies rebuilding it. |
| 497 | // Unfortunately, one cannot pass rebuild parameters to the resize function. |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 498 | // When the resize completes, the server will be in RESIZE_VERIFY state. |
| 499 | // While in this state, you can explore the use of the new server's configuration. |
| 500 | // If you like it, call ConfirmResize() to commit the resize permanently. |
| 501 | // Otherwise, call RevertResize() to restore the old configuration. |
Ash Wilson | 9e87a92 | 2014-10-23 14:29:22 -0400 | [diff] [blame] | 502 | func Resize(client *gophercloud.ServiceClient, id string, opts ResizeOptsBuilder) ActionResult { |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 503 | var res ActionResult |
Ash Wilson | 5f7cf18 | 2014-10-23 08:35:24 -0400 | [diff] [blame] | 504 | reqBody, err := opts.ToServerResizeMap() |
| 505 | if err != nil { |
| 506 | res.Err = err |
| 507 | return res |
| 508 | } |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 509 | |
| 510 | _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{ |
Ash Wilson | 5f7cf18 | 2014-10-23 08:35:24 -0400 | [diff] [blame] | 511 | ReqBody: reqBody, |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 512 | MoreHeaders: client.AuthenticatedHeaders(), |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 513 | OkCodes: []int{202}, |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 514 | }) |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 515 | |
| 516 | return res |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | // ConfirmResize confirms a previous resize operation on a server. |
| 520 | // See Resize() for more details. |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 521 | func ConfirmResize(client *gophercloud.ServiceClient, id string) ActionResult { |
| 522 | var res ActionResult |
| 523 | |
| 524 | _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{ |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 525 | ReqBody: map[string]interface{}{"confirmResize": nil}, |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 526 | MoreHeaders: client.AuthenticatedHeaders(), |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 527 | OkCodes: []int{204}, |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 528 | }) |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 529 | |
| 530 | return res |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | // RevertResize cancels a previous resize operation on a server. |
| 534 | // See Resize() for more details. |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 535 | func RevertResize(client *gophercloud.ServiceClient, id string) ActionResult { |
| 536 | var res ActionResult |
| 537 | |
| 538 | _, res.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{ |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 539 | ReqBody: map[string]interface{}{"revertResize": nil}, |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 540 | MoreHeaders: client.AuthenticatedHeaders(), |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 541 | OkCodes: []int{202}, |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 542 | }) |
Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 543 | |
| 544 | return res |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 545 | } |