From f4f09597b5e22042f06e22bb6813a229ba376c74 Mon Sep 17 00:00:00 2001 From: Francesco Lombardo Date: Sat, 18 Feb 2023 14:38:04 +0100 Subject: [PATCH] enable settings ip_tenant_inheritance_order with import_inventory --- module/sources/check_redfish/config.py | 29 +++++++++++++++++++++++++- module/sources/common/source_base.py | 4 ++-- settings-example.ini | 9 ++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/module/sources/check_redfish/config.py b/module/sources/check_redfish/config.py index 7463287..4d33885 100644 --- a/module/sources/check_redfish/config.py +++ b/module/sources/check_redfish/config.py @@ -14,6 +14,7 @@ from module.config.base import ConfigBase from module.config.option import ConfigOption from module.sources.common.conifg import * from module.common.logging import get_logger +from module.common.misc import quoted_split from module.sources.common.permitted_subnets import PermittedSubnets log = get_logger() @@ -71,7 +72,21 @@ class CheckRedfishConfig(ConfigBase): bool, description="""define if existing interface attributes are overwritten with data discovered via check_redfish if False only data which is not preset in NetBox will be added""", - default_value=True) + default_value=True), + + ConfigOption("ip_tenant_inheritance_order", + str, + description="""\ + define in which order the IP address tenant will be assigned if tenant is undefined. + possible values: + * device : host or VM tenant will be assigned to the IP address + * prefix : if the IP address belongs to an existing prefix and this prefix has a tenant assigned, then this one is used + * disabled : no tenant assignment to the IP address will be performed + the order of the definition is important, the default is "device, prefix" which means: + If the device has a tenant then this one will be used. If not, the prefix tenant will be used if defined + """, + default_value="device, prefix" + ), ] super().__init__() @@ -93,6 +108,18 @@ class CheckRedfishConfig(ConfigBase): log.error(f"Inventory file path '{option.value}' not readable.") self.set_validation_failed() + if option.key == "ip_tenant_inheritance_order": + option.set_value(quoted_split(option.value)) + for ip_tenant_inheritance in option.value: + if ip_tenant_inheritance not in ["device", "prefix", "disabled"]: + log.error(f"Config value '{ip_tenant_inheritance}' invalid for " + f"config option 'ip_tenant_inheritance_order'!") + self.set_validation_failed() + + if len(option.value) > 2: + log.error("Config option 'ip_tenant_inheritance_order' can contain only 2 items max") + self.set_validation_failed() + permitted_subnets_option = self.get_option_by_name("permitted_subnets") if permitted_subnets_option is not None: diff --git a/module/sources/common/source_base.py b/module/sources/common/source_base.py index 3c33df3..4f902db 100644 --- a/module/sources/common/source_base.py +++ b/module/sources/common/source_base.py @@ -271,8 +271,8 @@ class SourceBase: disable_vlan_sync = self.disable_vlan_sync ip_tenant_inheritance_order = None - if hasattr(self, "ip_tenant_inheritance_order"): - ip_tenant_inheritance_order = self.ip_tenant_inheritance_order + if hasattr(self.settings, "ip_tenant_inheritance_order"): + ip_tenant_inheritance_order = self.settings.ip_tenant_inheritance_order if not isinstance(interface_data, dict): log.error(f"Attribute 'interface_data' must be a dict() got {type(interface_data)}.") diff --git a/settings-example.ini b/settings-example.ini index b1e85af..f053f57 100644 --- a/settings-example.ini +++ b/settings-example.ini @@ -376,4 +376,13 @@ inventory_file_path = /full/path/to/inventory/files ; check_redfish if False only data which is not preset in NetBox will be added ;overwrite_interface_attributes = True +; define in which order the IP address tenant will be assigned if tenant is undefined. +; possible values: +; * device : host or VM tenant will be assigned to the IP address +; * prefix : if the IP address belongs to an existing prefix and this prefix has a tenant assigned, then this one is used +; * disabled : no tenant assignment to the IP address will be performed +; the order of the definition is important, the default is "device, prefix" which means: +; If the device has a tenant then this one will be used. If not, the prefix tenant will be used if defined +;ip_tenant_inheritance_order = device, prefix + ;EOF