2022-01-21 11:42:37 +03:00
|
|
|
import attr
|
|
|
|
from attrs import fields
|
|
|
|
|
2022-01-18 19:56:57 +03:00
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
config = settings.PLUGINS_CONFIG["netbox_interface_sync"]
|
2022-01-21 11:42:37 +03:00
|
|
|
COMPARE_DESCRIPTIONS: bool = config["compare_description"]
|
2021-12-28 17:43:30 +03:00
|
|
|
|
|
|
|
|
2022-01-21 11:42:37 +03:00
|
|
|
@attr.s(frozen=True, auto_attribs=True)
|
|
|
|
class BaseComparison:
|
2021-12-28 20:32:33 +03:00
|
|
|
"""Common fields of a device component"""
|
|
|
|
|
2022-01-21 11:42:37 +03:00
|
|
|
# Do not compare IDs
|
|
|
|
id: int = attr.ib(eq=False, hash=False, metadata={'printable': False})
|
|
|
|
# Compare names case-insensitively and spaces-insensitively
|
|
|
|
name: str = attr.ib(eq=lambda name: name.lower().replace(" ", ""), metadata={'printable': False})
|
|
|
|
label: str = attr.ib(hash=False)
|
|
|
|
# Compare descriptions if it is set by the configuration
|
|
|
|
description: str = attr.ib(eq=COMPARE_DESCRIPTIONS, hash=False)
|
|
|
|
# Do not compare `is_template` properties
|
|
|
|
is_template: bool = attr.ib(kw_only=True, default=False, eq=False, hash=False, metadata={'printable': False})
|
|
|
|
|
|
|
|
@property
|
|
|
|
def fields_display(self) -> str:
|
|
|
|
"""Generate human-readable list of printable fields to display in the comparison table"""
|
2022-01-20 12:07:21 +03:00
|
|
|
fields_to_display = []
|
2022-01-21 11:42:37 +03:00
|
|
|
for field in fields(self.__class__):
|
|
|
|
if not field.metadata.get('printable', True):
|
2022-01-20 12:07:21 +03:00
|
|
|
continue
|
|
|
|
field_value = getattr(self, field.name)
|
|
|
|
if not field_value:
|
|
|
|
continue
|
2022-01-21 11:42:37 +03:00
|
|
|
field_caption = field.metadata.get('displayed_caption') or field.name.replace('_', ' ').capitalize()
|
|
|
|
fields_to_display.append(f'{field_caption}: {field_value}')
|
2022-01-20 12:07:21 +03:00
|
|
|
return '\n'.join(fields_to_display)
|
2022-01-12 14:59:05 +03:00
|
|
|
|
2021-12-28 17:43:30 +03:00
|
|
|
|
2022-01-21 11:42:37 +03:00
|
|
|
@attr.s(frozen=True, auto_attribs=True)
|
|
|
|
class BaseTypedComparison(BaseComparison):
|
2021-12-28 20:32:33 +03:00
|
|
|
"""Common fields of a device typed component"""
|
|
|
|
|
2022-01-21 11:42:37 +03:00
|
|
|
type: str = attr.ib(hash=False, metadata={'printable': False})
|
|
|
|
type_display: str = attr.ib(eq=False, hash=False, metadata={'displayed_caption': 'Type'})
|
2021-12-28 17:43:30 +03:00
|
|
|
|
2022-01-18 19:56:57 +03:00
|
|
|
|
2022-01-21 11:42:37 +03:00
|
|
|
@attr.s(frozen=True, auto_attribs=True)
|
|
|
|
class DeviceBayComparison(BaseComparison):
|
|
|
|
"""A unified way to represent the device bay and device bay template"""
|
|
|
|
pass
|
2021-12-28 17:43:30 +03:00
|
|
|
|
|
|
|
|
2022-01-21 11:42:37 +03:00
|
|
|
@attr.s(frozen=True, auto_attribs=True)
|
|
|
|
class InterfaceComparison(BaseTypedComparison):
|
2021-12-28 17:43:30 +03:00
|
|
|
"""A unified way to represent the interface and interface template"""
|
|
|
|
|
2022-01-21 11:42:37 +03:00
|
|
|
mgmt_only: bool = attr.ib(hash=False)
|
2022-01-18 19:56:57 +03:00
|
|
|
|
2021-12-28 22:29:37 +03:00
|
|
|
|
2022-01-21 11:42:37 +03:00
|
|
|
@attr.s(frozen=True, auto_attribs=True)
|
|
|
|
class FrontPortComparison(BaseTypedComparison):
|
2021-12-28 20:32:33 +03:00
|
|
|
"""A unified way to represent the front port and front port template"""
|
|
|
|
|
2022-01-21 11:42:37 +03:00
|
|
|
color: str = attr.ib(hash=False)
|
2022-01-11 15:47:43 +03:00
|
|
|
# rear_port_id: int
|
2022-01-21 11:42:37 +03:00
|
|
|
rear_port_position: int = attr.ib(hash=False, metadata={'displayed_caption': 'Position'})
|
2022-01-18 19:56:57 +03:00
|
|
|
|
2022-01-11 15:47:43 +03:00
|
|
|
|
2022-01-21 11:42:37 +03:00
|
|
|
@attr.s(frozen=True, auto_attribs=True)
|
|
|
|
class RearPortComparison(BaseTypedComparison):
|
2021-12-28 20:32:33 +03:00
|
|
|
"""A unified way to represent the rear port and rear port template"""
|
|
|
|
|
2022-01-21 11:42:37 +03:00
|
|
|
color: str = attr.ib(hash=False)
|
|
|
|
positions: int = attr.ib(hash=False)
|
2022-01-11 15:47:43 +03:00
|
|
|
|
2021-12-28 20:32:33 +03:00
|
|
|
|
2022-01-21 11:42:37 +03:00
|
|
|
@attr.s(frozen=True, auto_attribs=True)
|
|
|
|
class ConsolePortComparison(BaseTypedComparison):
|
2021-12-28 20:32:33 +03:00
|
|
|
"""A unified way to represent the consoleport and consoleport template"""
|
2022-01-21 11:42:37 +03:00
|
|
|
pass
|
2021-12-28 20:32:33 +03:00
|
|
|
|
|
|
|
|
2022-01-21 11:42:37 +03:00
|
|
|
@attr.s(frozen=True, auto_attribs=True)
|
|
|
|
class ConsoleServerPortComparison(BaseTypedComparison):
|
2021-12-28 20:32:33 +03:00
|
|
|
"""A unified way to represent the consoleserverport and consoleserverport template"""
|
2022-01-21 11:42:37 +03:00
|
|
|
pass
|
2021-12-28 20:32:33 +03:00
|
|
|
|
|
|
|
|
2022-01-21 11:42:37 +03:00
|
|
|
@attr.s(frozen=True, auto_attribs=True)
|
|
|
|
class PowerPortComparison(BaseTypedComparison):
|
2021-12-28 20:32:33 +03:00
|
|
|
"""A unified way to represent the power port and power port template"""
|
|
|
|
|
2022-01-21 11:42:37 +03:00
|
|
|
maximum_draw: str = attr.ib(hash=False)
|
|
|
|
allocated_draw: str = attr.ib(hash=False)
|
2022-01-18 19:56:57 +03:00
|
|
|
|
2022-01-11 16:09:46 +03:00
|
|
|
|
2022-01-21 11:42:37 +03:00
|
|
|
@attr.s(frozen=True, auto_attribs=True)
|
|
|
|
class PowerOutletComparison(BaseTypedComparison):
|
2021-12-28 20:32:33 +03:00
|
|
|
"""A unified way to represent the power outlet and power outlet template"""
|
2021-12-28 17:43:30 +03:00
|
|
|
|
2022-01-21 11:42:37 +03:00
|
|
|
power_port_name: str = attr.ib(hash=False)
|
|
|
|
feed_leg: str = attr.ib(hash=False)
|