2022-03-03 12:39:16 +03:00
|
|
|
from typing import Optional
|
|
|
|
|
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
|
|
|
|
|
2022-03-03 12:39:16 +03:00
|
|
|
from netbox.models import PrimaryModel
|
|
|
|
|
2022-01-18 19:56:57 +03:00
|
|
|
config = settings.PLUGINS_CONFIG["netbox_interface_sync"]
|
2022-03-03 12:39:16 +03:00
|
|
|
SYNC_DESCRIPTIONS: bool = config["sync_descriptions"]
|
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
|
2022-03-03 12:39:16 +03:00
|
|
|
id: int = attr.ib(eq=False, metadata={'printable': False, 'netbox_exportable': False})
|
2022-01-21 11:42:37 +03:00
|
|
|
# Compare names case-insensitively and spaces-insensitively
|
2022-03-03 12:39:16 +03:00
|
|
|
name: str = attr.ib(metadata={'printable': False})
|
|
|
|
label: str = attr.ib()
|
2022-01-21 11:42:37 +03:00
|
|
|
# Compare descriptions if it is set by the configuration
|
2022-03-03 12:39:16 +03:00
|
|
|
description: str = attr.ib(eq=SYNC_DESCRIPTIONS, metadata={'synced': SYNC_DESCRIPTIONS})
|
2022-01-21 11:42:37 +03:00
|
|
|
# Do not compare `is_template` properties
|
2022-03-03 12:39:16 +03:00
|
|
|
is_template: bool = attr.ib(
|
|
|
|
default=False, kw_only=True, eq=False,
|
|
|
|
metadata={'printable': False, 'netbox_exportable': False}
|
|
|
|
)
|
2022-01-21 11:42:37 +03:00
|
|
|
|
|
|
|
@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()
|
2022-03-03 12:39:16 +03:00
|
|
|
if isinstance(field_value, BaseComparison):
|
|
|
|
field_value = f'{field_value.name} (ID: {field_value.id})'
|
2022-01-21 11:42:37 +03:00
|
|
|
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
|
|
|
|
2022-03-03 12:39:16 +03:00
|
|
|
def get_fields_for_netbox_component(self, sync=False):
|
|
|
|
"""
|
|
|
|
Returns a dict of fields and values for creating or updating a NetBox component object
|
|
|
|
:param sync: if True, returns fields for syncing an existing component, otherwise - for creating a new one.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def field_filter(field: attr.Attribute, _):
|
|
|
|
result = field.metadata.get('netbox_exportable', True)
|
|
|
|
if sync:
|
|
|
|
result &= field.metadata.get('synced', True)
|
|
|
|
return result
|
|
|
|
|
|
|
|
return attr.asdict(self, recurse=True, filter=field_filter)
|
|
|
|
|
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-03-03 12:39:16 +03:00
|
|
|
type: str = attr.ib(metadata={'printable': False})
|
|
|
|
type_display: str = attr.ib(eq=False, metadata={'displayed_caption': 'Type', 'netbox_exportable': False})
|
2021-12-28 20:32:33 +03:00
|
|
|
|
2022-03-03 12:39:16 +03:00
|
|
|
|
|
|
|
@attr.s(frozen=True, auto_attribs=True)
|
|
|
|
class ConsolePortComparison(BaseTypedComparison):
|
|
|
|
"""A unified way to represent the consoleport and consoleport template"""
|
|
|
|
pass
|
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)
|
2022-03-03 12:39:16 +03:00
|
|
|
class ConsoleServerPortComparison(BaseTypedComparison):
|
|
|
|
"""A unified way to represent the consoleserverport and consoleserverport template"""
|
2022-01-21 11:42:37 +03:00
|
|
|
pass
|
2021-12-28 17:43:30 +03:00
|
|
|
|
|
|
|
|
2022-03-03 12:39:16 +03:00
|
|
|
@attr.s(frozen=True, auto_attribs=True)
|
|
|
|
class PowerPortComparison(BaseTypedComparison):
|
|
|
|
"""A unified way to represent the power port and power port template"""
|
|
|
|
maximum_draw: str = attr.ib()
|
|
|
|
allocated_draw: str = attr.ib()
|
|
|
|
|
|
|
|
|
|
|
|
@attr.s(frozen=True, auto_attribs=True)
|
|
|
|
class PowerOutletComparison(BaseTypedComparison):
|
|
|
|
"""A unified way to represent the power outlet and power outlet template"""
|
|
|
|
power_port: PowerPortComparison = attr.ib()
|
|
|
|
feed_leg: str = attr.ib()
|
|
|
|
|
|
|
|
|
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-03-03 12:39:16 +03:00
|
|
|
mgmt_only: bool = attr.ib()
|
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-03-03 12:39:16 +03:00
|
|
|
color: str = attr.ib()
|
2022-01-11 15:47:43 +03:00
|
|
|
# rear_port_id: int
|
2022-03-03 12:39:16 +03:00
|
|
|
rear_port_position: int = attr.ib(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-03-03 12:39:16 +03:00
|
|
|
color: str = attr.ib()
|
|
|
|
positions: int = attr.ib()
|
2021-12-28 20:32:33 +03:00
|
|
|
|
|
|
|
|
2022-01-21 11:42:37 +03:00
|
|
|
@attr.s(frozen=True, auto_attribs=True)
|
2022-03-03 12:39:16 +03:00
|
|
|
class DeviceBayComparison(BaseComparison):
|
|
|
|
"""A unified way to represent the device bay and device bay template"""
|
2022-01-21 11:42:37 +03:00
|
|
|
pass
|
2021-12-28 20:32:33 +03:00
|
|
|
|
|
|
|
|
2022-03-03 12:39:16 +03:00
|
|
|
def from_netbox_object(netbox_object: PrimaryModel) -> Optional[BaseComparison]:
|
|
|
|
"""Makes a comparison object from the NetBox object"""
|
|
|
|
type_map = {
|
|
|
|
"DeviceBay": DeviceBayComparison,
|
|
|
|
"Interface": InterfaceComparison,
|
|
|
|
"FrontPort": FrontPortComparison,
|
|
|
|
"RearPort": RearPortComparison,
|
|
|
|
"ConsolePort": ConsolePortComparison,
|
|
|
|
"ConsoleServerPort": ConsoleServerPortComparison,
|
|
|
|
"PowerPort": PowerPortComparison,
|
|
|
|
"PowerOutlet": PowerOutletComparison
|
|
|
|
}
|
|
|
|
|
|
|
|
obj_name = netbox_object._meta.object_name
|
|
|
|
if obj_name.endswith("Template"):
|
|
|
|
is_template = True
|
|
|
|
obj_name = obj_name[:-8] # TODO: use `removesuffix` introduced in Python 3.9
|
|
|
|
else:
|
|
|
|
is_template = False
|
|
|
|
|
|
|
|
comparison = type_map.get(obj_name)
|
|
|
|
if not comparison:
|
|
|
|
return
|
|
|
|
|
|
|
|
values = {}
|
|
|
|
for field in fields(comparison):
|
|
|
|
if field.name == "is_template":
|
|
|
|
continue
|
|
|
|
if field.name == "type_display":
|
|
|
|
values[field.name] = netbox_object.get_type_display()
|
|
|
|
else:
|
|
|
|
field_value = getattr(netbox_object, field.name)
|
|
|
|
if isinstance(field_value, PrimaryModel):
|
|
|
|
field_value = from_netbox_object(field_value)
|
|
|
|
values[field.name] = field_value
|
|
|
|
|
|
|
|
return comparison(**values, is_template=is_template)
|