mirror of
https://github.com/ansible/awx.git
synced 2024-10-31 23:51:09 +03:00
Enable CTinT category-specific validation.
This commit is contained in:
parent
5d867d51f4
commit
69dc56c5a1
@ -14,5 +14,9 @@ def register(setting, **kwargs):
|
||||
settings_registry.register(setting, **kwargs)
|
||||
|
||||
|
||||
def register_validate(category, func):
|
||||
settings_registry.register_validate(category, func)
|
||||
|
||||
|
||||
def autodiscover():
|
||||
autodiscover_modules('conf', register_to=settings_registry)
|
||||
|
@ -28,6 +28,7 @@ class SettingsRegistry(object):
|
||||
if settings is None:
|
||||
from django.conf import settings
|
||||
self._registry = OrderedDict()
|
||||
self._validate_registry = {}
|
||||
self._dependent_settings = {}
|
||||
self.settings = settings
|
||||
|
||||
@ -56,6 +57,12 @@ class SettingsRegistry(object):
|
||||
for dependent_settings in self._dependent_settings.values():
|
||||
dependent_settings.discard(setting)
|
||||
|
||||
def register_validate(self, category_slug, func):
|
||||
self._validate_registry[category_slug] = func
|
||||
|
||||
def unregister_validate(self, category_slug):
|
||||
self._validate_registry.pop(category_slug, None)
|
||||
|
||||
def get_dependent_settings(self, setting):
|
||||
return self._dependent_settings.get(setting, set())
|
||||
|
||||
@ -99,6 +106,9 @@ class SettingsRegistry(object):
|
||||
setting_names.append(setting)
|
||||
return setting_names
|
||||
|
||||
def get_registered_validate_func(self, category_slug):
|
||||
return self._validate_registry.get(category_slug, None)
|
||||
|
||||
def is_setting_encrypted(self, setting):
|
||||
return bool(self._registry.get(setting, {}).get('encrypted', False))
|
||||
|
||||
|
@ -65,6 +65,14 @@ class SettingSingletonSerializer(serializers.Serializer):
|
||||
assert instance is None or not hasattr(instance, 'pk')
|
||||
super(SettingSingletonSerializer, self).__init__(instance, data, **kwargs)
|
||||
|
||||
def validate(self, attrs):
|
||||
try:
|
||||
category_slug = self.context['view'].kwargs.get('category_slug', 'all')
|
||||
except (KeyError, AttributeError):
|
||||
category_slug = ''
|
||||
custom_validate = settings_registry.get_registered_validate_func(category_slug)
|
||||
return custom_validate(self, attrs) if custom_validate else attrs
|
||||
|
||||
def get_fields(self):
|
||||
fields = super(SettingSingletonSerializer, self).get_fields()
|
||||
try:
|
||||
|
@ -309,3 +309,12 @@ def test_default_value_from_settings_with_custom_representation(reg):
|
||||
|
||||
field = reg.get_setting_field('AWX_SOME_SETTING')
|
||||
assert field.default == 'default'
|
||||
|
||||
|
||||
def test_validate_registry(reg):
|
||||
validate_func = lambda x: None
|
||||
reg.register_validate('FOO_BAR', validate_func)
|
||||
assert reg.get_registered_validate_func('FOO_BAR') == validate_func
|
||||
assert reg.get_registered_validate_func('NOT_EXIST') is None
|
||||
reg.unregister_validate('FOO_BAR')
|
||||
assert reg.get_registered_validate_func('FOO_BAR') is None
|
||||
|
@ -6,8 +6,11 @@ import os
|
||||
# Django
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
# Django REST Framework
|
||||
from rest_framework import serializers
|
||||
|
||||
# Tower
|
||||
from awx.conf import fields, register
|
||||
from awx.conf import fields, register, register_validate
|
||||
|
||||
logger = logging.getLogger('awx.main.conf')
|
||||
|
||||
@ -347,3 +350,22 @@ register(
|
||||
category=_('Logging'),
|
||||
category_slug='logging',
|
||||
)
|
||||
|
||||
|
||||
def logging_validate(serializer, attrs):
|
||||
if not serializer.instance:
|
||||
return attrs
|
||||
errors = []
|
||||
if attrs.get('LOG_AGGREGATOR_ENABLED', False):
|
||||
if not serializer.instance.LOG_AGGREGATOR_HOST and not attrs.get('LOG_AGGREGATOR_HOST', None) or\
|
||||
serializer.instance.LOG_AGGREGATOR_HOST and not attrs.get('LOG_AGGREGATOR_HOST', True):
|
||||
errors.append('Cannot enable log aggregator without providing host.')
|
||||
if not serializer.instance.LOG_AGGREGATOR_TYPE and not attrs.get('LOG_AGGREGATOR_TYPE', None) or\
|
||||
serializer.instance.LOG_AGGREGATOR_TYPE and not attrs.get('LOG_AGGREGATOR_TYPE', True):
|
||||
errors.append('Cannot enable log aggregator without providing type.')
|
||||
if errors:
|
||||
raise serializers.ValidationError(_('\n'.join(errors)))
|
||||
return attrs
|
||||
|
||||
|
||||
register_validate('logging', logging_validate)
|
||||
|
Loading…
Reference in New Issue
Block a user