-
Notifications
You must be signed in to change notification settings - Fork 0
/
wait.go
64 lines (52 loc) · 2.15 KB
/
wait.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package tfresource
import (
"context"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)
type WaitOpts struct {
ContinuousTargetOccurence int // Number of times the target state has to occur continuously.
Delay time.Duration // Wait this time before starting checks.
MinTimeout time.Duration // Smallest time to wait before refreshes.
PollInterval time.Duration // Override MinTimeout/backoff and only poll this often.
}
const (
targetStateError = "ERROR"
targetStateFalse = "FALSE"
targetStateTrue = "TRUE"
)
// WaitUntilContext waits for the function `f` to return `true`.
// If `f` returns an error, return immediately with that error.
// If `timeout` is exceeded before `f` returns `true`, return an error.
// Waits between calls to `f` using exponential backoff, except when waiting for the target state to reoccur.
func WaitUntilContext(ctx context.Context, timeout time.Duration, f func() (bool, error), opts WaitOpts) error {
refresh := func() (interface{}, string, error) {
done, err := f()
if err != nil {
return nil, targetStateError, err
}
if done {
return "", targetStateTrue, nil
}
return "", targetStateFalse, nil
}
stateConf := &resource.StateChangeConf{
Pending: []string{targetStateFalse},
Target: []string{targetStateTrue},
Refresh: refresh,
Timeout: timeout,
ContinuousTargetOccurence: opts.ContinuousTargetOccurence,
Delay: opts.Delay,
MinTimeout: opts.MinTimeout,
PollInterval: opts.PollInterval,
}
_, err := stateConf.WaitForStateContext(ctx)
return err
}
// WaitUntil waits for the function `f` to return `true`.
// If `f` returns an error, return immediately with that error.
// If `timeout` is exceeded before `f` returns `true`, return an error.
// Waits between calls to `f` using exponential backoff, except when waiting for the target state to reoccur.
func WaitUntil(timeout time.Duration, f func() (bool, error), opts WaitOpts) error {
return WaitUntilContext(context.Background(), timeout, f, opts)
}