Skip to content

Commit

Permalink
fix(instance): wait for volumes after server deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
Codelax committed Sep 27, 2024
1 parent d972d49 commit 0568053
Showing 1 changed file with 45 additions and 15 deletions.
60 changes: 45 additions & 15 deletions internal/namespaces/instance/v1/custom_server_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

0 comments on commit 0568053

Please sign in to comment.