terraform/helper/resource/wait.go

32 lines
644 B
Go
Raw Normal View History

2014-07-01 03:03:27 +02:00
package resource
import (
"time"
)
2014-10-08 06:44:51 +02:00
// RetryFunc is the function retried until it succeeds.
type RetryFunc func() error
// Retry is a basic wrapper around StateChangeConf that will just retry
// a function until it no longer returns an error.
func Retry(timeout time.Duration, f RetryFunc) error {
var err error
c := &StateChangeConf{
Pending: []string{"error"},
Target: "success",
Timeout: timeout,
MinTimeout: 500 * time.Millisecond,
Refresh: func() (interface{}, string, error) {
err = f()
2014-07-01 03:03:27 +02:00
if err != nil {
2014-10-08 06:44:51 +02:00
return 42, "error", nil
}
2014-10-08 06:44:51 +02:00
return 42, "success", nil
},
2014-07-01 03:03:27 +02:00
}
2014-10-08 06:44:51 +02:00
c.WaitForState()
return err
2014-07-01 03:03:27 +02:00
}