Consolidation of variables parsing throughout codebase

* Remove attempted support of key=value pattern, because
  it is not actually allowed in practice
* Have variables validator defer to the utils variables parser
* Prune serializers of a handful of cases that previous
  attempts at cleanup have missed
This commit is contained in:
AlanCoding
2017-10-11 14:22:17 -04:00
parent d4af743805
commit 8b41810189
4 changed files with 43 additions and 91 deletions

View File

@@ -4,8 +4,6 @@
# Python
import base64
import re
import yaml
import json
# Django
from django.utils.translation import ugettext_lazy as _
@@ -14,6 +12,9 @@ from django.core.exceptions import ValidationError
# REST framework
from rest_framework.serializers import ValidationError as RestValidationError
# AWX
from awx.main.utils import parse_yaml_or_json
def validate_pem(data, min_keys=0, max_keys=None, min_certs=0, max_certs=None):
"""
@@ -179,21 +180,9 @@ def vars_validate_or_raise(vars_str):
job templates, inventories, or hosts are either an acceptable
blank string, or are valid JSON or YAML dict
"""
if isinstance(vars_str, dict) or (isinstance(vars_str, basestring) and vars_str == '""'):
try:
parse_yaml_or_json(vars_str, silent_failure=False)
return vars_str
try:
r = json.loads((vars_str or '').strip() or '{}')
if isinstance(r, dict):
return vars_str
except ValueError:
pass
try:
r = yaml.safe_load(vars_str)
# Can be None if '---'
if isinstance(r, dict) or r is None:
return vars_str
except yaml.YAMLError:
pass
raise RestValidationError(_('Must be valid JSON or YAML.'))
except:
raise RestValidationError(_('Must be valid JSON or YAML.'))