2021-12-28 17:43:30 +03:00
|
|
|
from dataclasses import dataclass
|
2022-01-18 19:56:57 +03:00
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
config = settings.PLUGINS_CONFIG["netbox_interface_sync"]
|
2021-12-28 17:43:30 +03:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
class ParentComparison:
|
2021-12-28 20:32:33 +03:00
|
|
|
"""Common fields of a device component"""
|
|
|
|
|
2021-12-28 17:43:30 +03:00
|
|
|
id: int
|
|
|
|
name: str
|
|
|
|
label: str
|
|
|
|
description: str
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
2022-01-12 16:36:55 +03:00
|
|
|
# Ignore some fields when comparing; ignore component name case and whitespaces
|
2022-01-18 19:56:57 +03:00
|
|
|
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
|
2021-12-28 17:43:30 +03:00
|
|
|
|
|
|
|
def __hash__(self):
|
2022-01-12 16:36:55 +03:00
|
|
|
# Ignore some fields when hashing; ignore component name case and whitespaces
|
2021-12-28 17:43:30 +03:00
|
|
|
return hash(self.name.lower().replace(" ", ""))
|
|
|
|
|
2022-01-12 14:59:05 +03:00
|
|
|
def __str__(self):
|
|
|
|
return f"Label: {self.label}\nDescription: {self.description}"
|
|
|
|
|
2021-12-28 17:43:30 +03:00
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
class ParentTypedComparison(ParentComparison):
|
2021-12-28 20:32:33 +03:00
|
|
|
"""Common fields of a device typed component"""
|
|
|
|
|
2021-12-28 17:43:30 +03:00
|
|
|
type: str
|
|
|
|
type_display: str
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
2022-01-18 19:56:57 +03:00
|
|
|
eq = (
|
2021-12-28 20:32:33 +03:00
|
|
|
(self.name.lower().replace(" ", "") == other.name.lower().replace(" ", ""))
|
|
|
|
and (self.label == other.label)
|
|
|
|
and (self.type == other.type)
|
|
|
|
)
|
2021-12-28 17:43:30 +03:00
|
|
|
|
2022-01-18 19:56:57 +03:00
|
|
|
if config["compare_description"]:
|
|
|
|
eq = eq and (self.description == other.description)
|
|
|
|
|
|
|
|
return eq
|
|
|
|
|
2021-12-28 17:43:30 +03:00
|
|
|
def __hash__(self):
|
|
|
|
return hash((self.name.lower().replace(" ", ""), self.type))
|
|
|
|
|
2022-01-12 14:59:05 +03:00
|
|
|
def __str__(self):
|
|
|
|
return f"{super().__str__()}\nType: {self.type_display}"
|
|
|
|
|
2021-12-28 17:43:30 +03:00
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
2021-12-28 20:32:33 +03:00
|
|
|
class InterfaceComparison(ParentTypedComparison):
|
2021-12-28 17:43:30 +03:00
|
|
|
"""A unified way to represent the interface and interface template"""
|
|
|
|
|
2021-12-28 20:32:33 +03:00
|
|
|
mgmt_only: bool
|
2021-12-28 17:43:30 +03:00
|
|
|
is_template: bool = False
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
2022-01-18 19:56:57 +03:00
|
|
|
eq = (
|
2021-12-28 20:32:33 +03:00
|
|
|
(self.name.lower().replace(" ", "") == other.name.lower().replace(" ", ""))
|
|
|
|
and (self.label == other.label)
|
|
|
|
and (self.type == other.type)
|
|
|
|
and (self.mgmt_only == other.mgmt_only)
|
|
|
|
)
|
2021-12-28 17:43:30 +03:00
|
|
|
|
2022-01-18 19:56:57 +03:00
|
|
|
if config["compare_description"]:
|
|
|
|
eq = eq and (self.description == other.description)
|
|
|
|
|
|
|
|
return eq
|
|
|
|
|
2021-12-28 22:29:37 +03:00
|
|
|
def __hash__(self):
|
|
|
|
return hash((self.name.lower().replace(" ", ""), self.type))
|
|
|
|
|
2022-01-12 14:59:05 +03:00
|
|
|
def __str__(self):
|
|
|
|
return f"{super().__str__()}\nManagement only: {self.mgmt_only}"
|
|
|
|
|
2021-12-28 20:32:33 +03:00
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
class FrontPortComparison(ParentTypedComparison):
|
|
|
|
"""A unified way to represent the front port and front port template"""
|
|
|
|
|
|
|
|
color: str
|
2022-01-11 15:47:43 +03:00
|
|
|
# rear_port_id: int
|
|
|
|
rear_port_position: int
|
2021-12-28 20:32:33 +03:00
|
|
|
is_template: bool = False
|
|
|
|
|
2022-01-11 15:47:43 +03:00
|
|
|
def __eq__(self, other):
|
2022-01-18 19:56:57 +03:00
|
|
|
eq = (
|
2022-01-11 15:47:43 +03:00
|
|
|
(self.name.lower().replace(" ", "") == other.name.lower().replace(" ", ""))
|
|
|
|
and (self.label == other.label)
|
|
|
|
and (self.type == other.type)
|
|
|
|
and (self.color == other.color)
|
|
|
|
and (self.rear_port_position == other.rear_port_position)
|
|
|
|
)
|
|
|
|
|
2022-01-18 19:56:57 +03:00
|
|
|
if config["compare_description"]:
|
|
|
|
eq = eq and (self.description == other.description)
|
|
|
|
|
|
|
|
return eq
|
|
|
|
|
2022-01-11 15:47:43 +03:00
|
|
|
def __hash__(self):
|
|
|
|
return hash((self.name.lower().replace(" ", ""), self.type))
|
|
|
|
|
2022-01-12 14:59:05 +03:00
|
|
|
def __str__(self):
|
|
|
|
return f"{super().__str__()}\nColor: {self.color}\nPosition: {self.rear_port_position}"
|
|
|
|
|
2021-12-28 20:32:33 +03:00
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
class RearPortComparison(ParentTypedComparison):
|
|
|
|
"""A unified way to represent the rear port and rear port template"""
|
|
|
|
|
|
|
|
color: str
|
2022-01-11 15:47:43 +03:00
|
|
|
positions: int
|
2021-12-28 20:32:33 +03:00
|
|
|
is_template: bool = False
|
|
|
|
|
2022-01-11 15:47:43 +03:00
|
|
|
def __eq__(self, other):
|
2022-01-18 19:56:57 +03:00
|
|
|
eq = (
|
2022-01-11 15:47:43 +03:00
|
|
|
(self.name.lower().replace(" ", "") == other.name.lower().replace(" ", ""))
|
|
|
|
and (self.label == other.label)
|
|
|
|
and (self.type == other.type)
|
|
|
|
and (self.color == other.color)
|
|
|
|
and (self.positions == other.positions)
|
|
|
|
)
|
|
|
|
|
2022-01-18 19:56:57 +03:00
|
|
|
if config["compare_description"]:
|
|
|
|
eq = eq and (self.description == other.description)
|
|
|
|
|
|
|
|
return eq
|
|
|
|
|
2022-01-11 15:47:43 +03:00
|
|
|
def __hash__(self):
|
|
|
|
return hash((self.name.lower().replace(" ", ""), self.type))
|
|
|
|
|
2022-01-12 14:59:05 +03:00
|
|
|
def __str__(self):
|
|
|
|
return f"{super().__str__()}\nColor: {self.color}\nPositions: {self.positions}"
|
|
|
|
|
2021-12-28 20:32:33 +03:00
|
|
|
|
|
|
|
@dataclass(frozen=True, eq=False)
|
|
|
|
class ConsolePortComparison(ParentTypedComparison):
|
|
|
|
"""A unified way to represent the consoleport and consoleport template"""
|
|
|
|
|
|
|
|
is_template: bool = False
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True, eq=False)
|
|
|
|
class ConsoleServerPortComparison(ParentTypedComparison):
|
|
|
|
"""A unified way to represent the consoleserverport and consoleserverport template"""
|
|
|
|
|
|
|
|
is_template: bool = False
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
|
|
class PowerPortComparison(ParentTypedComparison):
|
|
|
|
"""A unified way to represent the power port and power port template"""
|
|
|
|
|
|
|
|
maximum_draw: str
|
|
|
|
allocated_draw: str
|
|
|
|
is_template: bool = False
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
2022-01-18 19:56:57 +03:00
|
|
|
eq = (
|
2021-12-28 20:32:33 +03:00
|
|
|
(self.name.lower().replace(" ", "") == other.name.lower().replace(" ", ""))
|
|
|
|
and (self.label == other.label)
|
|
|
|
and (self.type == other.type)
|
|
|
|
and (self.maximum_draw == other.maximum_draw)
|
|
|
|
and (self.allocated_draw == other.allocated_draw)
|
|
|
|
)
|
2021-12-28 17:43:30 +03:00
|
|
|
|
2022-01-18 19:56:57 +03:00
|
|
|
if config["compare_description"]:
|
|
|
|
eq = eq and (self.description == other.description)
|
|
|
|
|
|
|
|
return eq
|
|
|
|
|
2022-01-11 16:09:46 +03:00
|
|
|
def __hash__(self):
|
|
|
|
return hash((self.name.lower().replace(" ", ""), self.type))
|
|
|
|
|
2022-01-12 14:59:05 +03:00
|
|
|
def __str__(self):
|
|
|
|
return f"{super().__str__()}\nMaximum draw: {self.maximum_draw}\nAllocated draw: {self.allocated_draw}"
|
|
|
|
|
2021-12-28 17:43:30 +03:00
|
|
|
|
|
|
|
@dataclass(frozen=True)
|
2021-12-28 20:32:33 +03:00
|
|
|
class PowerOutletComparison(ParentTypedComparison):
|
|
|
|
"""A unified way to represent the power outlet and power outlet template"""
|
2021-12-28 17:43:30 +03:00
|
|
|
|
|
|
|
power_port_name: str = ""
|
|
|
|
feed_leg: str = ""
|
|
|
|
is_template: bool = False
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
2022-01-18 19:56:57 +03:00
|
|
|
eq = (
|
2021-12-28 17:43:30 +03:00
|
|
|
(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)
|
|
|
|
)
|
|
|
|
|
2022-01-18 19:56:57 +03:00
|
|
|
if config["compare_description"]:
|
|
|
|
eq = eq and (self.description == other.description)
|
|
|
|
|
|
|
|
return eq
|
|
|
|
|
2021-12-28 17:43:30 +03:00
|
|
|
def __hash__(self):
|
|
|
|
return hash(
|
|
|
|
(self.name.lower().replace(" ", ""), self.type, self.power_port_name)
|
|
|
|
)
|
2021-12-28 20:32:33 +03:00
|
|
|
|
2022-01-12 14:59:05 +03:00
|
|
|
def __str__(self):
|
|
|
|
return f"{super().__str__()}\nPower port name: {self.power_port_name}\nFeed leg: {self.feed_leg}"
|
|
|
|
|
2021-12-28 20:32:33 +03:00
|
|
|
|
|
|
|
@dataclass(frozen=True, eq=False)
|
|
|
|
class DeviceBayComparison(ParentComparison):
|
2022-01-12 16:36:55 +03:00
|
|
|
"""A unified way to represent the device bay and device bay template"""
|
2021-12-28 20:32:33 +03:00
|
|
|
|
|
|
|
is_template: bool = False
|