Jon Perritt | fa2c65e | 2014-10-02 20:32:43 -0500 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
| 3 | import ( |
Joe Topjian | 48584fb | 2017-02-13 21:18:26 -0700 | [diff] [blame] | 4 | "fmt" |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 5 | "net/url" |
| 6 | "path/filepath" |
Ash Wilson | a844064 | 2014-10-07 09:55:58 -0400 | [diff] [blame] | 7 | "strings" |
Jon Perritt | fa2c65e | 2014-10-02 20:32:43 -0500 | [diff] [blame] | 8 | "time" |
| 9 | ) |
| 10 | |
Jamie Hannaford | b280dea | 2014-10-24 15:14:06 +0200 | [diff] [blame] | 11 | // WaitFor polls a predicate function, once per second, up to a timeout limit. |
Joe Topjian | 48584fb | 2017-02-13 21:18:26 -0700 | [diff] [blame] | 12 | // 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 Wilson | 03af57e | 2014-10-31 14:33:39 -0400 | [diff] [blame] | 15 | // 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 Hannaford | f9b8bf5 | 2014-10-23 16:56:36 +0200 | [diff] [blame] | 17 | func WaitFor(timeout int, predicate func() (bool, error)) error { |
Joe Topjian | 48584fb | 2017-02-13 21:18:26 -0700 | [diff] [blame] | 18 | type WaitForResult struct { |
| 19 | Success bool |
| 20 | Error error |
| 21 | } |
| 22 | |
| 23 | start := time.Now().Unix() |
| 24 | |
Jamie Hannaford | f9b8bf5 | 2014-10-23 16:56:36 +0200 | [diff] [blame] | 25 | for { |
Joe Topjian | 48584fb | 2017-02-13 21:18:26 -0700 | [diff] [blame] | 26 | // 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 Perritt | fa2c65e | 2014-10-02 20:32:43 -0500 | [diff] [blame] | 31 | time.Sleep(1 * time.Second) |
| 32 | |
Joe Topjian | 48584fb | 2017-02-13 21:18:26 -0700 | [diff] [blame] | 33 | 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 Hannaford | 3ac3aa7 | 2014-10-23 17:30:49 +0200 | [diff] [blame] | 41 | |
Joe Topjian | 48584fb | 2017-02-13 21:18:26 -0700 | [diff] [blame] | 42 | 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 Perritt | fa2c65e | 2014-10-02 20:32:43 -0500 | [diff] [blame] | 53 | } |
| 54 | } |
Jon Perritt | fa2c65e | 2014-10-02 20:32:43 -0500 | [diff] [blame] | 55 | } |
Ash Wilson | a844064 | 2014-10-07 09:55:58 -0400 | [diff] [blame] | 56 | |
Ash Wilson | ad108b9 | 2014-10-31 16:12:05 -0400 | [diff] [blame] | 57 | // 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 Wilson | a844064 | 2014-10-07 09:55:58 -0400 | [diff] [blame] | 61 | func NormalizeURL(url string) string { |
| 62 | if !strings.HasSuffix(url, "/") { |
| 63 | return url + "/" |
| 64 | } |
| 65 | return url |
| 66 | } |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 67 | |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 68 | // NormalizePathURL is used to convert rawPath to a fqdn, using basePath as |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 69 | // 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 |
| 72 | func 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 Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 92 | } |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 93 | |
| 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 Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 102 | } |