diff --git a/awx/ui/static/js/helpers/JobSubmission.js b/awx/ui/static/js/helpers/JobSubmission.js index 440db4f66d..a79c276f2d 100644 --- a/awx/ui/static/js/helpers/JobSubmission.js +++ b/awx/ui/static/js/helpers/JobSubmission.js @@ -105,15 +105,17 @@ function($location, Wait, GetBasePath, LookUpInit, JobTemplateForm, CredentialLi -.factory('CreateLaunchDialog', ['$compile', 'Rest', 'GetBasePath', 'TextareaResize', 'CreateDialog', 'GenerateForm', 'JobVarsPromptForm', 'Wait', - function($compile, Rest, GetBasePath, TextareaResize,CreateDialog, GenerateForm, JobVarsPromptForm, Wait) { +.factory('CreateLaunchDialog', ['$compile', 'Rest', 'GetBasePath', 'TextareaResize', 'CreateDialog', 'GenerateForm', + 'JobVarsPromptForm', 'Wait', 'ProcessErrors', 'ToJSON', + function($compile, Rest, GetBasePath, TextareaResize,CreateDialog, GenerateForm, + JobVarsPromptForm, Wait, ProcessErrors, ToJSON) { return function(params) { var buttons, scope = params.scope, html = params.html, - // callback = params.callback, - // job = params.job, - // url = params.url, + job_launch_data = {}, + callback = params.callback || 'PlaybookLaunchFinished', + url = params.url, e; html+='
job_launch_form.$valid = {{job_launch_form.$valid}}
'; @@ -122,10 +124,46 @@ function($location, Wait, GetBasePath, LookUpInit, JobTemplateForm, CredentialLi e = angular.element(document.getElementById('password-modal')); $compile(e)(scope); + scope.jobLaunchFormAccept = function(){ + if(scope.passwords_needed_to_start.length>0){ + scope.passwords.forEach(function(password) { + job_launch_data[password] = scope[password]; + }); + } + if(scope.prompt_for_vars===true){ + job_launch_data.extra_vars = ToJSON(scope.parseType, scope.variables, true); + } + if(scope.survey_enabled===true){ + for ( var fld in scope.job_launch_form){ + if(scope[fld]){ + job_launch_data[fld] = scope[fld]; + } + } + } + + Rest.setUrl(url); + Rest.post(job_launch_data) + .success(function(data) { + Wait('stop'); + $('#password-modal').dialog('close'); + scope.$emit(callback, data); + scope.$destroy(); + }) + .error(function(data, status) { + ProcessErrors(scope, data, status, null, { hdr: 'Error!', + msg: 'Failed updating job ' + scope.job_template_id + ' with variables. PUT returned: ' + status }); + }); + + }; + + + + + buttons = [{ label: "Cancel", onClick: function() { - scope.passwordCancel(); + $('password-modal').close(); }, icon: "fa-times", "class": "btn btn-default", @@ -133,7 +171,7 @@ function($location, Wait, GetBasePath, LookUpInit, JobTemplateForm, CredentialLi },{ label: "Continue", onClick: function() { - scope.passwordAccept(); + scope.jobLaunchFormAccept(); }, icon: "fa-check", "class": "btn btn-primary", @@ -175,6 +213,7 @@ function($location, Wait, GetBasePath, LookUpInit, JobTemplateForm, CredentialLi return function(params) { var scope = params.scope, callback = params.callback || 'PasswordsAccepted', + url = params.url, form = CredentialForm, acceptedPasswords = {}, fld, field, @@ -239,7 +278,7 @@ function($location, Wait, GetBasePath, LookUpInit, JobTemplateForm, CredentialLi // $('#password-modal').empty().html(buildHtml); // e = angular.element(document.getElementById('password-modal')); // $compile(e)(scope); - scope.$emit(callback, html); + scope.$emit(callback, html, url); // CreateLaunchDialog({scope: scope}) // buttons = [{ // label: "Cancel", @@ -320,9 +359,9 @@ function($location, Wait, GetBasePath, LookUpInit, JobTemplateForm, CredentialLi }]) .factory('PromptForVars', ['$compile', 'Rest', 'GetBasePath', 'TextareaResize', 'CreateLaunchDialog', 'GenerateForm', 'JobVarsPromptForm', 'Wait', - 'ParseVariableString', 'ToJSON', 'ProcessErrors', + 'ParseVariableString', 'ToJSON', 'ProcessErrors', '$routeParams' , function($compile, Rest, GetBasePath, TextareaResize,CreateLaunchDialog, GenerateForm, JobVarsPromptForm, Wait, - ParseVariableString, ToJSON, ProcessErrors) { + ParseVariableString, ToJSON, ProcessErrors, $routeParams) { return function(params) { var // parent_scope = params.scope, @@ -330,27 +369,61 @@ function($location, Wait, GetBasePath, LookUpInit, JobTemplateForm, CredentialLi callback = params.callback, job = params.job, url = params.url, + vars_url = GetBasePath('job_templates')+scope.job_template_id + '/', // e, helpContainer, html = params.html || ""; - html += GenerateForm.buildHTML(JobVarsPromptForm, { mode: 'edit', modal: true, scope: scope }); - scope.helpContainer = "
\n" + - "" + - " click for help
\n"; - scope.helpText = "

After defining any extra variables, click Continue to start the job. Otherwise, click cancel to abort.

" + - "

Extra variables are passed as command line variables to the playbook run. It is equivalent to the -e or --extra-vars " + - "command line parameter for ansible-playbook. Provide key/value pairs using either YAML or JSON.

" + - "JSON:
\n" + - "
{
\"somevar\": \"somevalue\",
\"password\": \"magic\"
}
\n" + - "YAML:
\n" + - "
---
somevar: somevalue
password: magic
\n" + - "
esc or click to close
\n"; + function buildHtml(extra_vars){ + // html += GenerateForm.buildHTML(JobVarsPromptForm, { mode: 'edit', scope: scope }); + // scope.helpContainer = "
\n" + + // "" + + // " click for help
\n"; + + // scope.helpText = "

After defining any extra variables, click Continue to start the job. Otherwise, click cancel to abort.

" + + // "

Extra variables are passed as command line variables to the playbook run. It is equivalent to the -e or --extra-vars " + + // "command line parameter for ansible-playbook. Provide key/value pairs using either YAML or JSON.

" + + // "JSON:
\n" + + // "
{
\"somevar\": \"somevalue\",
\"password\": \"magic\"
}
\n" + + // "YAML:
\n" + + // "
---
somevar: somevalue
password: magic
\n" + + // "
esc or click to close
\n"; + + //
+ html+= '
'+ + '
Parse as: YAML'+ + ' JSON'+ + '
'+ + ''+ + ''+ + '
'+ + '
'; + //
+ + scope.variables = ParseVariableString(extra_vars); + scope.parseType = 'yaml'; + scope.$emit(callback, html, url); + } + + Rest.setUrl(vars_url); + Rest.get() + .success(function (data) { + buildHtml(data.extra_vars); + + }) + .error(function (data, status) { + ProcessErrors(scope, data, status, { hdr: 'Error!', + msg: 'Failed to retrieve organization: ' + $routeParams.id + '. GET status: ' + status }); + }); + - scope.variables = ParseVariableString(params.variables); - scope.parseType = 'yaml'; - scope.$emit(callback, html); // CreateLaunchDialog({scope: scope, html: html}) // Reuse password modal // $('#password-modal').empty().html(html); @@ -457,6 +530,7 @@ function($location, Wait, GetBasePath, LookUpInit, JobTemplateForm, CredentialLi var html = params.html || "", form = SurveyTakerForm, id= params.id, + url = params.url, scope = params.scope, i, j, requiredAsterisk, @@ -471,8 +545,8 @@ function($location, Wait, GetBasePath, LookUpInit, JobTemplateForm, CredentialLi function buildHtml(question, index){ question.index = index; - if(!$('#question_'+question.index+':eq(0)').is('div')){ - html+='
'; + if(!$('#taker_'+question.index+':eq(0)').is('div')){ + html+='
'; $('#survey_taker_finalized_questions').append(html); } @@ -509,7 +583,7 @@ function($location, Wait, GetBasePath, LookUpInit, JobTemplateForm, CredentialLi '
'+ '
'; } - if(question.type === 'multiplechoice' || question.type === "multiselect"){ + if(question.type === 'multiplechoice'){ choices = question.choices.split(/\n/); element = (question.type==="multiselect") ? "checkbox" : 'radio'; question.default = (question.default) ? question.default : (question.default_multiselect) ? question.default_multiselect : "" ; @@ -530,6 +604,24 @@ function($location, Wait, GetBasePath, LookUpInit, JobTemplateForm, CredentialLi '
'; html+= ''; } + if(question.type === "multiselect"){ + choices = question.choices.split(/\n/); + element = (question.type==="multiselect") ? "checkbox" : 'radio'; + question.default = (question.default) ? question.default : (question.default_multiselect) ? question.default_multiselect : "" ; + // scope[question.variable].choices = choices; + html+='
'; + + for( j = 0; j' + + ''+choices[j] +'
' ; + + } + html+= '
A value is required!
'+ + '
'; + html+= '
'; + } if(question.type === 'integer'){ min = (!Empty(question.min)) ? Number(question.min) : ""; max = (!Empty(question.max)) ? Number(question.max) : "" ; @@ -556,10 +648,16 @@ function($location, Wait, GetBasePath, LookUpInit, JobTemplateForm, CredentialLi } html+=''; if(question.index === scope.survey_questions.length-1){ - CreateLaunchDialog({scope: scope, html: html}); + CreateLaunchDialog({scope: scope, html: html, url: url}); } } + scope.someSelected = function (object) { + return Object.keys(object).some(function (key) { + return object[key]; + }); + }; + // question.index = index; // question[question.variable] = question.default; // scope[question.variable] = question.default; @@ -714,7 +812,6 @@ function($location, Wait, GetBasePath, LookUpInit, JobTemplateForm, CredentialLi extra_vars, new_job_id, new_job, - survey_enabled, launch_url, prompt_for_vars = false, html, @@ -790,9 +887,9 @@ function($location, Wait, GetBasePath, LookUpInit, JobTemplateForm, CredentialLi if (scope.removePlaybookLaunchFinished) { scope.removePlaybookLaunchFinished(); } - scope.removePlaybookLaunchFinished = scope.$on('PlaybookLaunchFinished', function() { + scope.removePlaybookLaunchFinished = scope.$on('PlaybookLaunchFinished', function(e, data) { //var base = $location.path().replace(/^\//, '').split('/')[0]; - $location.path('/jobs/' + scope.new_job_id); + $location.path('/jobs/' + data.job); }); if (scope.removeStartPlaybookRun) { @@ -801,7 +898,7 @@ function($location, Wait, GetBasePath, LookUpInit, JobTemplateForm, CredentialLi scope.removeStartPlaybookRun = scope.$on('StartPlaybookRun', function() { LaunchJob({ scope: scope, - url: launch_url, + url: url, callback: 'PlaybookLaunchFinished', passwords: passwords }); @@ -810,11 +907,12 @@ function($location, Wait, GetBasePath, LookUpInit, JobTemplateForm, CredentialLi if (scope.removePromptForPasswords) { scope.removePromptForPasswords(); } - scope.removePromptForPasswords = scope.$on('PromptForPasswords', function(e, passwords_needed_to_start,html) { + scope.removePromptForPasswords = scope.$on('PromptForPasswords', function(e, passwords_needed_to_start,html, url) { PromptForPasswords({ scope: scope, passwords: passwords_needed_to_start, callback: 'PromptForVars', - html: html + html: html, + url: url }); }); @@ -828,21 +926,22 @@ function($location, Wait, GetBasePath, LookUpInit, JobTemplateForm, CredentialLi if (scope.removePromptForVars) { scope.removePromptForVars(); } - scope.removePromptForVars = scope.$on('PromptForVars', function(e, html) { + scope.removePromptForVars = scope.$on('PromptForVars', function(e, html, url) { // passwords = pwds; - if (prompt_for_vars) { + if (scope.prompt_for_vars) { // call prompt with callback of StartPlaybookRun, passwords PromptForVars({ scope: scope, job: {id:scope.job_template_id}, variables: extra_vars, callback: 'PromptForSurvey', - html: html + html: html, + url: url }); } else { - scope.$emit('PromptForSurvey', html); + scope.$emit('PromptForSurvey', html, url); } }); @@ -850,9 +949,9 @@ function($location, Wait, GetBasePath, LookUpInit, JobTemplateForm, CredentialLi if (scope.removePromptForSurvey) { scope.removePromptForSurvey(); } - scope.removePromptForSurvey = scope.$on('PromptForSurvey', function(e, html) { + scope.removePromptForSurvey = scope.$on('PromptForSurvey', function(e, html, url) { - if (survey_enabled) { + if (scope.survey_enabled) { // call prompt with callback of StartPlaybookRun, passwords PromptForSurvey({ scope: scope, @@ -865,7 +964,7 @@ function($location, Wait, GetBasePath, LookUpInit, JobTemplateForm, CredentialLi } else { // scope.$emit('StartPlaybookRun'); - CreateLaunchDialog({scope: scope, html: html}); + CreateLaunchDialog({scope: scope, html: html, url: url}); } }); @@ -889,21 +988,23 @@ function($location, Wait, GetBasePath, LookUpInit, JobTemplateForm, CredentialLi .success(function (data) { new_job_id = data.id; launch_url = url;//data.related.start; - prompt_for_vars = data.ask_variables_on_launch; - extra_vars = data.variables_needed_to_start; - survey_enabled = data.survey_enabled; + scope.passwords_needed_to_start = data.passwords_needed_to_start; + scope.prompt_for_vars = data.ask_variables_on_launch; + // scope.extra_vars = data.variables_needed_to_start; + scope.survey_enabled = data.survey_enabled; + // new_job = data; html = '
'; if (data.passwords_needed_to_start.length > 0) { - scope.$emit('PromptForPasswords', data.passwords_needed_to_start, html); + scope.$emit('PromptForPasswords', data.passwords_needed_to_start, html, url); } else if (data.ask_variables_on_launch) { - scope.$emit('PromptForVars', html); + scope.$emit('PromptForVars', html, url); } else if (data.survey_enabled===true) { - scope.$emit('PromptForSurvey', html); + scope.$emit('PromptForSurvey', html, url); } else { - scope.$emit('StartPlaybookRun'); + scope.$emit('StartPlaybookRun', url); } })