1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00
This commit is contained in:
jeremystretch
2022-05-06 16:17:27 -04:00
parent 304282bd4f
commit b44bfa1aa6
3 changed files with 25 additions and 14 deletions

View File

@ -330,6 +330,8 @@ class CablePath(models.Model):
def from_origin(cls, terminations): def from_origin(cls, terminations):
""" """
Create a new CablePath instance as traced from the given path origin. Create a new CablePath instance as traced from the given path origin.
:param terminations: An iterable of one or more CableTermination objects.
""" """
from circuits.models import CircuitTermination from circuits.models import CircuitTermination
@ -348,7 +350,7 @@ class CablePath(models.Model):
]) ])
node = terminations[0].termination node = terminations[0].termination
while node.link is not None: while terminations and node.link is not None:
if hasattr(node.link, 'status') and node.link.status != LinkStatusChoices.STATUS_CONNECTED: if hasattr(node.link, 'status') and node.link.status != LinkStatusChoices.STATUS_CONNECTED:
is_active = False is_active = False
@ -371,9 +373,10 @@ class CablePath(models.Model):
termination_id__in=[t.termination_id for t in peer_terminations] termination_id__in=[t.termination_id for t in peer_terminations]
) )
rear_ports = RearPort.objects.filter(pk__in=[t.termination.rear_port_id for t in peer_terminations]) rear_ports = RearPort.objects.filter(pk__in=[t.termination.rear_port_id for t in peer_terminations])
# TODO: We're assuming that each of the front-to-rear mapping use equivalent positions.
node = rear_ports[0] node = rear_ports[0]
if node.positions > 1: if rear_ports[0].positions > 1:
position_stack.append(node.rear_port_position) position_stack.append(peer_terminations[0].termination.rear_port_position)
path.append([ path.append([
object_to_path_node(rp) for rp in rear_ports object_to_path_node(rp) for rp in rear_ports
]) ])
@ -395,10 +398,14 @@ class CablePath(models.Model):
break break
# Map FrontPorts to their corresponding RearPorts # Map FrontPorts to their corresponding RearPorts
terminations = FrontPort.objects.filter( front_ports = FrontPort.objects.filter(
rear_port_id__in=[t.rear_port_id for t in peer_terminations], rear_port_id__in=[t.rear_port_id for t in peer_terminations],
rear_port_position=position rear_port_position=position
) )
terminations = CableTermination.objects.filter(
termination_type=ContentType.objects.get_for_model(FrontPort),
termination_id__in=[fp.pk for fp in front_ports]
)
if terminations: if terminations:
path.append([ path.append([
object_to_path_node(t.termination) for t in terminations object_to_path_node(t.termination) for t in terminations

View File

@ -1,3 +1,4 @@
from collections import defaultdict
import logging import logging
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
@ -84,25 +85,26 @@ def update_connected_endpoints(instance, created, raw=False, **kwargs):
term for term in instance.terminations if not term.pk term for term in instance.terminations if not term.pk
]) ])
# Split terminations into A/B sets
_terms = defaultdict(list)
for term in instance.terminations:
_terms[term.cable_end].append(term)
# TODO: Update link peers # TODO: Update link peers
# Set cable on terminating endpoints # Set cable on terminating endpoints
_terms = {
'A': [],
'B': [],
}
for term in instance.terminations: for term in instance.terminations:
if term.termination.cable != instance: if term.termination.cable != instance:
term.termination.cable = instance term.termination.cable = instance
term.termination.save() term.termination.save()
_terms[term.cable_end].append(term)
# Create/update cable paths # Create/update cable paths
if created: if created:
for terms in _terms.values(): for terms in _terms.values():
# Examine type of first termination to determine object type (all must be the same)
if isinstance(terms[0].termination, PathEndpoint): if isinstance(terms[0].termination, PathEndpoint):
create_cablepath(terms) create_cablepath(terms)
else: # else:
rebuild_paths(terms) # rebuild_paths(terms)
# 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 # # 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 # # may change in the future.) However, we do need to capture status changes and update

View File

@ -39,13 +39,15 @@ def flatten_path(path):
return ret return ret
def create_cablepath(node): def create_cablepath(terminations):
""" """
Create CablePaths for all paths originating from the specified node. Create CablePaths for all paths originating from the specified set of nodes.
:param terminations: Iterable of CableTermination objects
""" """
from dcim.models import CablePath from dcim.models import CablePath
cp = CablePath.from_origin(node) cp = CablePath.from_origin(terminations)
if cp: if cp:
cp.save() cp.save()