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 | |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 12 | // List makes a request against the API to list servers accessible to you. |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 13 | func List(client *gophercloud.ServiceClient) pagination.Pager { |
| 14 | createPage := func(r pagination.LastHTTPResponse) pagination.Page { |
Ash Wilson | 397c78b | 2014-09-25 15:19:14 -0400 | [diff] [blame] | 15 | return ServerPage{pagination.LinkedPageBase{LastHTTPResponse: r}} |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 16 | } |
| 17 | |
Ash Wilson | 31f6bde | 2014-09-25 14:52:12 -0400 | [diff] [blame] | 18 | return pagination.NewPager(client, detailURL(client), createPage) |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 19 | } |
| 20 | |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 21 | // CreateOptsLike describes struct types that can be accepted by the Create call. |
| 22 | // The CreateOpts struct in this package does. |
| 23 | type CreateOptsLike interface { |
Ash Wilson | e45c973 | 2014-09-29 10:54:12 -0400 | [diff] [blame^] | 24 | ToServerCreateMap() map[string]interface{} |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | // Network is used within CreateOpts to control a new server's network attachments. |
| 28 | type Network struct { |
| 29 | // UUID of a nova-network to attach to the newly provisioned server. |
| 30 | // Required unless Port is provided. |
| 31 | UUID string |
| 32 | |
| 33 | // Port of a neutron network to attach to the newly provisioned server. |
| 34 | // Required unless UUID is provided. |
| 35 | Port string |
| 36 | |
| 37 | // FixedIP [optional] specifies a fixed IPv4 address to be used on this network. |
| 38 | FixedIP string |
| 39 | } |
| 40 | |
| 41 | // CreateOpts specifies server creation parameters. |
| 42 | type CreateOpts struct { |
| 43 | // Name [required] is the name to assign to the newly launched server. |
| 44 | Name string |
| 45 | |
| 46 | // ImageRef [required] is the ID or full URL to the image that contains the server's OS and initial state. |
| 47 | // Optional if using the boot-from-volume extension. |
| 48 | ImageRef string |
| 49 | |
| 50 | // FlavorRef [required] is the ID or full URL to the flavor that describes the server's specs. |
| 51 | FlavorRef string |
| 52 | |
| 53 | // SecurityGroups [optional] lists the names of the security groups to which this server should belong. |
| 54 | SecurityGroups []string |
| 55 | |
| 56 | // UserData [optional] contains configuration information or scripts to use upon launch. |
| 57 | // Create will base64-encode it for you. |
| 58 | UserData []byte |
| 59 | |
| 60 | // AvailabilityZone [optional] in which to launch the server. |
| 61 | AvailabilityZone string |
| 62 | |
| 63 | // Networks [optional] dictates how this server will be attached to available networks. |
| 64 | // By default, the server will be attached to all isolated networks for the tenant. |
| 65 | Networks []Network |
| 66 | |
| 67 | // Metadata [optional] contains key-value pairs (up to 255 bytes each) to attach to the server. |
| 68 | Metadata map[string]string |
| 69 | |
| 70 | // Personality [optional] includes the path and contents of a file to inject into the server at launch. |
| 71 | // The maximum size of the file is 255 bytes (decoded). |
| 72 | Personality []byte |
| 73 | |
| 74 | // ConfigDrive [optional] enables metadata injection through a configuration drive. |
| 75 | ConfigDrive bool |
| 76 | } |
| 77 | |
Ash Wilson | e45c973 | 2014-09-29 10:54:12 -0400 | [diff] [blame^] | 78 | // ToServerCreateMap assembles a request body based on the contents of a CreateOpts. |
| 79 | func (opts CreateOpts) ToServerCreateMap() map[string]interface{} { |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 80 | server := make(map[string]interface{}) |
| 81 | |
| 82 | server["name"] = opts.Name |
| 83 | server["imageRef"] = opts.ImageRef |
| 84 | server["flavorRef"] = opts.FlavorRef |
| 85 | |
| 86 | if opts.UserData != nil { |
| 87 | encoded := base64.StdEncoding.EncodeToString(opts.UserData) |
| 88 | server["user_data"] = &encoded |
| 89 | } |
| 90 | if opts.Personality != nil { |
| 91 | encoded := base64.StdEncoding.EncodeToString(opts.Personality) |
| 92 | server["personality"] = &encoded |
| 93 | } |
| 94 | if opts.ConfigDrive { |
| 95 | server["config_drive"] = "true" |
| 96 | } |
| 97 | if opts.AvailabilityZone != "" { |
| 98 | server["availability_zone"] = opts.AvailabilityZone |
| 99 | } |
| 100 | if opts.Metadata != nil { |
| 101 | server["metadata"] = opts.Metadata |
| 102 | } |
| 103 | |
| 104 | if len(opts.SecurityGroups) > 0 { |
| 105 | securityGroups := make([]map[string]interface{}, len(opts.SecurityGroups)) |
| 106 | for i, groupName := range opts.SecurityGroups { |
| 107 | securityGroups[i] = map[string]interface{}{"name": groupName} |
| 108 | } |
| 109 | } |
| 110 | if len(opts.Networks) > 0 { |
| 111 | networks := make([]map[string]interface{}, len(opts.Networks)) |
| 112 | for i, net := range opts.Networks { |
| 113 | networks[i] = make(map[string]interface{}) |
| 114 | if net.UUID != "" { |
| 115 | networks[i]["uuid"] = net.UUID |
| 116 | } |
| 117 | if net.Port != "" { |
| 118 | networks[i]["port"] = net.Port |
| 119 | } |
| 120 | if net.FixedIP != "" { |
| 121 | networks[i]["fixed_ip"] = net.FixedIP |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return map[string]interface{}{"server": server} |
| 127 | } |
| 128 | |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 129 | // Create requests a server to be provisioned to the user in the current tenant. |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 130 | func Create(client *gophercloud.ServiceClient, opts CreateOptsLike) CreateResult { |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 131 | var result CreateResult |
Ash Wilson | 31f6bde | 2014-09-25 14:52:12 -0400 | [diff] [blame] | 132 | _, result.Err = perigee.Request("POST", listURL(client), perigee.Options{ |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 133 | Results: &result.Resp, |
Ash Wilson | e45c973 | 2014-09-29 10:54:12 -0400 | [diff] [blame^] | 134 | ReqBody: opts.ToServerCreateMap(), |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 135 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 136 | OkCodes: []int{202}, |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 137 | }) |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 138 | return result |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | // Delete requests that a server previously provisioned be removed from your account. |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 142 | func Delete(client *gophercloud.ServiceClient, id string) error { |
Ash Wilson | 31f6bde | 2014-09-25 14:52:12 -0400 | [diff] [blame] | 143 | _, err := perigee.Request("DELETE", serverURL(client, id), perigee.Options{ |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 144 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 145 | OkCodes: []int{204}, |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 146 | }) |
| 147 | return err |
| 148 | } |
| 149 | |
Ash Wilson | 7ddf036 | 2014-09-17 10:59:09 -0400 | [diff] [blame] | 150 | // Get requests details on a single server, by ID. |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 151 | func Get(client *gophercloud.ServiceClient, id string) GetResult { |
| 152 | var result GetResult |
Ash Wilson | 31f6bde | 2014-09-25 14:52:12 -0400 | [diff] [blame] | 153 | _, result.Err = perigee.Request("GET", serverURL(client, id), perigee.Options{ |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 154 | Results: &result.Resp, |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 155 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 156 | }) |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 157 | return result |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 158 | } |
| 159 | |
Ash Wilson | dcbc8fb | 2014-09-29 09:05:44 -0400 | [diff] [blame] | 160 | // UpdateOptsLike allows extentions to add additional attributes to the Update request. |
| 161 | type UpdateOptsLike interface { |
Ash Wilson | e45c973 | 2014-09-29 10:54:12 -0400 | [diff] [blame^] | 162 | ToServerUpdateMap() map[string]interface{} |
Ash Wilson | dcbc8fb | 2014-09-29 09:05:44 -0400 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | // UpdateOpts specifies the base attributes that may be updated on an existing server. |
| 166 | type UpdateOpts struct { |
| 167 | // Name [optional] changes the displayed name of the server. |
| 168 | // The server host name will *not* change. |
| 169 | // Server names are not constrained to be unique, even within the same tenant. |
| 170 | Name string |
| 171 | |
| 172 | // AccessIPv4 [optional] provides a new IPv4 address for the instance. |
| 173 | AccessIPv4 string |
| 174 | |
| 175 | // AccessIPv6 [optional] provides a new IPv6 address for the instance. |
| 176 | AccessIPv6 string |
| 177 | } |
| 178 | |
Ash Wilson | e45c973 | 2014-09-29 10:54:12 -0400 | [diff] [blame^] | 179 | // ToServerUpdateMap formats an UpdateOpts structure into a request body. |
| 180 | func (opts UpdateOpts) ToServerUpdateMap() map[string]interface{} { |
Ash Wilson | dcbc8fb | 2014-09-29 09:05:44 -0400 | [diff] [blame] | 181 | server := make(map[string]string) |
| 182 | if opts.Name != "" { |
| 183 | server["name"] = opts.Name |
| 184 | } |
| 185 | if opts.AccessIPv4 != "" { |
| 186 | server["accessIPv4"] = opts.AccessIPv4 |
| 187 | } |
| 188 | if opts.AccessIPv6 != "" { |
| 189 | server["accessIPv6"] = opts.AccessIPv6 |
| 190 | } |
| 191 | return map[string]interface{}{"server": server} |
| 192 | } |
| 193 | |
Samuel A. Falvo II | 22d3b77 | 2014-02-13 19:27:05 -0800 | [diff] [blame] | 194 | // Update requests that various attributes of the indicated server be changed. |
Ash Wilson | dcbc8fb | 2014-09-29 09:05:44 -0400 | [diff] [blame] | 195 | func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsLike) UpdateResult { |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 196 | var result UpdateResult |
Ash Wilson | 31f6bde | 2014-09-25 14:52:12 -0400 | [diff] [blame] | 197 | _, result.Err = perigee.Request("PUT", serverURL(client, id), perigee.Options{ |
Ash Wilson | dcbc8fb | 2014-09-29 09:05:44 -0400 | [diff] [blame] | 198 | Results: &result.Resp, |
Ash Wilson | e45c973 | 2014-09-29 10:54:12 -0400 | [diff] [blame^] | 199 | ReqBody: opts.ToServerUpdateMap(), |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 200 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Samuel A. Falvo II | 22d3b77 | 2014-02-13 19:27:05 -0800 | [diff] [blame] | 201 | }) |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 202 | return result |
Samuel A. Falvo II | 22d3b77 | 2014-02-13 19:27:05 -0800 | [diff] [blame] | 203 | } |
Samuel A. Falvo II | ca5f9a3 | 2014-03-11 17:52:58 -0700 | [diff] [blame] | 204 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 205 | // ChangeAdminPassword alters the administrator or root password for a specified server. |
| 206 | func ChangeAdminPassword(client *gophercloud.ServiceClient, id, newPassword string) error { |
Ash Wilson | dc7daa8 | 2014-09-23 16:34:42 -0400 | [diff] [blame] | 207 | var req struct { |
| 208 | ChangePassword struct { |
| 209 | AdminPass string `json:"adminPass"` |
| 210 | } `json:"changePassword"` |
| 211 | } |
| 212 | |
| 213 | req.ChangePassword.AdminPass = newPassword |
| 214 | |
Ash Wilson | 31f6bde | 2014-09-25 14:52:12 -0400 | [diff] [blame] | 215 | _, err := perigee.Request("POST", actionURL(client, id), perigee.Options{ |
Ash Wilson | dc7daa8 | 2014-09-23 16:34:42 -0400 | [diff] [blame] | 216 | ReqBody: req, |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 217 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 218 | OkCodes: []int{202}, |
Samuel A. Falvo II | ca5f9a3 | 2014-03-11 17:52:58 -0700 | [diff] [blame] | 219 | }) |
| 220 | return err |
| 221 | } |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 222 | |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 223 | // 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] | 224 | // fails to fall within acceptable values. For example, the Reboot() function |
| 225 | // expects the "how" parameter to be one of HardReboot or SoftReboot. These |
| 226 | // constants are (currently) strings, leading someone to wonder if they can pass |
| 227 | // other string values instead, perhaps in an effort to break the API of their |
| 228 | // provider. Reboot() returns this error in this situation. |
| 229 | // |
| 230 | // Function identifies which function was called/which function is generating |
| 231 | // the error. |
| 232 | // Argument identifies which formal argument was responsible for producing the |
| 233 | // error. |
| 234 | // 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] | 235 | type ErrArgument struct { |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 236 | Function, Argument string |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 237 | Value interface{} |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | // Error yields a useful diagnostic for debugging purposes. |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 241 | func (e *ErrArgument) Error() string { |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 242 | return fmt.Sprintf("Bad argument in call to %s, formal parameter %s, value %#v", e.Function, e.Argument, e.Value) |
| 243 | } |
| 244 | |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 245 | func (e *ErrArgument) String() string { |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 246 | return e.Error() |
| 247 | } |
| 248 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 249 | // RebootMethod describes the mechanisms by which a server reboot can be requested. |
| 250 | type RebootMethod string |
| 251 | |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 252 | // These constants determine how a server should be rebooted. |
| 253 | // See the Reboot() function for further details. |
| 254 | const ( |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 255 | SoftReboot RebootMethod = "SOFT" |
| 256 | HardReboot RebootMethod = "HARD" |
| 257 | OSReboot = SoftReboot |
| 258 | PowerCycle = HardReboot |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 259 | ) |
| 260 | |
| 261 | // Reboot requests that a given server reboot. |
| 262 | // Two methods exist for rebooting a server: |
| 263 | // |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 264 | // HardReboot (aka PowerCycle) restarts the server instance by physically cutting power to the machine, or if a VM, |
| 265 | // terminating it at the hypervisor level. |
| 266 | // It's done. Caput. Full stop. |
| 267 | // 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] | 268 | // |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 269 | // SoftReboot (aka OSReboot) simply tells the OS to restart under its own procedures. |
| 270 | // E.g., in Linux, asking it to enter runlevel 6, or executing "sudo shutdown -r now", or by asking Windows to restart the machine. |
| 271 | func Reboot(client *gophercloud.ServiceClient, id string, how RebootMethod) error { |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 272 | if (how != SoftReboot) && (how != HardReboot) { |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 273 | return &ErrArgument{ |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 274 | Function: "Reboot", |
| 275 | Argument: "how", |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 276 | Value: how, |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 277 | } |
| 278 | } |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 279 | |
Ash Wilson | 31f6bde | 2014-09-25 14:52:12 -0400 | [diff] [blame] | 280 | _, err := perigee.Request("POST", actionURL(client, id), perigee.Options{ |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 281 | ReqBody: struct { |
| 282 | C map[string]string `json:"reboot"` |
| 283 | }{ |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 284 | map[string]string{"type": string(how)}, |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 285 | }, |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 286 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 287 | OkCodes: []int{202}, |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 288 | }) |
| 289 | return err |
| 290 | } |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 291 | |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 292 | // Rebuild requests that the Openstack provider reprovision the server. |
| 293 | // The rebuild will need to know the server's name and new image reference or ID. |
| 294 | // In addition, and unlike building a server with Create(), you must provide an administrator password. |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 295 | // |
| 296 | // Additional options may be specified with the additional map. |
| 297 | // This function treats a nil map the same as an empty map. |
| 298 | // |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 299 | // Rebuild returns a server result as though you had called GetDetail() on the server's ID. |
| 300 | // The information, however, refers to the new server, not the old. |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 301 | func Rebuild(client *gophercloud.ServiceClient, id, name, password, imageRef string, additional map[string]interface{}) RebuildResult { |
| 302 | var result RebuildResult |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 303 | |
| 304 | if id == "" { |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 305 | result.Err = &ErrArgument{ |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 306 | Function: "Rebuild", |
| 307 | Argument: "id", |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 308 | Value: "", |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 309 | } |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 310 | return result |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | if name == "" { |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 314 | result.Err = &ErrArgument{ |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 315 | Function: "Rebuild", |
| 316 | Argument: "name", |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 317 | Value: "", |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 318 | } |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 319 | return result |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | if password == "" { |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 323 | result.Err = &ErrArgument{ |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 324 | Function: "Rebuild", |
| 325 | Argument: "password", |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 326 | Value: "", |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 327 | } |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 328 | return result |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | if imageRef == "" { |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 332 | result.Err = &ErrArgument{ |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 333 | Function: "Rebuild", |
| 334 | Argument: "imageRef", |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 335 | Value: "", |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 336 | } |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 337 | return result |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | if additional == nil { |
| 341 | additional = make(map[string]interface{}, 0) |
| 342 | } |
| 343 | |
| 344 | additional["name"] = name |
| 345 | additional["imageRef"] = imageRef |
| 346 | additional["adminPass"] = password |
| 347 | |
Ash Wilson | 31f6bde | 2014-09-25 14:52:12 -0400 | [diff] [blame] | 348 | _, result.Err = perigee.Request("POST", actionURL(client, id), perigee.Options{ |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 349 | ReqBody: struct { |
| 350 | R map[string]interface{} `json:"rebuild"` |
| 351 | }{ |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 352 | additional, |
| 353 | }, |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 354 | Results: &result.Resp, |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 355 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 356 | OkCodes: []int{202}, |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 357 | }) |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 358 | return result |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | // Resize instructs the provider to change the flavor of the server. |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 362 | // Note that this implies rebuilding it. |
| 363 | // Unfortunately, one cannot pass rebuild parameters to the resize function. |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 364 | // When the resize completes, the server will be in RESIZE_VERIFY state. |
| 365 | // While in this state, you can explore the use of the new server's configuration. |
| 366 | // If you like it, call ConfirmResize() to commit the resize permanently. |
| 367 | // Otherwise, call RevertResize() to restore the old configuration. |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 368 | func Resize(client *gophercloud.ServiceClient, id, flavorRef string) error { |
Ash Wilson | 31f6bde | 2014-09-25 14:52:12 -0400 | [diff] [blame] | 369 | _, err := perigee.Request("POST", actionURL(client, id), perigee.Options{ |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 370 | ReqBody: struct { |
| 371 | R map[string]interface{} `json:"resize"` |
| 372 | }{ |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 373 | map[string]interface{}{"flavorRef": flavorRef}, |
| 374 | }, |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 375 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 376 | OkCodes: []int{202}, |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 377 | }) |
| 378 | return err |
| 379 | } |
| 380 | |
| 381 | // ConfirmResize confirms a previous resize operation on a server. |
| 382 | // See Resize() for more details. |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 383 | func ConfirmResize(client *gophercloud.ServiceClient, id string) error { |
Ash Wilson | 31f6bde | 2014-09-25 14:52:12 -0400 | [diff] [blame] | 384 | _, err := perigee.Request("POST", actionURL(client, id), perigee.Options{ |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 385 | ReqBody: map[string]interface{}{"confirmResize": nil}, |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 386 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 387 | OkCodes: []int{204}, |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 388 | }) |
| 389 | return err |
| 390 | } |
| 391 | |
| 392 | // RevertResize cancels a previous resize operation on a server. |
| 393 | // See Resize() for more details. |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 394 | func RevertResize(client *gophercloud.ServiceClient, id string) error { |
Ash Wilson | 31f6bde | 2014-09-25 14:52:12 -0400 | [diff] [blame] | 395 | _, err := perigee.Request("POST", actionURL(client, id), perigee.Options{ |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 396 | ReqBody: map[string]interface{}{"revertResize": nil}, |
Ash Wilson | 01626a3 | 2014-09-17 10:38:07 -0400 | [diff] [blame] | 397 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 398 | OkCodes: []int{202}, |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 399 | }) |
| 400 | return err |
| 401 | } |