Skip to content

Commit

Permalink
Add filtering support, untested,
Browse files Browse the repository at this point in the history
  • Loading branch information
terjekv committed Jan 24, 2025
1 parent ceeda90 commit 92fd521
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
33 changes: 33 additions & 0 deletions mreg/api/v1/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down Expand Up @@ -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,
}
10 changes: 8 additions & 2 deletions mreg/api/v1/views_network_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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):
Expand All @@ -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')
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
1 change: 1 addition & 0 deletions mreg/migrations/0014_network_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions mreg/models/network_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

1 comment on commit 92fd521

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.