Add WaitForStatus to OpenStack servers.
diff --git a/openstack/compute/v2/servers/util.go b/openstack/compute/v2/servers/util.go
new file mode 100644
index 0000000..e6baf74
--- /dev/null
+++ b/openstack/compute/v2/servers/util.go
@@ -0,0 +1,20 @@
+package servers
+
+import "github.com/rackspace/gophercloud"
+
+// WaitForStatus will continually poll a server until it successfully transitions to a specified
+// status. It will do this for at most the number of seconds specified.
+func WaitForStatus(c *gophercloud.ServiceClient, id, status string, secs int) error {
+ return gophercloud.WaitFor(secs, func() (bool, error) {
+ current, err := Get(c, id).Extract()
+ if err != nil {
+ return false, err
+ }
+
+ if current.Status == status {
+ return true, nil
+ }
+
+ return false, nil
+ })
+}
diff --git a/openstack/compute/v2/servers/util_test.go b/openstack/compute/v2/servers/util_test.go
new file mode 100644
index 0000000..e192ae3
--- /dev/null
+++ b/openstack/compute/v2/servers/util_test.go
@@ -0,0 +1,38 @@
+package servers
+
+import (
+ "fmt"
+ "net/http"
+ "testing"
+ "time"
+
+ th "github.com/rackspace/gophercloud/testhelper"
+ "github.com/rackspace/gophercloud/testhelper/client"
+)
+
+func TestWaitForStatus(t *testing.T) {
+ th.SetupHTTP()
+ defer th.TeardownHTTP()
+
+ th.Mux.HandleFunc("/servers/4321", func(w http.ResponseWriter, r *http.Request) {
+ time.Sleep(2 * time.Second)
+ w.Header().Add("Content-Type", "application/json")
+ w.WriteHeader(http.StatusOK)
+ fmt.Fprintf(w, `
+ {
+ "server": {
+ "name": "the-server",
+ "id": "4321",
+ "status": "ACTIVE"
+ }
+ }`)
+ })
+
+ err := WaitForStatus(client.ServiceClient(), "4321", "ACTIVE", 0)
+ if err == nil {
+ t.Errorf("Expected error: 'Time Out in WaitFor'")
+ }
+
+ err = WaitForStatus(client.ServiceClient(), "4321", "ACTIVE", 3)
+ th.CheckNoErr(t, err)
+}