mirror of
https://github.com/bb-Ricardo/netbox-sync.git
synced 2026-01-21 00:10:25 -06:00
sanitize manufacturer for all objects #145
This commit is contained in:
77
module/netbox/manufacturer_mapping.py
Normal file
77
module/netbox/manufacturer_mapping.py
Normal file
@@ -0,0 +1,77 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright (c) 2020 - 2022 Ricardo Bartels. All rights reserved.
|
||||
#
|
||||
# netbox-sync.py
|
||||
#
|
||||
# This work is licensed under the terms of the MIT license.
|
||||
# For a copy, see file LICENSE.txt included in this
|
||||
# repository or visit: <https://opensource.org/licenses/MIT>.
|
||||
|
||||
import re
|
||||
|
||||
AMD = "AMD"
|
||||
BROADCOM = "Broadcom"
|
||||
CISCO = "Cisco"
|
||||
DELL = "Dell"
|
||||
FUJITSU = "Fujitsu"
|
||||
HISILICON = "HiSilicon"
|
||||
HPE = "HPE"
|
||||
HUAWEI = "Huawei"
|
||||
HYNIX = "Hynix"
|
||||
INSPUR = "Inspur"
|
||||
INTEL = "Intel"
|
||||
LENOVO = "Lenovo"
|
||||
MICRON = "Micron"
|
||||
NVIDIA = "Nvidia"
|
||||
SAMSUNG = "Samsung"
|
||||
SUPERMICRO = "Supermicro"
|
||||
TOSHIBA = "Toshiba"
|
||||
WD = "Western Digital"
|
||||
|
||||
# Key must be a regex expression
|
||||
manufacturer_mappings = {
|
||||
"^AMD$": AMD,
|
||||
".*Broadcom.*": BROADCOM,
|
||||
".*Cisco.*": CISCO,
|
||||
".*Dell.*": DELL,
|
||||
"FTS Corp": FUJITSU,
|
||||
".*Fujitsu.*": FUJITSU,
|
||||
".*HiSilicon.*": HISILICON,
|
||||
"^HP$": HPE,
|
||||
"^HPE$": HPE,
|
||||
".*Huawei.*": HUAWEI,
|
||||
".*Hynix.*": HYNIX,
|
||||
".*Inspur.*": INSPUR,
|
||||
".*Intel.*": INTEL,
|
||||
"LEN": LENOVO,
|
||||
".*Lenovo.*": LENOVO,
|
||||
".*Micron.*": MICRON,
|
||||
".*Nvidea.*": NVIDIA,
|
||||
".*Samsung.*": SAMSUNG,
|
||||
".*Supermicro.*": SUPERMICRO,
|
||||
".*Toshiba.*": TOSHIBA,
|
||||
"^WD$": WD,
|
||||
".*Western Digital.*": WD
|
||||
}
|
||||
|
||||
compiled_manufacturer_mappings = dict()
|
||||
|
||||
for regex_expression, name in manufacturer_mappings.items():
|
||||
try:
|
||||
compiled_manufacturer_mappings[re.compile(regex_expression, flags=re.IGNORECASE)] = name
|
||||
except re.error:
|
||||
raise ValueError(f"Unable to compile regular expression '{regex_expression}'")
|
||||
|
||||
|
||||
def sanitize_manufacturer_name(manufacturer_name):
|
||||
|
||||
if manufacturer_name is None:
|
||||
return
|
||||
|
||||
for regex_test, resolved_name in compiled_manufacturer_mappings.items():
|
||||
if regex_test.match(manufacturer_name):
|
||||
return resolved_name
|
||||
|
||||
return manufacturer_name
|
||||
|
||||
# EOF
|
||||
@@ -12,6 +12,7 @@ from ipaddress import ip_network, IPv4Network, IPv6Network
|
||||
|
||||
from module.common.misc import grab, do_error_exit
|
||||
from module.common.logging import get_logger
|
||||
from module.netbox.manufacturer_mapping import sanitize_manufacturer_name
|
||||
|
||||
log = get_logger()
|
||||
|
||||
@@ -535,6 +536,9 @@ class NetBoxObject:
|
||||
if defined_value_type == NBVLANList:
|
||||
value = self.compile_vlans(value)
|
||||
|
||||
if defined_value_type == NBManufacturer:
|
||||
value = self.sanitize_manufacturer_name(value)
|
||||
|
||||
if defined_value_type == NBCustomField:
|
||||
if not isinstance(value, dict):
|
||||
log.error(f"Invalid data type for '{key}' (must be 'dict'), got: '{value}'")
|
||||
@@ -968,6 +972,50 @@ class NetBoxObject:
|
||||
|
||||
return new_vlan_list
|
||||
|
||||
def sanitize_manufacturer_name(self, manufacturer_data):
|
||||
"""
|
||||
Sanitize the manufacturer name to use a generic one for different representations
|
||||
|
||||
Parameters
|
||||
----------
|
||||
manufacturer_data: dict, NBManufacturer
|
||||
manufacturer data or object
|
||||
|
||||
Returns
|
||||
-------
|
||||
NBManufacturer
|
||||
"""
|
||||
|
||||
if manufacturer_data is None:
|
||||
return
|
||||
|
||||
sanitized_name = None
|
||||
if isinstance(manufacturer_data, dict):
|
||||
sanitized_name = sanitize_manufacturer_name(manufacturer_data.get("name"))
|
||||
|
||||
elif isinstance(manufacturer_data, NBManufacturer):
|
||||
sanitized_name = sanitize_manufacturer_name(manufacturer_data.get_display_name())
|
||||
|
||||
if sanitized_name is None:
|
||||
return manufacturer_data
|
||||
|
||||
manufacturer_object = self.inventory.get_by_data(NBManufacturer, {
|
||||
"slug": self.format_slug(sanitized_name)
|
||||
})
|
||||
if manufacturer_object is None:
|
||||
manufacturer_object = self.inventory.add_update_object(NBManufacturer, {
|
||||
"name": sanitized_name
|
||||
})
|
||||
else:
|
||||
manufacturer_object.update({
|
||||
"name": sanitized_name
|
||||
})
|
||||
|
||||
if manufacturer_object.source is None:
|
||||
manufacturer_object.source = self.source
|
||||
|
||||
return manufacturer_object
|
||||
|
||||
def unset_attribute(self, attribute_name=None):
|
||||
"""
|
||||
Unset a certain attribute. This will delete the value of this attribute in NetBox on the first run of
|
||||
|
||||
Reference in New Issue
Block a user