-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tem): implement domain waiter (#1447)
- Loading branch information
1 parent
cb2ed66
commit 0b7341d
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package tem | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/scaleway/scaleway-sdk-go/internal/async" | ||
"github.com/scaleway/scaleway-sdk-go/internal/errors" | ||
"github.com/scaleway/scaleway-sdk-go/scw" | ||
) | ||
|
||
const ( | ||
defaultTimeout = 5 * time.Minute | ||
defaultRetryInterval = 15 * time.Second | ||
) | ||
|
||
// WaitForDomainRequest is used by WaitForDomain method | ||
type WaitForDomainRequest struct { | ||
DomainID string | ||
Region scw.Region | ||
Timeout *time.Duration | ||
RetryInterval *time.Duration | ||
} | ||
|
||
// WaitForDomain wait for the domain to be in a "terminal state" before returning. | ||
// This function can be used to wait for a domain to be checked for example. | ||
func (s *API) WaitForDomain(req *WaitForDomainRequest, opts ...scw.RequestOption) (*Domain, error) { | ||
timeout := defaultTimeout | ||
if req.Timeout != nil { | ||
timeout = *req.Timeout | ||
} | ||
retryInterval := defaultRetryInterval | ||
if req.RetryInterval != nil { | ||
retryInterval = *req.RetryInterval | ||
} | ||
|
||
terminalStatus := map[DomainStatus]struct{}{ | ||
DomainStatusChecked: {}, | ||
DomainStatusUnchecked: {}, | ||
DomainStatusInvalid: {}, | ||
DomainStatusLocked: {}, | ||
DomainStatusRevoked: {}, | ||
DomainStatusUnknown: {}, | ||
} | ||
|
||
domain, err := async.WaitSync(&async.WaitSyncConfig{ | ||
Get: func() (interface{}, bool, error) { | ||
img, err := s.GetDomain(&GetDomainRequest{ | ||
Region: req.Region, | ||
DomainID: req.DomainID, | ||
}, opts...) | ||
if err != nil { | ||
return nil, false, err | ||
} | ||
|
||
_, isTerminal := terminalStatus[img.Status] | ||
|
||
return img, isTerminal, err | ||
}, | ||
Timeout: timeout, | ||
IntervalStrategy: async.LinearIntervalStrategy(retryInterval), | ||
}) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "waiting for domain failed") | ||
} | ||
return domain.(*Domain), nil | ||
} |