Possibility to choose if description is used during diff and also if it should be synced in device

This commit is contained in:
rizlas 2022-01-18 17:56:57 +01:00
parent e50d8e8633
commit 1bb75cfe69
4 changed files with 64 additions and 18 deletions

View File

@ -10,7 +10,11 @@ class Config(PluginConfig):
author_email = 'drygdryg2014@yandex.ru' author_email = 'drygdryg2014@yandex.ru'
default_settings = { default_settings = {
'exclude_virtual_interfaces': True, '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
} }

View File

@ -1,4 +1,7 @@
from dataclasses import dataclass from dataclasses import dataclass
from django.conf import settings
config = settings.PLUGINS_CONFIG["netbox_interface_sync"]
@dataclass(frozen=True) @dataclass(frozen=True)
@ -12,11 +15,14 @@ class ParentComparison:
def __eq__(self, other): def __eq__(self, other):
# Ignore some fields when comparing; ignore component name case and whitespaces # Ignore some fields when comparing; ignore component name case and whitespaces
return ( eq = (
(self.name.lower().replace(" ", "") == other.name.lower().replace(" ", "")) self.name.lower().replace(" ", "") == other.name.lower().replace(" ", "")
and (self.label == other.label) ) and (self.label == other.label)
and (self.description == other.description)
) if config["compare_description"]:
eq = eq and (self.description == other.description)
return eq
def __hash__(self): def __hash__(self):
# Ignore some fields when hashing; ignore component name case and whitespaces # Ignore some fields when hashing; ignore component name case and whitespaces
@ -34,13 +40,17 @@ class ParentTypedComparison(ParentComparison):
type_display: str type_display: str
def __eq__(self, other): def __eq__(self, other):
return ( eq = (
(self.name.lower().replace(" ", "") == other.name.lower().replace(" ", "")) (self.name.lower().replace(" ", "") == other.name.lower().replace(" ", ""))
and (self.label == other.label) and (self.label == other.label)
and (self.description == other.description)
and (self.type == other.type) and (self.type == other.type)
) )
if config["compare_description"]:
eq = eq and (self.description == other.description)
return eq
def __hash__(self): def __hash__(self):
return hash((self.name.lower().replace(" ", ""), self.type)) return hash((self.name.lower().replace(" ", ""), self.type))
@ -56,14 +66,18 @@ class InterfaceComparison(ParentTypedComparison):
is_template: bool = False is_template: bool = False
def __eq__(self, other): def __eq__(self, other):
return ( eq = (
(self.name.lower().replace(" ", "") == other.name.lower().replace(" ", "")) (self.name.lower().replace(" ", "") == other.name.lower().replace(" ", ""))
and (self.label == other.label) and (self.label == other.label)
and (self.description == other.description)
and (self.type == other.type) and (self.type == other.type)
and (self.mgmt_only == other.mgmt_only) and (self.mgmt_only == other.mgmt_only)
) )
if config["compare_description"]:
eq = eq and (self.description == other.description)
return eq
def __hash__(self): def __hash__(self):
return hash((self.name.lower().replace(" ", ""), self.type)) return hash((self.name.lower().replace(" ", ""), self.type))
@ -81,15 +95,19 @@ class FrontPortComparison(ParentTypedComparison):
is_template: bool = False is_template: bool = False
def __eq__(self, other): def __eq__(self, other):
return ( eq = (
(self.name.lower().replace(" ", "") == other.name.lower().replace(" ", "")) (self.name.lower().replace(" ", "") == other.name.lower().replace(" ", ""))
and (self.label == other.label) and (self.label == other.label)
and (self.description == other.description)
and (self.type == other.type) and (self.type == other.type)
and (self.color == other.color) and (self.color == other.color)
and (self.rear_port_position == other.rear_port_position) 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): def __hash__(self):
return hash((self.name.lower().replace(" ", ""), self.type)) return hash((self.name.lower().replace(" ", ""), self.type))
@ -106,15 +124,19 @@ class RearPortComparison(ParentTypedComparison):
is_template: bool = False is_template: bool = False
def __eq__(self, other): def __eq__(self, other):
return ( eq = (
(self.name.lower().replace(" ", "") == other.name.lower().replace(" ", "")) (self.name.lower().replace(" ", "") == other.name.lower().replace(" ", ""))
and (self.label == other.label) and (self.label == other.label)
and (self.description == other.description)
and (self.type == other.type) and (self.type == other.type)
and (self.color == other.color) and (self.color == other.color)
and (self.positions == other.positions) and (self.positions == other.positions)
) )
if config["compare_description"]:
eq = eq and (self.description == other.description)
return eq
def __hash__(self): def __hash__(self):
return hash((self.name.lower().replace(" ", ""), self.type)) return hash((self.name.lower().replace(" ", ""), self.type))
@ -145,15 +167,19 @@ class PowerPortComparison(ParentTypedComparison):
is_template: bool = False is_template: bool = False
def __eq__(self, other): def __eq__(self, other):
return ( eq = (
(self.name.lower().replace(" ", "") == other.name.lower().replace(" ", "")) (self.name.lower().replace(" ", "") == other.name.lower().replace(" ", ""))
and (self.label == other.label) and (self.label == other.label)
and (self.description == other.description)
and (self.type == other.type) and (self.type == other.type)
and (self.maximum_draw == other.maximum_draw) and (self.maximum_draw == other.maximum_draw)
and (self.allocated_draw == other.allocated_draw) and (self.allocated_draw == other.allocated_draw)
) )
if config["compare_description"]:
eq = eq and (self.description == other.description)
return eq
def __hash__(self): def __hash__(self):
return hash((self.name.lower().replace(" ", ""), self.type)) return hash((self.name.lower().replace(" ", ""), self.type))
@ -170,15 +196,19 @@ class PowerOutletComparison(ParentTypedComparison):
is_template: bool = False is_template: bool = False
def __eq__(self, other): def __eq__(self, other):
return ( eq = (
(self.name.lower().replace(" ", "") == other.name.lower().replace(" ", "")) (self.name.lower().replace(" ", "") == other.name.lower().replace(" ", ""))
and (self.label == other.label) and (self.label == other.label)
and (self.type == other.type) and (self.type == other.type)
and (self.power_port_name == other.power_port_name) and (self.power_port_name == other.power_port_name)
and (self.feed_leg == other.feed_leg) 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): def __hash__(self):
return hash( return hash(
(self.name.lower().replace(" ", ""), self.type, self.power_port_name) (self.name.lower().replace(" ", ""), self.type, self.power_port_name)

View File

@ -3,6 +3,9 @@ from typing import Iterable
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from django.contrib import messages from django.contrib import messages
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.conf import settings
config = settings.PLUGINS_CONFIG['netbox_interface_sync']
def split(s): def split(s):
@ -81,6 +84,9 @@ def post_components(
updated = 0 updated = 0
keys_to_avoid = ["id"] keys_to_avoid = ["id"]
if config["exclude_description"]:
keys_to_avoid.append("description")
for i in add_to_device_component.values(): for i in add_to_device_component.values():
to_create = False to_create = False

View File

@ -269,6 +269,9 @@ class PowerOutletComparisonView(LoginRequiredMixin, PermissionRequiredMixin, Vie
updated = 0 updated = 0
keys_to_avoid = ["id"] keys_to_avoid = ["id"]
if config["exclude_description"]:
keys_to_avoid.append("description")
for i in add_to_device_component.values(): for i in add_to_device_component.values():
to_create = False to_create = False
@ -403,6 +406,9 @@ class FrontPortComparisonView(LoginRequiredMixin, PermissionRequiredMixin, View)
updated = 0 updated = 0
keys_to_avoid = ["id"] keys_to_avoid = ["id"]
if config["exclude_description"]:
keys_to_avoid.append("description")
for i in add_to_device_component.values(): for i in add_to_device_component.values():
to_create = False to_create = False