From 92fd5211b44e7e13a20cdbbee668eb8665b7e1a7 Mon Sep 17 00:00:00 2001 From: Terje Kvernes Date: Fri, 24 Jan 2025 15:45:41 +0100 Subject: [PATCH] Add filtering support, untested, --- mreg/api/v1/filters.py | 33 ++++++++++++++++++++++++++ mreg/api/v1/views_network_policy.py | 10 ++++++-- mreg/migrations/0014_network_policy.py | 1 + mreg/models/network_policy.py | 1 + 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/mreg/api/v1/filters.py b/mreg/api/v1/filters.py index fcf55962..90298b0e 100644 --- a/mreg/api/v1/filters.py +++ b/mreg/api/v1/filters.py @@ -16,6 +16,8 @@ from mreg.models.zone import (ForwardZone, ForwardZoneDelegation, NameServer, ReverseZone, ReverseZoneDelegation) +from mreg.models.network_policy import (NetworkPolicyAttribute, NetworkPolicy, Community) + from netaddr import IPNetwork, AddrFormatError mreg_log = structlog.getLogger(__name__) @@ -400,3 +402,34 @@ class Meta: **HOST_FIELDS, **CREATED_UPDATED, } + +class NetworkPolicyAttributeFilterSet(filters.FilterSet): + class Meta: + model = NetworkPolicyAttribute + fields = { + "id": INT_OPERATORS, + "name": STRING_OPERATORS, + "description": STRING_OPERATORS, + **CREATED_UPDATED, + } + +class NetworkPolicyFilterSet(filters.FilterSet): + class Meta: + model = NetworkPolicy + fields = { + "id": INT_OPERATORS, + "name": STRING_OPERATORS, + "description": STRING_OPERATORS, + **CREATED_UPDATED, + } + +class CommunityFilterSet(filters.FilterSet): + class Meta: + model = Community + fields = { + "id": INT_OPERATORS, + "policy": INT_OPERATORS, + "name": STRING_OPERATORS, + "description": STRING_OPERATORS, + **CREATED_UPDATED, + } \ No newline at end of file diff --git a/mreg/api/v1/views_network_policy.py b/mreg/api/v1/views_network_policy.py index 352a41e0..dddfaa91 100644 --- a/mreg/api/v1/views_network_policy.py +++ b/mreg/api/v1/views_network_policy.py @@ -10,6 +10,9 @@ CommunitySerializer, HostSerializer, ) + +from mreg.api.v1.filters import NetworkPolicyAttributeFilterSet, NetworkPolicyFilterSet, CommunityFilterSet, HostFilterSet + from mreg.api.v1.views import JSONContentTypeMixin from mreg.api.permissions import IsGrantedNetGroupRegexPermission, IsSuperOrNetworkAdminMember @@ -20,6 +23,7 @@ class NetworkPolicyList(JSONContentTypeMixin, generics.ListCreateAPIView): serializer_class = NetworkPolicySerializer permission_classes = (IsGrantedNetGroupRegexPermission,) ordering_fields = ('id',) + filterset_class = NetworkPolicyFilterSet def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) @@ -55,6 +59,7 @@ class NetworkPolicyAttributeList(JSONContentTypeMixin, generics.ListCreateAPIVie queryset = NetworkPolicyAttribute.objects.all() serializer_class = NetworkPolicyAttributeSerializer permission_classes = (IsSuperOrNetworkAdminMember,) + filterset_class = NetworkPolicyAttributeFilterSet ordering_fields = ('id',) class NetworkPolicyAttributeDetail(JSONContentTypeMixin, generics.RetrieveUpdateDestroyAPIView): @@ -65,6 +70,7 @@ class NetworkPolicyAttributeDetail(JSONContentTypeMixin, generics.RetrieveUpdate class NetworkCommunityList(JSONContentTypeMixin, generics.ListCreateAPIView): serializer_class = CommunitySerializer permission_classes = (IsSuperOrNetworkAdminMember,) + filterset_class = CommunityFilterSet def get_queryset(self): policy_pk = self.kwargs.get('pk') @@ -125,7 +131,7 @@ class NetworkCommunityHostList(HostInCommunityMixin, generics.ListCreateAPIView) def get_queryset(self): # Retrieve community via helper. The policy is not used directly here. _, community = self.get_policy_and_community() - return Host.objects.filter(network_community=community) + return HostFilterSet(data=self.request.GET, queryset=Host.objects.filter(network_community=community).order_by('id')).qs def create(self, request, *args, **kwargs): _, community = self.get_policy_and_community() @@ -150,7 +156,7 @@ class NetworkCommunityHostDetail(HostInCommunityMixin, generics.RetrieveDestroyA def get_queryset(self): _, community = self.get_policy_and_community() - return Host.objects.filter(network_community=community) + return HostFilterSet(data=self.request.GET, queryset=Host.objects.filter(network_community=community).order_by('id')).qs def get_object(self): queryset = self.get_queryset() diff --git a/mreg/migrations/0014_network_policy.py b/mreg/migrations/0014_network_policy.py index 6939d523..cf5b4d7b 100644 --- a/mreg/migrations/0014_network_policy.py +++ b/mreg/migrations/0014_network_policy.py @@ -31,6 +31,7 @@ class Migration(migrations.Migration): ('created_at', models.DateTimeField(auto_now_add=True)), ('updated_at', models.DateTimeField(auto_now=True)), ('name', models.CharField(help_text='Name of the network policy.', max_length=100, unique=True)), + ('description', models.TextField(blank=True, help_text='Description of the network policy.')), ], options={ 'abstract': False, diff --git a/mreg/models/network_policy.py b/mreg/models/network_policy.py index 55fb64de..f0c647ab 100644 --- a/mreg/models/network_policy.py +++ b/mreg/models/network_policy.py @@ -16,6 +16,7 @@ class NetworkPolicy(BaseModel): Represents a network policy which consists of a set of NetworkPolicyAttributes. """ name = models.CharField(max_length=100, unique=True, help_text="Name of the network policy.") + description = models.TextField(blank=True, help_text="Description of the network policy.") attributes = models.ManyToManyField( NetworkPolicyAttribute, through='NetworkPolicyAttributeValue',