Skip to content

Commit 9a4e57d

Browse files
authored
feat(baremetal): add wait method for server options (#1465)
1 parent d8d6844 commit 9a4e57d

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

api/baremetal/v1/server_utils.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,57 @@ func (s *API) GetOfferByName(req *GetOfferByNameRequest) (*Offer, error) {
153153

154154
return nil, errors.New("could not find the offer ID from name %s", req.OfferName)
155155
}
156+
157+
// WaitForServerOptionsRequest is used by WaitForServerOptions method.
158+
type WaitForServerOptionsRequest struct {
159+
ServerID string
160+
Zone scw.Zone
161+
Timeout *time.Duration
162+
RetryInterval *time.Duration
163+
}
164+
165+
// WaitForServerOptions wait for all server options to be in a "terminal state" before returning.
166+
// This function can be used to wait for all server options to be set.
167+
func (s *API) WaitForServerOptions(req *WaitForServerOptionsRequest, opts ...scw.RequestOption) (*Server, error) {
168+
timeout := defaultTimeout
169+
if req.Timeout != nil {
170+
timeout = *req.Timeout
171+
}
172+
retryInterval := defaultRetryInterval
173+
if req.RetryInterval != nil {
174+
retryInterval = *req.RetryInterval
175+
}
176+
177+
terminalStatus := map[ServerOptionOptionStatus]struct{}{
178+
ServerOptionOptionStatusOptionStatusEnable: {},
179+
ServerOptionOptionStatusOptionStatusError: {},
180+
ServerOptionOptionStatusOptionStatusUnknown: {},
181+
}
182+
183+
server, err := async.WaitSync(&async.WaitSyncConfig{
184+
Get: func() (interface{}, bool, error) {
185+
res, err := s.GetServer(&GetServerRequest{
186+
ServerID: req.ServerID,
187+
Zone: req.Zone,
188+
}, opts...)
189+
if err != nil {
190+
return nil, false, err
191+
}
192+
193+
for i := range res.Options {
194+
_, isTerminal := terminalStatus[res.Options[i].Status]
195+
if !isTerminal {
196+
return res, isTerminal, nil
197+
}
198+
}
199+
return res, true, err
200+
},
201+
Timeout: timeout,
202+
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
203+
})
204+
if err != nil {
205+
return nil, errors.Wrap(err, "waiting for server options failed")
206+
}
207+
208+
return server.(*Server), nil
209+
}

0 commit comments

Comments
 (0)