Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 1 | package servers |
| 2 | |
| 3 | import ( |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 4 | "fmt" |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 5 | "github.com/racker/perigee" |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 6 | ) |
| 7 | |
| 8 | // ListResult abstracts the raw results of making a List() request against the |
| 9 | // API. As OpenStack extensions may freely alter the response bodies of |
| 10 | // structures returned to the client, you may only safely access the data |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 11 | // provided through separate, type-safe accessors or methods. |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 12 | type ListResult map[string]interface{} |
| 13 | |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 14 | // ServerResult abstracts a single server description, |
| 15 | // as returned by the OpenStack provider. |
| 16 | // As OpenStack extensions may freely alter the response bodies of the |
| 17 | // structures returned to the client, |
| 18 | // you may only safely access the data provided through |
| 19 | // separate, type-safe accessors or methods. |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 20 | type ServerResult map[string]interface{} |
| 21 | |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 22 | // List makes a request against the API to list servers accessible to you. |
| 23 | func List(c *Client) (ListResult, error) { |
| 24 | var lr ListResult |
| 25 | |
| 26 | h, err := c.getListHeaders() |
| 27 | if err != nil { |
| 28 | return nil, err |
| 29 | } |
| 30 | |
| 31 | err = perigee.Get(c.getListUrl(), perigee.Options{ |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 32 | Results: &lr, |
Samuel A. Falvo II | c007c27 | 2014-02-10 20:49:26 -0800 | [diff] [blame] | 33 | MoreHeaders: h, |
| 34 | }) |
| 35 | return lr, err |
| 36 | } |
| 37 | |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 38 | // Create requests a server to be provisioned to the user in the current tenant. |
| 39 | func Create(c *Client, opts map[string]interface{}) (ServerResult, error) { |
| 40 | var sr ServerResult |
| 41 | |
| 42 | h, err := c.getCreateHeaders() |
| 43 | if err != nil { |
| 44 | return nil, err |
| 45 | } |
| 46 | |
| 47 | err = perigee.Post(c.getCreateUrl(), perigee.Options{ |
| 48 | Results: &sr, |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 49 | ReqBody: map[string]interface{}{ |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 50 | "server": opts, |
| 51 | }, |
| 52 | MoreHeaders: h, |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 53 | OkCodes: []int{202}, |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 54 | }) |
| 55 | return sr, err |
| 56 | } |
| 57 | |
| 58 | // Delete requests that a server previously provisioned be removed from your account. |
| 59 | func Delete(c *Client, id string) error { |
| 60 | h, err := c.getDeleteHeaders() |
| 61 | if err != nil { |
| 62 | return err |
| 63 | } |
| 64 | |
| 65 | err = perigee.Delete(c.getDeleteUrl(id), perigee.Options{ |
| 66 | MoreHeaders: h, |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 67 | OkCodes: []int{204}, |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 68 | }) |
| 69 | return err |
| 70 | } |
| 71 | |
| 72 | // GetDetail requests details on a single server, by ID. |
| 73 | func GetDetail(c *Client, id string) (ServerResult, error) { |
| 74 | var sr ServerResult |
| 75 | |
| 76 | h, err := c.getDetailHeaders() |
| 77 | if err != nil { |
| 78 | return nil, err |
| 79 | } |
| 80 | |
| 81 | err = perigee.Get(c.getDetailUrl(id), perigee.Options{ |
Samuel A. Falvo II | e246ac0 | 2014-02-13 23:20:09 -0800 | [diff] [blame] | 82 | Results: &sr, |
Samuel A. Falvo II | ce00073 | 2014-02-13 18:53:53 -0800 | [diff] [blame] | 83 | MoreHeaders: h, |
| 84 | }) |
| 85 | return sr, err |
| 86 | } |
| 87 | |
Samuel A. Falvo II | 22d3b77 | 2014-02-13 19:27:05 -0800 | [diff] [blame] | 88 | // Update requests that various attributes of the indicated server be changed. |
| 89 | func Update(c *Client, id string, opts map[string]interface{}) (ServerResult, error) { |
| 90 | var sr ServerResult |
| 91 | |
| 92 | h, err := c.getUpdateHeaders() |
| 93 | if err != nil { |
| 94 | return nil, err |
| 95 | } |
| 96 | |
| 97 | err = perigee.Put(c.getUpdateUrl(id), perigee.Options{ |
| 98 | Results: &sr, |
| 99 | ReqBody: map[string]interface{}{ |
| 100 | "server": opts, |
| 101 | }, |
| 102 | MoreHeaders: h, |
| 103 | }) |
| 104 | return sr, err |
| 105 | } |
Samuel A. Falvo II | ca5f9a3 | 2014-03-11 17:52:58 -0700 | [diff] [blame] | 106 | |
| 107 | // ChangeAdminPassword alters the administrator or root password for a specified |
| 108 | // server. |
| 109 | func ChangeAdminPassword(c *Client, id, newPassword string) error { |
| 110 | h, err := c.getActionHeaders() |
| 111 | if err != nil { |
| 112 | return err |
| 113 | } |
| 114 | |
| 115 | err = perigee.Post(c.getActionUrl(id), perigee.Options{ |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 116 | ReqBody: struct { |
| 117 | C map[string]string `json:"changePassword"` |
| 118 | }{ |
Samuel A. Falvo II | ca5f9a3 | 2014-03-11 17:52:58 -0700 | [diff] [blame] | 119 | map[string]string{"adminPass": newPassword}, |
| 120 | }, |
| 121 | MoreHeaders: h, |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 122 | OkCodes: []int{202}, |
Samuel A. Falvo II | ca5f9a3 | 2014-03-11 17:52:58 -0700 | [diff] [blame] | 123 | }) |
| 124 | return err |
| 125 | } |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 126 | |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 127 | // 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] | 128 | // fails to fall within acceptable values. For example, the Reboot() function |
| 129 | // expects the "how" parameter to be one of HardReboot or SoftReboot. These |
| 130 | // constants are (currently) strings, leading someone to wonder if they can pass |
| 131 | // other string values instead, perhaps in an effort to break the API of their |
| 132 | // provider. Reboot() returns this error in this situation. |
| 133 | // |
| 134 | // Function identifies which function was called/which function is generating |
| 135 | // the error. |
| 136 | // Argument identifies which formal argument was responsible for producing the |
| 137 | // error. |
| 138 | // 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] | 139 | type ErrArgument struct { |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 140 | Function, Argument string |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 141 | Value interface{} |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // Error yields a useful diagnostic for debugging purposes. |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 145 | func (e *ErrArgument) Error() string { |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 146 | return fmt.Sprintf("Bad argument in call to %s, formal parameter %s, value %#v", e.Function, e.Argument, e.Value) |
| 147 | } |
| 148 | |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 149 | func (e *ErrArgument) String() string { |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 150 | return e.Error() |
| 151 | } |
| 152 | |
| 153 | // These constants determine how a server should be rebooted. |
| 154 | // See the Reboot() function for further details. |
| 155 | const ( |
| 156 | SoftReboot = "SOFT" |
| 157 | HardReboot = "HARD" |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 158 | OSReboot = SoftReboot |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 159 | PowerCycle = HardReboot |
| 160 | ) |
| 161 | |
| 162 | // Reboot requests that a given server reboot. |
| 163 | // Two methods exist for rebooting a server: |
| 164 | // |
| 165 | // HardReboot (aka PowerCycle) -- restarts the server instance by physically |
| 166 | // cutting power to the machine, or if a VM, terminating it at the hypervisor |
| 167 | // level. It's done. Caput. Full stop. Then, after a brief while, power is |
| 168 | // restored or the VM instance restarted. |
| 169 | // |
| 170 | // SoftReboot (aka OSReboot). This approach simply tells the OS to restart |
| 171 | // under its own procedures. E.g., in Linux, asking it to enter runlevel 6, |
| 172 | // or executing "sudo shutdown -r now", or by wasking Windows to restart the |
| 173 | // machine. |
| 174 | func Reboot(c *Client, id, how string) error { |
| 175 | if (how != SoftReboot) && (how != HardReboot) { |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 176 | return &ErrArgument{ |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 177 | Function: "Reboot", |
| 178 | Argument: "how", |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 179 | Value: how, |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 182 | |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 183 | h, err := c.getActionHeaders() |
| 184 | if err != nil { |
| 185 | return err |
| 186 | } |
| 187 | |
| 188 | err = perigee.Post(c.getActionUrl(id), perigee.Options{ |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 189 | ReqBody: struct { |
| 190 | C map[string]string `json:"reboot"` |
| 191 | }{ |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 192 | map[string]string{"type": how}, |
| 193 | }, |
| 194 | MoreHeaders: h, |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 195 | OkCodes: []int{202}, |
Samuel A. Falvo II | 41c9f61 | 2014-03-11 19:00:10 -0700 | [diff] [blame] | 196 | }) |
| 197 | return err |
| 198 | } |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 199 | |
| 200 | // Rebuild requests that the Openstack provider reprovision the |
| 201 | // server. The rebuild will need to know the server's name and |
| 202 | // new image reference or ID. In addition, and unlike building |
| 203 | // a server with Create(), you must provide an administrator |
| 204 | // password. |
| 205 | // |
| 206 | // Additional options may be specified with the additional map. |
| 207 | // This function treats a nil map the same as an empty map. |
| 208 | // |
| 209 | // Rebuild returns a server result as though you had called |
| 210 | // GetDetail() on the server's ID. The information, however, |
| 211 | // refers to the new server, not the old. |
| 212 | func Rebuild(c *Client, id, name, password, imageRef string, additional map[string]interface{}) (ServerResult, error) { |
| 213 | var sr ServerResult |
| 214 | |
| 215 | if id == "" { |
| 216 | return sr, &ErrArgument{ |
| 217 | Function: "Rebuild", |
| 218 | Argument: "id", |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 219 | Value: "", |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
| 223 | if name == "" { |
| 224 | return sr, &ErrArgument{ |
| 225 | Function: "Rebuild", |
| 226 | Argument: "name", |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 227 | Value: "", |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | if password == "" { |
| 232 | return sr, &ErrArgument{ |
| 233 | Function: "Rebuild", |
| 234 | Argument: "password", |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 235 | Value: "", |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
| 239 | if imageRef == "" { |
| 240 | return sr, &ErrArgument{ |
| 241 | Function: "Rebuild", |
| 242 | Argument: "imageRef", |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 243 | Value: "", |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
| 247 | if additional == nil { |
| 248 | additional = make(map[string]interface{}, 0) |
| 249 | } |
| 250 | |
| 251 | additional["name"] = name |
| 252 | additional["imageRef"] = imageRef |
| 253 | additional["adminPass"] = password |
| 254 | |
| 255 | h, err := c.getActionHeaders() |
| 256 | if err != nil { |
| 257 | return sr, err |
| 258 | } |
| 259 | |
| 260 | err = perigee.Post(c.getActionUrl(id), perigee.Options{ |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 261 | ReqBody: struct { |
| 262 | R map[string]interface{} `json:"rebuild"` |
| 263 | }{ |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 264 | additional, |
| 265 | }, |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 266 | Results: &sr, |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 267 | MoreHeaders: h, |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 268 | OkCodes: []int{202}, |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 269 | }) |
| 270 | return sr, err |
| 271 | } |
| 272 | |
| 273 | // Resize instructs the provider to change the flavor of the server. |
| 274 | // Note that this implies rebuilding it. Unfortunately, one cannot pass rebuild parameters to the resize function. |
| 275 | // When the resize completes, the server will be in RESIZE_VERIFY state. |
| 276 | // While in this state, you can explore the use of the new server's configuration. |
| 277 | // If you like it, call ConfirmResize() to commit the resize permanently. |
| 278 | // Otherwise, call RevertResize() to restore the old configuration. |
| 279 | func Resize(c *Client, id, flavorRef string) error { |
| 280 | h, err := c.getActionHeaders() |
| 281 | if err != nil { |
| 282 | return err |
| 283 | } |
| 284 | |
| 285 | err = perigee.Post(c.getActionUrl(id), perigee.Options{ |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 286 | ReqBody: struct { |
| 287 | R map[string]interface{} `json:"resize"` |
| 288 | }{ |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 289 | map[string]interface{}{"flavorRef": flavorRef}, |
| 290 | }, |
| 291 | MoreHeaders: h, |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 292 | OkCodes: []int{202}, |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 293 | }) |
| 294 | return err |
| 295 | } |
| 296 | |
| 297 | // ConfirmResize confirms a previous resize operation on a server. |
| 298 | // See Resize() for more details. |
| 299 | func ConfirmResize(c *Client, id string) error { |
| 300 | h, err := c.getActionHeaders() |
| 301 | if err != nil { |
| 302 | return err |
| 303 | } |
| 304 | |
| 305 | err = perigee.Post(c.getActionUrl(id), perigee.Options{ |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 306 | ReqBody: map[string]interface{}{"confirmResize": nil}, |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 307 | MoreHeaders: h, |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 308 | OkCodes: []int{204}, |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 309 | }) |
| 310 | return err |
| 311 | } |
| 312 | |
| 313 | // RevertResize cancels a previous resize operation on a server. |
| 314 | // See Resize() for more details. |
| 315 | func RevertResize(c *Client, id string) error { |
| 316 | h, err := c.getActionHeaders() |
| 317 | if err != nil { |
| 318 | return err |
| 319 | } |
| 320 | |
| 321 | err = perigee.Post(c.getActionUrl(id), perigee.Options{ |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 322 | ReqBody: map[string]interface{}{"revertResize": nil}, |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 323 | MoreHeaders: h, |
Jon Perritt | 3055864 | 2014-04-14 17:07:12 -0500 | [diff] [blame] | 324 | OkCodes: []int{202}, |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 325 | }) |
| 326 | return err |
| 327 | } |