Skip to content

Commit 1dc290c

Browse files
authored
feat(dedibox): add waiter support for Service and Server (#2157)
1 parent 2cf3200 commit 1dc290c

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

api/dedibox/v1/dedibox_utils.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package dedibox
2+
3+
import (
4+
"github.com/scaleway/scaleway-sdk-go/internal/async"
5+
"github.com/scaleway/scaleway-sdk-go/internal/errors"
6+
"github.com/scaleway/scaleway-sdk-go/scw"
7+
"time"
8+
)
9+
10+
const (
11+
defaultRetryInterval = time.Second * 15
12+
defaultTimeout = time.Minute * 30
13+
)
14+
15+
type WaitForServiceRequest struct {
16+
ServiceID uint64
17+
Zone scw.Zone
18+
Timeout *time.Duration
19+
RetryInterval *time.Duration
20+
}
21+
22+
func (s *API) WaitForService(req *WaitForServiceRequest, opts ...scw.RequestOption) (*Service, error) {
23+
timeout := defaultTimeout
24+
if req.Timeout != nil {
25+
timeout = *req.Timeout
26+
}
27+
retryInterval := defaultRetryInterval
28+
if req.RetryInterval != nil {
29+
retryInterval = *req.RetryInterval
30+
}
31+
terminalStatus := map[ServiceProvisioningStatus]struct{}{
32+
ServiceProvisioningStatusReady: {},
33+
ServiceProvisioningStatusError: {},
34+
ServiceProvisioningStatusExpired: {},
35+
}
36+
service, err := async.WaitSync(&async.WaitSyncConfig{
37+
Get: func() (interface{}, bool, error) {
38+
service, err := s.GetService(&GetServiceRequest{
39+
Zone: req.Zone,
40+
ServiceID: req.ServiceID,
41+
}, opts...)
42+
if err != nil {
43+
return nil, false, err
44+
}
45+
_, isTerminal := terminalStatus[service.ProvisioningStatus]
46+
return service, isTerminal, nil
47+
},
48+
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
49+
Timeout: timeout,
50+
})
51+
if err != nil {
52+
return nil, errors.Wrap(err, "waiting for service failed")
53+
}
54+
return service.(*Service), nil
55+
}
56+
57+
type WaitForServerRequest struct {
58+
ServerID uint64
59+
Zone scw.Zone
60+
Timeout *time.Duration
61+
RetryInterval *time.Duration
62+
}
63+
64+
func (s *API) WaitForServer(req *WaitForServerRequest, opts ...scw.RequestOption) (*Server, error) {
65+
timeout := defaultTimeout
66+
if req.Timeout != nil {
67+
timeout = *req.Timeout
68+
}
69+
retryInterval := defaultRetryInterval
70+
if req.RetryInterval != nil {
71+
retryInterval = *req.RetryInterval
72+
}
73+
terminalStatus := map[ServerStatus]struct{}{
74+
ServerStatusReady: {},
75+
ServerStatusError: {},
76+
ServerStatusLocked: {},
77+
ServerStatusStopped: {},
78+
ServerStatusBusy: {},
79+
ServerStatusRescue: {},
80+
}
81+
server, err := async.WaitSync(&async.WaitSyncConfig{
82+
Get: func() (interface{}, bool, error) {
83+
server, err := s.GetServer(&GetServerRequest{
84+
Zone: req.Zone,
85+
ServerID: req.ServerID,
86+
}, opts...)
87+
if err != nil {
88+
return nil, false, err
89+
}
90+
_, isTerminal := terminalStatus[server.Status]
91+
return server, isTerminal, nil
92+
},
93+
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
94+
Timeout: timeout,
95+
})
96+
if err != nil {
97+
return nil, errors.Wrap(err, "waiting for server failed")
98+
}
99+
return server.(*Server), nil
100+
}

0 commit comments

Comments
 (0)