diff --git a/changes/278.changed b/changes/278.changed new file mode 100644 index 00000000..201540f2 --- /dev/null +++ b/changes/278.changed @@ -0,0 +1,2 @@ +Optimized VLAN loading into diffsync from Nautbot +Improved error handling when creating VLANs \ No newline at end of file diff --git a/docs/dev/dev_environment.md b/docs/dev/dev_environment.md index 48b8337d..1ec591ff 100644 --- a/docs/dev/dev_environment.md +++ b/docs/dev/dev_environment.md @@ -466,4 +466,4 @@ To run an individual test, you can run any or all of the following: ➜ invoke unittest ➜ invoke ruff ➜ invoke pylint -``` +``` \ No newline at end of file diff --git a/nautobot_device_onboarding/diffsync/adapters/sync_devices_adapters.py b/nautobot_device_onboarding/diffsync/adapters/sync_devices_adapters.py index e0a4e0f8..201b8242 100644 --- a/nautobot_device_onboarding/diffsync/adapters/sync_devices_adapters.py +++ b/nautobot_device_onboarding/diffsync/adapters/sync_devices_adapters.py @@ -214,7 +214,6 @@ def execute_command_getter(self): raise Exception( # pylint: disable=broad-exception-raised "Platform.network_driver missing" ) - result = sync_devices_command_getter( self.job.job_result, self.job.logger.getEffectiveLevel(), diff --git a/nautobot_device_onboarding/diffsync/adapters/sync_network_data_adapters.py b/nautobot_device_onboarding/diffsync/adapters/sync_network_data_adapters.py index 2670d830..316f7dcb 100644 --- a/nautobot_device_onboarding/diffsync/adapters/sync_network_data_adapters.py +++ b/nautobot_device_onboarding/diffsync/adapters/sync_network_data_adapters.py @@ -130,9 +130,11 @@ def load_vlans(self): """ Load Vlans into the Diffsync store. - Only Vlans that were returned by the CommandGetter job should be synced. + Only Vlans that share locations with devices included in the sync should be loaded. """ - for vlan in VLAN.objects.all(): + # TODO: update this to support multiple locations per VLAN after the setting for this feature has been added. + location_ids = list(self.job.devices_to_load.values_list("location__id", flat=True)) + for vlan in VLAN.objects.filter(location__in=location_ids): network_vlan = self.vlan( adapter=self, name=vlan.name, @@ -595,7 +597,6 @@ def load_vlans(self): ) in self.job.command_getter_result.items(): # pylint: disable=too-many-nested-blocks if self.job.debug: self.job.logger.debug(f"Loading Vlans from {hostname}") - # for interface in device_data["interfaces"]: for _, interface_data in device_data["interfaces"].items(): # add tagged vlans for tagged_vlan in interface_data["tagged_vlans"]: diff --git a/nautobot_device_onboarding/diffsync/models/sync_network_data_models.py b/nautobot_device_onboarding/diffsync/models/sync_network_data_models.py index 4d0b9e5c..760f7678 100644 --- a/nautobot_device_onboarding/diffsync/models/sync_network_data_models.py +++ b/nautobot_device_onboarding/diffsync/models/sync_network_data_models.py @@ -210,18 +210,18 @@ def create(cls, adapter, ids, attrs): location = None try: location = Location.objects.get(name=ids["location__name"]) - except ObjectDoesNotExist: - adapter.job.logger.warning( + except ObjectDoesNotExist as err: + adapter.job.logger.error( f"While creating VLAN {ids['vid']} - {ids['name']}, " - f"unable to find a Location with name: {ids['location__name']}. " - "This VLAN will be created without a Location" + f"unable to find a Location with name: {ids['location__name']}." ) - except MultipleObjectsReturned: - adapter.job.logger.warning( + raise diffsync_exceptions.ObjectNotCreated(err) + except MultipleObjectsReturned as err: + adapter.job.logger.error( f"While creating VLAN {ids['vid']} - {ids['name']}, " - f"Multiple Locations were found with name: {ids['location__name']}. " - "This VLAN will be created without a Location" + f"Multiple Locations were found with name: {ids['location__name']}." ) + raise diffsync_exceptions.ObjectNotCreated(err) try: vlan = VLAN( name=ids["name"], @@ -232,6 +232,7 @@ def create(cls, adapter, ids, attrs): vlan.validated_save() except ValidationError as err: adapter.job.logger.error(f"VLAN {vlan} failed to create, {err}") + raise diffsync_exceptions.ObjectNotCreated(err) return super().create(adapter, ids, attrs)