policies: fix api updating issues
Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
parent
c92a2ecbf5
commit
55f68a9197
|
@ -1,6 +1,7 @@
|
||||||
"""policy binding API Views"""
|
"""policy binding API Views"""
|
||||||
|
from typing import OrderedDict
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
from rest_framework.serializers import ModelSerializer, PrimaryKeyRelatedField
|
from rest_framework.serializers import ModelSerializer, PrimaryKeyRelatedField, ValidationError
|
||||||
from rest_framework.viewsets import ModelViewSet
|
from rest_framework.viewsets import ModelViewSet
|
||||||
from structlog.stdlib import get_logger
|
from structlog.stdlib import get_logger
|
||||||
|
|
||||||
|
@ -28,8 +29,8 @@ class PolicyBindingModelForeignKey(PrimaryKeyRelatedField):
|
||||||
# won't return anything. This is because the direct lookup
|
# won't return anything. This is because the direct lookup
|
||||||
# checks the PK of PolicyBindingModel (for example),
|
# checks the PK of PolicyBindingModel (for example),
|
||||||
# but we get given the Primary Key of the inheriting class
|
# but we get given the Primary Key of the inheriting class
|
||||||
for model in self.get_queryset().select_subclasses().all().select_related():
|
for model in self.get_queryset().select_subclasses().all():
|
||||||
if model.pk == data:
|
if str(model.pk) == data:
|
||||||
return model
|
return model
|
||||||
# as a fallback we still try a direct lookup
|
# as a fallback we still try a direct lookup
|
||||||
return self.get_queryset().get_subclass(pk=data)
|
return self.get_queryset().get_subclass(pk=data)
|
||||||
|
@ -53,9 +54,9 @@ class PolicyBindingSerializer(ModelSerializer):
|
||||||
required=True,
|
required=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
policy = PolicySerializer(required=False)
|
policy_obj = PolicySerializer(required=False, read_only=True, source="policy")
|
||||||
group = GroupSerializer(required=False)
|
group_obj = GroupSerializer(required=False, read_only=True, source="group")
|
||||||
user = UserSerializer(required=False)
|
user_obj = UserSerializer(required=False, read_only=True, source="user")
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|
||||||
|
@ -65,13 +66,26 @@ class PolicyBindingSerializer(ModelSerializer):
|
||||||
"policy",
|
"policy",
|
||||||
"group",
|
"group",
|
||||||
"user",
|
"user",
|
||||||
|
"policy_obj",
|
||||||
|
"group_obj",
|
||||||
|
"user_obj",
|
||||||
"target",
|
"target",
|
||||||
"enabled",
|
"enabled",
|
||||||
"order",
|
"order",
|
||||||
"timeout",
|
"timeout",
|
||||||
]
|
]
|
||||||
depth = 2
|
|
||||||
|
|
||||||
|
def validate(self, data: OrderedDict) -> OrderedDict:
|
||||||
|
"""Check that either policy, group or user is set."""
|
||||||
|
count = sum([bool(data["policy"]), bool(
|
||||||
|
data["group"]), bool(data["user"])])
|
||||||
|
invalid = count > 1
|
||||||
|
empty = count < 1
|
||||||
|
if invalid:
|
||||||
|
raise ValidationError("Only one of 'policy', 'group' or 'user' can be set.")
|
||||||
|
if empty:
|
||||||
|
raise ValidationError("One of 'policy', 'group' or 'user' must be set.")
|
||||||
|
return data
|
||||||
|
|
||||||
class PolicyBindingViewSet(ModelViewSet):
|
class PolicyBindingViewSet(ModelViewSet):
|
||||||
"""PolicyBinding Viewset"""
|
"""PolicyBinding Viewset"""
|
||||||
|
|
18
swagger.yaml
18
swagger.yaml
|
@ -16006,10 +16006,24 @@ definitions:
|
||||||
format: uuid
|
format: uuid
|
||||||
readOnly: true
|
readOnly: true
|
||||||
policy:
|
policy:
|
||||||
$ref: '#/definitions/Policy'
|
title: Policy
|
||||||
|
type: string
|
||||||
|
format: uuid
|
||||||
|
x-nullable: true
|
||||||
group:
|
group:
|
||||||
$ref: '#/definitions/Group'
|
title: Group
|
||||||
|
type: string
|
||||||
|
format: uuid
|
||||||
|
x-nullable: true
|
||||||
user:
|
user:
|
||||||
|
title: User
|
||||||
|
type: integer
|
||||||
|
x-nullable: true
|
||||||
|
policy_obj:
|
||||||
|
$ref: '#/definitions/Policy'
|
||||||
|
group_obj:
|
||||||
|
$ref: '#/definitions/Group'
|
||||||
|
user_obj:
|
||||||
$ref: '#/definitions/User'
|
$ref: '#/definitions/User'
|
||||||
target:
|
target:
|
||||||
title: Target
|
title: Target
|
||||||
|
|
Reference in New Issue