Catch AssertionError's in signals. Handle accordingly

This commit is contained in:
Daniel Sheppard 2023-08-01 13:01:36 -05:00
parent 0b10131564
commit 7c09eb298d
3 changed files with 18 additions and 2 deletions

View File

@ -478,8 +478,11 @@ class CablePath(models.Model):
Cable or WirelessLink connects (interfaces, console ports, circuit termination, etc.). All terminations must be Cable or WirelessLink connects (interfaces, console ports, circuit termination, etc.). All terminations must be
of the same type and must belong to the same parent object. of the same type and must belong to the same parent object.
""" """
import logging
from circuits.models import CircuitTermination from circuits.models import CircuitTermination
logger = logging.getLogger('netbox.dcim.cablepath')
if not terminations: if not terminations:
return None return None
@ -558,6 +561,7 @@ class CablePath(models.Model):
pk__in=[t.rear_port_id for t in remote_terminations] pk__in=[t.rear_port_id for t in remote_terminations]
) )
if len(rear_ports) > 1: if len(rear_ports) > 1:
logger.warning(f'All rear-port positions do not match. Cannot continue path trace.')
assert all(rp.positions == 1 for rp in rear_ports) assert all(rp.positions == 1 for rp in rear_ports)
elif rear_ports[0].positions > 1: elif rear_ports[0].positions > 1:
position_stack.append([fp.rear_port_position for fp in remote_terminations]) position_stack.append([fp.rear_port_position for fp in remote_terminations])

View File

@ -95,7 +95,11 @@ def update_connected_endpoints(instance, created, raw=False, **kwargs):
if not nodes: if not nodes:
continue continue
if isinstance(nodes[0], PathEndpoint): if isinstance(nodes[0], PathEndpoint):
create_cablepath(nodes) try:
create_cablepath(nodes)
except AssertionError:
# This is likely an unsupported path. Catch the assertion error and don't save the path
logger.error(f'Unsupported path from nodes: {[node.name for node in nodes].join(",")}')
else: else:
rebuild_paths(nodes) rebuild_paths(nodes)

View File

@ -49,12 +49,20 @@ def rebuild_paths(terminations):
""" """
Rebuild all CablePaths which traverse the specified nodes. Rebuild all CablePaths which traverse the specified nodes.
""" """
import logging
from dcim.models import CablePath from dcim.models import CablePath
logger = logging.getLogger('netbox.dcim.cable')
for obj in terminations: for obj in terminations:
cable_paths = CablePath.objects.filter(_nodes__contains=obj) cable_paths = CablePath.objects.filter(_nodes__contains=obj)
with transaction.atomic(): with transaction.atomic():
for cp in cable_paths: for cp in cable_paths:
cp.delete() cp.delete()
create_cablepath(cp.origins) try:
create_cablepath(cp.origins)
except AssertionError:
# This is likely an unsupported path. Catch the assertion error and don't save the path
logger.error(f'Unsupported path from cable path: {cp._nodes}')
pass