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

Add vlan filtering on the vm interface like the device #12007

Closed
abhi1693 opened this issue Mar 19, 2023 · 2 comments · Fixed by #12200
Closed

Add vlan filtering on the vm interface like the device #12007

abhi1693 opened this issue Mar 19, 2023 · 2 comments · Fixed by #12200
Assignees
Labels
status: accepted This issue has been accepted for implementation type: feature Introduction of new functionality to the application

Comments

@abhi1693
Copy link
Member

NetBox version

v3.4.6

Feature type

Change to existing functionality

Proposed functionality

diff --git a/netbox/virtualization/filtersets.py b/netbox/virtualization/filtersets.py
index 8f656811a..9a36fa0f6 100644
--- a/netbox/virtualization/filtersets.py
+++ b/netbox/virtualization/filtersets.py
@@ -4,7 +4,7 @@ from django.utils.translation import gettext as _
 
 from dcim.models import Device, DeviceRole, Platform, Region, Site, SiteGroup
 from extras.filtersets import LocalConfigContextFilterSet
-from ipam.models import L2VPN, VRF
+from ipam.models import L2VPN, VLAN, VRF
 from netbox.filtersets import OrganizationalModelFilterSet, NetBoxModelFilterSet
 from tenancy.filtersets import TenancyFilterSet, ContactModelFilterSet
 from utilities.filters import MultiValueCharFilter, MultiValueMACAddressFilter, TreeNodeMultipleChoiceFilter
@@ -308,6 +308,14 @@ class VMInterfaceFilterSet(NetBoxModelFilterSet):
         to_field_name='identifier',
         label=_('L2VPN'),
     )
+    vlan_id = django_filters.CharFilter(
+        method='filter_vlan_id',
+        label=_('Assigned VLAN')
+    )
+    vlan = django_filters.CharFilter(
+        method='filter_vlan',
+        label=_('Assigned VID')
+    )
 
     class Meta:
         model = VMInterface
@@ -320,3 +328,21 @@ class VMInterfaceFilterSet(NetBoxModelFilterSet):
             Q(name__icontains=value) |
             Q(description__icontains=value)
         )
+
+    def filter_vlan_id(self, queryset, name, value):
+        value = value.strip()
+        if not value:
+            return queryset
+        return queryset.filter(
+            Q(untagged_vlan_id=value) |
+            Q(tagged_vlans=value)
+        )
+
+    def filter_vlan(self, queryset, name, value):
+        value = value.strip()
+        if not value:
+            return queryset
+        return queryset.filter(
+            Q(untagged_vlan_id__vid=value) |
+            Q(tagged_vlans__vid=value)
+        )

Use case

Should be able to filter the VLANs by the vm interface

Database changes

No response

External dependencies

No response

@abhi1693 abhi1693 added the type: feature Introduction of new functionality to the application label Mar 19, 2023
@jeremystretch
Copy link
Member

Rather than replicating the existing filters, they should be refactors into a common class that can be inherited from both the DCIM and VM interface filter sets.

@jeremystretch jeremystretch added the status: needs owner This issue is tentatively accepted pending a volunteer committed to its implementation label Mar 20, 2023
@abhi1693
Copy link
Member Author

I can do that

@jeremystretch jeremystretch added status: accepted This issue has been accepted for implementation and removed status: needs owner This issue is tentatively accepted pending a volunteer committed to its implementation labels Mar 20, 2023
jeremystretch pushed a commit that referenced this issue Apr 7, 2023
* moved interface filterset under common class #12007

* lint fix
jeremystretch added a commit that referenced this issue Apr 7, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: accepted This issue has been accepted for implementation type: feature Introduction of new functionality to the application
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants