diff --git a/netbox_interface_sync/__init__.py b/netbox_interface_sync/__init__.py index eed32d5..23ec7a1 100644 --- a/netbox_interface_sync/__init__.py +++ b/netbox_interface_sync/__init__.py @@ -10,7 +10,11 @@ class Config(PluginConfig): author_email = 'drygdryg2014@yandex.ru' default_settings = { 'exclude_virtual_interfaces': True, - 'include_interfaces_panel': False + 'include_interfaces_panel': False, + # Compare description during diff + 'compare_description': False, + # Sync or not description from device type + 'exclude_description': False } diff --git a/netbox_interface_sync/comparison.py b/netbox_interface_sync/comparison.py index b0896d1..32aae37 100644 --- a/netbox_interface_sync/comparison.py +++ b/netbox_interface_sync/comparison.py @@ -1,4 +1,7 @@ from dataclasses import dataclass +from django.conf import settings + +config = settings.PLUGINS_CONFIG["netbox_interface_sync"] @dataclass(frozen=True) @@ -12,11 +15,14 @@ class ParentComparison: def __eq__(self, other): # Ignore some fields when comparing; ignore component name case and whitespaces - return ( - (self.name.lower().replace(" ", "") == other.name.lower().replace(" ", "")) - and (self.label == other.label) - and (self.description == other.description) - ) + eq = ( + self.name.lower().replace(" ", "") == other.name.lower().replace(" ", "") + ) and (self.label == other.label) + + if config["compare_description"]: + eq = eq and (self.description == other.description) + + return eq def __hash__(self): # Ignore some fields when hashing; ignore component name case and whitespaces @@ -34,13 +40,17 @@ class ParentTypedComparison(ParentComparison): type_display: str def __eq__(self, other): - return ( + eq = ( (self.name.lower().replace(" ", "") == other.name.lower().replace(" ", "")) and (self.label == other.label) - and (self.description == other.description) and (self.type == other.type) ) + if config["compare_description"]: + eq = eq and (self.description == other.description) + + return eq + def __hash__(self): return hash((self.name.lower().replace(" ", ""), self.type)) @@ -56,14 +66,18 @@ class InterfaceComparison(ParentTypedComparison): is_template: bool = False def __eq__(self, other): - return ( + eq = ( (self.name.lower().replace(" ", "") == other.name.lower().replace(" ", "")) and (self.label == other.label) - and (self.description == other.description) and (self.type == other.type) and (self.mgmt_only == other.mgmt_only) ) + if config["compare_description"]: + eq = eq and (self.description == other.description) + + return eq + def __hash__(self): return hash((self.name.lower().replace(" ", ""), self.type)) @@ -81,15 +95,19 @@ class FrontPortComparison(ParentTypedComparison): is_template: bool = False def __eq__(self, other): - return ( + eq = ( (self.name.lower().replace(" ", "") == other.name.lower().replace(" ", "")) and (self.label == other.label) - and (self.description == other.description) and (self.type == other.type) and (self.color == other.color) and (self.rear_port_position == other.rear_port_position) ) + if config["compare_description"]: + eq = eq and (self.description == other.description) + + return eq + def __hash__(self): return hash((self.name.lower().replace(" ", ""), self.type)) @@ -106,15 +124,19 @@ class RearPortComparison(ParentTypedComparison): is_template: bool = False def __eq__(self, other): - return ( + eq = ( (self.name.lower().replace(" ", "") == other.name.lower().replace(" ", "")) and (self.label == other.label) - and (self.description == other.description) and (self.type == other.type) and (self.color == other.color) and (self.positions == other.positions) ) + if config["compare_description"]: + eq = eq and (self.description == other.description) + + return eq + def __hash__(self): return hash((self.name.lower().replace(" ", ""), self.type)) @@ -145,15 +167,19 @@ class PowerPortComparison(ParentTypedComparison): is_template: bool = False def __eq__(self, other): - return ( + eq = ( (self.name.lower().replace(" ", "") == other.name.lower().replace(" ", "")) and (self.label == other.label) - and (self.description == other.description) and (self.type == other.type) and (self.maximum_draw == other.maximum_draw) and (self.allocated_draw == other.allocated_draw) ) + if config["compare_description"]: + eq = eq and (self.description == other.description) + + return eq + def __hash__(self): return hash((self.name.lower().replace(" ", ""), self.type)) @@ -170,15 +196,19 @@ class PowerOutletComparison(ParentTypedComparison): is_template: bool = False def __eq__(self, other): - return ( + eq = ( (self.name.lower().replace(" ", "") == other.name.lower().replace(" ", "")) and (self.label == other.label) and (self.type == other.type) and (self.power_port_name == other.power_port_name) and (self.feed_leg == other.feed_leg) - and (self.description == other.description) ) + if config["compare_description"]: + eq = eq and (self.description == other.description) + + return eq + def __hash__(self): return hash( (self.name.lower().replace(" ", ""), self.type, self.power_port_name) diff --git a/netbox_interface_sync/utils.py b/netbox_interface_sync/utils.py index 2073c37..d46ab9e 100644 --- a/netbox_interface_sync/utils.py +++ b/netbox_interface_sync/utils.py @@ -3,6 +3,9 @@ from typing import Iterable from django.shortcuts import render, redirect from django.contrib import messages from django.core.exceptions import ObjectDoesNotExist +from django.conf import settings + +config = settings.PLUGINS_CONFIG['netbox_interface_sync'] def split(s): @@ -81,6 +84,9 @@ def post_components( updated = 0 keys_to_avoid = ["id"] + if config["exclude_description"]: + keys_to_avoid.append("description") + for i in add_to_device_component.values(): to_create = False diff --git a/netbox_interface_sync/views.py b/netbox_interface_sync/views.py index a093062..0762bb8 100644 --- a/netbox_interface_sync/views.py +++ b/netbox_interface_sync/views.py @@ -269,6 +269,9 @@ class PowerOutletComparisonView(LoginRequiredMixin, PermissionRequiredMixin, Vie updated = 0 keys_to_avoid = ["id"] + if config["exclude_description"]: + keys_to_avoid.append("description") + for i in add_to_device_component.values(): to_create = False @@ -403,6 +406,9 @@ class FrontPortComparisonView(LoginRequiredMixin, PermissionRequiredMixin, View) updated = 0 keys_to_avoid = ["id"] + if config["exclude_description"]: + keys_to_avoid.append("description") + for i in add_to_device_component.values(): to_create = False