mirror of
https://github.com/ansible/awx.git
synced 2026-05-17 06:17:36 -02:30
Survey mode validation when launching job templates
This commit is contained in:
@@ -1347,12 +1347,22 @@ class JobTemplateLaunch(GenericAPIView):
|
|||||||
data['can_start_without_user_input'] = obj.can_start_without_user_input()
|
data['can_start_without_user_input'] = obj.can_start_without_user_input()
|
||||||
data['passwords_needed_to_start'] = obj.passwords_needed_to_start
|
data['passwords_needed_to_start'] = obj.passwords_needed_to_start
|
||||||
data['ask_variables_on_launch'] = obj.ask_variables_on_launch
|
data['ask_variables_on_launch'] = obj.ask_variables_on_launch
|
||||||
|
data['variables_needed_to_start'] = obj.variables_needed_to_start
|
||||||
return Response(data)
|
return Response(data)
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
obj = self.get_object()
|
obj = self.get_object()
|
||||||
if not request.user.can_access(self.model, 'start', obj):
|
if not request.user.can_access(self.model, 'start', obj):
|
||||||
raise PermissionDenied()
|
raise PermissionDenied()
|
||||||
|
if obj.survey_enabled:
|
||||||
|
if request.DATA == "":
|
||||||
|
request_data = {}
|
||||||
|
else:
|
||||||
|
request_data = request.DATA
|
||||||
|
validation_errors = obj.survey_variable_validation(request_data)
|
||||||
|
if validation_errors:
|
||||||
|
return Response(dict(errors=validation_errors),
|
||||||
|
status=status.HTTP_400_BAD_REQUEST)
|
||||||
new_job = obj.create_unified_job()
|
new_job = obj.create_unified_job()
|
||||||
result = new_job.signal_start(**request.DATA)
|
result = new_job.signal_start(**request.DATA)
|
||||||
if not result:
|
if not result:
|
||||||
@@ -1385,9 +1395,10 @@ class JobTemplateSurveySpec(GenericAPIView):
|
|||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
obj = self.get_object()
|
obj = self.get_object()
|
||||||
if not request.user.can_access(self.model, 'change', obj):
|
if not request.user.can_access(self.model, 'change', obj, request.DATA):
|
||||||
raise PermissionDenied()
|
raise PermissionDenied()
|
||||||
print request.DATA
|
obj.survey_spec = json.dumps(request.DATA)
|
||||||
|
obj.save()
|
||||||
return Response()
|
return Response()
|
||||||
|
|
||||||
class JobTemplateActivityStreamList(SubListAPIView):
|
class JobTemplateActivityStreamList(SubListAPIView):
|
||||||
|
|||||||
@@ -211,6 +211,59 @@ class JobTemplate(UnifiedJobTemplate, JobOptions):
|
|||||||
needed.append(pw)
|
needed.append(pw)
|
||||||
return needed
|
return needed
|
||||||
|
|
||||||
|
@property
|
||||||
|
def variables_needed_to_start(self):
|
||||||
|
vars = []
|
||||||
|
if self.survey_enabled:
|
||||||
|
for survey_element in self.survey_spec:
|
||||||
|
if survey_element['required']:
|
||||||
|
vars.append(survey_element['variable'])
|
||||||
|
return vars
|
||||||
|
|
||||||
|
def survey_variable_validation(self, data):
|
||||||
|
errors = []
|
||||||
|
if not self.survey_enabled:
|
||||||
|
return errors
|
||||||
|
for survey_element in self.survey_spec:
|
||||||
|
if survey_element['variable'] not in data and \
|
||||||
|
survey_element['required']:
|
||||||
|
errors.append("'%s' value missing" % survey_element['variable'])
|
||||||
|
elif survey_element['type'] in ["textarea", "text"]:
|
||||||
|
if survey_element['min'] != "" and survey_element['variable'] in data and \
|
||||||
|
len(data[survey_element['variable']]) < survey_element['min']:
|
||||||
|
errors.append("'%s' value %s is too small (must be at least %s)" %
|
||||||
|
(survey_element['variable'], data[survey_element['variable']], survey_element['min']))
|
||||||
|
if survey_element['max'] != "" and survey_element['variable'] in data and \
|
||||||
|
len(data[survey_element['variable']]) > survey_element['max']:
|
||||||
|
errors.append("'%s' value %s is too large (must be no more than%s)" %
|
||||||
|
(survey_element['variable'], data[survey_element['variable']], survey_element['max']))
|
||||||
|
elif survey_element['type'] == 'integer':
|
||||||
|
if survey_element['min'] != "" and survey_element['variable'] in data and \
|
||||||
|
data[survey_element['variable']] < survey_element['min']:
|
||||||
|
errors.append("'%s' value %s is too small (must be at least %s)" %
|
||||||
|
(survey_element['variable'], data[survey_element['variable']], survey_element['min']))
|
||||||
|
if survey_element['max'] != "" and survey_element['variable'] in data and \
|
||||||
|
len(data[survey_element['variable']]) > survey_element['max']:
|
||||||
|
errors.append("'%s' value %s is too large (must be no more than%s)" %
|
||||||
|
(survey_element['variable'], data[survey_element['variable']], survey_element['max']))
|
||||||
|
elif survey_element['type'] == 'multiselect':
|
||||||
|
if survey_element['variable'] in data:
|
||||||
|
if type(data[survey_element['variable']]) != list:
|
||||||
|
errors.append("'%s' value is expected to be a list" % survey_element['variable'])
|
||||||
|
else:
|
||||||
|
for val in data[survey_element['variable']]:
|
||||||
|
if val not in survey_element['choices']:
|
||||||
|
errors.append("Value %s for %s expected to be one of %s" % (val, survey_element['variable'],
|
||||||
|
survey_element['choices']))
|
||||||
|
elif survey_element['type'] == 'multiplechoice':
|
||||||
|
if survey_element['variable'] in data:
|
||||||
|
if data[survey_element['variable']] not in survey_element['choices']:
|
||||||
|
errors.append("Value %s for %s expected to be one of %s" % (data[survey_element['variable']],
|
||||||
|
survey_element['variable'],
|
||||||
|
survey_element['choices']))
|
||||||
|
return errors
|
||||||
|
|
||||||
|
|
||||||
def _can_update(self):
|
def _can_update(self):
|
||||||
return self.can_start_without_user_input()
|
return self.can_start_without_user_input()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user