From 4c016dcde3bce6d39e29f7b5376fdcd31d515879 Mon Sep 17 00:00:00 2001 From: Pavel Raiskup Date: Tue, 16 Jan 2024 10:52:30 +0100 Subject: [PATCH] Don't remove attached volumes Fixes: #21 --- resalloc_ibm_cloud/ibm_cloud_vm.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/resalloc_ibm_cloud/ibm_cloud_vm.py b/resalloc_ibm_cloud/ibm_cloud_vm.py index 3fdbff7..c1c752c 100755 --- a/resalloc_ibm_cloud/ibm_cloud_vm.py +++ b/resalloc_ibm_cloud/ibm_cloud_vm.py @@ -277,23 +277,30 @@ def delete_instance_attempt(service, instance_name, opts): assert resp.status_code == 204 log.debug("Delete IP request delivered") - # Query all volumes only after already potentionaly deleting an instance. - # The volumes might have been deleted automatically + # Query all volumes only after already potentially deleting an instance. + # The volumes should already be deleted automatically. volume_ids = [] volumes = service.list_volumes(limit=LIMIT).result["volumes"] for volume in volumes: if not volume["name"].startswith(instance_name): continue + if volume.get('attachment_state') == 'attached': + # Error 409 - can't remove attached volumes + continue + # Otherwise Error: Delete volume failed. Volume can be deleted # only when its status is available or failed., Code: 409 if not volume["status"] in ["available", "failed"]: continue + log.info("Volume '%s' (%s) is %s, removing manually", volume["name"], + volume["id"], volume["status"]) volume_ids.append(volume["id"]) if volume_ids: for volume_id in volume_ids: + log.info("Deleting volume %s (%s)", volume_id) resp = service.delete_volume(volume_id) assert resp.status_code == 204 log.debug("Delete volume request delivered")