Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(iot): add WaitForHub IoT helper #445

Merged
merged 1 commit into from
Jun 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions api/iot/v1beta1/iot_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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 := waitForHubDefaultTimeout
if req.Timeout != nil {
timeout = *req.Timeout
}
retryInterval := defaultRetryInterval
if req.RetryInterval != nil {
retryInterval = *req.RetryInterval
}

terminalStatus := map[HubStatus]struct{}{
HubStatusError: {},
HubStatusReady: {},
HubStatusDisabled: {},
}

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
}

_, isTerminal := terminalStatus[hub.Status]
return hub, isTerminal, nil
},
Timeout: timeout,
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
})
if err != nil {
return nil, errors.Wrap(err, "waiting for hub failed")
}

return hub.(*Hub), nil
}