diff --git a/api/baremetal/v1alpha1/server_utils.go b/api/baremetal/v1alpha1/server_utils.go index af8008225..a57683683 100644 --- a/api/baremetal/v1alpha1/server_utils.go +++ b/api/baremetal/v1alpha1/server_utils.go @@ -8,7 +8,7 @@ import ( "github.com/scaleway/scaleway-sdk-go/scw" ) -// WaitForServerRequest is used by WaitForServer method +// WaitForServerRequest is used by WaitForServer method. type WaitForServerRequest struct { ServerID string Zone scw.Zone @@ -16,21 +16,52 @@ type WaitForServerRequest struct { } // 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 installed for example. +// This function can be used to wait for a server to be created. func (s *API) WaitForServer(req *WaitForServerRequest) (*Server, scw.SdkError) { - terminalStatus := map[ServerStatus]struct{}{ - ServerStatusReady: {}, - ServerStatusStopped: {}, - ServerStatusError: {}, - ServerStatusUndelivered: {}, - ServerStatusLocked: {}, - ServerStatusUnknown: {}, + ServerStatusReady: {}, + ServerStatusStopped: {}, + ServerStatusError: {}, + ServerStatusLocked: {}, + ServerStatusUnknown: {}, } + server, err := async.WaitSync(&async.WaitSyncConfig{ + Get: func() (interface{}, error, bool) { + res, err := s.GetServer(&GetServerRequest{ + ServerID: req.ServerID, + Zone: req.Zone, + }) + if err != nil { + return nil, err, false + } + + _, isTerminal := terminalStatus[res.Status] + return res, err, isTerminal + }, + Timeout: req.Timeout, + IntervalStrategy: async.LinearIntervalStrategy(5 * time.Second), + }) + if err != nil { + return nil, errors.Wrap(err, "waiting for server failed") + } + + return server.(*Server), nil +} + +// WaitForServerInstallRequest is used by WaitForServerInstall method. +type WaitForServerInstallRequest struct { + ServerID string + Zone scw.Zone + Timeout 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, scw.SdkError) { installTerminalStatus := map[ServerInstallStatus]struct{}{ ServerInstallStatusCompleted: {}, - ServerInstallStatusToInstall: {}, ServerInstallStatusError: {}, ServerInstallStatusUnknown: {}, } @@ -41,23 +72,22 @@ func (s *API) WaitForServer(req *WaitForServerRequest) (*Server, scw.SdkError) { ServerID: req.ServerID, Zone: req.Zone, }) - if err != nil { return nil, err, false } - _, isTerminal := terminalStatus[res.Status] - isTerminalInstall := true - if res.Install != nil { - _, isTerminalInstall = installTerminalStatus[res.Install.Status] + + if res.Install == nil { + return nil, errors.New("server creation has not begun for server %s", req.ServerID), false } - return res, err, isTerminal && isTerminalInstall + _, isTerminal := installTerminalStatus[res.Install.Status] + return res, err, isTerminal }, Timeout: req.Timeout, - IntervalStrategy: async.LinearIntervalStrategy(5 * time.Second), + IntervalStrategy: async.LinearIntervalStrategy(15 * time.Second), }) if err != nil { - return nil, errors.Wrap(err, "waiting for server failed") + return nil, errors.Wrap(err, "waiting for server installation failed") } return server.(*Server), nil