From 0ca67727086fc880d467cc771569d8986f7e3fe4 Mon Sep 17 00:00:00 2001 From: Kyrylo Maksymenko Date: Thu, 9 Feb 2023 08:43:49 +0100 Subject: [PATCH] fix get or create network command to ignore exception if network was created in another thread --- .../cp/vcenter/flows/connectivity_flow.py | 20 +++++++++++-------- .../cp/vcenter/handlers/cluster_handler.py | 6 +++++- .../cp/vcenter/handlers/switch_handler.py | 6 ++++++ 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/cloudshell/cp/vcenter/flows/connectivity_flow.py b/cloudshell/cp/vcenter/flows/connectivity_flow.py index a470eac..c8567a0 100644 --- a/cloudshell/cp/vcenter/flows/connectivity_flow.py +++ b/cloudshell/cp/vcenter/flows/connectivity_flow.py @@ -30,6 +30,7 @@ from cloudshell.cp.vcenter.handlers.switch_handler import ( AbstractSwitchHandler, DvSwitchNotFound, + PortGroupExists, ) from cloudshell.cp.vcenter.handlers.vm_handler import VmHandler from cloudshell.cp.vcenter.handlers.vsphere_api_handler import ( @@ -206,14 +207,17 @@ def _create_network_based_on_vlan_id( try: network = dc.get_network(pg_name) except NetworkNotFound: - switch.create_port_group( - pg_name, - vlan_id, - port_mode, - promiscuous_mode, - forged_transmits, - mac_changes, - ) + try: + switch.create_port_group( + pg_name, + vlan_id, + port_mode, + promiscuous_mode, + forged_transmits, + mac_changes, + ) + except PortGroupExists: + pass port_group = switch.wait_port_group_appears(pg_name) network = dc.wait_network_appears(pg_name) if self._vsphere_client: diff --git a/cloudshell/cp/vcenter/handlers/cluster_handler.py b/cloudshell/cp/vcenter/handlers/cluster_handler.py index 109296e..c244e21 100644 --- a/cloudshell/cp/vcenter/handlers/cluster_handler.py +++ b/cloudshell/cp/vcenter/handlers/cluster_handler.py @@ -22,6 +22,7 @@ from cloudshell.cp.vcenter.handlers.resource_pool import ResourcePoolHandler from cloudshell.cp.vcenter.handlers.si_handler import ResourceInUse from cloudshell.cp.vcenter.handlers.switch_handler import ( + PortGroupExists, VSwitchHandler, VSwitchNotFound, ) @@ -226,4 +227,7 @@ def remove_port_group(self, name: str): raise ResourceInUse(name) def add_port_group(self, port_group_spec): - self._vc_obj.configManager.networkSystem.AddPortGroup(port_group_spec) + try: + self._vc_obj.configManager.networkSystem.AddPortGroup(port_group_spec) + except vim.fault.AlreadyExists: + raise PortGroupExists(port_group_spec.name) diff --git a/cloudshell/cp/vcenter/handlers/switch_handler.py b/cloudshell/cp/vcenter/handlers/switch_handler.py index ad493bd..0d39dd9 100644 --- a/cloudshell/cp/vcenter/handlers/switch_handler.py +++ b/cloudshell/cp/vcenter/handlers/switch_handler.py @@ -41,6 +41,12 @@ def __init__(self, entity: ManagedEntityHandler, name: str): super().__init__(f"VirtualSwitch with name {name} not found in the {entity}") +class PortGroupExists(BaseVCenterException): + def __init__(self, name: str): + self.name = name + super().__init__(f"PortGroup with name {name} already exists") + + def get_vlan_spec(port_mode: ConnectionModeEnum, vlan_range: str): if port_mode is port_mode.ACCESS: spec = vim.dvs.VmwareDistributedVirtualSwitch.VlanIdSpec