blob: c66af8993f933c0e5f167ca75ca9c7448e1b8a9c [file] [log] [blame]
Jon Perrittfa2c65e2014-10-02 20:32:43 -05001package gophercloud
2
3import (
4 "fmt"
Ash Wilsona8440642014-10-07 09:55:58 -04005 "strings"
Jon Perrittfa2c65e2014-10-02 20:32:43 -05006 "time"
7)
8
9// WaitFor polls a predicate function once per second up to secs times to wait for a certain state to arrive.
10func 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 Wilsona8440642014-10-07 09:55:58 -040024
25// NormalizeURL ensures that each endpoint URL has a closing `/`, as expected by ServiceClient.
26func NormalizeURL(url string) string {
27 if !strings.HasSuffix(url, "/") {
28 return url + "/"
29 }
30 return url
31}
Jamie Hannafordee049ec2014-10-22 17:02:55 +020032
33// BuildQuery constructs the query section of a URI from a map.
34func BuildQuery(params map[string]string) string {
35 if len(params) == 0 {
36 return ""
37 }
38
39 query := "?"
40 for k, v := range params {
41 query += k + "=" + v + "&"
42 }
43 query = query[:len(query)-1]
44 return query
45}