diff --git a/api/baremetal/v1/server_utils.go b/api/baremetal/v1/server_utils.go index 6df64bfd6..7b6672705 100644 --- a/api/baremetal/v1/server_utils.go +++ b/api/baremetal/v1/server_utils.go @@ -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{}{ @@ -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{}{ diff --git a/api/baremetal/v1alpha1/server_utils.go b/api/baremetal/v1alpha1/server_utils.go index 07acbb2ef..7b6672705 100644 --- a/api/baremetal/v1alpha1/server_utils.go +++ b/api/baremetal/v1alpha1/server_utils.go @@ -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: {}, @@ -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") @@ -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: {}, @@ -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") diff --git a/api/instance/v1/image_utils.go b/api/instance/v1/image_utils.go index 681d220c4..e9d4a90f7 100644 --- a/api/instance/v1/image_utils.go +++ b/api/instance/v1/image_utils.go @@ -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{}{ @@ -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") diff --git a/api/instance/v1/server_utils.go b/api/instance/v1/server_utils.go index 66c352939..88f958351 100644 --- a/api/instance/v1/server_utils.go +++ b/api/instance/v1/server_utils.go @@ -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. @@ -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: {}, @@ -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") @@ -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{ @@ -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 diff --git a/api/instance/v1/server_utils_test.go b/api/instance/v1/server_utils_test.go index 39ba9c7df..ceffcf202 100644 --- a/api/instance/v1/server_utils_test.go +++ b/api/instance/v1/server_utils_test.go @@ -13,13 +13,6 @@ import ( "github.com/scaleway/scaleway-sdk-go/scw" ) -func init() { - // set interval strategy to 0 when replaying cassettes - if httprecorder.IsUpdatingCassette() { - RetryInterval = 0 - } -} - func TestAPI_GetServerType(t *testing.T) { client, r, err := httprecorder.CreateRecordedScwClient("get-server-type") testhelpers.AssertNoError(t, err) diff --git a/api/instance/v1/snapshot_utils.go b/api/instance/v1/snapshot_utils.go index fb5068d90..683b0056a 100644 --- a/api/instance/v1/snapshot_utils.go +++ b/api/instance/v1/snapshot_utils.go @@ -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{}{ @@ -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") diff --git a/api/instance/v1/volume_utils.go b/api/instance/v1/volume_utils.go index 0690ff209..1f9b7ad60 100644 --- a/api/instance/v1/volume_utils.go +++ b/api/instance/v1/volume_utils.go @@ -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{}{ @@ -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") diff --git a/api/k8s/v1/k8s_helpers.go b/api/k8s/v1/k8s_helpers.go index 6ed362ca6..6f61d5939 100644 --- a/api/k8s/v1/k8s_helpers.go +++ b/api/k8s/v1/k8s_helpers.go @@ -8,33 +8,33 @@ 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 ( - waitForClusterDefaultTimeout = time.Minute * 15 - waitForPoolDefaultTimeout = time.Minute * 15 - waitForNodeDefaultTimeout = time.Minute * 15 - defaultRetryInterval = time.Second * 5 + waitForClusterDefaultTimeout = 15 * time.Minute + waitForPoolDefaultTimeout = 15 * time.Minute + waitForNodeDefaultTimeout = 15 * time.Minute + defaultRetryInterval = 5 * time.Second ) // WaitForClusterRequest is used by WaitForCluster method. type WaitForClusterRequest struct { - ClusterID string - Region scw.Region - Status ClusterStatus - Timeout *time.Duration + ClusterID string + Region scw.Region + Status ClusterStatus + Timeout *time.Duration + RetryInterval *time.Duration } // WaitForCluster waits for the cluster to be in a "terminal state" before returning. func (s *API) WaitForCluster(req *WaitForClusterRequest) (*Cluster, error) { - timeout := waitForClusterDefaultTimeout - if req.Timeout != nil { - timeout = *req.Timeout + timeout := *req.Timeout + if timeout == 0 { + timeout = waitForClusterDefaultTimeout + } + retryInterval := defaultRetryInterval + if req.RetryInterval != nil { + retryInterval = *req.RetryInterval } + terminalStatus := map[ClusterStatus]struct{}{ ClusterStatusReady: {}, ClusterStatusLocked: {}, @@ -56,7 +56,7 @@ func (s *API) WaitForCluster(req *WaitForClusterRequest) (*Cluster, error) { return cluster, isTerminal, nil }, Timeout: timeout, - IntervalStrategy: async.LinearIntervalStrategy(RetryInterval), + IntervalStrategy: async.LinearIntervalStrategy(retryInterval), }) if err != nil { return nil, errors.Wrap(err, "waiting for cluster failed") @@ -67,23 +67,28 @@ func (s *API) WaitForCluster(req *WaitForClusterRequest) (*Cluster, error) { // WaitForPoolRequest is used by WaitForPool method. type WaitForPoolRequest struct { - PoolID string - Region scw.Region - Timeout *time.Duration + PoolID string + Region scw.Region + Timeout *time.Duration + RetryInterval *time.Duration } // WaitForPool waits for a pool to be ready func (s *API) WaitForPool(req *WaitForPoolRequest) (*Pool, error) { + timeout := *req.Timeout + if timeout == 0 { + timeout = waitForPoolDefaultTimeout + } + retryInterval := defaultRetryInterval + if req.RetryInterval != nil { + retryInterval = *req.RetryInterval + } + terminalStatus := map[PoolStatus]struct{}{ PoolStatusReady: {}, PoolStatusWarning: {}, } - timeout := waitForPoolDefaultTimeout - if req.Timeout != nil { - timeout = *req.Timeout - } - pool, err := async.WaitSync(&async.WaitSyncConfig{ Get: func() (interface{}, bool, error) { res, err := s.GetPool(&GetPoolRequest{ @@ -99,7 +104,7 @@ func (s *API) WaitForPool(req *WaitForPoolRequest) (*Pool, error) { return res, isTerminal, nil }, Timeout: timeout, - IntervalStrategy: async.LinearIntervalStrategy(RetryInterval), + IntervalStrategy: async.LinearIntervalStrategy(retryInterval), }) if err != nil { return nil, errors.Wrap(err, "waiting for pool failed") @@ -110,22 +115,27 @@ func (s *API) WaitForPool(req *WaitForPoolRequest) (*Pool, error) { // WaitForNodeRequest is used by WaitForNode method. type WaitForNodeRequest struct { - NodeID string - Region scw.Region - Timeout *time.Duration + NodeID string + Region scw.Region + Timeout *time.Duration + RetryInterval *time.Duration } // WaitForNode waits for a Node to be ready func (s *API) WaitForNode(req *WaitForNodeRequest) (*Node, error) { - terminalStatus := map[NodeStatus]struct{}{ - NodeStatusCreationError: {}, - NodeStatusReady: {}, - } - timeout := waitForNodeDefaultTimeout if req.Timeout != nil { timeout = *req.Timeout } + retryInterval := defaultRetryInterval + if req.RetryInterval != nil { + retryInterval = *req.RetryInterval + } + + terminalStatus := map[NodeStatus]struct{}{ + NodeStatusCreationError: {}, + NodeStatusReady: {}, + } node, err := async.WaitSync(&async.WaitSyncConfig{ Get: func() (interface{}, bool, error) { @@ -142,7 +152,7 @@ func (s *API) WaitForNode(req *WaitForNodeRequest) (*Node, error) { return res, isTerminal, nil }, Timeout: timeout, - IntervalStrategy: async.LinearIntervalStrategy(RetryInterval), + IntervalStrategy: async.LinearIntervalStrategy(retryInterval), }) if err != nil { return nil, errors.Wrap(err, "waiting for node failed") diff --git a/api/k8s/v1beta3/k8s_helpers.go b/api/k8s/v1beta3/k8s_helpers.go index 66ae31d87..a8ce755f1 100644 --- a/api/k8s/v1beta3/k8s_helpers.go +++ b/api/k8s/v1beta3/k8s_helpers.go @@ -8,16 +8,31 @@ import ( "github.com/scaleway/scaleway-sdk-go/scw" ) +const ( + waitForClusterDefaultTimeout = 15 * time.Minute + defaultRetryInterval = 5 * time.Second +) + // WaitForClusterRequest is used by WaitForCluster method. type WaitForClusterRequest struct { - ClusterID string - Region scw.Region - Status ClusterStatus - Timeout time.Duration + ClusterID string + Region scw.Region + Status ClusterStatus + Timeout *time.Duration + RetryInterval *time.Duration } // WaitForCluster waits for the cluster to be in a "terminal state" before returning. func (s *API) WaitForCluster(req *WaitForClusterRequest) (*Cluster, error) { + timeout := waitForClusterDefaultTimeout + if req.Timeout != nil { + timeout = *req.Timeout + } + retryInterval := defaultRetryInterval + if req.RetryInterval != nil { + retryInterval = *req.RetryInterval + } + terminalStatus := map[ClusterStatus]struct{}{ ClusterStatusReady: {}, ClusterStatusError: {}, @@ -39,8 +54,8 @@ func (s *API) WaitForCluster(req *WaitForClusterRequest) (*Cluster, error) { _, isTerminal := terminalStatus[cluster.Status] return cluster, isTerminal, nil }, - Timeout: req.Timeout, - IntervalStrategy: async.LinearIntervalStrategy(5 * time.Second), + Timeout: timeout, + IntervalStrategy: async.LinearIntervalStrategy(retryInterval), }) if err != nil { return nil, errors.Wrap(err, "waiting for cluster failed") diff --git a/api/k8s/v1beta4/k8s_helpers.go b/api/k8s/v1beta4/k8s_helpers.go index 73a7e0a9b..0a24fc814 100644 --- a/api/k8s/v1beta4/k8s_helpers.go +++ b/api/k8s/v1beta4/k8s_helpers.go @@ -8,25 +8,20 @@ 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 ( - waitForClusterDefaultTimeout = time.Minute * 15 - waitForPoolDefaultTimeout = time.Minute * 15 - waitForNodeDefaultTimeout = time.Minute * 15 - defaultRetryInterval = time.Second * 5 + waitForClusterDefaultTimeout = 15 * time.Minute + waitForPoolDefaultTimeout = 15 * time.Minute + waitForNodeDefaultTimeout = 15 * time.Minute + defaultRetryInterval = 5 * time.Second ) // WaitForClusterRequest is used by WaitForCluster method. type WaitForClusterRequest struct { - ClusterID string - Region scw.Region - Status ClusterStatus - Timeout *time.Duration + ClusterID string + Region scw.Region + Status ClusterStatus + Timeout *time.Duration + RetryInterval *time.Duration } // WaitForCluster waits for the cluster to be in a "terminal state" before returning. @@ -35,6 +30,11 @@ func (s *API) WaitForCluster(req *WaitForClusterRequest) (*Cluster, error) { if req.Timeout != nil { timeout = *req.Timeout } + retryInterval := defaultRetryInterval + if req.RetryInterval != nil { + retryInterval = *req.RetryInterval + } + terminalStatus := map[ClusterStatus]struct{}{ ClusterStatusReady: {}, ClusterStatusError: {}, @@ -57,7 +57,7 @@ func (s *API) WaitForCluster(req *WaitForClusterRequest) (*Cluster, error) { return cluster, isTerminal, nil }, Timeout: timeout, - IntervalStrategy: async.LinearIntervalStrategy(RetryInterval), + IntervalStrategy: async.LinearIntervalStrategy(retryInterval), }) if err != nil { return nil, errors.Wrap(err, "waiting for cluster failed") @@ -67,22 +67,27 @@ func (s *API) WaitForCluster(req *WaitForClusterRequest) (*Cluster, error) { // WaitForPoolRequest is used by WaitForPool method. type WaitForPoolRequest struct { - PoolID string - Region scw.Region - Timeout *time.Duration + PoolID string + Region scw.Region + Timeout *time.Duration + RetryInterval *time.Duration } // WaitForPool waits for a pool to be ready func (s *API) WaitForPool(req *WaitForPoolRequest) (*Pool, error) { - terminalStatus := map[PoolStatus]struct{}{ - PoolStatusReady: {}, - PoolStatusWarning: {}, - } - timeout := waitForPoolDefaultTimeout if req.Timeout != nil { timeout = *req.Timeout } + retryInterval := defaultRetryInterval + if req.RetryInterval != nil { + retryInterval = *req.RetryInterval + } + + terminalStatus := map[PoolStatus]struct{}{ + PoolStatusReady: {}, + PoolStatusWarning: {}, + } pool, err := async.WaitSync(&async.WaitSyncConfig{ Get: func() (interface{}, bool, error) { @@ -99,7 +104,7 @@ func (s *API) WaitForPool(req *WaitForPoolRequest) (*Pool, error) { return res, isTerminal, nil }, Timeout: timeout, - IntervalStrategy: async.LinearIntervalStrategy(RetryInterval), + IntervalStrategy: async.LinearIntervalStrategy(retryInterval), }) return pool.(*Pool), err @@ -107,22 +112,27 @@ func (s *API) WaitForPool(req *WaitForPoolRequest) (*Pool, error) { // WaitForNodeRequest is used by WaitForNode method. type WaitForNodeRequest struct { - NodeID string - Region scw.Region - Timeout *time.Duration + NodeID string + Region scw.Region + Timeout *time.Duration + RetryInterval *time.Duration } // WaitForNode waits for a Node to be ready func (s *API) WaitForNode(req *WaitForNodeRequest) (*Node, error) { - terminalStatus := map[NodeStatus]struct{}{ - NodeStatusCreationError: {}, - NodeStatusReady: {}, - } - timeout := waitForNodeDefaultTimeout if req.Timeout != nil { timeout = *req.Timeout } + retryInterval := defaultRetryInterval + if req.RetryInterval != nil { + retryInterval = *req.RetryInterval + } + + terminalStatus := map[NodeStatus]struct{}{ + NodeStatusCreationError: {}, + NodeStatusReady: {}, + } node, err := async.WaitSync(&async.WaitSyncConfig{ Get: func() (interface{}, bool, error) { @@ -139,7 +149,7 @@ func (s *API) WaitForNode(req *WaitForNodeRequest) (*Node, error) { return res, isTerminal, nil }, Timeout: timeout, - IntervalStrategy: async.LinearIntervalStrategy(RetryInterval), + IntervalStrategy: async.LinearIntervalStrategy(retryInterval), }) return node.(*Node), err diff --git a/api/lb/v1/lb_utils.go b/api/lb/v1/lb_utils.go index 6e5d60a72..f7a43f571 100644 --- a/api/lb/v1/lb_utils.go +++ b/api/lb/v1/lb_utils.go @@ -8,16 +8,31 @@ import ( "github.com/scaleway/scaleway-sdk-go/scw" ) +const ( + defaultRetryInterval = 2 * time.Second + defaultTimeout = 5 * time.Minute +) + // WaitForLbRequest is used by WaitForLb method. type WaitForLbRequest struct { - LbID string - Region scw.Region - Timeout time.Duration + LbID string + Region scw.Region + Timeout *time.Duration + RetryInterval *time.Duration } // WaitForLb waits for the lb to be in a "terminal state" before returning. // This function can be used to wait for a lb to be ready for example. func (s *API) WaitForLb(req *WaitForLbRequest) (*Lb, error) { + timeout := defaultTimeout + if req.Timeout != nil { + timeout = *req.Timeout + } + retryInterval := defaultRetryInterval + if req.RetryInterval != nil { + retryInterval = *req.RetryInterval + } + terminalStatus := map[LbStatus]struct{}{ LbStatusReady: {}, LbStatusStopped: {}, @@ -39,8 +54,8 @@ func (s *API) WaitForLb(req *WaitForLbRequest) (*Lb, error) { return res, isTerminal, nil }, - Timeout: req.Timeout, - IntervalStrategy: async.LinearIntervalStrategy(5 * time.Second), + Timeout: timeout, + IntervalStrategy: async.LinearIntervalStrategy(retryInterval), }) if err != nil { return nil, errors.Wrap(err, "waiting for lb failed") diff --git a/api/rdb/v1/rdb_utils.go b/api/rdb/v1/rdb_utils.go index ce97648ef..70f52cec0 100644 --- a/api/rdb/v1/rdb_utils.go +++ b/api/rdb/v1/rdb_utils.go @@ -8,16 +8,31 @@ import ( "github.com/scaleway/scaleway-sdk-go/scw" ) +const ( + defaultRetryInterval = 15 * time.Second + defaultTimeout = 15 * time.Minute +) + // WaitForInstanceRequest is used by WaitForInstance method. type WaitForInstanceRequest struct { - InstanceID string - Region scw.Region - Timeout time.Duration + InstanceID string + Region scw.Region + Timeout *time.Duration + RetryInterval *time.Duration } // WaitForInstance waits for the instance to be in a "terminal state" before returning. // This function can be used to wait for an instance to be ready for example. func (s *API) WaitForInstance(req *WaitForInstanceRequest) (*Instance, error) { + timeout := defaultTimeout + if req.Timeout != nil { + timeout = *req.Timeout + } + retryInterval := defaultRetryInterval + if req.RetryInterval != nil { + retryInterval = *req.RetryInterval + } + terminalStatus := map[InstanceStatus]struct{}{ InstanceStatusReady: {}, InstanceStatusDiskFull: {}, @@ -38,8 +53,8 @@ func (s *API) WaitForInstance(req *WaitForInstanceRequest) (*Instance, error) { return res, isTerminal, nil }, - Timeout: req.Timeout, - IntervalStrategy: async.LinearIntervalStrategy(5 * time.Second), + Timeout: timeout, + IntervalStrategy: async.LinearIntervalStrategy(retryInterval), }) if err != nil { return nil, errors.Wrap(err, "waiting for instance failed") diff --git a/api/registry/v1/image_utils.go b/api/registry/v1/image_utils.go index 0ea40002d..2eba338e1 100644 --- a/api/registry/v1/image_utils.go +++ b/api/registry/v1/image_utils.go @@ -12,20 +12,20 @@ import ( type WaitForImageRequest struct { ImageID string Region scw.Region - Timeout time.Duration - RetryInterval time.Duration + Timeout *time.Duration + RetryInterval *time.Duration } // WaitForImage wait for the image to be in a "terminal state" before returning. // This function can be used to wait for an image to be ready for example. func (s *API) WaitForImage(req *WaitForImageRequest) (*Image, 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[ImageStatus]struct{}{ diff --git a/api/registry/v1/registry_utils.go b/api/registry/v1/registry_utils.go index b03ecbc22..b29f48784 100644 --- a/api/registry/v1/registry_utils.go +++ b/api/registry/v1/registry_utils.go @@ -17,20 +17,20 @@ const ( type WaitForNamespaceRequest struct { NamespaceID string Region scw.Region - Timeout time.Duration - RetryInterval time.Duration + Timeout *time.Duration + RetryInterval *time.Duration } // WaitForNamespace wait for the namespace to be in a "terminal state" before returning. // This function can be used to wait for a namespace to be ready for example. func (s *API) WaitForNamespace(req *WaitForNamespaceRequest) (*Namespace, 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[NamespaceStatus]struct{}{ diff --git a/api/registry/v1/tag_utils.go b/api/registry/v1/tag_utils.go index 9f834a558..c0c4f3cf5 100644 --- a/api/registry/v1/tag_utils.go +++ b/api/registry/v1/tag_utils.go @@ -12,20 +12,20 @@ import ( type WaitForTagRequest struct { TagID string Region scw.Region - Timeout time.Duration - RetryInterval time.Duration + Timeout *time.Duration + RetryInterval *time.Duration } // WaitForTag wait for the tag to be in a "terminal state" before returning. // This function can be used to wait for a tag to be ready for example. func (s *API) WaitForTag(req *WaitForTagRequest) (*Tag, 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[TagStatus]struct{}{ diff --git a/example_test.go b/example_test.go index 5f2c99276..77a3f5ee8 100644 --- a/example_test.go +++ b/example_test.go @@ -109,10 +109,11 @@ func Example_createServer() { } // Start the server and wait until it's ready. + timeout := 5 * time.Minute err = instanceAPI.ServerActionAndWait(&instance.ServerActionAndWaitRequest{ ServerID: createRes.Server.ID, Action: instance.ServerActionPoweron, - Timeout: 5 * time.Minute, + Timeout: &timeout, }) if err != nil { panic(err) @@ -139,13 +140,14 @@ func Example_rebootAllServers() { } // For each server if they are running we reboot them using ServerActionAndWait + timeout := 5 * time.Minute for _, server := range response.Servers { if server.State == instance.ServerStateRunning { fmt.Println("Rebooting server with ID", server.ID) err = instanceAPI.ServerActionAndWait(&instance.ServerActionAndWaitRequest{ ServerID: server.ID, Action: instance.ServerActionReboot, - Timeout: 5 * time.Minute, + Timeout: &timeout, }) if err != nil { panic(err)