Skip to content

Commit

Permalink
fix: 🐛 Fix SoftwareVersion assignment during Device update.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdrew82 committed Dec 11, 2024
1 parent d27068b commit a53b676
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
1 change: 1 addition & 0 deletions changes/626.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed SoftwareVersion update on Devices in DNA Center integration.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -63,6 +64,7 @@ class NautobotAdapter(Adapter):
site_map = {}
floor_map = {}
device_map = {}
platform_map = {}
port_map = {}
namespace_map = {}
prefix_map = {}
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"]],
Expand Down Expand Up @@ -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"]]
Expand All @@ -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"})
Expand Down
5 changes: 2 additions & 3 deletions nautobot_ssot/integrations/dna_center/utils/nautobot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 3 additions & 0 deletions nautobot_ssot/tests/dna_center/test_models_nautobot.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Location,
LocationType,
Manufacturer,
Platform,
)
from nautobot.extras.models import Role, Status
from nautobot.tenancy.models import Tenant
Expand Down Expand Up @@ -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}

Expand Down

0 comments on commit a53b676

Please sign in to comment.