Skip to content

Commit

Permalink
feat(dedibox): add waiter support for Service and Server (#2157)
Browse files Browse the repository at this point in the history
  • Loading branch information
Laure-di committed Jul 31, 2024
1 parent 2cf3200 commit 1dc290c
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions api/dedibox/v1/dedibox_utils.go
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
}

0 comments on commit 1dc290c

Please sign in to comment.