Skip to content

Commit 32bb2d9

Browse files
authored
feat: add utils for webhosting v1 (scaleway#2435)
1 parent dbe840c commit 32bb2d9

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package webhosting
2+
3+
import (
4+
"time"
5+
6+
"github.com/scaleway/scaleway-sdk-go/errors"
7+
"github.com/scaleway/scaleway-sdk-go/internal/async"
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 waits for a hosting to be in a "terminal" state before returning.
25+
// Terminal states are defined as: HostingStatusReady, HostingStatusError, HostingStatusUnknownStatus, and HostingStatusLocked.
26+
func (s *HostingAPI) WaitForHosting(req *WaitForHostingRequest, opts ...scw.RequestOption) (*Hosting, error) {
27+
timeout := defaultTimeout
28+
if req.Timeout != nil {
29+
timeout = *req.Timeout
30+
}
31+
retryInterval := defaultRetryInterval
32+
if req.RetryInterval != nil {
33+
retryInterval = *req.RetryInterval
34+
}
35+
36+
terminalStatus := map[HostingStatus]struct{}{
37+
HostingStatusReady: {},
38+
HostingStatusError: {},
39+
HostingStatusUnknownStatus: {},
40+
HostingStatusLocked: {},
41+
}
42+
43+
res, err := async.WaitSync(&async.WaitSyncConfig{
44+
Get: func() (interface{}, bool, error) {
45+
hosting, err := s.GetHosting(&HostingAPIGetHostingRequest{
46+
HostingID: req.HostingID,
47+
Region: req.Region,
48+
}, opts...)
49+
if err != nil {
50+
return nil, false, err
51+
}
52+
53+
_, isTerminal := terminalStatus[hosting.Status]
54+
return hosting, isTerminal, nil
55+
},
56+
Timeout: timeout,
57+
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
58+
})
59+
if err != nil {
60+
return nil, errors.Wrap(err, "waiting for hosting failed")
61+
}
62+
63+
return res.(*Hosting), nil
64+
}

0 commit comments

Comments
 (0)