From a36a141141ace1a1ba6ee759b002562d0a91b615 Mon Sep 17 00:00:00 2001 From: AlanCoding Date: Thu, 31 Aug 2017 23:10:09 -0400 Subject: [PATCH] fuller validation for host_filter --- awx/api/serializers.py | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/awx/api/serializers.py b/awx/api/serializers.py index 88ea77b783..0e6dd72863 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -1171,15 +1171,30 @@ class InventorySerializer(BaseSerializerWithVariables): ret['organization'] = None return ret + def validate_host_filter(self, host_filter): + if host_filter: + try: + SmartFilter().query_from_string(host_filter) + except RuntimeError, e: + raise models.base.ValidationError(e) + return host_filter + def validate(self, attrs): - kind = attrs.get('kind', 'standard') - if kind == 'smart': - host_filter = attrs.get('host_filter') - if host_filter is not None: - try: - SmartFilter().query_from_string(host_filter) - except RuntimeError, e: - raise models.base.ValidationError(e) + kind = None + if 'kind' in attrs: + kind = attrs['kind'] + elif self.instance: + kind = self.instance.kind + + host_filter = None + if 'host_filter' in attrs: + host_filter = attrs['host_filter'] + elif self.instance: + host_filter = self.instance.host_filter + + if kind == 'smart' and not host_filter: + raise serializers.ValidationError({'host_filter': _( + 'Smart inventories must specify host_filter')}) return super(InventorySerializer, self).validate(attrs)