fix: skip patchpanel automation on edits
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from netbox.plugins import PluginConfig, get_plugin_config
|
||||
|
||||
__version__ = "0.7.3"
|
||||
__version__ = "0.7.4"
|
||||
|
||||
|
||||
class NetBoxUtilitiesConfig(PluginConfig):
|
||||
|
||||
@@ -117,8 +117,8 @@ def synchronize_patchpanel(device_id):
|
||||
return len(pairs)
|
||||
|
||||
|
||||
def _component_saved(sender, instance, raw=False, **kwargs):
|
||||
if not raw and not _component_instantiation_active.get():
|
||||
def _component_saved(sender, instance, created=False, raw=False, **kwargs):
|
||||
if created and not raw and not _component_instantiation_active.get():
|
||||
synchronize_patchpanel(getattr(instance, "device_id", None))
|
||||
|
||||
|
||||
@@ -131,6 +131,10 @@ def _wrap_component_container_save(model, device_id_getter):
|
||||
|
||||
@wraps(original_save)
|
||||
def patchpanel_aware_save(instance, *args, **kwargs):
|
||||
created = instance._state.adding
|
||||
if not created:
|
||||
return original_save(instance, *args, **kwargs)
|
||||
|
||||
token = _component_instantiation_active.set(True)
|
||||
try:
|
||||
result = original_save(instance, *args, **kwargs)
|
||||
|
||||
@@ -4,7 +4,14 @@ from unittest.mock import MagicMock, patch
|
||||
|
||||
from django.test import SimpleTestCase
|
||||
|
||||
from netbox_utilities.patchpanel import is_patchpanel, pair_ports, port_identifier, retrace_patchpanel_paths
|
||||
from netbox_utilities.patchpanel import (
|
||||
_component_saved,
|
||||
_wrap_component_container_save,
|
||||
is_patchpanel,
|
||||
pair_ports,
|
||||
port_identifier,
|
||||
retrace_patchpanel_paths,
|
||||
)
|
||||
|
||||
|
||||
def port(pk, name):
|
||||
@@ -12,6 +19,41 @@ def port(pk, name):
|
||||
|
||||
|
||||
class PatchpanelPairingTest(SimpleTestCase):
|
||||
@patch("netbox_utilities.patchpanel.synchronize_patchpanel")
|
||||
def test_component_edits_do_not_trigger_automation(self, synchronize):
|
||||
component = SimpleNamespace(device_id=42)
|
||||
|
||||
_component_saved(None, component, created=False)
|
||||
|
||||
synchronize.assert_not_called()
|
||||
|
||||
@patch("netbox_utilities.patchpanel.synchronize_patchpanel")
|
||||
def test_new_components_trigger_automation(self, synchronize):
|
||||
component = SimpleNamespace(device_id=42)
|
||||
|
||||
_component_saved(None, component, created=True)
|
||||
|
||||
synchronize.assert_called_once_with(42)
|
||||
|
||||
@patch("netbox_utilities.patchpanel.synchronize_patchpanel")
|
||||
def test_existing_device_save_does_not_trigger_automation(self, synchronize):
|
||||
class Container:
|
||||
def __init__(self, adding):
|
||||
self._state = SimpleNamespace(adding=adding)
|
||||
self.pk = 42
|
||||
|
||||
def save(self):
|
||||
self._state.adding = False
|
||||
return "saved"
|
||||
|
||||
_wrap_component_container_save(Container, lambda instance: instance.pk)
|
||||
|
||||
self.assertEqual(Container(adding=False).save(), "saved")
|
||||
synchronize.assert_not_called()
|
||||
|
||||
self.assertEqual(Container(adding=True).save(), "saved")
|
||||
synchronize.assert_called_once_with(42)
|
||||
|
||||
def test_existing_device_migrations_are_noops(self):
|
||||
migration_names = (
|
||||
"0004_patchpanel_port_mappings",
|
||||
|
||||
Reference in New Issue
Block a user