Skip to content

Fix ingesting cable terminations of circuit types #343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/326.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added testing for cable termination type when adding cables to diffsync store.
1 change: 1 addition & 0 deletions changes/326.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed incorrect call to cable termination type.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import diffsync
from diffsync.enum import DiffSyncModelFlags
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from nautobot.dcim.models import Interface
from nautobot.ipam.models import VLAN, VRF, IPAddress
Expand Down Expand Up @@ -249,9 +250,13 @@ def load_cables(self):

Only cables returned by the CommandGetter job should be synced.
"""
dcim_interface_content_type = ContentType.objects.get_for_model(Interface)
for device in self.job.devices_to_load:
for cable in device.get_cables():
if cable.termination_a.type != "dcim.interface" or cable.termination_b.type != "dcim.interface":
if (
cable.termination_a_type != dcim_interface_content_type
or cable.termination_b_type != dcim_interface_content_type
):
self.job.logger.warning(
f"Skipping Cable: {cable}. Only cables with interface terminations are supported."
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from unittest.mock import MagicMock, patch

from django.contrib.contenttypes.models import ContentType
from nautobot.core.testing import TransactionTestCase
from nautobot.dcim.models import Cable, Device, Interface
from nautobot.extras.models import JobResult
Expand Down Expand Up @@ -351,17 +352,27 @@ def test_load_vrfs(self):

def test_load_cables(self):
"""Test loading Nautobot cable data into the diffsync store."""
self.sync_network_data_adapter.load_cables()
for cable in Cable.objects.all():
if not cable.termination_a.type == "dcim.interface" or cable.termination_b.type == "dcim.interface":
# skip loading cables connected to circuits (they should be skipped by the adapter)
continue
unique_id = f"dcim__interface__{cable.termination_a.device.name}__{cable.termination_a.name}__dcim__interface__{cable.termination_b.device.name}__{cable.termination_b.name}"
diffsync_obj = self.sync_network_data_adapter.get("cable", unique_id)
self.assertEqual(cable.termination_a.device.name, diffsync_obj.termination_a__device__name)
self.assertEqual(cable.termination_a.name, diffsync_obj.termination_a__name)
self.assertEqual(cable.termination_b.device.name, diffsync_obj.termination_b__device__name)
self.assertEqual(cable.termination_b.name, diffsync_obj.termination_b__name)
dcim_interface_content_type = ContentType.objects.get_for_model(Interface)

with self.assertLogs(self.job.logger, level="WARNING") as logs:
self.sync_network_data_adapter.load_cables()
for cable in Cable.objects.all():
if (
cable.termination_a_type != dcim_interface_content_type
or cable.termination_b_type != dcim_interface_content_type
):
self.assertIn(
f"WARNING:nautobot_device_onboarding.jobs:Skipping Cable: {cable}. Only cables with interface terminations are supported.",
logs.output[0],
)
continue
unique_id = f"dcim__interface__{cable.termination_a.device.name}__{cable.termination_a.name}__dcim__interface__{cable.termination_b.device.name}__{cable.termination_b.name}"
diffsync_obj = self.sync_network_data_adapter.get("cable", unique_id)
self.assertEqual(cable.termination_a.device.name, diffsync_obj.termination_a__device__name)
self.assertEqual(cable.termination_a.name, diffsync_obj.termination_a__name)
self.assertEqual(cable.termination_b.device.name, diffsync_obj.termination_b__device__name)
self.assertEqual(cable.termination_b.name, diffsync_obj.termination_b__name)
# self.assertIn(f"WARNING - Skipping Cable: #{cable}. Only cables with interface terminations are supported.", logs.output[0])

def test_load_vrf_to_interface(self):
"""Test loading Nautobot vrf interface assignments into the Diffsync store."""
Expand Down
2 changes: 1 addition & 1 deletion nautobot_device_onboarding/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def sync_network_data_ensure_required_nautobot_objects():
location=location,
)

cable_to_circuit_1 = Cable.objects.get_or_create(
cable_to_circuit_1, _ = Cable.objects.get_or_create(
termination_a_type=ContentType.objects.get_for_model(Interface),
termination_a_id=interface_4.id,
termination_b_type=ContentType.objects.get_for_model(CircuitTermination),
Expand Down