From b899f4869f0521a4b39ed4ef8e7a4e3449abc4cd Mon Sep 17 00:00:00 2001 From: Ricardo Bartels Date: Tue, 23 May 2023 21:58:03 +0200 Subject: [PATCH] adds config option 'host_nic_exclude_by_mac_list' #280 --- module/sources/vmware/config.py | 24 ++++++++++++++++++++++++ module/sources/vmware/connection.py | 10 ++++++++-- settings-example.ini | 4 ++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/module/sources/vmware/config.py b/module/sources/vmware/config.py index 5bcec15..211283d 100644 --- a/module/sources/vmware/config.py +++ b/module/sources/vmware/config.py @@ -18,6 +18,7 @@ from module.config.group import ConfigOptionGroup from module.sources.common.conifg import * from module.sources.common.permitted_subnets import PermittedSubnets from module.common.logging import get_logger +from module.common.support import normalize_mac_address log = get_logger() @@ -329,6 +330,13 @@ class VMWareConfig(ConfigBase): synchronisation by setting this option to 'False' """, default_value=True), + ConfigOption("host_nic_exclude_by_mac_list", + str, + description="""defines a comma separated list of MAC addresses which should be excluded + from sync. Any host NIC with a matching MAC address will be excluded from sync. + """, + config_example="AA:BB:CC:11:22:33, 66:77:88:AA:BB:CC" + ), # removed settings ConfigOption("netbox_host_device_role", @@ -474,6 +482,22 @@ class VMWareConfig(ConfigBase): log.error("Config option 'ip_tenant_inheritance_order' can contain only 2 items max") self.set_validation_failed() + if option.key == "host_nic_exclude_by_mac_list": + + value_list = list() + + for mac_address in quoted_split(option.value) or list(): + + normalized_mac_address = normalize_mac_address(mac_address) + + if len(f"{normalized_mac_address}") != 17: + log.error(f"MAC address '{mac_address}' for 'host_nic_exclude_by_mac_list' invalid.") + self.set_validation_failed() + else: + value_list.append(normalized_mac_address) + + option.set_value(value_list) + permitted_subnets_option = self.get_option_by_name("permitted_subnets") if permitted_subnets_option is not None: diff --git a/module/sources/vmware/connection.py b/module/sources/vmware/connection.py index 3520d83..410b2e5 100644 --- a/module/sources/vmware/connection.py +++ b/module/sources/vmware/connection.py @@ -493,7 +493,7 @@ class VMWareHandler(SourceBase): the highest amount of matching interfaces. If the ration of matching interfaces exceeds 2.0 then the top matching machine is chosen as desired object. - If the ration is below 2.0 then None will be returned. The probability is to low that + If the ration is below 2.0 then None will be returned. The probability is too low that this one is the correct one. None will also be returned if no machine was found at all. @@ -1738,10 +1738,16 @@ class VMWareHandler(SourceBase): "vid": pg_data.get("vlan_id") }) + pnic_mac_address = normalize_mac_address(grab(pnic, "mac")) + + if pnic_mac_address in self.settings.host_nic_exclude_by_mac_list: + log.debug2(f"Host NIC with MAC '{pnic_mac_address}' excluded from sync. Skipping") + continue + pnic_data = { "name": unquote(pnic_name), "device": None, # will be set once we found the correct device - "mac_address": normalize_mac_address(grab(pnic, "mac")), + "mac_address": pnic_mac_address, "enabled": bool(grab(pnic, "linkSpeed")), "description": unquote(pnic_description), "type": NetBoxInterfaceType(pnic_link_speed).get_this_netbox_type() diff --git a/settings-example.ini b/settings-example.ini index 6d8be2f..70c5195 100644 --- a/settings-example.ini +++ b/settings-example.ini @@ -350,6 +350,10 @@ password = super-secret ; setting this option to 'False' ;sync_vm_interface_mtu = True +; defines a comma separated list of MAC addresses which should be excluded from sync. Any +; host NIC with a matching MAC address will be excluded from sync. +;host_nic_exclude_by_mac_list = AA:BB:CC:11:22:33, 66:77:88:AA:BB:CC + [source/my-redfish-example] ; Defines if this source is enabled or not