Skip to content

Commit

Permalink
fix get or create network command to ignore exception if network was …
Browse files Browse the repository at this point in the history
…created in another thread
  • Loading branch information
saklar13 committed Feb 9, 2023
1 parent 3beba57 commit 0ca6772
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
20 changes: 12 additions & 8 deletions cloudshell/cp/vcenter/flows/connectivity_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 5 additions & 1 deletion cloudshell/cp/vcenter/handlers/cluster_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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)
6 changes: 6 additions & 0 deletions cloudshell/cp/vcenter/handlers/switch_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0ca6772

Please sign in to comment.