Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
32 changes: 16 additions & 16 deletions api/baremetal/v1/server_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ const (
type WaitForServerRequest struct {
ServerID string
Zone scw.Zone
Timeout time.Duration
RetryInterval time.Duration
Timeout *time.Duration
RetryInterval *time.Duration
}

// WaitForServer wait for the server to be in a "terminal state" before returning.
// This function can be used to wait for a server to be created.
func (s *API) WaitForServer(req *WaitForServerRequest) (*Server, error) {
timeout := req.Timeout
if timeout == 0 {
timeout = defaultTimeout
timeout := defaultTimeout
if req.Timeout != nil {
timeout = *req.Timeout
}
retryInterval := req.RetryInterval
if retryInterval == 0 {
retryInterval = defaultRetryInterval
retryInterval := defaultRetryInterval
if req.RetryInterval != nil {
retryInterval = *req.RetryInterval
}

terminalStatus := map[ServerStatus]struct{}{
Expand Down Expand Up @@ -68,21 +68,21 @@ func (s *API) WaitForServer(req *WaitForServerRequest) (*Server, error) {
type WaitForServerInstallRequest struct {
ServerID string
Zone scw.Zone
Timeout time.Duration
RetryInterval time.Duration
Timeout *time.Duration
RetryInterval *time.Duration
}

// WaitForServerInstall wait for the server install to be in a
// "terminal state" before returning.
// This function can be used to wait for a server to be installed.
func (s *API) WaitForServerInstall(req *WaitForServerInstallRequest) (*Server, error) {
timeout := req.Timeout
if timeout == 0 {
timeout = defaultTimeout
timeout := defaultTimeout
if req.Timeout != nil {
timeout = *req.Timeout
}
retryInterval := req.RetryInterval
if retryInterval == 0 {
retryInterval = defaultRetryInterval
retryInterval := defaultRetryInterval
if req.RetryInterval != nil {
retryInterval = *req.RetryInterval
}

installTerminalStatus := map[ServerInstallStatus]struct{}{
Expand Down
47 changes: 31 additions & 16 deletions api/baremetal/v1alpha1/server_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,31 @@ import (
"github.com/scaleway/scaleway-sdk-go/scw"
)

var (
// RetryInterval is needed when running recorded tests (e.g. on scaleway-cli)
// it allows to execute the WaitFor funcs immediately
RetryInterval = defaultRetryInterval
)

const (
defaultRetryInterval = 15 * time.Second
defaultTimeout = 2 * time.Hour
)

// WaitForServerRequest is used by WaitForServer method.
type WaitForServerRequest struct {
ServerID string
Zone scw.Zone
Timeout time.Duration
ServerID string
Zone scw.Zone
Timeout *time.Duration
RetryInterval *time.Duration
}

// WaitForServer wait for the server to be in a "terminal state" before returning.
// This function can be used to wait for a server to be created.
func (s *API) WaitForServer(req *WaitForServerRequest) (*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: {},
ServerStatusStopped: {},
Expand All @@ -49,8 +54,8 @@ func (s *API) WaitForServer(req *WaitForServerRequest) (*Server, error) {
_, isTerminal := terminalStatus[res.Status]
return res, isTerminal, err
},
Timeout: req.Timeout,
IntervalStrategy: async.LinearIntervalStrategy(RetryInterval),
Timeout: timeout,
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
})
if err != nil {
return nil, errors.Wrap(err, "waiting for server failed")
Expand All @@ -61,15 +66,25 @@ func (s *API) WaitForServer(req *WaitForServerRequest) (*Server, error) {

// WaitForServerInstallRequest is used by WaitForServerInstall method.
type WaitForServerInstallRequest struct {
ServerID string
Zone scw.Zone
Timeout time.Duration
ServerID string
Zone scw.Zone
Timeout *time.Duration
RetryInterval *time.Duration
}

// WaitForServerInstall wait for the server install to be in a
// "terminal state" before returning.
// This function can be used to wait for a server to be installed.
func (s *API) WaitForServerInstall(req *WaitForServerInstallRequest) (*Server, error) {
timeout := defaultTimeout
if req.Timeout != nil {
timeout = *req.Timeout
}
retryInterval := defaultRetryInterval
if req.RetryInterval != nil {
retryInterval = *req.RetryInterval
}

installTerminalStatus := map[ServerInstallStatus]struct{}{
ServerInstallStatusCompleted: {},
ServerInstallStatusError: {},
Expand All @@ -93,8 +108,8 @@ func (s *API) WaitForServerInstall(req *WaitForServerInstallRequest) (*Server, e
_, isTerminal := installTerminalStatus[res.Install.Status]
return res, isTerminal, err
},
Timeout: req.Timeout,
IntervalStrategy: async.LinearIntervalStrategy(RetryInterval),
Timeout: timeout,
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
})
if err != nil {
return nil, errors.Wrap(err, "waiting for server installation failed")
Expand Down
20 changes: 13 additions & 7 deletions api/instance/v1/image_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@ import (

// WaitForImageRequest is used by WaitForImage method.
type WaitForImageRequest struct {
ImageID string
Zone scw.Zone
Timeout time.Duration
ImageID string
Zone scw.Zone
Timeout *time.Duration
RetryInterval *time.Duration
}

// WaitForImage wait for the image to be in a "terminal state" before returning.
func (s *API) WaitForImage(req *WaitForImageRequest) (*Image, error) {
if req.Timeout == 0 {
req.Timeout = defaultTimeout
timeout := defaultTimeout
if req.Timeout != nil {
timeout = *req.Timeout
}
retryInterval := defaultRetryInterval
if req.RetryInterval != nil {
retryInterval = *req.RetryInterval
}

terminalStatus := map[ImageState]struct{}{
Expand All @@ -40,8 +46,8 @@ func (s *API) WaitForImage(req *WaitForImageRequest) (*Image, error) {

return res.Image, isTerminal, err
},
Timeout: req.Timeout,
IntervalStrategy: async.LinearIntervalStrategy(RetryInterval),
Timeout: timeout,
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
})
if err != nil {
return nil, errors.Wrap(err, "waiting for image failed")
Expand Down
43 changes: 28 additions & 15 deletions api/instance/v1/server_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ const (
defaultRetryInterval = 5 * time.Second
)

var (
RetryInterval = defaultRetryInterval
)

// CreateServer creates a server.
func (s *API) CreateServer(req *CreateServerRequest, opts ...scw.RequestOption) (*CreateServerResponse, error) {
// If image is not a UUID we try to fetch it from marketplace.
Expand Down Expand Up @@ -52,14 +48,24 @@ func (s *API) UpdateServer(req *UpdateServerRequest, opts ...scw.RequestOption)

// WaitForServerRequest is used by WaitForServer method.
type WaitForServerRequest struct {
ServerID string
Zone scw.Zone
Timeout time.Duration
ServerID string
Zone scw.Zone
Timeout *time.Duration
RetryInterval *time.Duration
}

// WaitForServer wait for the server to be in a "terminal state" before returning.
// This function can be used to wait for a server to be started for example.
func (s *API) WaitForServer(req *WaitForServerRequest) (*Server, error) {
timeout := defaultTimeout
if req.Timeout != nil {
timeout = *req.Timeout
}
retryInterval := defaultRetryInterval
if req.RetryInterval != nil {
retryInterval = *req.RetryInterval
}

terminalStatus := map[ServerState]struct{}{
ServerStateStopped: {},
ServerStateStoppedInPlace: {},
Expand All @@ -81,8 +87,8 @@ func (s *API) WaitForServer(req *WaitForServerRequest) (*Server, error) {

return res.Server, isTerminal, err
},
Timeout: req.Timeout,
IntervalStrategy: async.LinearIntervalStrategy(RetryInterval),
Timeout: timeout,
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
})
if err != nil {
return nil, errors.Wrap(err, "waiting for server failed")
Expand All @@ -97,14 +103,20 @@ type ServerActionAndWaitRequest struct {
Action ServerAction

// Timeout: maximum time to wait before (default: 5 minutes)
Timeout time.Duration
Timeout *time.Duration
RetryInterval *time.Duration
}

// ServerActionAndWait start an action and wait for the server to be in the correct "terminal state"
// expected by this action.
func (s *API) ServerActionAndWait(req *ServerActionAndWaitRequest) error {
if req.Timeout == 0 {
req.Timeout = defaultTimeout
timeout := defaultTimeout
if req.Timeout != nil {
timeout = *req.Timeout
}
retryInterval := defaultRetryInterval
if req.RetryInterval != nil {
retryInterval = *req.RetryInterval
}

_, err := s.ServerAction(&ServerActionRequest{
Expand All @@ -117,9 +129,10 @@ func (s *API) ServerActionAndWait(req *ServerActionAndWaitRequest) error {
}

finalServer, err := s.WaitForServer(&WaitForServerRequest{
Zone: req.Zone,
ServerID: req.ServerID,
Timeout: req.Timeout,
Zone: req.Zone,
ServerID: req.ServerID,
Timeout: &timeout,
RetryInterval: &retryInterval,
})
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion api/instance/v1/server_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func init() {
// set interval strategy to 0 when replaying cassettes
if httprecorder.IsUpdatingCassette() {
RetryInterval = 0

}
}

Expand Down
20 changes: 13 additions & 7 deletions api/instance/v1/snapshot_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@ import (

// WaitForImageRequest is used by WaitForImage method.
type WaitForSnapshotRequest struct {
SnapshotID string
Zone scw.Zone
Timeout time.Duration
SnapshotID string
Zone scw.Zone
Timeout *time.Duration
RetryInterval *time.Duration
}

// WaitForSnapshot wait for the snapshot to be in a "terminal state" before returning.
func (s *API) WaitForSnapshot(req *WaitForSnapshotRequest) (*Snapshot, error) {
if req.Timeout == 0 {
req.Timeout = defaultTimeout
timeout := defaultTimeout
if req.Timeout != nil {
timeout = *req.Timeout
}
retryInterval := defaultRetryInterval
if req.RetryInterval != nil {
retryInterval = *req.RetryInterval
}

terminalStatus := map[SnapshotState]struct{}{
Expand All @@ -40,8 +46,8 @@ func (s *API) WaitForSnapshot(req *WaitForSnapshotRequest) (*Snapshot, error) {

return res.Snapshot, isTerminal, err
},
Timeout: req.Timeout,
IntervalStrategy: async.LinearIntervalStrategy(RetryInterval),
Timeout: timeout,
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
})
if err != nil {
return nil, errors.Wrap(err, "waiting for snapshot failed")
Expand Down
20 changes: 13 additions & 7 deletions api/instance/v1/volume_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@ import (

// WaitForImageRequest is used by WaitForImage method.
type WaitForVolumeRequest struct {
VolumeID string
Zone scw.Zone
Timeout time.Duration
VolumeID string
Zone scw.Zone
Timeout *time.Duration
RetryInterval *time.Duration
}

// WaitForSnapshot wait for the snapshot to be in a "terminal state" before returning.
func (s *API) WaitForVolume(req *WaitForVolumeRequest) (*Volume, error) {
if req.Timeout == 0 {
req.Timeout = defaultTimeout
timeout := defaultTimeout
if req.Timeout != nil {
timeout = *req.Timeout
}
retryInterval := defaultRetryInterval
if req.RetryInterval != nil {
retryInterval = *req.RetryInterval
}

terminalStatus := map[VolumeState]struct{}{
Expand All @@ -40,8 +46,8 @@ func (s *API) WaitForVolume(req *WaitForVolumeRequest) (*Volume, error) {

return res.Volume, isTerminal, err
},
Timeout: req.Timeout,
IntervalStrategy: async.LinearIntervalStrategy(RetryInterval),
Timeout: timeout,
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
})
if err != nil {
return nil, errors.Wrap(err, "waiting for volume failed")
Expand Down
Loading