Skip to content
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

Closes #17669: Enable filtering VLANs by assigned interface #17674

Merged
merged 2 commits into from
Oct 3, 2024
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
26 changes: 26 additions & 0 deletions netbox/ipam/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,16 @@ class VLANFilterSet(NetBoxModelFilterSet, TenancyFilterSet):
to_field_name='identifier',
label=_('L2VPN'),
)
interface_id = django_filters.ModelChoiceFilter(
queryset=Interface.objects.all(),
method='filter_interface_id',
label=_('Assigned interface')
)
vminterface_id = django_filters.ModelChoiceFilter(
queryset=VMInterface.objects.all(),
method='filter_vminterface_id',
label=_('Assigned VM interface')
)

class Meta:
model = VLAN
Expand Down Expand Up @@ -1062,6 +1072,22 @@ def get_for_device(self, queryset, name, value):
def get_for_virtualmachine(self, queryset, name, value):
return queryset.get_for_virtualmachine(value)

def filter_interface_id(self, queryset, name, value):
if value is None:
return queryset.none()
return queryset.filter(
Q(interfaces_as_tagged=value) |
Q(interfaces_as_untagged=value)
)

def filter_vminterface_id(self, queryset, name, value):
if value is None:
return queryset.none()
return queryset.filter(
Q(vminterfaces_as_tagged=value) |
Q(vminterfaces_as_untagged=value)
)


class ServiceTemplateFilterSet(NetBoxModelFilterSet):
port = NumericArrayFilter(
Expand Down
40 changes: 40 additions & 0 deletions netbox/ipam/tests/test_filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1658,6 +1658,13 @@ def setUpTestData(cls):
)
Device.objects.bulk_create(devices)

interfaces = (
Interface(device=devices[0], name='Interface 1', type=InterfaceTypeChoices.TYPE_1GE_FIXED),
Interface(device=devices[1], name='Interface 2', type=InterfaceTypeChoices.TYPE_1GE_FIXED),
Interface(device=devices[2], name='Interface 3', type=InterfaceTypeChoices.TYPE_1GE_FIXED),
)
Interface.objects.bulk_create(interfaces)

cluster_groups = (
ClusterGroup(name='Cluster Group 1', slug='cluster-group-1'),
ClusterGroup(name='Cluster Group 2', slug='cluster-group-2'),
Expand All @@ -1680,6 +1687,13 @@ def setUpTestData(cls):
)
VirtualMachine.objects.bulk_create(virtual_machines)

vm_interfaces = (
VMInterface(virtual_machine=virtual_machines[0], name='VM Interface 1'),
VMInterface(virtual_machine=virtual_machines[1], name='VM Interface 2'),
VMInterface(virtual_machine=virtual_machines[2], name='VM Interface 3'),
)
VMInterface.objects.bulk_create(vm_interfaces)

groups = (
# Scoped VLAN groups
VLANGroup(name='Region 1', slug='region-1', scope=regions[0]),
Expand Down Expand Up @@ -1773,6 +1787,22 @@ def setUpTestData(cls):
)
VLAN.objects.bulk_create(vlans)

# Assign VLANs to device interfaces
jeremystretch marked this conversation as resolved.
Show resolved Hide resolved
interfaces[0].untagged_vlan = vlans[0]
interfaces[0].tagged_vlans.add(vlans[1])
interfaces[1].untagged_vlan = vlans[2]
interfaces[1].tagged_vlans.add(vlans[3])
interfaces[2].untagged_vlan = vlans[4]
interfaces[2].tagged_vlans.add(vlans[5])

# Assign VLANs to VM interfaces
vm_interfaces[0].untagged_vlan = vlans[0]
vm_interfaces[0].tagged_vlans.add(vlans[1])
vm_interfaces[1].untagged_vlan = vlans[2]
vm_interfaces[1].tagged_vlans.add(vlans[3])
vm_interfaces[2].untagged_vlan = vlans[4]
vm_interfaces[2].tagged_vlans.add(vlans[5])

def test_q(self):
params = {'q': 'foobar1'}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)
Expand Down Expand Up @@ -1857,6 +1887,16 @@ def test_available_at_site(self):
params = {'available_at_site': site_id}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 5) # 4 scoped + 1 global group + 1 global

def test_interface(self):
interface_id = Interface.objects.first().pk
params = {'interface_id': interface_id}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)

def test_vminterface(self):
vminterface_id = VMInterface.objects.first().pk
params = {'vminterface_id': vminterface_id}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1)


class ServiceTemplateTestCase(TestCase, ChangeLoggedFilterSetTests):
queryset = ServiceTemplate.objects.all()
Expand Down