Skip to content

Commit 0b7341d

Browse files
authored
feat(tem): implement domain waiter (#1447)
1 parent cb2ed66 commit 0b7341d

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

api/tem/v1alpha1/tem_utils.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package tem
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+
defaultTimeout = 5 * time.Minute
13+
defaultRetryInterval = 15 * time.Second
14+
)
15+
16+
// WaitForDomainRequest is used by WaitForDomain method
17+
type WaitForDomainRequest struct {
18+
DomainID string
19+
Region scw.Region
20+
Timeout *time.Duration
21+
RetryInterval *time.Duration
22+
}
23+
24+
// WaitForDomain wait for the domain to be in a "terminal state" before returning.
25+
// This function can be used to wait for a domain to be checked for example.
26+
func (s *API) WaitForDomain(req *WaitForDomainRequest, opts ...scw.RequestOption) (*Domain, 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[DomainStatus]struct{}{
37+
DomainStatusChecked: {},
38+
DomainStatusUnchecked: {},
39+
DomainStatusInvalid: {},
40+
DomainStatusLocked: {},
41+
DomainStatusRevoked: {},
42+
DomainStatusUnknown: {},
43+
}
44+
45+
domain, err := async.WaitSync(&async.WaitSyncConfig{
46+
Get: func() (interface{}, bool, error) {
47+
img, err := s.GetDomain(&GetDomainRequest{
48+
Region: req.Region,
49+
DomainID: req.DomainID,
50+
}, opts...)
51+
if err != nil {
52+
return nil, false, err
53+
}
54+
55+
_, isTerminal := terminalStatus[img.Status]
56+
57+
return img, isTerminal, err
58+
},
59+
Timeout: timeout,
60+
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
61+
})
62+
if err != nil {
63+
return nil, errors.Wrap(err, "waiting for domain failed")
64+
}
65+
return domain.(*Domain), nil
66+
}

0 commit comments

Comments
 (0)