/*********************************************
* Copyright (c) 2014 AnsibleWorks, Inc.
*
* JobSubmission.js
*
*/
/**
* @ngdoc function
* @name helpers.function:JobSubmission
* @description
*/
'use strict';
export default
angular.module('JobSubmissionHelper', [ 'RestServices', 'Utilities', 'CredentialFormDefinition', 'CredentialsListDefinition',
'LookUpHelper', 'JobSubmissionHelper', 'JobTemplateFormDefinition', 'ModalDialog', 'FormGenerator', 'JobVarsPromptFormDefinition'])
.factory('LaunchJob', ['Rest', 'Wait', 'ProcessErrors', 'ToJSON', 'Empty', 'GetBasePath',
function(Rest, Wait, ProcessErrors, ToJSON, Empty, GetBasePath) {
return function(params) {
var scope = params.scope,
callback = params.callback || 'JobLaunched',
job_launch_data = {},
url = params.url,
vars_url = GetBasePath('job_templates')+scope.job_template_id + '/',
// fld,
extra_vars;
//found it easier to assume that there will be extra vars, and then check for a blank object at the end
job_launch_data.extra_vars = {};
//gather the extra vars from the job template if survey is enabled and prompt for vars is false
if (scope.removeGetExtraVars) {
scope.removeGetExtraVars();
}
scope.removeGetExtraVars = scope.$on('GetExtraVars', function() {
Rest.setUrl(vars_url);
Rest.get()
.success(function (data) {
if(!Empty(data.extra_vars)){
data.extra_vars = ToJSON('yaml', data.extra_vars, false);
$.each(data.extra_vars, function(key,value){
job_launch_data.extra_vars[key] = value;
});
}
scope.$emit('BuildData');
})
.error(function (data, status) {
ProcessErrors(scope, data, status, { hdr: 'Error!',
msg: 'Failed to retrieve job template extra variables.' });
});
});
//build the data object to be sent to the job launch endpoint. Any variables gathered from the survey and the extra variables text editor are inserted into the extra_vars dict of the job_launch_data
if (scope.removeBuildData) {
scope.removeBuildData();
}
scope.removeBuildData = scope.$on('BuildData', function() {
if(!Empty(scope.passwords_needed_to_start) && scope.passwords_needed_to_start.length>0){
scope.passwords.forEach(function(password) {
job_launch_data[password] = scope[password];
scope.passwords_needed_to_start.push(password+'_confirm'); // i'm pushing these values into this array for use during the survey taker parsing
});
}
if(scope.prompt_for_vars===true){
extra_vars = ToJSON(scope.parseType, scope.extra_vars, false);
if(!Empty(extra_vars)){
$.each(extra_vars, function(key,value){
job_launch_data.extra_vars[key] = value;
});
}
}
if(scope.survey_enabled===true){
for (var i=0; i < scope.survey_questions.length; i++){
var fld = scope.survey_questions[i].variable;
// grab all survey questions that have answers
if(scope[fld]) {
job_launch_data.extra_vars[fld] = scope[fld];
}
// for optional text and text-areas, submit a blank string if min length is 0
if(scope.survey_questions[i].required === false && (scope.survey_questions[i].type === "text" || scope.survey_questions[i].type === "textarea") && scope.survey_questions[i].min === 0 && (scope[fld] === "" || scope[fld] === undefined)){
job_launch_data.extra_vars[fld] = "";
}
}
}
// include the credential used if the user was prompted to choose a cred
if(!Empty(scope.credential)){
job_launch_data.credential_id = scope.credential;
}
// If the extra_vars dict is empty, we don't want to include it if we didn't prompt for anything.
if(jQuery.isEmptyObject(job_launch_data.extra_vars)===true && scope.prompt_for_vars===false){
delete job_launch_data.extra_vars;
}
Rest.setUrl(url);
Rest.post(job_launch_data)
.success(function(data) {
Wait('stop');
if(!$('#password-modal').is(':hidden')){
$('#password-modal').dialog('close');
}
scope.$emit(callback, data);
})
.error(function(data, status) {
ProcessErrors(scope, data, status, null, { hdr: 'Error!',
msg: 'Failed updating job ' + scope.job_template_id + ' with variables. POST returned: ' + status });
});
});
// if the user has a survey and does not have 'prompt for vars' selected, then we want to
// include the extra vars from the job template in the job launch. so first check for these conditions
// and then overlay any survey vars over those.
if(scope.prompt_for_vars===false && scope.survey_enabled===true){
scope.$emit('GetExtraVars');
}
else {
scope.$emit('BuildData');
}
};
}])
.factory('PromptForCredential', ['$location', 'Wait', 'GetBasePath', 'LookUpInit', 'JobTemplateForm', 'CredentialList', 'Rest', 'Prompt', 'ProcessErrors',
function($location, Wait, GetBasePath, LookUpInit, JobTemplateForm, CredentialList, Rest, Prompt, ProcessErrors) {
return function(params) {
var scope = params.scope,
callback = params.callback || 'CredentialReady',
selectionMade;
Wait('stop');
scope.credential = '';
if (scope.removeShowLookupDialog) {
scope.removeShowLookupDialog();
}
scope.removeShowLookupDialog = scope.$on('ShowLookupDialog', function() {
selectionMade = function () {
scope.$emit(callback, scope.credential);
};
LookUpInit({
url: GetBasePath('credentials') + '?kind=ssh',
scope: scope,
form: JobTemplateForm(),
current_item: null,
list: CredentialList,
field: 'credential',
hdr: 'Credential Required',
instructions: "Launching this job requires a machine credential. Please select your machine credential now or Cancel to quit.",
postAction: selectionMade,
input_type: 'radio'
});
scope.lookUpCredential();
});
if (scope.removeAlertNoCredentials) {
scope.removeAlertNoCredentials();
}
scope.removeAlertNoCredentials = scope.$on('AlertNoCredentials', function() {
var action = function () {
$('#prompt-modal').modal('hide');
$location.url('/credentials/add');
};
Prompt({
hdr: 'Machine Credential Required',
body: "
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.
'; //end survey_taker_input
}
if(question.type === "multiselect"){
//seperate the choices out into an array
choices = question.choices.split(/\n/);
question.default = (question.default) ? question.default : (question.default_multiselect) ? question.default_multiselect : "" ;
//ensure that the default answers are in an array
scope[question.variable] = question.default.split(/\n/);
//create a new object to be used by the surveyCheckboxes directive
scope[question.variable + '_object'] = {
name: question.variable,
value: (question.default.split(/\n/)[0]==="") ? [] : question.default.split(/\n/) ,
required: question.required,
options:[]
};
//load the options into the 'options' key of the new object
for(j=0; j'+
'{{job_launch_form.'+question.variable+'_object.$error.checkbox}}'+
'