Skip to content

Commit d7d9501

Browse files
committed
feat: Add waiters for server operations
1 parent 43530ee commit d7d9501

File tree

2 files changed

+463
-3
lines changed

2 files changed

+463
-3
lines changed

services/iaas/wait/wait.go

Lines changed: 133 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@ const (
1515
CreateSuccess = "CREATED"
1616
VolumeAvailableStatus = "AVAILABLE"
1717
DeleteSuccess = "DELETED"
18-
ErrorStatus = "ERROR"
19-
ServerActiveStatus = "ACTIVE"
20-
ServerResizingStatus = "RESIZING"
18+
19+
ErrorStatus = "ERROR"
20+
21+
ServerActiveStatus = "ACTIVE"
22+
ServerResizingStatus = "RESIZING"
23+
ServerInactiveStatus = "INACTIVE"
24+
ServerDeallocatedStatus = "DEALLOCATED"
25+
ServerRescueStatus = "RESCUE"
2126

2227
RequestCreateAction = "CREATE"
2328
RequestUpdateAction = "UPDATE"
@@ -303,6 +308,131 @@ func DeleteServerWaitHandler(ctx context.Context, a APIClientInterface, projectI
303308
return handler
304309
}
305310

311+
// StartServerWaitHandler will wait for server start
312+
func StartServerWaitHandler(ctx context.Context, a APIClientInterface, projectId, serverId string) *wait.AsyncActionHandler[iaas.Server] {
313+
handler := wait.New(func() (waitFinished bool, response *iaas.Server, err error) {
314+
server, err := a.GetServerExecute(ctx, projectId, serverId)
315+
if err != nil {
316+
return false, server, err
317+
}
318+
if server.Id == nil || server.Status == nil {
319+
return false, server, fmt.Errorf("start failed for server with id %s, the response is not valid: the id or the status are missing", serverId)
320+
}
321+
if *server.Id == serverId && *server.Status == ServerActiveStatus {
322+
return true, server, nil
323+
}
324+
if *server.Id == serverId && *server.Status == ErrorStatus {
325+
if server.ErrorMessage != nil {
326+
return true, server, fmt.Errorf("start failed for server with id %s: %s", serverId, *server.ErrorMessage)
327+
}
328+
return true, server, fmt.Errorf("start failed for server with id %s", serverId)
329+
}
330+
return false, server, nil
331+
})
332+
handler.SetTimeout(20 * time.Minute)
333+
return handler
334+
}
335+
336+
// StopServerWaitHandler will wait for server stop
337+
func StopServerWaitHandler(ctx context.Context, a APIClientInterface, projectId, serverId string) *wait.AsyncActionHandler[iaas.Server] {
338+
handler := wait.New(func() (waitFinished bool, response *iaas.Server, err error) {
339+
server, err := a.GetServerExecute(ctx, projectId, serverId)
340+
if err != nil {
341+
return false, server, err
342+
}
343+
if server.Id == nil || server.Status == nil {
344+
return false, server, fmt.Errorf("stop failed for server with id %s, the response is not valid: the id or the status are missing", serverId)
345+
}
346+
if *server.Id == serverId && *server.Status == ServerInactiveStatus {
347+
return true, server, nil
348+
}
349+
if *server.Id == serverId && *server.Status == ErrorStatus {
350+
if server.ErrorMessage != nil {
351+
return true, server, fmt.Errorf("stop failed for server with id %s: %s", serverId, *server.ErrorMessage)
352+
}
353+
return true, server, fmt.Errorf("stop failed for server with id %s", serverId)
354+
}
355+
return false, server, nil
356+
})
357+
handler.SetTimeout(20 * time.Minute)
358+
return handler
359+
}
360+
361+
// DeallocateServerWaitHandler will wait for server deallocation
362+
func DeallocateServerWaitHandler(ctx context.Context, a APIClientInterface, projectId, serverId string) *wait.AsyncActionHandler[iaas.Server] {
363+
handler := wait.New(func() (waitFinished bool, response *iaas.Server, err error) {
364+
server, err := a.GetServerExecute(ctx, projectId, serverId)
365+
if err != nil {
366+
return false, server, err
367+
}
368+
if server.Id == nil || server.Status == nil {
369+
return false, server, fmt.Errorf("deallocate failed for server with id %s, the response is not valid: the id or the status are missing", serverId)
370+
}
371+
if *server.Id == serverId && *server.Status == ServerDeallocatedStatus {
372+
return true, server, nil
373+
}
374+
if *server.Id == serverId && *server.Status == ErrorStatus {
375+
if server.ErrorMessage != nil {
376+
return true, server, fmt.Errorf("deallocate failed for server with id %s: %s", serverId, *server.ErrorMessage)
377+
}
378+
return true, server, fmt.Errorf("deallocate failed for server with id %s", serverId)
379+
}
380+
return false, server, nil
381+
})
382+
handler.SetTimeout(20 * time.Minute)
383+
return handler
384+
}
385+
386+
// RescueServerWaitHandler will wait for server rescue
387+
func RescueServerWaitHandler(ctx context.Context, a APIClientInterface, projectId, serverId string) *wait.AsyncActionHandler[iaas.Server] {
388+
handler := wait.New(func() (waitFinished bool, response *iaas.Server, err error) {
389+
server, err := a.GetServerExecute(ctx, projectId, serverId)
390+
if err != nil {
391+
return false, server, err
392+
}
393+
if server.Id == nil || server.Status == nil {
394+
return false, server, fmt.Errorf("rescue failed for server with id %s, the response is not valid: the id or the status are missing", serverId)
395+
}
396+
if *server.Id == serverId && *server.Status == ServerRescueStatus {
397+
return true, server, nil
398+
}
399+
if *server.Id == serverId && *server.Status == ErrorStatus {
400+
if server.ErrorMessage != nil {
401+
return true, server, fmt.Errorf("rescue failed for server with id %s: %s", serverId, *server.ErrorMessage)
402+
}
403+
return true, server, fmt.Errorf("rescue failed for server with id %s", serverId)
404+
}
405+
return false, server, nil
406+
})
407+
handler.SetTimeout(20 * time.Minute)
408+
return handler
409+
}
410+
411+
// UnrescueServerWaitHandler will wait for server unrescue
412+
func UnrescueServerWaitHandler(ctx context.Context, a APIClientInterface, projectId, serverId string) *wait.AsyncActionHandler[iaas.Server] {
413+
handler := wait.New(func() (waitFinished bool, response *iaas.Server, err error) {
414+
server, err := a.GetServerExecute(ctx, projectId, serverId)
415+
if err != nil {
416+
return false, server, err
417+
}
418+
if server.Id == nil || server.Status == nil {
419+
return false, server, fmt.Errorf("unrescue failed for server with id %s, the response is not valid: the id or the status are missing", serverId)
420+
}
421+
if *server.Id == serverId && *server.Status == ServerActiveStatus {
422+
return true, server, nil
423+
}
424+
if *server.Id == serverId && *server.Status == ErrorStatus {
425+
if server.ErrorMessage != nil {
426+
return true, server, fmt.Errorf("unrescue failed for server with id %s: %s", serverId, *server.ErrorMessage)
427+
}
428+
return true, server, fmt.Errorf("unrescue failed for server with id %s", serverId)
429+
}
430+
return false, server, nil
431+
})
432+
handler.SetTimeout(20 * time.Minute)
433+
return handler
434+
}
435+
306436
// ProjectRequestWaitHandler will wait for a request to succeed.
307437
//
308438
// It receives a request ID that can be obtained from the "X-Request-Id" header in the HTTP response of any operation in the IaaS API.

0 commit comments

Comments
 (0)