Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(baremetal): add server helpers func in v1 #397

Merged
merged 1 commit into from
Apr 29, 2020
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
140 changes: 140 additions & 0 deletions api/baremetal/v1/server_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package baremetal

import (
"time"

"github.com/scaleway/scaleway-sdk-go/internal/async"
"github.com/scaleway/scaleway-sdk-go/internal/errors"
"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
)

// 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 created.
func (s *API) WaitForServer(req *WaitForServerRequest) (*Server, error) {
terminalStatus := map[ServerStatus]struct{}{
ServerStatusReady: {},
ServerStatusStopped: {},
ServerStatusError: {},
ServerStatusLocked: {},
ServerStatusUnknown: {},
}

server, err := async.WaitSync(&async.WaitSyncConfig{
Get: func() (interface{}, bool, error) {
res, err := s.GetServer(&GetServerRequest{
ServerID: req.ServerID,
Zone: req.Zone,
})
if err != nil {
return nil, false, err
}

_, isTerminal := terminalStatus[res.Status]
return res, isTerminal, err
},
Timeout: req.Timeout,
IntervalStrategy: async.LinearIntervalStrategy(RetryInterval),
})
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, error) {
installTerminalStatus := map[ServerInstallStatus]struct{}{
ServerInstallStatusCompleted: {},
ServerInstallStatusError: {},
ServerInstallStatusUnknown: {},
}

server, err := async.WaitSync(&async.WaitSyncConfig{
Get: func() (interface{}, bool, error) {
res, err := s.GetServer(&GetServerRequest{
ServerID: req.ServerID,
Zone: req.Zone,
})
if err != nil {
return nil, false, err
}

if res.Install == nil {
return nil, false, errors.New("server creation has not begun for server %s", req.ServerID)
}

_, isTerminal := installTerminalStatus[res.Install.Status]
return res, isTerminal, err
},
Timeout: req.Timeout,
IntervalStrategy: async.LinearIntervalStrategy(RetryInterval),
})
if err != nil {
return nil, errors.Wrap(err, "waiting for server installation failed")
}

return server.(*Server), nil
}

// GetServerOffer returns the offer of a baremetal server
func (s *API) GetServerOffer(server *Server) (*Offer, error) {
offer, err := s.GetOffer(&GetOfferRequest{
OfferID: server.OfferID,
Zone: server.Zone,
})
if err != nil {
return nil, err
}

return offer, nil
}

type GetOfferByNameRequest struct {
OfferName string
Zone scw.Zone
}

// GetOfferByName returns an offer from its commercial name
func (s *API) GetOfferByName(req *GetOfferByNameRequest) (*Offer, error) {
res, err := s.ListOffers(&ListOffersRequest{
Zone: req.Zone,
}, scw.WithAllPages())
if err != nil {
return nil, err
}

for _, offer := range res.Offers {
if req.OfferName == offer.Name {
return offer, nil
}
}

return nil, errors.New("could not find the offer ID from name %s", req.OfferName)
}