Workaround for gophercloud.ServerById crashing, fixes #1257

gophercloud.ServerById is broken in v0.1.0 - it will crash if you feed it a
non-existing server ID (see [rackspace/gophercloud #168](https://github.com/rackspace/gophercloud/issues/168))

Instead, list all servers and iterate over them. If the server id isn't found,
return "DELETED" as a state. Not perfect but it works until next version of
gophercloud is released.
This commit is contained in:
Ian Delahorne 2014-06-29 23:45:50 -05:00
parent 29d8bb605c
commit 946843982f
1 changed files with 12 additions and 2 deletions

View File

@ -33,12 +33,22 @@ type StateChangeConf struct {
// an openstacn server.
func ServerStateRefreshFunc(csp gophercloud.CloudServersProvider, s *gophercloud.Server) StateRefreshFunc {
return func() (interface{}, string, int, error) {
resp, err := csp.ServerById(s.Id)
servers, err := csp.ListServers()
if err != nil {
log.Printf("Error on ServerStateRefresh: %s", err)
return nil, "", 0, err
}
var resp *gophercloud.Server
found := false
for _, server := range servers {
if server.Id == s.Id {
found = true
resp = &server
}
}
if found == false {
return nil, "DELETED", 0, nil
}
return resp, resp.Status, resp.Progress, nil
}
}