mirror of
https://github.com/ZwareBear/awx.git
synced 2026-05-06 16:32:01 -05:00
Merge pull request #406 from AlanCoding/variables_debt
Consolidation of variables parsing throughout codebase
This commit is contained in:
@@ -11,9 +11,6 @@ import urllib
|
||||
from collections import OrderedDict
|
||||
from dateutil import rrule
|
||||
|
||||
# PyYAML
|
||||
import yaml
|
||||
|
||||
# Django
|
||||
from django.conf import settings
|
||||
from django.contrib.auth import authenticate
|
||||
@@ -28,7 +25,7 @@ from django.utils.timezone import now
|
||||
from django.utils.functional import cached_property
|
||||
|
||||
# Django REST Framework
|
||||
from rest_framework.exceptions import ValidationError, PermissionDenied
|
||||
from rest_framework.exceptions import ValidationError, PermissionDenied, ParseError
|
||||
from rest_framework import fields
|
||||
from rest_framework import serializers
|
||||
from rest_framework import validators
|
||||
@@ -1312,6 +1309,9 @@ class HostSerializer(BaseSerializerWithVariables):
|
||||
raise serializers.ValidationError({"detail": _("Cannot create Host for Smart Inventory")})
|
||||
return value
|
||||
|
||||
def validate_variables(self, value):
|
||||
return vars_validate_or_raise(value)
|
||||
|
||||
def validate(self, attrs):
|
||||
name = force_text(attrs.get('name', self.instance and self.instance.name or ''))
|
||||
host, port = self._get_host_port_from_name(name)
|
||||
@@ -1319,19 +1319,9 @@ class HostSerializer(BaseSerializerWithVariables):
|
||||
if port:
|
||||
attrs['name'] = host
|
||||
variables = force_text(attrs.get('variables', self.instance and self.instance.variables or ''))
|
||||
try:
|
||||
vars_dict = json.loads(variables.strip() or '{}')
|
||||
vars_dict['ansible_ssh_port'] = port
|
||||
attrs['variables'] = json.dumps(vars_dict)
|
||||
except (ValueError, TypeError):
|
||||
try:
|
||||
vars_dict = yaml.safe_load(variables)
|
||||
if vars_dict is None:
|
||||
vars_dict = {}
|
||||
vars_dict['ansible_ssh_port'] = port
|
||||
attrs['variables'] = yaml.dump(vars_dict)
|
||||
except (yaml.YAMLError, TypeError):
|
||||
raise serializers.ValidationError({'variables': _('Must be valid JSON or YAML.')})
|
||||
vars_dict = parse_yaml_or_json(variables)
|
||||
vars_dict['ansible_ssh_port'] = port
|
||||
attrs['variables'] = json.dumps(vars_dict)
|
||||
|
||||
return super(HostSerializer, self).validate(attrs)
|
||||
|
||||
@@ -3326,18 +3316,11 @@ class JobLaunchSerializer(BaseSerializer):
|
||||
|
||||
extra_vars = attrs.get('extra_vars', {})
|
||||
|
||||
if isinstance(extra_vars, basestring):
|
||||
try:
|
||||
extra_vars = json.loads(extra_vars)
|
||||
except (ValueError, TypeError):
|
||||
try:
|
||||
extra_vars = yaml.safe_load(extra_vars)
|
||||
assert isinstance(extra_vars, dict)
|
||||
except (yaml.YAMLError, TypeError, AttributeError, AssertionError):
|
||||
errors['extra_vars'] = _('Must be valid JSON or YAML.')
|
||||
|
||||
if not isinstance(extra_vars, dict):
|
||||
extra_vars = {}
|
||||
try:
|
||||
extra_vars = parse_yaml_or_json(extra_vars, silent_failure=False)
|
||||
except ParseError as e:
|
||||
# Catch known user variable formatting errors
|
||||
errors['extra_vars'] = str(e)
|
||||
|
||||
if self.get_survey_enabled(obj):
|
||||
validation_errors = obj.survey_variable_validation(extra_vars)
|
||||
@@ -3411,18 +3394,11 @@ class WorkflowJobLaunchSerializer(BaseSerializer):
|
||||
|
||||
extra_vars = attrs.get('extra_vars', {})
|
||||
|
||||
if isinstance(extra_vars, basestring):
|
||||
try:
|
||||
extra_vars = json.loads(extra_vars)
|
||||
except (ValueError, TypeError):
|
||||
try:
|
||||
extra_vars = yaml.safe_load(extra_vars)
|
||||
assert isinstance(extra_vars, dict)
|
||||
except (yaml.YAMLError, TypeError, AttributeError, AssertionError):
|
||||
errors['extra_vars'] = _('Must be valid JSON or YAML.')
|
||||
|
||||
if not isinstance(extra_vars, dict):
|
||||
extra_vars = {}
|
||||
try:
|
||||
extra_vars = parse_yaml_or_json(extra_vars, silent_failure=False)
|
||||
except ParseError as e:
|
||||
# Catch known user variable formatting errors
|
||||
errors['extra_vars'] = str(e)
|
||||
|
||||
if self.get_survey_enabled(obj):
|
||||
validation_errors = obj.survey_variable_validation(extra_vars)
|
||||
|
||||
Reference in New Issue
Block a user