Skip to content

Optimize VLAN Adapter #350

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 18 commits into from
Apr 8, 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
2 changes: 2 additions & 0 deletions changes/278.changed
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Optimized VLAN loading into diffsync from Nautbot
Improved error handling when creating VLANs
2 changes: 1 addition & 1 deletion docs/dev/dev_environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -466,4 +466,4 @@ To run an individual test, you can run any or all of the following:
➜ invoke unittest
➜ invoke ruff
➜ invoke pylint
```
```
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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)

Expand Down