-
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(dedibox): add waiter support for Service and Server (#2157)
- Loading branch information
Showing
1 changed file
with
100 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,100 @@ | ||
package dedibox | ||
|
||
import ( | ||
"github.com/scaleway/scaleway-sdk-go/internal/async" | ||
"github.com/scaleway/scaleway-sdk-go/internal/errors" | ||
"github.com/scaleway/scaleway-sdk-go/scw" | ||
"time" | ||
) | ||
|
||
const ( | ||
defaultRetryInterval = time.Second * 15 | ||
defaultTimeout = time.Minute * 30 | ||
) | ||
|
||
type WaitForServiceRequest struct { | ||
ServiceID uint64 | ||
Zone scw.Zone | ||
Timeout *time.Duration | ||
RetryInterval *time.Duration | ||
} | ||
|
||
func (s *API) WaitForService(req *WaitForServiceRequest, opts ...scw.RequestOption) (*Service, error) { | ||
timeout := defaultTimeout | ||
if req.Timeout != nil { | ||
timeout = *req.Timeout | ||
} | ||
retryInterval := defaultRetryInterval | ||
if req.RetryInterval != nil { | ||
retryInterval = *req.RetryInterval | ||
} | ||
terminalStatus := map[ServiceProvisioningStatus]struct{}{ | ||
ServiceProvisioningStatusReady: {}, | ||
ServiceProvisioningStatusError: {}, | ||
ServiceProvisioningStatusExpired: {}, | ||
} | ||
service, err := async.WaitSync(&async.WaitSyncConfig{ | ||
Get: func() (interface{}, bool, error) { | ||
service, err := s.GetService(&GetServiceRequest{ | ||
Zone: req.Zone, | ||
ServiceID: req.ServiceID, | ||
}, opts...) | ||
if err != nil { | ||
return nil, false, err | ||
} | ||
_, isTerminal := terminalStatus[service.ProvisioningStatus] | ||
return service, isTerminal, nil | ||
}, | ||
IntervalStrategy: async.LinearIntervalStrategy(retryInterval), | ||
Timeout: timeout, | ||
}) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "waiting for service failed") | ||
} | ||
return service.(*Service), nil | ||
} | ||
|
||
type WaitForServerRequest struct { | ||
ServerID uint64 | ||
Zone scw.Zone | ||
Timeout *time.Duration | ||
RetryInterval *time.Duration | ||
} | ||
|
||
func (s *API) WaitForServer(req *WaitForServerRequest, opts ...scw.RequestOption) (*Server, error) { | ||
timeout := defaultTimeout | ||
if req.Timeout != nil { | ||
timeout = *req.Timeout | ||
} | ||
retryInterval := defaultRetryInterval | ||
if req.RetryInterval != nil { | ||
retryInterval = *req.RetryInterval | ||
} | ||
terminalStatus := map[ServerStatus]struct{}{ | ||
ServerStatusReady: {}, | ||
ServerStatusError: {}, | ||
ServerStatusLocked: {}, | ||
ServerStatusStopped: {}, | ||
ServerStatusBusy: {}, | ||
ServerStatusRescue: {}, | ||
} | ||
server, err := async.WaitSync(&async.WaitSyncConfig{ | ||
Get: func() (interface{}, bool, error) { | ||
server, err := s.GetServer(&GetServerRequest{ | ||
Zone: req.Zone, | ||
ServerID: req.ServerID, | ||
}, opts...) | ||
if err != nil { | ||
return nil, false, err | ||
} | ||
_, isTerminal := terminalStatus[server.Status] | ||
return server, isTerminal, nil | ||
}, | ||
IntervalStrategy: async.LinearIntervalStrategy(retryInterval), | ||
Timeout: timeout, | ||
}) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "waiting for server failed") | ||
} | ||
return server.(*Server), nil | ||
} |