From 0568053f59011d1fdf1e1561ecbc4f2b99ae967b Mon Sep 17 00:00:00 2001 From: Jules Casteran Date: Fri, 27 Sep 2024 15:48:46 +0200 Subject: [PATCH] fix(instance): wait for volumes after server deletion --- .../instance/v1/custom_server_delete.go | 60 ++++++++++++++----- 1 file changed, 45 insertions(+), 15 deletions(-) diff --git a/internal/namespaces/instance/v1/custom_server_delete.go b/internal/namespaces/instance/v1/custom_server_delete.go index 4a99c35c3d..a69ee6787c 100644 --- a/internal/namespaces/instance/v1/custom_server_delete.go +++ b/internal/namespaces/instance/v1/custom_server_delete.go @@ -185,22 +185,9 @@ func serverDeleteCommand() *core.Command { case volume.VolumeType == instance.VolumeServerVolumeTypeScratch: continue } - if volume.VolumeType == instance.VolumeServerVolumeTypeSbsVolume { - err = block.NewAPI(client).DeleteVolume(&block.DeleteVolumeRequest{ - Zone: deleteServerArgs.Zone, - VolumeID: volume.ID, - }) - } else { - err = api.DeleteVolume(&instance.DeleteVolumeRequest{ - Zone: deleteServerArgs.Zone, - VolumeID: volume.ID, - }) - } + err := serverDeleteVolume(volume, api, block.NewAPI(client)) if err != nil { - return nil, &core.CliError{ - Err: err, - Hint: "Make sure this resource have been deleted or try to delete it manually.", - } + return nil, err } humanSize, err := human.Marshal(volume.Size, nil) if err != nil { @@ -226,3 +213,46 @@ func serverDeleteCommand() *core.Command { }, } } + +func errorDeletingResource(err error) error { + return &core.CliError{ + Err: err, + Hint: "Make sure this resource have been deleted or try to delete it manually.", + } +} + +func serverDeleteVolume(volume *instance.VolumeServer, instanceAPI *instance.API, blockAPI *block.API) error { + var err error + + if volume.VolumeType == instance.VolumeServerVolumeTypeSbsVolume { + _, err = blockAPI.WaitForVolumeAndReferences(&block.WaitForVolumeAndReferencesRequest{ + Zone: volume.Zone, + VolumeID: volume.ID, + }) + if err != nil { + return errorDeletingResource(err) + } + + err = blockAPI.DeleteVolume(&block.DeleteVolumeRequest{ + Zone: volume.Zone, + VolumeID: volume.ID, + }) + } else { + _, err = instanceAPI.WaitForVolume(&instance.WaitForVolumeRequest{ + VolumeID: volume.ID, + Zone: volume.Zone, + }) + if err != nil { + return errorDeletingResource(err) + } + err = instanceAPI.DeleteVolume(&instance.DeleteVolumeRequest{ + Zone: volume.Zone, + VolumeID: volume.ID, + }) + } + if err != nil { + return errorDeletingResource(err) + + } + return nil +}