Skip to content

Commit 27a9f05

Browse files
authored
feat(webhosting): add wait method for hosting (#1581)
1 parent 7a27864 commit 27a9f05

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package webhosting
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 = 5 * time.Second
13+
defaultTimeout = 5 * time.Minute
14+
)
15+
16+
// WaitForHostingRequest is used by WaitForHosting method.
17+
type WaitForHostingRequest struct {
18+
HostingID string
19+
Region scw.Region
20+
Timeout *time.Duration
21+
RetryInterval *time.Duration
22+
}
23+
24+
// WaitForHosting wait for a hosting to be in a "terminal state" before returning.
25+
func (s *API) WaitForHosting(req *WaitForHostingRequest, opts ...scw.RequestOption) (*Hosting, error) {
26+
timeout := defaultTimeout
27+
if req.Timeout != nil {
28+
timeout = *req.Timeout
29+
}
30+
retryInterval := defaultRetryInterval
31+
if req.RetryInterval != nil {
32+
retryInterval = *req.RetryInterval
33+
}
34+
35+
terminalStatus := map[HostingStatus]struct{}{
36+
HostingStatusReady: {},
37+
HostingStatusError: {},
38+
HostingStatusUnknownStatus: {},
39+
HostingStatusLocked: {},
40+
}
41+
42+
res, err := async.WaitSync(&async.WaitSyncConfig{
43+
Get: func() (interface{}, bool, error) {
44+
hosting, err := s.GetHosting(&GetHostingRequest{
45+
HostingID: req.HostingID,
46+
Region: req.Region,
47+
}, opts...)
48+
if err != nil {
49+
return nil, false, err
50+
}
51+
52+
_, isTerminal := terminalStatus[hosting.Status]
53+
return hosting, isTerminal, nil
54+
},
55+
Timeout: timeout,
56+
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
57+
})
58+
if err != nil {
59+
return nil, errors.Wrap(err, "waiting for hosting failed")
60+
}
61+
62+
return res.(*Hosting), nil
63+
}

0 commit comments

Comments
 (0)