Jon Perritt | fa2c65e | 2014-10-02 20:32:43 -0500 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
Ash Wilson | a844064 | 2014-10-07 09:55:58 -0400 | [diff] [blame] | 5 | "strings" |
Jon Perritt | fa2c65e | 2014-10-02 20:32:43 -0500 | [diff] [blame] | 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | // WaitFor polls a predicate function once per second up to secs times to wait for a certain state to arrive. |
| 10 | func WaitFor(secs int, predicate func() (bool, error)) error { |
| 11 | for i := 0; i < secs; i++ { |
| 12 | time.Sleep(1 * time.Second) |
| 13 | |
| 14 | satisfied, err := predicate() |
| 15 | if err != nil { |
| 16 | return err |
| 17 | } |
| 18 | if satisfied { |
| 19 | return nil |
| 20 | } |
| 21 | } |
| 22 | return fmt.Errorf("Time out in WaitFor.") |
| 23 | } |
Ash Wilson | a844064 | 2014-10-07 09:55:58 -0400 | [diff] [blame] | 24 | |
| 25 | // NormalizeURL ensures that each endpoint URL has a closing `/`, as expected by ServiceClient. |
| 26 | func NormalizeURL(url string) string { |
| 27 | if !strings.HasSuffix(url, "/") { |
| 28 | return url + "/" |
| 29 | } |
| 30 | return url |
| 31 | } |