util.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package gophercloud
  2. import (
  3. "fmt"
  4. "net/url"
  5. "path/filepath"
  6. "strings"
  7. "time"
  8. )
  9. // WaitFor polls a predicate function, once per second, up to a timeout limit.
  10. // This is useful to wait for a resource to transition to a certain state.
  11. // To handle situations when the predicate might hang indefinitely, the
  12. // predicate will be prematurely cancelled after the timeout.
  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.
  15. func WaitFor(timeout int, predicate func() (bool, error)) error {
  16. type WaitForResult struct {
  17. Success bool
  18. Error error
  19. }
  20. start := time.Now().Unix()
  21. for {
  22. // If a timeout is set, and that's been exceeded, shut it down.
  23. if timeout >= 0 && time.Now().Unix()-start >= int64(timeout) {
  24. return fmt.Errorf("A timeout occurred")
  25. }
  26. time.Sleep(1 * time.Second)
  27. var result WaitForResult
  28. ch := make(chan bool, 1)
  29. go func() {
  30. defer close(ch)
  31. satisfied, err := predicate()
  32. result.Success = satisfied
  33. result.Error = err
  34. }()
  35. select {
  36. case <-ch:
  37. if result.Error != nil {
  38. return result.Error
  39. }
  40. if result.Success {
  41. return nil
  42. }
  43. // If the predicate has not finished by the timeout, cancel it.
  44. case <-time.After(time.Duration(timeout) * time.Second):
  45. return fmt.Errorf("A timeout occurred")
  46. }
  47. }
  48. }
  49. // NormalizeURL is an internal function to be used by provider clients.
  50. //
  51. // It ensures that each endpoint URL has a closing `/`, as expected by
  52. // ServiceClient's methods.
  53. func NormalizeURL(url string) string {
  54. if !strings.HasSuffix(url, "/") {
  55. return url + "/"
  56. }
  57. return url
  58. }
  59. // NormalizePathURL is used to convert rawPath to a fqdn, using basePath as
  60. // a reference in the filesystem, if necessary. basePath is assumed to contain
  61. // either '.' when first used, or the file:// type fqdn of the parent resource.
  62. // e.g. myFavScript.yaml => file://opt/lib/myFavScript.yaml
  63. func NormalizePathURL(basePath, rawPath string) (string, error) {
  64. u, err := url.Parse(rawPath)
  65. if err != nil {
  66. return "", err
  67. }
  68. // if a scheme is defined, it must be a fqdn already
  69. if u.Scheme != "" {
  70. return u.String(), nil
  71. }
  72. // if basePath is a url, then child resources are assumed to be relative to it
  73. bu, err := url.Parse(basePath)
  74. if err != nil {
  75. return "", err
  76. }
  77. var basePathSys, absPathSys string
  78. if bu.Scheme != "" {
  79. basePathSys = filepath.FromSlash(bu.Path)
  80. absPathSys = filepath.Join(basePathSys, rawPath)
  81. bu.Path = filepath.ToSlash(absPathSys)
  82. return bu.String(), nil
  83. }
  84. absPathSys = filepath.Join(basePath, rawPath)
  85. u.Path = filepath.ToSlash(absPathSys)
  86. if err != nil {
  87. return "", err
  88. }
  89. u.Scheme = "file"
  90. return u.String(), nil
  91. }