Skip to content

Commit

Permalink
raise exception if cannot get associated tags
Browse files Browse the repository at this point in the history
  • Loading branch information
saklar13 committed Aug 18, 2023
1 parent 58f9a92 commit 23c022c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
22 changes: 10 additions & 12 deletions cloudshell/cp/vcenter/flows/connectivity_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,15 +304,16 @@ def _remove_pg(self, action: VcenterConnectivityActionModel) -> set[str]:
check_pg_can_be_removed(pg_name, action)
network = self._dc.get_network(pg_name)
network.wait_network_become_free(raise_=True)
tags = self._get_network_tags(network)

if isinstance(network, DVPortGroupHandler):
network.destroy()
del network
else:
vm = self.get_target(action)
# remove from the host where the VM is located
vm.host.remove_port_group(network.name)
try:
tags = self._get_network_tags(network)
finally:
if isinstance(network, DVPortGroupHandler):
network.destroy()
else:
vm = self.get_target(action)
# remove from the host where the VM is located
vm.host.remove_port_group(network.name)
del network
logger.info(f"Network {pg_name} was removed")
return tags
Expand All @@ -323,10 +324,7 @@ def _get_network_tags(
"""Get network's tag IDs."""
tags = set()
if self._vsphere_client and self._resource_conf.is_static:
try:
tags |= self._vsphere_client.get_attached_tags(network)
except Exception as e:
logger.warning(f"Failed to get {network} tags. Error: {e}")
tags |= self._vsphere_client.get_attached_tags(network)
return tags

def _remove_tags(self, tags: set[str]) -> None:
Expand Down
40 changes: 23 additions & 17 deletions cloudshell/cp/vcenter/flows/delete_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ def __attrs_post_init__(self):
self._dc = DcHandler.get_dc(self._resource_conf.default_datacenter, self._si)

def delete(self) -> None:
tags = self._delete_vm()
tags |= self._delete_folder()
self._delete_tags(tags)
tags = set()
try:
tags |= self._delete_vm()
finally:
try:
tags |= self._delete_folder()
finally:
self._delete_tags(tags)

def _delete_vm(self) -> set[str]:
vm_uuid = self._deployed_app.vmdetails.uid
Expand All @@ -64,13 +69,15 @@ def _delete_vm(self) -> set[str]:
except VmNotFound:
logger.warning(f"Trying to remove vm {vm_uuid} but it is not exists")
else:
self._si.delete_customization_spec(vm.name)
tags |= self._get_tags(vm)

soft = self._resource_conf.shutdown_method is ShutdownMethod.SOFT
vm.power_off(soft=soft)

vm.delete()
try:
self._si.delete_customization_spec(vm.name)
finally:
try:
tags |= self._get_tags(vm)
finally:
soft = self._resource_conf.shutdown_method is ShutdownMethod.SOFT
vm.power_off(soft=soft)
vm.delete()
return tags

def _delete_folder(self) -> set[str]:
Expand All @@ -86,18 +93,17 @@ def _delete_folder(self) -> set[str]:
except FolderNotFound:
pass
else:
tags |= self._get_tags(folder)
with suppress(FolderIsNotEmpty):
folder.destroy()
try:
tags |= self._get_tags(folder)
finally:
with suppress(FolderIsNotEmpty):
folder.destroy()
return tags

def _get_tags(self, obj) -> set[str]:
tags = set()
if self._vsphere_client:
try:
tags |= set(self._vsphere_client.get_attached_tags(obj))
except Exception as e:
logger.warning(f"Failed to get {obj} tags. Error: {e}")
tags |= set(self._vsphere_client.get_attached_tags(obj))
return tags

def _delete_tags(self, tags: set[str]) -> None:
Expand Down

0 comments on commit 23c022c

Please sign in to comment.