No longer create\/execute job when template is saved. Execute job from list view by clicking 'play' button. Prompts for missing credential and all required password.

This commit is contained in:
chouseknecht
2013-05-17 00:17:11 -04:00
parent ac172928f8
commit 66f93364b1
4 changed files with 121 additions and 41 deletions

View File

@@ -12,7 +12,8 @@
function JobTemplatesList ($scope, $rootScope, $location, $log, $routeParams, Rest, Alert, JobTemplateList, function JobTemplatesList ($scope, $rootScope, $location, $log, $routeParams, Rest, Alert, JobTemplateList,
GenerateList, LoadBreadCrumbs, Prompt, SearchInit, PaginateInit, ReturnToCaller, GenerateList, LoadBreadCrumbs, Prompt, SearchInit, PaginateInit, ReturnToCaller,
ClearScope, ProcessErrors, GetBasePath) ClearScope, ProcessErrors, GetBasePath, PromptPasswords, JobTemplateForm, CredentialList,
LookUpInit)
{ {
ClearScope('htmlTemplate'); //Garbage collection. Don't leave behind any listeners/watchers from the prior ClearScope('htmlTemplate'); //Garbage collection. Don't leave behind any listeners/watchers from the prior
//scope. //scope.
@@ -20,8 +21,8 @@ function JobTemplatesList ($scope, $rootScope, $location, $log, $routeParams, Re
var defaultUrl = GetBasePath('job_templates'); var defaultUrl = GetBasePath('job_templates');
var view = GenerateList; var view = GenerateList;
var base = $location.path().replace(/^\//,'').split('/')[0]; var base = $location.path().replace(/^\//,'').split('/')[0];
var mode = (base == 'job_templates') ? 'edit' : 'select'; // if base path 'credentials', we're here to add/edit var mode = (base == 'job_templates') ? 'edit' : 'select';
var scope = view.inject(list, { mode: mode }); // Inject our view var scope = view.inject(list, { mode: mode });
scope.selected = []; scope.selected = [];
SearchInit({ scope: scope, set: 'job_templates', list: list, url: defaultUrl }); SearchInit({ scope: scope, set: 'job_templates', list: list, url: defaultUrl });
@@ -39,7 +40,6 @@ function JobTemplatesList ($scope, $rootScope, $location, $log, $routeParams, Re
} }
scope.deleteJobTemplate = function(id, name) { scope.deleteJobTemplate = function(id, name) {
var action = function() { var action = function() {
var url = defaultUrl + id + '/'; var url = defaultUrl + id + '/';
Rest.setUrl(url); Rest.setUrl(url);
@@ -124,15 +124,87 @@ function JobTemplatesList ($scope, $rootScope, $location, $log, $routeParams, Re
scope.selected.push(idx); scope.selected.push(idx);
} }
} }
function postJob(data) {
// Once we have a credential and all required passwords, use this
// to create and start a job
var dt = new Date().toISOString();
Rest.setUrl(data.related.jobs);
Rest.post({
name: data.name + ' ' + dt, // job name required and unique
description: data.description,
job_template: data.id,
inventory: data.inventory,
project: data.project,
playbook: data.playbook,
credential: data.credential,
forks: data.forks,
limit: data.limit,
verbosity: data.verbosity,
extra_vars: data.extra_vars
})
.success( function(data, status, headers, config) {
// Prompt for passwords and start the job
PromptPasswords({
scope: scope,
passwords: data.passwords_needed_to_start,
start_url: data.related.start
});
})
.error( function(data, status, headers, config) {
ProcessErrors(scope, data, status, null,
{ hdr: 'Error!', msg: 'Failed to create job. POST returned status: ' + status });
});
};
scope.submitJob = function(id) {
// Get the job details
Rest.setUrl(defaultUrl + id + '/');
Rest.get()
.success( function(data, status, headers, config) {
// Create a job record
if (data.credential == '' || data.credential == null) {
// Template does not have credential, prompt for one
scope.$watch('credential', function(newVal, oldVal) {
if (newVal !== oldVal) {
console.log('credential is: ' + scope.credential);
// After user selects a credential from the modal,
// submit the job
data.credential = scope.credential;
postJob(data);
}
});
LookUpInit({
scope: scope,
form: JobTemplateForm,
current_item: null,
list: CredentialList,
field: 'credential',
hdr: 'Credential Required'
});
scope.lookUpCredential();
}
else {
// We have what we need, submit the job
postJob(data);
}
})
.error( function(data, status, headers, config) {
ProcessErrors(scope, data, status, null,
{ hdr: 'Error!', msg: 'Failed to get job template details. GET returned status: ' + status });
});
};
} }
JobTemplatesList.$inject = [ '$scope', '$rootScope', '$location', '$log', '$routeParams', 'Rest', 'Alert', 'JobTemplateList', JobTemplatesList.$inject = [ '$scope', '$rootScope', '$location', '$log', '$routeParams', 'Rest', 'Alert', 'JobTemplateList',
'GenerateList', 'LoadBreadCrumbs', 'Prompt', 'SearchInit', 'PaginateInit', 'ReturnToCaller', 'ClearScope', 'GenerateList', 'LoadBreadCrumbs', 'Prompt', 'SearchInit', 'PaginateInit', 'ReturnToCaller', 'ClearScope',
'ProcessErrors','GetBasePath' ]; 'ProcessErrors','GetBasePath', 'PromptPasswords', 'JobTemplateForm', 'CredentialList', 'LookUpInit'
];
function JobTemplatesAdd ($scope, $rootScope, $compile, $location, $log, $routeParams, JobTemplateForm, function JobTemplatesAdd ($scope, $rootScope, $compile, $location, $log, $routeParams, JobTemplateForm,
GenerateForm, Rest, Alert, ProcessErrors, LoadBreadCrumbs, ReturnToCaller, ClearScope, GenerateForm, Rest, Alert, ProcessErrors, LoadBreadCrumbs, ReturnToCaller, ClearScope,
GetBasePath, InventoryList, CredentialList, ProjectList, LookUpInit, PromptPasswords) GetBasePath, InventoryList, CredentialList, ProjectList, LookUpInit)
{ {
ClearScope('htmlTemplate'); //Garbage collection. Don't leave behind any listeners/watchers from the prior ClearScope('htmlTemplate'); //Garbage collection. Don't leave behind any listeners/watchers from the prior
//scope. //scope.
@@ -214,34 +286,8 @@ function JobTemplatesAdd ($scope, $rootScope, $compile, $location, $log, $routeP
} }
Rest.post(data) Rest.post(data)
.success( function(data, status, headers, config) { .success( function(data, status, headers, config) {
// Template saved. Now create a new job var base = $location.path().replace(/^\//,'').split('/')[0];
Rest.setUrl(data.related.jobs); (base == 'job_templates') ? ReturnToCaller() : ReturnToCaller(1);
Rest.post({
name: data.name,
description: data.description,
job_template: data.job_template,
job_type: data.job_type,
inventory: data.inventory,
project: data.project,
playbook: data.playbook,
credential: data.credential,
forks: data.forks,
limit: data.limit,
verbosity: data.verbosity,
extra_vars: data.extra_vars})
.success( function(data, status, headers, config) {
console.log('Job posted!');
console.log(data);
PromptPasswords({
scope: scope,
passwords: data.passwords_needed_to_start,
start_url: data.related.start
});
})
.error( function(data, status, headers, config) {
ProcessErrors(scope, data, status, form,
{ hdr: 'Error!', msg: 'Failed to post job. POST returned status: ' + status });
});
}) })
.error( function(data, status, headers, config) { .error( function(data, status, headers, config) {
ProcessErrors(scope, data, status, form, ProcessErrors(scope, data, status, form,
@@ -259,7 +305,7 @@ function JobTemplatesAdd ($scope, $rootScope, $compile, $location, $log, $routeP
JobTemplatesAdd.$inject = [ '$scope', '$rootScope', '$compile', '$location', '$log', '$routeParams', 'JobTemplateForm', JobTemplatesAdd.$inject = [ '$scope', '$rootScope', '$compile', '$location', '$log', '$routeParams', 'JobTemplateForm',
'GenerateForm', 'Rest', 'Alert', 'ProcessErrors', 'LoadBreadCrumbs', 'ReturnToCaller', 'ClearScope', 'GenerateForm', 'Rest', 'Alert', 'ProcessErrors', 'LoadBreadCrumbs', 'ReturnToCaller', 'ClearScope',
'GetBasePath', 'InventoryList', 'CredentialList', 'ProjectList', 'LookUpInit', 'PromptPasswords' ]; 'GetBasePath', 'InventoryList', 'CredentialList', 'ProjectList', 'LookUpInit' ];
function JobTemplatesEdit ($scope, $rootScope, $compile, $location, $log, $routeParams, JobTemplateForm, function JobTemplatesEdit ($scope, $rootScope, $compile, $location, $log, $routeParams, JobTemplateForm,

View File

@@ -82,3 +82,28 @@ angular.module('JobTemplateHelper', [ 'RestServices', 'Utilities', 'CredentialFo
$('#password-modal').modal(); $('#password-modal').modal();
} }
}]); }]);

View File

@@ -9,7 +9,8 @@
* form: <form object>, * form: <form object>,
* current_item: <id of item to select on open>, * current_item: <id of item to select on open>,
* list: <list object>, * list: <list object>,
* field: <name of the form field with which the lookup is associated> * field: <name of the form field with which the lookup is associated>,
* hdr: <optional. modal dialog header>
* }) * })
*/ */
@@ -26,9 +27,11 @@ angular.module('LookUpHelper', [ 'RestServices', 'Utilities', 'SearchHelper', 'P
var field = params.field; // form field var field = params.field; // form field
var postAction = params.postAction //action to perform post user selection var postAction = params.postAction //action to perform post user selection
// Show pop-up to select user // Show pop-up to select user
var name = list.iterator.charAt(0).toUpperCase() + list.iterator.substring(1); var name = list.iterator.charAt(0).toUpperCase() + list.iterator.substring(1);
var defaultUrl = (list.name == 'inventories') ? GetBasePath('inventory') : GetBasePath(list.name); var defaultUrl = (list.name == 'inventories') ? GetBasePath('inventory') : GetBasePath(list.name);
var hdr = (params.hdr) ? params.hdr : 'Select ' + name;
$('input[name="' + form.fields[field].sourceModel + '_' + form.fields[field].sourceField + '"]').attr('data-url',defaultUrl + $('input[name="' + form.fields[field].sourceModel + '_' + form.fields[field].sourceField + '"]').attr('data-url',defaultUrl +
'?' + form.fields[field].sourceField + '__' + 'iexact=:value'); '?' + form.fields[field].sourceField + '__' + 'iexact=:value');
@@ -36,7 +39,7 @@ angular.module('LookUpHelper', [ 'RestServices', 'Utilities', 'SearchHelper', 'P
scope['lookUp' + name] = function() { scope['lookUp' + name] = function() {
var listGenerator = GenerateList; var listGenerator = GenerateList;
var listScope = listGenerator.inject(list, { mode: 'lookup', hdr: 'Select ' + name }); var listScope = listGenerator.inject(list, { mode: 'lookup', hdr: hdr });
listScope.selectAction = function() { listScope.selectAction = function() {
var found = false; var found = false;
var name; var name;
@@ -44,7 +47,7 @@ angular.module('LookUpHelper', [ 'RestServices', 'Utilities', 'SearchHelper', 'P
if (listScope[list.iterator + "_" + listScope[list.name][i].id + "_class"] == "success") { if (listScope[list.iterator + "_" + listScope[list.name][i].id + "_class"] == "success") {
found = true; found = true;
scope[field] = listScope[list.name][i].id; scope[field] = listScope[list.name][i].id;
if (form.fields[field] && form.fields[field].sourceModel) { if (scope[form.name + '_form'] && form.fields[field] && form.fields[field].sourceModel) {
scope[form.fields[field].sourceModel + '_' + form.fields[field].sourceField] = scope[form.fields[field].sourceModel + '_' + form.fields[field].sourceField] =
listScope[list.name][i][form.fields[field].sourceField]; listScope[list.name][i][form.fields[field].sourceField];
scope[form.name + '_form'][form.fields[field].sourceModel + '_' + form.fields[field].sourceField] scope[form.name + '_form'][form.fields[field].sourceModel + '_' + form.fields[field].sourceField]

View File

@@ -38,12 +38,18 @@ angular.module('JobTemplatesListDefinition', [])
}, },
fieldActions: { fieldActions: {
submit: {
icon: 'icon-play',
mode: 'all',
ngClick: 'submitJob(\{\{ job_template.id \}\})',
class: 'btn btn-mini',
awToolTip: 'Create and run a job using this template'
},
edit: { edit: {
ngClick: "editJobTemplate(\{\{ job_template.id \}\})", ngClick: "editJobTemplate(\{\{ job_template.id \}\})",
icon: 'icon-edit', icon: 'icon-edit',
awToolTip: 'Edit template' awToolTip: 'Edit template'
}, },
delete: { delete: {
ngClick: "deleteJobTemplate(\{\{ job_template.id \}\},'\{\{ job_template.name \}\}')", ngClick: "deleteJobTemplate(\{\{ job_template.id \}\},'\{\{ job_template.name \}\}')",
icon: 'icon-remove', icon: 'icon-remove',