Skip to content

Commit 4f030ee

Browse files
authored
feat(datawarehouse): add waiters for Deployment (scaleway#2724)
1 parent d231482 commit 4f030ee

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package datawarehouse
2+
3+
import (
4+
"errors"
5+
"net/http"
6+
"time"
7+
8+
"github.com/scaleway/scaleway-sdk-go/internal/async"
9+
"github.com/scaleway/scaleway-sdk-go/scw"
10+
)
11+
12+
const (
13+
waitForDeploymentDefaultTimeout = 30 * time.Minute
14+
defaultRetryIntervalDW = 5 * time.Second
15+
)
16+
17+
type WaitForDeploymentRequest struct {
18+
DeploymentID string
19+
Region scw.Region
20+
Timeout *time.Duration
21+
RetryInterval *time.Duration
22+
}
23+
24+
// WaitForDeployment waits until deployment reaches a terminal state.
25+
func (s *API) WaitForDeployment(req *WaitForDeploymentRequest, opts ...scw.RequestOption) (*Deployment, error) {
26+
timeout := waitForDeploymentDefaultTimeout
27+
if req.Timeout != nil {
28+
timeout = *req.Timeout
29+
}
30+
retryInterval := defaultRetryIntervalDW
31+
if req.RetryInterval != nil {
32+
retryInterval = *req.RetryInterval
33+
}
34+
35+
terminalStatus := map[DeploymentStatus]struct{}{
36+
DeploymentStatusReady: {},
37+
DeploymentStatusError: {},
38+
DeploymentStatusLocked: {},
39+
}
40+
41+
deployment, err := async.WaitSync(&async.WaitSyncConfig{
42+
Get: func() (any, bool, error) {
43+
res, err := s.GetDeployment(&GetDeploymentRequest{
44+
DeploymentID: req.DeploymentID,
45+
Region: req.Region,
46+
}, opts...)
47+
if err != nil {
48+
var respErr *scw.ResponseError
49+
if errors.As(err, &respErr) && respErr.StatusCode == http.StatusNotFound {
50+
return nil, false, nil
51+
}
52+
return nil, false, err
53+
}
54+
55+
_, isTerminal := terminalStatus[res.Status]
56+
return res, isTerminal, nil
57+
},
58+
Timeout: timeout,
59+
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
60+
})
61+
if err != nil {
62+
return nil, err
63+
}
64+
return deployment.(*Deployment), nil
65+
}

0 commit comments

Comments
 (0)