blob: 68f9a5d3eca50f2d29d6a09638cc1ea48e3d895e [file] [log] [blame]
Jon Perrittfa2c65e2014-10-02 20:32:43 -05001package gophercloud
2
3import (
Joe Topjian48584fb2017-02-13 21:18:26 -07004 "fmt"
Pratik Mallya5fddb2a2015-09-14 14:04:49 -05005 "net/url"
6 "path/filepath"
Ash Wilsona8440642014-10-07 09:55:58 -04007 "strings"
Jon Perrittfa2c65e2014-10-02 20:32:43 -05008 "time"
9)
10
Jamie Hannafordb280dea2014-10-24 15:14:06 +020011// WaitFor polls a predicate function, once per second, up to a timeout limit.
Joe Topjian48584fb2017-02-13 21:18:26 -070012// This is useful to wait for a resource to transition to a certain state.
13// To handle situations when the predicate might hang indefinitely, the
14// predicate will be prematurely cancelled after the timeout.
Ash Wilson03af57e2014-10-31 14:33:39 -040015// Resource packages will wrap this in a more convenient function that's
16// specific to a certain resource, but it can also be useful on its own.
Jamie Hannafordf9b8bf52014-10-23 16:56:36 +020017func WaitFor(timeout int, predicate func() (bool, error)) error {
Joe Topjian48584fb2017-02-13 21:18:26 -070018 type WaitForResult struct {
19 Success bool
20 Error error
21 }
22
23 start := time.Now().Unix()
24
Jamie Hannafordf9b8bf52014-10-23 16:56:36 +020025 for {
Joe Topjian48584fb2017-02-13 21:18:26 -070026 // If a timeout is set, and that's been exceeded, shut it down.
27 if timeout >= 0 && time.Now().Unix()-start >= int64(timeout) {
28 return fmt.Errorf("A timeout occurred")
29 }
30
Jon Perrittfa2c65e2014-10-02 20:32:43 -050031 time.Sleep(1 * time.Second)
32
Joe Topjian48584fb2017-02-13 21:18:26 -070033 var result WaitForResult
34 ch := make(chan bool, 1)
35 go func() {
36 defer close(ch)
37 satisfied, err := predicate()
38 result.Success = satisfied
39 result.Error = err
40 }()
Jamie Hannaford3ac3aa72014-10-23 17:30:49 +020041
Joe Topjian48584fb2017-02-13 21:18:26 -070042 select {
43 case <-ch:
44 if result.Error != nil {
45 return result.Error
46 }
47 if result.Success {
48 return nil
49 }
50 // If the predicate has not finished by the timeout, cancel it.
51 case <-time.After(time.Duration(timeout) * time.Second):
52 return fmt.Errorf("A timeout occurred")
Jon Perrittfa2c65e2014-10-02 20:32:43 -050053 }
54 }
Jon Perrittfa2c65e2014-10-02 20:32:43 -050055}
Ash Wilsona8440642014-10-07 09:55:58 -040056
Ash Wilsonad108b92014-10-31 16:12:05 -040057// NormalizeURL is an internal function to be used by provider clients.
58//
59// It ensures that each endpoint URL has a closing `/`, as expected by
60// ServiceClient's methods.
Ash Wilsona8440642014-10-07 09:55:58 -040061func NormalizeURL(url string) string {
62 if !strings.HasSuffix(url, "/") {
63 return url + "/"
64 }
65 return url
66}
Pratik Mallya5fddb2a2015-09-14 14:04:49 -050067
Pratik Mallya3de347f2015-09-22 12:25:59 -050068// NormalizePathURL is used to convert rawPath to a fqdn, using basePath as
Pratik Mallya5fddb2a2015-09-14 14:04:49 -050069// a reference in the filesystem, if necessary. basePath is assumed to contain
70// either '.' when first used, or the file:// type fqdn of the parent resource.
71// e.g. myFavScript.yaml => file://opt/lib/myFavScript.yaml
72func NormalizePathURL(basePath, rawPath string) (string, error) {
73 u, err := url.Parse(rawPath)
74 if err != nil {
75 return "", err
76 }
77 // if a scheme is defined, it must be a fqdn already
78 if u.Scheme != "" {
79 return u.String(), nil
80 }
81 // if basePath is a url, then child resources are assumed to be relative to it
82 bu, err := url.Parse(basePath)
83 if err != nil {
84 return "", err
85 }
86 var basePathSys, absPathSys string
87 if bu.Scheme != "" {
88 basePathSys = filepath.FromSlash(bu.Path)
89 absPathSys = filepath.Join(basePathSys, rawPath)
90 bu.Path = filepath.ToSlash(absPathSys)
91 return bu.String(), nil
Pratik Mallya5fddb2a2015-09-14 14:04:49 -050092 }
Pratik Mallya3de347f2015-09-22 12:25:59 -050093
94 absPathSys = filepath.Join(basePath, rawPath)
95 u.Path = filepath.ToSlash(absPathSys)
96 if err != nil {
97 return "", err
98 }
99 u.Scheme = "file"
100 return u.String(), nil
101
Pratik Mallya5fddb2a2015-09-14 14:04:49 -0500102}