Skip to content

Commit

Permalink
feat(iot): add WaitForHub IoT helper
Browse files Browse the repository at this point in the history
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.
56 changes: 56 additions & 0 deletions api/iot/v1beta1/iot_helpers.go
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
}

0 comments on commit 9803a9e

Please sign in to comment.