-
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(iot): add WaitForHub IoT helper
Signed-off-by: Clément Decoodt <cdecoodt@online.net>
- Loading branch information
Clément Decoodt
committed
Jun 16, 2020
1 parent
a6a7bca
commit 9803a9e
Showing
1 changed file
with
56 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,56 @@ | ||
package iot | ||
|
||
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 ( | ||
waitForHubDefaultTimeout = 15 * time.Minute | ||
defaultRetryInterval = 5 * time.Second | ||
) | ||
|
||
// WaitForHubRequest is used by WaitForHub method. | ||
type WaitForHubRequest struct { | ||
HubID string | ||
Region scw.Region | ||
Status HubStatus | ||
Timeout *time.Duration | ||
RetryInterval *time.Duration | ||
} | ||
|
||
// WaitForHub waits for the hub to be in a ready state before returning. | ||
func (s *API) WaitForHub(req *WaitForHubRequest) (*Hub, error) { | ||
timeout := *req.Timeout | ||
if timeout == 0 { | ||
timeout = waitForHubDefaultTimeout | ||
} | ||
retryInterval := defaultRetryInterval | ||
if req.RetryInterval != nil { | ||
retryInterval = *req.RetryInterval | ||
} | ||
|
||
hub, err := async.WaitSync(&async.WaitSyncConfig{ | ||
Get: func() (interface{}, bool, error) { | ||
hub, err := s.GetHub(&GetHubRequest{ | ||
HubID: req.HubID, | ||
Region: req.Region, | ||
}) | ||
if err != nil { | ||
return nil, false, err | ||
} | ||
|
||
return hub, hub.Status == HubStatusReady, nil | ||
}, | ||
Timeout: timeout, | ||
IntervalStrategy: async.LinearIntervalStrategy(retryInterval), | ||
}) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "waiting for hub failed") | ||
} | ||
|
||
return hub.(*Hub), nil | ||
} |