Skip to content
Merged
Changes from all 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
66 changes: 48 additions & 18 deletions api/baremetal/v1alpha1/server_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,60 @@ 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
Timeout 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 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: {},
}
Expand All @@ -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
Expand Down