|
| 1 | +package rdb |
| 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 | +// WaitForInstanceRequest is used by WaitForInstance method. |
| 12 | +type WaitForInstanceRequest struct { |
| 13 | + InstanceID string |
| 14 | + Region scw.Region |
| 15 | + Timeout time.Duration |
| 16 | +} |
| 17 | + |
| 18 | +// WaitForInstance waits for the instance to be in a "terminal state" before returning. |
| 19 | +// This function can be used to wait for an instance to be ready for example. |
| 20 | +func (s *API) WaitForInstance(req *WaitForInstanceRequest) (*Instance, error) { |
| 21 | + |
| 22 | + terminalStatus := map[InstanceStatus]struct{}{ |
| 23 | + InstanceStatusReady: {}, |
| 24 | + InstanceStatusDiskFull: {}, |
| 25 | + InstanceStatusError: {}, |
| 26 | + } |
| 27 | + |
| 28 | + instance, err := async.WaitSync(&async.WaitSyncConfig{ |
| 29 | + Get: func() (interface{}, error, bool) { |
| 30 | + res, err := s.GetInstance(&GetInstanceRequest{ |
| 31 | + InstanceID: req.InstanceID, |
| 32 | + Region: req.Region, |
| 33 | + }) |
| 34 | + |
| 35 | + if err != nil { |
| 36 | + return nil, err, false |
| 37 | + } |
| 38 | + _, isTerminal := terminalStatus[res.Status] |
| 39 | + |
| 40 | + return res, nil, isTerminal |
| 41 | + }, |
| 42 | + Timeout: req.Timeout, |
| 43 | + IntervalStrategy: async.LinearIntervalStrategy(5 * time.Second), |
| 44 | + }) |
| 45 | + if err != nil { |
| 46 | + return nil, errors.Wrap(err, "waiting for instance failed") |
| 47 | + } |
| 48 | + return instance.(*Instance), nil |
| 49 | +} |
0 commit comments