Skip to content

Update jobs.py to support Dynamic Groups #378

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions nautobot_device_onboarding/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
)
from nautobot.extras.models import (
CustomField,
DynamicGroup,
Role,
SecretsGroup,
SecretsGroupAssociation,
Expand Down Expand Up @@ -642,6 +643,11 @@ class Meta:
required=False,
description="Only update devices with the selected platform.",
)
dynamic_group = ObjectVar(
model=DynamicGroup,
required=False,
description="Only update devices that belong to this Dynamic Group.",
)

def load_source_adapter(self):
"""Load network data adapter."""
Expand All @@ -668,6 +674,7 @@ def run(
devices,
device_role,
platform,
dynamic_group,
sync_vlans,
sync_vrfs,
sync_cables,
Expand All @@ -687,6 +694,7 @@ def run(
self.devices = devices
self.device_role = device_role
self.platform = platform
self.dynamic_group = dynamic_group
self.sync_vlans = sync_vlans
self.sync_vrfs = sync_vrfs
self.sync_cables = sync_cables
Expand Down Expand Up @@ -727,6 +735,12 @@ def run(
device_filter["platform"] = platform
if device_filter: # prevent all devices from being returned by an empty filter
self.filtered_devices = Device.objects.filter(**device_filter)
if self.dynamic_group:
# Get all Device IDs in that DynamicGroup
group_device_ids = self.dynamic_group.members.values_list("id", flat=True)
# Overwrite (or set) the id__in filter so we only get devices in that group
device_filter["id__in"] = list(group_device_ids)

else:
self.logger.error("No device filter options were provided, no devices will be synced.")

Expand Down