Fix partial-width integration with Netbox-Utilities
The default mapping pointed to netbox_utilities.RackDevicePosition / width_fraction, which never existed, so partial widths were silently ignored: copying a rack lost them and deploying a concept with devices sharing a rack unit failed with "already occupied". Use netbox_utilities.DeviceRackPlacement (width, horizontal_position), convert between 0-based concept positions and the 1-based positions of netbox_utilities, and stage the width on the device before full_clean() so the Utilities collision check sees it. Configurations with the old model name are migrated automatically. Verified on NetBox 4.7.2: copying a rack with two half-width devices in one U and deploying it twice recreates both side by side. Bump version to 0.2.6. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
9736a216be
commit
6ffe6348a6
6 files changed
+72
-17
No files matched your search
@@ -1,6 +1,6 @@
|
||||
from netbox.plugins import PluginConfig
|
||||
|
||||
__version__ = '0.2.5'
|
||||
__version__ = '0.2.6'
|
||||
|
||||
|
||||
class RackConceptConfig(PluginConfig):
|
||||
@@ -39,10 +39,12 @@ class RackConceptConfig(PluginConfig):
|
||||
# to never write partial-width data during deployment.
|
||||
'partial_width': {
|
||||
'enabled': True,
|
||||
'model': 'netbox_utilities.RackDevicePosition',
|
||||
'model': 'netbox_utilities.DeviceRackPlacement',
|
||||
'device_field': 'device',
|
||||
'fraction_field': 'width_fraction',
|
||||
'fraction_field': 'width',
|
||||
'position_field': 'horizontal_position',
|
||||
# netbox_utilities counts positions from 1 (left); concepts count from 0.
|
||||
'position_base': 1,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ from dcim.models import Device, Rack
|
||||
|
||||
from . import components
|
||||
from .choices import ConceptDeviceStatusChoices, ConceptStatusChoices
|
||||
from .integrations import apply_partial_width, read_partial_width
|
||||
from .integrations import apply_partial_width, read_partial_width, stage_partial_width
|
||||
from .models import ConceptDeployment, ConceptDevice, RackConcept
|
||||
|
||||
__all__ = (
|
||||
@@ -452,6 +452,7 @@ def deploy_concept(concept, rack_names, site, location=None, tenant=None, create
|
||||
serial=planned_device.serial,
|
||||
description=planned_device.description,
|
||||
)
|
||||
stage_partial_width(device, planned_device)
|
||||
device.full_clean()
|
||||
device.save()
|
||||
apply_partial_width(device, planned_device)
|
||||
|
||||
@@ -24,6 +24,7 @@ __all__ = (
|
||||
'apply_partial_width',
|
||||
'get_partial_width_adapter',
|
||||
'read_partial_width',
|
||||
'stage_partial_width',
|
||||
'reorder_rack_installed',
|
||||
)
|
||||
|
||||
@@ -46,13 +47,15 @@ class PartialWidthAdapter:
|
||||
either extra fields on dcim.Device, or a side model with a FK to the device.
|
||||
"""
|
||||
|
||||
def __init__(self, model, device_field, fraction_field, position_field):
|
||||
def __init__(self, model, device_field, fraction_field, position_field, position_base=0):
|
||||
self.model = model
|
||||
self.device_field = device_field
|
||||
self.fraction_field = fraction_field
|
||||
self.position_field = position_field
|
||||
self.position_base = position_base
|
||||
|
||||
def write(self, device, fraction, position):
|
||||
position += self.position_base
|
||||
if self.model is None:
|
||||
# Fields live directly on the Device.
|
||||
setattr(device, self.fraction_field, fraction)
|
||||
@@ -77,11 +80,25 @@ class PartialWidthAdapter:
|
||||
obj = self.model.objects.filter(**{self.device_field: device}).first()
|
||||
if obj is None:
|
||||
return None, None
|
||||
return getattr(obj, self.fraction_field, None), getattr(obj, self.position_field, None)
|
||||
position = getattr(obj, self.position_field, None)
|
||||
if position is not None:
|
||||
position = max(int(position) - self.position_base, 0)
|
||||
return getattr(obj, self.fraction_field, None), position
|
||||
|
||||
|
||||
_ADAPTER_CACHE = {}
|
||||
|
||||
# Defaults matching netbox_utilities.DeviceRackPlacement. Earlier versions of this plugin
|
||||
# shipped a model name that never existed; configurations copied from that are migrated.
|
||||
UTILITIES_PARTIAL_WIDTH = {
|
||||
'model': 'netbox_utilities.DeviceRackPlacement',
|
||||
'device_field': 'device',
|
||||
'fraction_field': 'width',
|
||||
'position_field': 'horizontal_position',
|
||||
'position_base': 1,
|
||||
}
|
||||
LEGACY_PARTIAL_WIDTH_MODEL = 'netbox_utilities.RackDevicePosition'
|
||||
|
||||
|
||||
def get_partial_width_adapter():
|
||||
"""Return a PartialWidthAdapter, or None when partial width is unavailable."""
|
||||
@@ -90,10 +107,13 @@ def get_partial_width_adapter():
|
||||
|
||||
adapter = None
|
||||
config = _config().get('partial_width') or {}
|
||||
if config.get('model') == LEGACY_PARTIAL_WIDTH_MODEL:
|
||||
config = {**config, **UTILITIES_PARTIAL_WIDTH}
|
||||
|
||||
if config.get('enabled') and plugin_installed('netbox_utilities'):
|
||||
fraction_field = config.get('fraction_field', 'width_fraction')
|
||||
position_field = config.get('position_field', 'horizontal_position')
|
||||
fraction_field = config.get('fraction_field', UTILITIES_PARTIAL_WIDTH['fraction_field'])
|
||||
position_field = config.get('position_field', UTILITIES_PARTIAL_WIDTH['position_field'])
|
||||
position_base = int(config.get('position_base', 0))
|
||||
model_path = config.get('model')
|
||||
model = None
|
||||
|
||||
@@ -119,13 +139,31 @@ def get_partial_width_adapter():
|
||||
)
|
||||
else:
|
||||
adapter = PartialWidthAdapter(
|
||||
model, config.get('device_field', 'device'), fraction_field, position_field
|
||||
model, config.get('device_field', 'device'), fraction_field, position_field, position_base
|
||||
)
|
||||
|
||||
_ADAPTER_CACHE['adapter'] = adapter
|
||||
return adapter
|
||||
|
||||
|
||||
def stage_partial_width(device, concept_device):
|
||||
"""
|
||||
Announce the partial width of a not yet saved device to netbox_utilities, so that its
|
||||
collision check treats devices sharing a rack unit side by side instead of overlapping.
|
||||
"""
|
||||
if concept_device.width_fraction == 1 or get_partial_width_adapter() is None:
|
||||
return False
|
||||
try:
|
||||
from netbox_utilities.rack_width import stage_width_position
|
||||
except ImportError:
|
||||
return False
|
||||
adapter = get_partial_width_adapter()
|
||||
stage_width_position(
|
||||
device, concept_device.width_fraction, concept_device.horizontal_position + adapter.position_base
|
||||
)
|
||||
return True
|
||||
|
||||
|
||||
def apply_partial_width(device, concept_device):
|
||||
"""
|
||||
Persist the partial-width placement of a concept device onto the real device that
|
||||
|
||||
Reference in new issue
Block a user