From 75cca21525f0e968dac9c5cf812741b3aa6c12a5 Mon Sep 17 00:00:00 2001 From: Matthew Jones Date: Tue, 9 Sep 2014 15:08:49 -0400 Subject: [PATCH] Survey mode validation when launching job templates --- awx/api/views.py | 15 ++++++++++-- awx/main/models/jobs.py | 53 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/awx/api/views.py b/awx/api/views.py index d0fbe98f9f..b678fd48c1 100644 --- a/awx/api/views.py +++ b/awx/api/views.py @@ -1347,12 +1347,22 @@ class JobTemplateLaunch(GenericAPIView): data['can_start_without_user_input'] = obj.can_start_without_user_input() data['passwords_needed_to_start'] = obj.passwords_needed_to_start data['ask_variables_on_launch'] = obj.ask_variables_on_launch + data['variables_needed_to_start'] = obj.variables_needed_to_start return Response(data) def post(self, request, *args, **kwargs): obj = self.get_object() if not request.user.can_access(self.model, 'start', obj): 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() result = new_job.signal_start(**request.DATA) if not result: @@ -1385,9 +1395,10 @@ class JobTemplateSurveySpec(GenericAPIView): def post(self, request, *args, **kwargs): 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() - print request.DATA + obj.survey_spec = json.dumps(request.DATA) + obj.save() return Response() class JobTemplateActivityStreamList(SubListAPIView): diff --git a/awx/main/models/jobs.py b/awx/main/models/jobs.py index 81b665cbe6..15fa47e154 100644 --- a/awx/main/models/jobs.py +++ b/awx/main/models/jobs.py @@ -211,6 +211,59 @@ class JobTemplate(UnifiedJobTemplate, JobOptions): needed.append(pw) 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): return self.can_start_without_user_input()