Skip to content

Commit

Permalink
[AAP-6433] Add sorting to Rule Audit endpoints (#558)
Browse files Browse the repository at this point in the history
Add sorting to the following endpoints using DRF's OrderingFilter module:

  * /api/eda/v1/audit-rules/
  * /api/eda/v1/audit-rules/<audit_rule_id>/actions/
  * /api/eda/v1/audit-rules/<audit_rule_id>/events/
  * /api/eda/v1/audit-events/

Solves: AAP-6433
  • Loading branch information
Dostonbek1 authored Dec 6, 2023
1 parent 34834b2 commit afa5561
Show file tree
Hide file tree
Showing 2 changed files with 238 additions and 39 deletions.
36 changes: 33 additions & 3 deletions src/aap_eda/api/views/rulebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
)
from rest_framework import status, viewsets
from rest_framework.decorators import action
from rest_framework.filters import OrderingFilter
from rest_framework.response import Response

from aap_eda.api import filters, serializers
Expand Down Expand Up @@ -204,8 +205,15 @@ class AuditRuleViewSet(
viewsets.ReadOnlyModelViewSet,
):
queryset = models.AuditRule.objects.all()
filter_backends = (defaultfilters.DjangoFilterBackend,)
filter_backends = (defaultfilters.DjangoFilterBackend, OrderingFilter)
filterset_class = filters.AuditRuleFilter
ordering_fields = [
"id",
"name",
"status",
"activation_instance__name",
"fired_at",
]

def get_serializer_class(self):
if self.action == "retrieve":
Expand Down Expand Up @@ -240,6 +248,14 @@ def get_serializer_class(self):
detail=False,
queryset=models.AuditAction.objects.order_by("id"),
filterset_class=filters.AuditRuleActionFilter,
ordering_fields=[
"id",
"name",
"status",
"url",
"fired_at",
"rule_fired_at",
],
rbac_action=Action.READ,
url_path="(?P<id>[^/.]+)/actions",
)
Expand Down Expand Up @@ -275,6 +291,13 @@ def actions(self, _request, id):
detail=False,
queryset=models.AuditEvent.objects.order_by("id"),
filterset_class=filters.AuditRuleEventFilter,
ordering_fields=[
"id",
"source_name",
"source_type",
"received_at",
"rule_fired_at",
],
rbac_resource_type=ResourceType.AUDIT_EVENT,
rbac_action=Action.READ,
url_path="(?P<id>[^/.]+)/events",
Expand All @@ -292,7 +315,7 @@ def events(self, _request, id):
self.filter_queryset(audit_action.audit_events.all())
).order_by("-received_at")

results = self.paginate_queryset(eqs)
results = self.paginate_queryset(self.filter_queryset(eqs))
serializer = serializers.AuditEventSerializer(results, many=True)

return self.get_paginated_response(serializer.data)
Expand Down Expand Up @@ -323,8 +346,15 @@ class AuditEventViewSet(
):
queryset = models.AuditEvent.objects.all()
serializer_class = serializers.AuditEventSerializer
filter_backends = (defaultfilters.DjangoFilterBackend,)
filter_backends = (defaultfilters.DjangoFilterBackend, OrderingFilter)
filterset_class = filters.AuditRuleEventFilter
ordering_fields = [
"id",
"source_name",
"source_type",
"received_at",
"rule_fired_at",
]

rbac_resource_type = ResourceType.AUDIT_EVENT

Expand Down
Loading

0 comments on commit afa5561

Please sign in to comment.