Jon Perritt | fa2c65e | 2014-10-02 20:32:43 -0500 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
| 3 | import ( |
Jamie Hannaford | f9b8bf5 | 2014-10-23 16:56:36 +0200 | [diff] [blame] | 4 | "errors" |
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. |
Ash Wilson | 03af57e | 2014-10-31 14:33:39 -0400 | [diff] [blame] | 12 | // It usually does this to wait for a resource to transition to a certain state. |
| 13 | // Resource packages will wrap this in a more convenient function that's |
| 14 | // 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] | 15 | func WaitFor(timeout int, predicate func() (bool, error)) error { |
| 16 | start := time.Now().Second() |
| 17 | for { |
| 18 | // Force a 1s sleep |
Jon Perritt | fa2c65e | 2014-10-02 20:32:43 -0500 | [diff] [blame] | 19 | time.Sleep(1 * time.Second) |
| 20 | |
Jamie Hannaford | 3ac3aa7 | 2014-10-23 17:30:49 +0200 | [diff] [blame] | 21 | // If a timeout is set, and that's been exceeded, shut it down |
| 22 | if timeout >= 0 && time.Now().Second()-start >= timeout { |
| 23 | return errors.New("A timeout occurred") |
| 24 | } |
| 25 | |
Jamie Hannaford | f9b8bf5 | 2014-10-23 16:56:36 +0200 | [diff] [blame] | 26 | // Execute the function |
Jon Perritt | fa2c65e | 2014-10-02 20:32:43 -0500 | [diff] [blame] | 27 | satisfied, err := predicate() |
| 28 | if err != nil { |
| 29 | return err |
| 30 | } |
| 31 | if satisfied { |
| 32 | return nil |
| 33 | } |
| 34 | } |
Jon Perritt | fa2c65e | 2014-10-02 20:32:43 -0500 | [diff] [blame] | 35 | } |
Ash Wilson | a844064 | 2014-10-07 09:55:58 -0400 | [diff] [blame] | 36 | |
Ash Wilson | ad108b9 | 2014-10-31 16:12:05 -0400 | [diff] [blame] | 37 | // NormalizeURL is an internal function to be used by provider clients. |
| 38 | // |
| 39 | // It ensures that each endpoint URL has a closing `/`, as expected by |
| 40 | // ServiceClient's methods. |
Ash Wilson | a844064 | 2014-10-07 09:55:58 -0400 | [diff] [blame] | 41 | func NormalizeURL(url string) string { |
| 42 | if !strings.HasSuffix(url, "/") { |
| 43 | return url + "/" |
| 44 | } |
| 45 | return url |
| 46 | } |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 47 | |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 48 | // NormalizePathURL is used to convert rawPath to a fqdn, using basePath as |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 49 | // a reference in the filesystem, if necessary. basePath is assumed to contain |
| 50 | // either '.' when first used, or the file:// type fqdn of the parent resource. |
| 51 | // e.g. myFavScript.yaml => file://opt/lib/myFavScript.yaml |
| 52 | func NormalizePathURL(basePath, rawPath string) (string, error) { |
| 53 | u, err := url.Parse(rawPath) |
| 54 | if err != nil { |
| 55 | return "", err |
| 56 | } |
| 57 | // if a scheme is defined, it must be a fqdn already |
| 58 | if u.Scheme != "" { |
| 59 | return u.String(), nil |
| 60 | } |
| 61 | // if basePath is a url, then child resources are assumed to be relative to it |
| 62 | bu, err := url.Parse(basePath) |
| 63 | if err != nil { |
| 64 | return "", err |
| 65 | } |
| 66 | var basePathSys, absPathSys string |
| 67 | if bu.Scheme != "" { |
| 68 | basePathSys = filepath.FromSlash(bu.Path) |
| 69 | absPathSys = filepath.Join(basePathSys, rawPath) |
| 70 | bu.Path = filepath.ToSlash(absPathSys) |
| 71 | return bu.String(), nil |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 72 | } |
Pratik Mallya | 3de347f | 2015-09-22 12:25:59 -0500 | [diff] [blame] | 73 | |
| 74 | absPathSys = filepath.Join(basePath, rawPath) |
| 75 | u.Path = filepath.ToSlash(absPathSys) |
| 76 | if err != nil { |
| 77 | return "", err |
| 78 | } |
| 79 | u.Scheme = "file" |
| 80 | return u.String(), nil |
| 81 | |
Pratik Mallya | 5fddb2a | 2015-09-14 14:04:49 -0500 | [diff] [blame] | 82 | } |