|
| 1 | +package redis |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + |
| 6 | + "github.com/scaleway/scaleway-sdk-go/internal/async" |
| 7 | + "github.com/scaleway/scaleway-sdk-go/internal/errors" |
| 8 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + defaultRetryInterval = 15 * time.Second |
| 13 | + defaultTimeout = 15 * time.Minute |
| 14 | +) |
| 15 | + |
| 16 | +// WaitForClusterRequest is used by WaitForCluster method. |
| 17 | +type WaitForClusterRequest struct { |
| 18 | + ClusterID string |
| 19 | + Zone scw.Zone |
| 20 | + Timeout *time.Duration |
| 21 | + RetryInterval *time.Duration |
| 22 | +} |
| 23 | + |
| 24 | +// WaitForCluster waits for the cluster to be in a "terminal state" before returning. |
| 25 | +// This function can be used to wait for a cluster to be ready for example. |
| 26 | +func (s *API) WaitForCluster(req *WaitForClusterRequest, opts ...scw.RequestOption) (*Cluster, error) { |
| 27 | + timeout := defaultTimeout |
| 28 | + if req.Timeout != nil { |
| 29 | + timeout = *req.Timeout |
| 30 | + } |
| 31 | + retryInterval := defaultRetryInterval |
| 32 | + if req.RetryInterval != nil { |
| 33 | + retryInterval = *req.RetryInterval |
| 34 | + } |
| 35 | + |
| 36 | + terminalStatus := map[ClusterStatus]struct{}{ |
| 37 | + ClusterStatusReady: {}, |
| 38 | + ClusterStatusLocked: {}, |
| 39 | + ClusterStatusError: {}, |
| 40 | + ClusterStatusSuspended: {}, |
| 41 | + } |
| 42 | + |
| 43 | + cluster, err := async.WaitSync(&async.WaitSyncConfig{ |
| 44 | + Get: func() (interface{}, bool, error) { |
| 45 | + res, err := s.GetCluster(&GetClusterRequest{ |
| 46 | + Zone: req.Zone, |
| 47 | + ClusterID: req.ClusterID, |
| 48 | + }, opts...) |
| 49 | + if err != nil { |
| 50 | + return nil, false, err |
| 51 | + } |
| 52 | + |
| 53 | + _, isTerminal := terminalStatus[res.Status] |
| 54 | + |
| 55 | + return res, isTerminal, nil |
| 56 | + }, |
| 57 | + Timeout: timeout, |
| 58 | + IntervalStrategy: async.LinearIntervalStrategy(retryInterval), |
| 59 | + }) |
| 60 | + if err != nil { |
| 61 | + return nil, errors.Wrap(err, "waiting for cluster failed") |
| 62 | + } |
| 63 | + return cluster.(*Cluster), nil |
| 64 | +} |
0 commit comments