func withServer(api gophercloud.CloudServersProvider, f func(string)) { id, err := createServer(api, "", "", "", "") if err != nil { panic(err) } for { s, err := api.ServerById(id) if err != nil { panic(err) } if s.Status == "ACTIVE" { break } time.Sleep(10 * time.Second) } f(id) // I've learned that resizing an instance can fail if a delete request // comes in prior to its completion. This ends up leaving the server // in an error state, and neither the resize NOR the delete complete. // This is a bug in OpenStack, as far as I'm concerned, but thankfully, // there's an easy work-around -- just wait for your server to return to // active state first! waitForServerState(api, id, "ACTIVE") err = api.DeleteServerById(id) if err != nil { panic(err) } }
// waitForServerState polls, every 10 seconds, for a given server to appear in the indicated state. // This call will block forever if it never appears in the desired state, so if a timeout is required, // make sure to call this function in a goroutine. func waitForServerState(api gophercloud.CloudServersProvider, id, state string) error { for { s, err := api.ServerById(id) if err != nil { return err } if s.Status == state { return nil } time.Sleep(10 * time.Second) } panic("Impossible") }
func tryAllAddresses(id string, api gophercloud.CloudServersProvider) { log("Getting the server instance") s, err := api.ServerById(id) if err != nil { panic(err) } log("Getting the complete set of pools") ps, err := s.AllAddressPools() if err != nil { panic(err) } log("Listing IPs for each pool") for k, v := range ps { log(fmt.Sprintf(" Pool %s", k)) for _, a := range v { log(fmt.Sprintf(" IP: %s, Version: %d", a.Addr, a.Version)) } } }