Skip to content

Commit 19c7ff4

Browse files
jerome-quereQuentinBrosse
authored andcommitted
feat(lb): add WaitForLb method (#212)
1 parent 8dc27fc commit 19c7ff4

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

api/lb/v1/lb_utils.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package lb
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+
// WaitForLbRequest is used by WaitForLb method.
12+
type WaitForLbRequest struct {
13+
LbID string
14+
Region scw.Region
15+
Timeout time.Duration
16+
}
17+
18+
// WaitForLb waits for the lb to be in a "terminal state" before returning.
19+
// This function can be used to wait for a lb to be ready for example.
20+
func (s *API) WaitForLb(req *WaitForLbRequest) (*Lb, error) {
21+
22+
terminalStatus := map[LbStatus]struct{}{
23+
LbStatusReady: {},
24+
LbStatusStopped: {},
25+
LbStatusError: {},
26+
LbStatusLocked: {},
27+
}
28+
29+
lb, err := async.WaitSync(&async.WaitSyncConfig{
30+
Get: func() (interface{}, error, bool) {
31+
res, err := s.GetLb(&GetLbRequest{
32+
LbID: req.LbID,
33+
Region: req.Region,
34+
})
35+
36+
if err != nil {
37+
return nil, err, false
38+
}
39+
_, isTerminal := terminalStatus[res.Status]
40+
41+
return res, nil, isTerminal
42+
},
43+
Timeout: req.Timeout,
44+
IntervalStrategy: async.LinearIntervalStrategy(5 * time.Second),
45+
})
46+
if err != nil {
47+
return nil, errors.Wrap(err, "waiting for lb failed")
48+
}
49+
return lb.(*Lb), nil
50+
}

0 commit comments

Comments
 (0)