1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Refactor cable tracing logic

This commit is contained in:
Jeremy Stretch
2020-04-15 15:46:41 -04:00
parent 5aadfff1de
commit 5205c4963f
4 changed files with 61 additions and 47 deletions

View File

@@ -10,6 +10,7 @@ from taggit.managers import TaggableManager
from dcim.choices import *
from dcim.constants import *
from dcim.exceptions import CableTraceSplit
from dcim.fields import MACAddressField
from extras.models import ObjectChange, TaggedItem
from extras.utils import extras_features
@@ -117,10 +118,7 @@ class CableTermination(models.Model):
# Can't map to a FrontPort without a position
if not position_stack:
# TODO: This behavior is broken. We need a mechanism by which to return all FrontPorts mapped
# to a given RearPort so that we can update end-to-end paths when a cable is created/deleted.
# For now, we're maintaining the current behavior of tracing only to the first FrontPort.
position_stack.append(1)
raise CableTraceSplit(termination)
position = position_stack.pop()
@@ -186,6 +184,25 @@ class CableTermination(models.Model):
if self._cabled_as_b.exists():
return self.cable.termination_a
def get_path_endpoints(self):
"""
Return all endpoints of paths which traverse this object.
"""
endpoints = []
# Get the far end of the last path segment
try:
endpoint = self.trace()[-1][2]
if endpoint is not None:
endpoints.append(endpoint)
# We've hit a RearPort mapped to multiple FrontPorts. Recurse to trace each of them individually.
except CableTraceSplit as e:
for frontport in e.termination.frontports.all():
endpoints.extend(frontport.get_path_endpoints())
return endpoints
#
# Console ports