policies: add policy_obj to Binding API
This commit is contained in:
parent
66b3635648
commit
2417d5a59e
|
@ -65,7 +65,10 @@ class FlowImporter:
|
||||||
return value
|
return value
|
||||||
|
|
||||||
for key, value in attrs.items():
|
for key, value in attrs.items():
|
||||||
if isinstance(value, (list, dict)):
|
if isinstance(value, dict):
|
||||||
|
for idx, _inner_key in enumerate(value):
|
||||||
|
value[_inner_key] = updater(value[_inner_key])
|
||||||
|
elif isinstance(value, list):
|
||||||
for idx, _inner_value in enumerate(value):
|
for idx, _inner_value in enumerate(value):
|
||||||
attrs[key][idx] = updater(_inner_value)
|
attrs[key][idx] = updater(_inner_value)
|
||||||
else:
|
else:
|
||||||
|
@ -97,6 +100,10 @@ class FlowImporter:
|
||||||
# Because a model might have multiple unique columns, we chain all identifiers together
|
# Because a model might have multiple unique columns, we chain all identifiers together
|
||||||
# to create an OR query.
|
# to create an OR query.
|
||||||
updated_identifiers = self.__update_pks_for_attrs(entry.identifiers)
|
updated_identifiers = self.__update_pks_for_attrs(entry.identifiers)
|
||||||
|
for key, value in list(updated_identifiers.items()):
|
||||||
|
if isinstance(value, dict) and "pk" in value:
|
||||||
|
del updated_identifiers[key]
|
||||||
|
updated_identifiers[f"{key}"] = value["pk"]
|
||||||
existing_models = model.objects.filter(
|
existing_models = model.objects.filter(
|
||||||
self.__query_from_identifier(updated_identifiers)
|
self.__query_from_identifier(updated_identifiers)
|
||||||
)
|
)
|
||||||
|
|
|
@ -41,30 +41,6 @@ class PolicyBindingModelForeignKey(PrimaryKeyRelatedField):
|
||||||
return correct_model.pk
|
return correct_model.pk
|
||||||
|
|
||||||
|
|
||||||
class PolicyBindingSerializer(ModelSerializer):
|
|
||||||
"""PolicyBinding Serializer"""
|
|
||||||
|
|
||||||
# Because we're not interested in the PolicyBindingModel's PK but rather the subclasses PK,
|
|
||||||
# we have to manually declare this field
|
|
||||||
target = PolicyBindingModelForeignKey(
|
|
||||||
queryset=PolicyBindingModel.objects.select_subclasses(),
|
|
||||||
required=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
|
|
||||||
model = PolicyBinding
|
|
||||||
fields = ["pk", "policy", "target", "enabled", "order", "timeout"]
|
|
||||||
|
|
||||||
|
|
||||||
class PolicyBindingViewSet(ModelViewSet):
|
|
||||||
"""PolicyBinding Viewset"""
|
|
||||||
|
|
||||||
queryset = PolicyBinding.objects.all()
|
|
||||||
serializer_class = PolicyBindingSerializer
|
|
||||||
filterset_fields = ["policy", "target", "enabled", "order", "timeout"]
|
|
||||||
|
|
||||||
|
|
||||||
class PolicySerializer(ModelSerializer):
|
class PolicySerializer(ModelSerializer):
|
||||||
"""Policy Serializer"""
|
"""Policy Serializer"""
|
||||||
|
|
||||||
|
@ -74,10 +50,17 @@ class PolicySerializer(ModelSerializer):
|
||||||
"""Get object type so that we know which API Endpoint to use to get the full object"""
|
"""Get object type so that we know which API Endpoint to use to get the full object"""
|
||||||
return obj._meta.object_name.lower().replace("policy", "")
|
return obj._meta.object_name.lower().replace("policy", "")
|
||||||
|
|
||||||
|
def to_representation(self, instance: Policy):
|
||||||
|
# pyright: reportGeneralTypeIssues=false
|
||||||
|
if instance.__class__ == Policy:
|
||||||
|
return super().to_representation(instance)
|
||||||
|
return instance.serializer(instance=instance).data
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|
||||||
model = Policy
|
model = Policy
|
||||||
fields = ["pk"] + GENERAL_FIELDS + ["__type__"]
|
fields = ["pk"] + GENERAL_FIELDS + ["__type__"]
|
||||||
|
depth = 3
|
||||||
|
|
||||||
|
|
||||||
class PolicyViewSet(ReadOnlyModelViewSet):
|
class PolicyViewSet(ReadOnlyModelViewSet):
|
||||||
|
@ -88,3 +71,29 @@ class PolicyViewSet(ReadOnlyModelViewSet):
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
return Policy.objects.select_subclasses()
|
return Policy.objects.select_subclasses()
|
||||||
|
|
||||||
|
|
||||||
|
class PolicyBindingSerializer(ModelSerializer):
|
||||||
|
"""PolicyBinding Serializer"""
|
||||||
|
|
||||||
|
# Because we're not interested in the PolicyBindingModel's PK but rather the subclasses PK,
|
||||||
|
# we have to manually declare this field
|
||||||
|
target = PolicyBindingModelForeignKey(
|
||||||
|
queryset=PolicyBindingModel.objects.select_subclasses(),
|
||||||
|
required=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
policy_obj = PolicySerializer(read_only=True, source="policy")
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
|
||||||
|
model = PolicyBinding
|
||||||
|
fields = ["pk", "policy", "policy_obj", "target", "enabled", "order", "timeout"]
|
||||||
|
|
||||||
|
|
||||||
|
class PolicyBindingViewSet(ModelViewSet):
|
||||||
|
"""PolicyBinding Viewset"""
|
||||||
|
|
||||||
|
queryset = PolicyBinding.objects.all()
|
||||||
|
serializer_class = PolicyBindingSerializer
|
||||||
|
filterset_fields = ["policy", "target", "enabled", "order", "timeout"]
|
||||||
|
|
|
@ -7058,6 +7058,8 @@ definitions:
|
||||||
title: Policy
|
title: Policy
|
||||||
type: string
|
type: string
|
||||||
format: uuid
|
format: uuid
|
||||||
|
policy_obj:
|
||||||
|
$ref: '#/definitions/Policy'
|
||||||
target:
|
target:
|
||||||
title: Target
|
title: Target
|
||||||
type: string
|
type: string
|
||||||
|
|
Reference in New Issue