Skip to content

Commit

Permalink
vmware_guest_network speedup module (#2277)
Browse files Browse the repository at this point in the history
SUMMARY
Speedup vmware_guest_network module. Current design loop though all networks and slow for big infrastructure with many networks (more then 100).
ISSUE TYPE

Bugfix Pull Request
Docs Pull Request

COMPONENT NAME
vmware_guest_network.py
ADDITIONAL INFORMATION
In my scenario current module spend 5 min to find network (this function run twice during module work and lead to 10min waiting)
        if compute_resource:
            for network in compute_resource.network:
                if isinstance(network, vim.dvs.DistributedVirtualPortgroup):
                    dvs = network.config.distributedVirtualSwitch
                    if (switch_name and dvs.config.name == switch_name) or not switch_name:
                        if network.config.name == network_name:
                            return network
                        if hasattr(network.config.defaultPortConfig.vlan, 'vlanId') and \
                           network.config.defaultPortConfig.vlan.vlanId == vlan_id:
                            return network
                        if hasattr(network.config.defaultPortConfig.vlan, 'pvlanId') and \
                           network.config.defaultPortConfig.vlan.pvlanId == vlan_id:
                            return network
                elif isinstance(network, vim.Network):
                    if network_name and network_name == network.name:
                        return network
                    if vlan_id:
                        for k in pg_lookup.keys():
                            if vlan_id == pg_lookup[k]['vlan_id']:
                                if k == network.name:
                                    return network
                                break
This code loop through networks and check if its name equal to self.params['network_name']. Its slow. Its about 30s to check 100 networks.
vmware.py has perfect function for this. PyVmomi.find_network_by_name works perfect and find network immediately

Reviewed-by: Mario Lenz <[email protected]>
  • Loading branch information
GuideGlyph authored Jan 21, 2025
1 parent ddaa171 commit 817b93f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/2277-vmware_guest_network.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minor_changes:
- vmware_guest_network - Speedup network search
(https://github.com/ansible-collections/community.vmware/pull/2277).
12 changes: 12 additions & 0 deletions plugins/modules/vmware_guest_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
network_name:
description:
- Name of network in vSphere.
- Work faster then o(vlan_id) for big infrastructure with many networks.
type: str
device_type:
default: vmxnet3
Expand Down Expand Up @@ -319,6 +320,17 @@ def _get_network_object(self, vm_obj):
for pg in vm_obj.runtime.host.config.network.portgroup:
pg_lookup[pg.spec.name] = {'switch': pg.spec.vswitchName, 'vlan_id': pg.spec.vlanId}

if network_name:
networks = self.find_network_by_name(network_name)
for network in networks:
if network in compute_resource.network:
if isinstance(network, vim.dvs.DistributedVirtualPortgroup):
dvs = network.config.distributedVirtualSwitch
if (switch_name and dvs.config.name == switch_name) or not switch_name:
return network
elif isinstance(network, vim.Network):
return network

if compute_resource:
for network in compute_resource.network:
if isinstance(network, vim.dvs.DistributedVirtualPortgroup):
Expand Down

0 comments on commit 817b93f

Please sign in to comment.