Skip to content

Commit

Permalink
Fix filtering for HostPolicy post LCI cleanups. (#536)
Browse files Browse the repository at this point in the history
* Also fix filtering for HostPolicy.
* Use explicit field operator management for name filtering on atoms and roles. I'm not sure why this is needed, but it is. :(
  • Loading branch information
terjekv authored May 24, 2024
1 parent 3fc4b79 commit fa6ca20
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
3 changes: 3 additions & 0 deletions hostpolicy/api/v1/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ def test_get_atoms_filtered_200_ok(self):
ret2 = self.assert_get('/api/v1/hostpolicy/atoms/?name=testatom2')
self.assertEqual(ret2.json()['count'], 1)

ret2 = self.assert_get('/api/v1/hostpolicy/atoms/?name=dontexist')
self.assertEqual(ret2.json()['count'], 0)

ret2 = self.assert_get('/api/v1/hostpolicy/atoms/?name__contains=testatom2')
self.assertEqual(ret2.json()['count'], 1)

Expand Down
26 changes: 11 additions & 15 deletions hostpolicy/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,26 @@

from . import serializers

# For some reason the name field for filtersets for HostPolicyAtom and HostPolicyRole does
# not support operators (e.g. __contains, __regex) in the same way as other fields. Yes,
# the name field is a LowerCaseCharField, but the operators work fine in mreg proper.
# To resolve this issue, we create custom fields for the filtersets that use the name field.

# We can't use fields = '__all__' due to our use of LCI-fields:
# https://github.com/unioslo/mreg/issues/489#issuecomment-1610209358
# For the HostPolicyAtom model, this applies to the field "name"
class HostPolicyAtomFilterSet(rest_filters.FilterSet):
name__contains = rest_filters.CharFilter(field_name="name", lookup_expr="contains")
name__regex = rest_filters.CharFilter(field_name="name", lookup_expr="regex")

class Meta:
model = HostPolicyAtom
fields = {
'name': ['exact', 'regex', 'contains'],
}
fields = "__all__"


# We can't use fields = '__all__' due to our use of LCI-fields:
# https://github.com/unioslo/mreg/issues/489#issuecomment-1610209358
# For the HostPolicyRole model, this applies to the field "name"
class HostPolicyRoleFilterSet(rest_filters.FilterSet):
name__contains = rest_filters.CharFilter(field_name="name", lookup_expr="contains")
name__regex = rest_filters.CharFilter(field_name="name", lookup_expr="regex")
class Meta:
model = HostPolicyRole
fields = {
'name': ['exact', 'regex', 'contains'],
'atoms__name': ['exact', 'regex', 'contains'],
'hosts__name': ['exact', 'regex', 'contains'],
'labels__name': ['exact', 'regex', 'contains'],
}
fields = "__all__"

class HostPolicyAtomLogMixin(HistoryLog):

Expand Down
3 changes: 0 additions & 3 deletions mreg/api/v1/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,6 @@ class Meta:
model = PtrOverride
fields = "__all__"

# Not that due to the email field being a CIEmailField, filtering on it
# with lookups (email__contains=..., email__regex=..., etc) won't work.
# This field is inherited from BaseZone.

class ReverseZoneFilterSet(filters.FilterSet):
network = CIDRFieldExactFilter(field_name="network")
Expand Down

0 comments on commit fa6ca20

Please sign in to comment.