Clean up cable termination changes

This commit is contained in:
jeremystretch 2022-06-03 14:15:28 -04:00
parent 537383e071
commit 0b0a646f87
4 changed files with 17 additions and 29 deletions

View File

@ -83,7 +83,7 @@ def get_cable_form(a_type, b_type):
label=term_cls._meta.verbose_name.title(), label=term_cls._meta.verbose_name.title(),
disabled_indicator='_occupied', disabled_indicator='_occupied',
query_params={ query_params={
'device_id': f'termination_{cable_end}_device', 'device_id': f'$termination_{cable_end}_device',
} }
) )
@ -107,7 +107,7 @@ def get_cable_form(a_type, b_type):
label='Power Feed', label='Power Feed',
disabled_indicator='_occupied', disabled_indicator='_occupied',
query_params={ query_params={
'powerpanel_id': f'termination_{cable_end}_powerpanel', 'powerpanel_id': f'$termination_{cable_end}_powerpanel',
} }
) )

View File

@ -316,7 +316,7 @@ class CableTermination(models.Model):
# Delete the cable association on the terminating object # Delete the cable association on the terminating object
termination_model = self.termination._meta.model termination_model = self.termination._meta.model
termination_model.objects.filter(pk=self.termination_id).update(cable=None) termination_model.objects.filter(pk=self.termination_id).update(cable=None, cable_end='', _path=None)
super().delete(*args, **kwargs) super().delete(*args, **kwargs)

View File

@ -71,22 +71,23 @@ def clear_virtualchassis_members(instance, **kwargs):
@receiver(trace_paths, sender=Cable) @receiver(trace_paths, sender=Cable)
def update_connected_endpoints(instance, created, raw=False, **kwargs): def update_connected_endpoints(instance, created, raw=False, **kwargs):
""" """
When a Cable is saved, check for and update its two connected endpoints When a Cable is saved with new terminations, retrace any affected cable paths.
""" """
logger = logging.getLogger('netbox.dcim.cable') logger = logging.getLogger('netbox.dcim.cable')
if raw: if raw:
logger.debug(f"Skipping endpoint updates for imported cable {instance}") logger.debug(f"Skipping endpoint updates for imported cable {instance}")
return return
# TODO: Update link peers # Update cable paths if new terminations have been set
if hasattr(instance, 'a_terminations') or hasattr(instance, 'b_terminations'):
# Create/update cable paths a_terminations = []
if created: b_terminations = []
_terms = { for t in instance.terminations.all():
'A': [t.termination for t in instance.terminations.filter(cable_end='A')], if t.cable_end == 'A':
'B': [t.termination for t in instance.terminations.filter(cable_end='B')], a_terminations.append(t.termination)
} else:
for nodes in _terms.values(): b_terminations.append(t.termination)
for nodes in [a_terminations, b_terminations]:
# Examine type of first termination to determine object type (all must be the same) # Examine type of first termination to determine object type (all must be the same)
if not nodes: if not nodes:
continue continue
@ -94,29 +95,15 @@ def update_connected_endpoints(instance, created, raw=False, **kwargs):
create_cablepath(nodes) create_cablepath(nodes)
else: else:
rebuild_paths(nodes) rebuild_paths(nodes)
# Update status of CablePaths if Cable status has been changed
elif instance.status != instance._orig_status: elif instance.status != instance._orig_status:
# We currently don't support modifying either termination of an existing Cable. (This
# may change in the future.) However, we do need to capture status changes and update
# any CablePaths accordingly.
if instance.status != LinkStatusChoices.STATUS_CONNECTED: if instance.status != LinkStatusChoices.STATUS_CONNECTED:
CablePath.objects.filter(_nodes__contains=instance).update(is_active=False) CablePath.objects.filter(_nodes__contains=instance).update(is_active=False)
else: else:
rebuild_paths([instance]) rebuild_paths([instance])
# @receiver(post_save, sender=CableTermination)
# def cache_cable_on_endpoints(instance, created, raw=False, **kwargs):
# if not raw:
# model = instance.termination_type.model_class()
# model.objects.filter(pk=instance.termination_id).update(cable=instance.cable)
#
#
# @receiver(post_delete, sender=CableTermination)
# def clear_cable_on_endpoints(instance, **kwargs):
# model = instance.termination_type.model_class()
# model.objects.filter(pk=instance.termination_id).update(cable=None)
@receiver(post_delete, sender=Cable) @receiver(post_delete, sender=Cable)
def retrace_cable_paths(instance, **kwargs): def retrace_cable_paths(instance, **kwargs):
""" """

View File

@ -1754,6 +1754,7 @@ class CablePathTestCase(TestCase):
self.assertEqual(CablePath.objects.count(), 2) self.assertEqual(CablePath.objects.count(), 2)
# Change cable 2's status to "planned" # Change cable 2's status to "planned"
cable2 = Cable.objects.get(pk=cable2.pk) # Rebuild object to ditch A/B terminations set earlier
cable2.status = LinkStatusChoices.STATUS_PLANNED cable2.status = LinkStatusChoices.STATUS_PLANNED
cable2.save() cable2.save()
self.assertPathExists( self.assertPathExists(