mirror of
https://github.com/ansible/awx.git
synced 2026-05-09 18:37:36 -02:30
Notification configuration type checking
This commit is contained in:
@@ -2059,6 +2059,9 @@ class NotifierSerializer(BaseSerializer):
|
|||||||
model = Notifier
|
model = Notifier
|
||||||
fields = ('*', 'organization', 'notification_type', 'notification_configuration')
|
fields = ('*', 'organization', 'notification_type', 'notification_configuration')
|
||||||
|
|
||||||
|
type_map = {"string": str, "int": int, "bool": bool, "list": list,
|
||||||
|
"password": str, "object": dict}
|
||||||
|
|
||||||
def get_related(self, obj):
|
def get_related(self, obj):
|
||||||
res = super(NotifierSerializer, self).get_related(obj)
|
res = super(NotifierSerializer, self).get_related(obj)
|
||||||
res.update(dict(
|
res.update(dict(
|
||||||
@@ -2072,12 +2075,22 @@ class NotifierSerializer(BaseSerializer):
|
|||||||
def validate(self, attrs):
|
def validate(self, attrs):
|
||||||
notification_class = Notifier.CLASS_FOR_NOTIFICATION_TYPE[attrs['notification_type']]
|
notification_class = Notifier.CLASS_FOR_NOTIFICATION_TYPE[attrs['notification_type']]
|
||||||
missing_fields = []
|
missing_fields = []
|
||||||
|
incorrect_type_fields = []
|
||||||
for field in notification_class.init_parameters:
|
for field in notification_class.init_parameters:
|
||||||
if field not in attrs['notification_configuration']:
|
if field not in attrs['notification_configuration']:
|
||||||
missing_fields.append(field)
|
missing_fields.append(field)
|
||||||
# TODO: Type checks
|
continue
|
||||||
|
field_val = attrs['notification_configuration'][field]
|
||||||
|
field_type = notification_class.init_parameters[field]['type']
|
||||||
|
expected_type = self.type_map[field_type]
|
||||||
|
if not isinstance(field_val, expected_type):
|
||||||
|
incorrect_type_fields.append((field, field_type))
|
||||||
if missing_fields:
|
if missing_fields:
|
||||||
raise serializers.ValidationError("Missing required fields for Notification Configuration: {}".format(missing_fields))
|
error_list = ["Missing required fields for Notification Configuration: {}".format(missing_fields)]
|
||||||
|
for type_field_error in incorrect_type_fields:
|
||||||
|
error_list.append("Configuration field {} incorrect type, expected {}".format(type_field_error[0],
|
||||||
|
type_field_error[1]))
|
||||||
|
raise serializers.ValidationError(error_list)
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
class NotificationSerializer(BaseSerializer):
|
class NotificationSerializer(BaseSerializer):
|
||||||
|
|||||||
Reference in New Issue
Block a user