From a53b676296cc6c42316db8ee58a26f238b162c63 Mon Sep 17 00:00:00 2001 From: Justin Drew <2396364+jdrew82@users.noreply.github.com> Date: Wed, 11 Dec 2024 08:29:36 -0600 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20Fix=20SoftwareVersion=20a?= =?UTF-8?q?ssignment=20during=20Device=20update.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- changes/626.fixed | 1 + .../integrations/dna_center/diffsync/adapters/nautobot.py | 5 +++++ .../integrations/dna_center/diffsync/models/nautobot.py | 7 +++++-- nautobot_ssot/integrations/dna_center/utils/nautobot.py | 5 ++--- nautobot_ssot/tests/dna_center/test_models_nautobot.py | 3 +++ 5 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 changes/626.fixed diff --git a/changes/626.fixed b/changes/626.fixed new file mode 100644 index 00000000..0d1f01ab --- /dev/null +++ b/changes/626.fixed @@ -0,0 +1 @@ +Fixed SoftwareVersion update on Devices in DNA Center integration. \ No newline at end of file diff --git a/nautobot_ssot/integrations/dna_center/diffsync/adapters/nautobot.py b/nautobot_ssot/integrations/dna_center/diffsync/adapters/nautobot.py index d0af61b0..a90139d7 100644 --- a/nautobot_ssot/integrations/dna_center/diffsync/adapters/nautobot.py +++ b/nautobot_ssot/integrations/dna_center/diffsync/adapters/nautobot.py @@ -20,6 +20,7 @@ from nautobot.dcim.models import Interface as OrmInterface from nautobot.dcim.models import Location as OrmLocation from nautobot.dcim.models import LocationType as OrmLocationType +from nautobot.dcim.models import Platform from nautobot.extras.models import Relationship as OrmRelationship from nautobot.extras.models import RelationshipAssociation as OrmRelationshipAssociation from nautobot.extras.models import Status as OrmStatus @@ -63,6 +64,7 @@ class NautobotAdapter(Adapter): site_map = {} floor_map = {} device_map = {} + platform_map = {} port_map = {} namespace_map = {} prefix_map = {} @@ -398,6 +400,9 @@ def load(self): self.status_map = {status.name: status.id for status in OrmStatus.objects.only("id", "name")} self.tenant_map = {tenant.name: tenant.id for tenant in OrmTenant.objects.only("id", "name")} self.namespace_map = {ns.name: ns.id for ns in Namespace.objects.only("id", "name")} + self.platform_map = { + platform.network_driver: platform.id for platform in Platform.objects.only("id", "network_driver") + } self.load_areas() self.load_buildings() diff --git a/nautobot_ssot/integrations/dna_center/diffsync/models/nautobot.py b/nautobot_ssot/integrations/dna_center/diffsync/models/nautobot.py index 267f1f0e..35cb6a76 100644 --- a/nautobot_ssot/integrations/dna_center/diffsync/models/nautobot.py +++ b/nautobot_ssot/integrations/dna_center/diffsync/models/nautobot.py @@ -207,6 +207,7 @@ def create(cls, adapter, ids, attrs): device_role.validated_save() device_type, _ = DeviceType.objects.get_or_create(model=attrs["model"], manufacturer=manufacturer) platform = verify_platform(platform_name=attrs["platform"], manu=manufacturer.id) + adapter.platform_map[attrs["platform"]] = platform.id new_device = Device( name=ids["name"], status_id=adapter.status_map[attrs["status"]], @@ -269,7 +270,9 @@ def update(self, attrs): if "platform" in attrs: vendor = attrs["vendor"] if attrs.get("vendor") else self.vendor manufacturer = Manufacturer.objects.get(name=vendor) - device.platform = verify_platform(platform_name=attrs["platform"], manu=manufacturer.id) + platform = verify_platform(platform_name=attrs["platform"], manu=manufacturer.id) + device.platform = platform + self.adapter.platform_map[attrs["platform"]] = platform.id if "tenant" in attrs: if attrs.get("tenant"): device.tenant_id = self.adapter.tenant_map[attrs["tenant"]] @@ -291,7 +294,7 @@ def update(self, attrs): platform = self.platform device.software_version = SoftwareVersion.objects.get_or_create( version=attrs["version"], - platform__name=platform, + platform_id=self.adapter.platform_map[platform], defaults={"status_id": self.adapter.status_map["Active"]}, )[0] device.custom_field_data.update({"system_of_record": "DNA Center"}) diff --git a/nautobot_ssot/integrations/dna_center/utils/nautobot.py b/nautobot_ssot/integrations/dna_center/utils/nautobot.py index d140179c..197a8b54 100644 --- a/nautobot_ssot/integrations/dna_center/utils/nautobot.py +++ b/nautobot_ssot/integrations/dna_center/utils/nautobot.py @@ -54,13 +54,12 @@ def add_software_lcm(adapter, platform: str, version: str): Returns: UUID: UUID of the OS Version that is being found or created. """ - platform_obj = Platform.objects.get(network_driver=platform) try: - os_ver = SoftwareLCM.objects.get(device_platform=platform_obj, version=version).id + os_ver = SoftwareLCM.objects.get(device_platform_id=adapter.platform_map[platform], version=version).id except SoftwareLCM.DoesNotExist: adapter.job.logger.info(f"Creating Version {version} for {platform}.") os_ver = SoftwareLCM( - device_platform=platform_obj, + device_platform_id=adapter.platform_map[platform], version=version, ) os_ver.validated_save() diff --git a/nautobot_ssot/tests/dna_center/test_models_nautobot.py b/nautobot_ssot/tests/dna_center/test_models_nautobot.py index 3cd39b65..5d011227 100644 --- a/nautobot_ssot/tests/dna_center/test_models_nautobot.py +++ b/nautobot_ssot/tests/dna_center/test_models_nautobot.py @@ -12,6 +12,7 @@ Location, LocationType, Manufacturer, + Platform, ) from nautobot.extras.models import Role, Status from nautobot.tenancy.models import Tenant @@ -290,6 +291,8 @@ def setUp(self): self.adapter.device_map = {} self.adapter.floor_map = {} self.adapter.site_map = {} + ios_platform = Platform.objects.get_or_create(name="IOS", network_driver="cisco_ios")[0] + self.adapter.platform_map = {"cisco_ios": ios_platform.id} self.adapter.status_map = {"Active": self.status_active.id} self.adapter.tenant_map = {"G&A": self.ga_tenant.id}