Skip to content

Commit 6a15f0c

Browse files
feat(rdb): wait for Instance (#249)
1 parent d81e1e1 commit 6a15f0c

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

api/rdb/v1/rdb_utils.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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

Comments
 (0)