slight cleanup of templates list controller

lint / fix all of the indentation issues
smaller functions
use a variable for any string a user sees
This commit is contained in:
Jake McDermott 2017-12-03 23:26:58 -05:00
parent 73fa8521d0
commit cfba11f8d7
No known key found for this signature in database
GPG Key ID: 3B02CAD476EECB35

View File

@ -1,91 +1,403 @@
function ListTemplatesController (model, JobTemplate, WorkflowJobTemplate, strings, $state, $scope, rbacUiControlService, Dataset, $filter, Alert, InitiatePlaybookRun, Prompt, Wait, ProcessErrors, TemplateCopyService, $q, Empty, i18n, PromptService) {
const vm = this || {},
unifiedJobTemplate = model,
jobTemplate = new JobTemplate(),
workflowJobTemplate = new WorkflowJobTemplate();
/** ***********************************************
* Copyright (c) 2017 Ansible, Inc.
*
* All Rights Reserved
************************************************ */
vm.strings = strings;
const ALERT_MISSING = 'Template parameter is missing';
const ALERT_NO_PERMISSION = 'You do not have permission to perform this action.';
const ALERT_UNKNOWN = 'We were unable to determine this template\'s type';
const ALERT_UNKNOWN_COPY = `${ALERT_UNKNOWN} while copying.`;
const ALERT_UNKNOWN_DELETE = `${ALERT_UNKNOWN} while deleting.`;
const ALERT_UNKNOWN_EDIT = `${ALERT_UNKNOWN} while routing to edit.`;
const ALERT_UNKNOWN_LAUNCH = `${ALERT_UNKNOWN} while launching.`;
const ALERT_UNKNOWN_SCHEDULE = `${ALERT_UNKNOWN} while routing to schedule.`;
const ERROR_EDIT = 'Error: Unable to edit template';
const ERROR_DELETE = 'Error: Unable to delete template';
const ERROR_LAUNCH = 'Error: Unable to launch template';
const ERROR_UNKNOWN = 'Error: Unable to determine template type';
const ERROR_JOB_SCHEDULE = 'Error: Unable to schedule job';
const ERROR_TEMPLATE_COPY = 'Error: Unable to copy job template';
const ERROR_WORKFLOW_COPY = 'Error: Unable to copy workflow job template';
// TODO: add the permission based functionality to the base model
$scope.canAdd = false;
rbacUiControlService.canAdd("job_templates")
.then(function(params) {
$scope.canAddJobTemplate = params.canAdd;
});
rbacUiControlService.canAdd("workflow_job_templates")
.then(function(params) {
const JOB_TEMPLATE_ALIASES = ['job_template', 'Job Template'];
const WORKFLOW_TEMPLATE_ALIASES = ['workflow_job_template', 'Workflow Job Template'];
const isJobTemplate = obj => _.includes(JOB_TEMPLATE_ALIASES, _.get(obj, 'type'));
const isWorkflowTemplate = obj => _.includes(WORKFLOW_TEMPLATE_ALIASES, _.get(obj, 'type'));
function TemplatesListController (
$scope,
$rootScope,
Alert,
TemplateList,
Prompt,
ProcessErrors,
GetBasePath,
InitiatePlaybookRun,
Wait,
$state,
$filter,
Dataset,
rbacUiControlService,
TemplatesService,
qs,
i18n,
JobTemplate,
WorkflowJobTemplate,
TemplatesStrings,
$q,
Empty,
i18n,
PromptService,
) {
const jobTemplate = new JobTemplate();
const list = TemplateList;
init();
function init () {
$scope.canAdd = false;
rbacUiControlService.canAdd('job_templates').then(params => {
$scope.canAddJobTemplate = params.canAdd;
});
rbacUiControlService.canAdd('workflow_job_templates').then(params => {
$scope.canAddWorkflowJobTemplate = params.canAdd;
});
$scope.$watchGroup(["canAddJobTemplate", "canAddWorkflowJobTemplate"], function() {
if ($scope.canAddJobTemplate || $scope.canAddWorkflowJobTemplate) {
$scope.canAdd = true;
// search init
$scope.list = list;
$scope[`${list.iterator}_dataset`] = Dataset.data;
$scope[list.name] = $scope[`${list.iterator}_dataset`].results;
$scope.options = {};
$rootScope.flashMessage = null;
}
$scope.$on(`${list.iterator}_options`, (event, data) => {
$scope.options = data.data.actions.GET;
optionsRequestDataProcessing();
});
$scope.$watchCollection('templates', () => {
optionsRequestDataProcessing();
});
$scope.$on('ws-jobs', () => {
const path = GetBasePath(list.basePath) || GetBasePath(list.name);
qs.search(path, $state.params[`${list.iterator}_search`])
.then(searchResponse => {
$scope[`${list.iterator}_dataset`] = searchResponse.data;
$scope[list.name] = $scope[`${list.iterator}_dataset`].results;
});
});
// iterate over the list and add fields like type label, after the
// OPTIONS request returns, or the list is sorted/paginated/searched
function optionsRequestDataProcessing () {
$scope[list.name].forEach((item, idx) => {
const itm = $scope[list.name][idx];
// Set the item type label
if (list.fields.type && _.has($scope.options, 'type.choices')) {
$scope.options.type.choices.forEach(choice => {
if (choice[0] === item.type) {
[itm.type_label] = choice;
}
});
}
});
}
$scope.editJobTemplate = template => {
if (!template) {
Alert(ERROR_EDIT, ALERT_MISSING);
return;
}
if (isJobTemplate(template)) {
$state.transitionTo('templates.editJobTemplate', { job_template_id: template.id });
} else if (isWorkflowTemplate(template)) {
$state.transitionTo('templates.editWorkflowJobTemplate', { workflow_job_template_id: template.id });
} else {
$scope.canAdd = false;
Alert(ERROR_UNKNOWN, ALERT_UNKNOWN_EDIT);
}
});
$scope.list = {
iterator: 'template',
name: 'templates'
};
$scope.collection = {
basePath: 'unified_job_templates',
iterator: 'template'
};
$scope[`${$scope.list.iterator}_dataset`] = Dataset.data;
$scope[$scope.list.name] = $scope[`${$scope.list.iterator}_dataset`].results;
$scope.$on('updateDataset', function(e, dataset) {
$scope[`${$scope.list.iterator}_dataset`] = dataset;
$scope[$scope.list.name] = dataset.results;
});
// get modified date and user who modified it
vm.getModified = function(template) {
let val = "";
if (template.modified) {
val += $filter('longDate')(template.modified);
}
if (_.has(template, 'summary_fields.modified_by.username')) {
val += ` by <a href="/#/users/${template.summary_fields.modified_by.id}">${template.summary_fields.modified_by.username}</a>`;
}
if (val === "") {
val = undefined;
}
return val;
};
// get last ran date and user who ran it
vm.getRan = function(template) {
let val = "";
if (template.last_job_run) {
val += $filter('longDate')(template.last_job_run);
$scope.submitJob = template => {
if (!template) {
Alert(ERROR_LAUNCH, ALERT_MISSING);
return;
}
// TODO: when API gives back a user who last ran the job in summary fields, uncomment and
// update this code
// if (template && template.summary_fields && template.summary_fields.modified_by &&
// template.summary_fields.modified_by.username) {
// val += ` by <a href="/#/users/${template.summary_fields.modified_by.id}">${template.summary_fields.modified_by.username}</a>`;
// }
if (val === "") {
val = undefined;
if (isJobTemplate(template)) {
submitJobTemplate(template)
} else if (isWorkflowTemplate(template)) {
InitiatePlaybookRun({ scope: $scope, id: template.id, job_type: 'workflow_job_template' });
} else {
Alert(ERROR_UNKNOWN, ALERT_UNKNOWN_LAUNCH);
}
return val;
};
// get pretified template type names from options
vm.templateTypes = unifiedJobTemplate.options('actions.GET.type.choices')
.reduce((acc, i) => {
acc[i[0]] = i[1];
return acc;
}, {});
$scope.scheduleJob = template => {
if (!template) {
Alert(ERROR_JOB_SCHEDULE, ALERT_MISSING);
return;
}
// get if you should show the active indicator for the row or not
// TODO: edit indicator doesn't update when you enter edit route after initial load right now
vm.activeId = parseInt($state.params.job_template_id || $state.params.workflow_template_id);
if (isJobTemplate(template)) {
$state.go('jobTemplateSchedules', { id: template.id });
} else if (isWorkflowTemplate(template)) {
$state.go('workflowJobTemplateSchedules', { id: template.id });
} else {
Alert(ERROR_UNKNOWN, ALERT_UNKNOWN_SCHEDULE);
}
};
vm.submitJob = function(template) {
$scope.deleteJobTemplate = template => {
if (!template) {
Alert(ERROR_DELETE, ALERT_MISSING);
return;
}
if (isWorkflowTemplate(template)) {
const body = TemplatesStrings.get('deleteResource.CONFIRM', 'workflow job template');
$scope.displayTemplateDeletePrompt(template, body);
} else if (isJobTemplate(template)) {
jobTemplate.getDependentResourceCounts(template.id)
.then(counts => {
const body = buildTemplateDeletePromptHTML(counts);
$scope.displayTemplateDeletePrompt(template, body);
});
} else {
Alert(ERROR_UNKNOWN, ALERT_UNKNOWN_DELETE);
}
};
$scope.copyTemplate = template => {
if (!template) {
Alert(ERROR_TEMPLATE_COPY, ALERT_MISSING);
return;
}
if (isJobTemplate(template)) {
$scope.copyJobTemplate(template);
} else if (isWorkflowTemplate(template)) {
$scope.copyWorkflowJobTemplate(template);
} else {
Alert(ERROR_UNKNOWN, ALERT_UNKNOWN_COPY);
}
};
$scope.copyJobTemplate = template => {
Wait('start');
new JobTemplate('get', template.id)
.then(model => model.copy())
.then(({ id }) => {
const params = { job_template_id: id };
$state.go('templates.editJobTemplate', params, { reload: true });
})
.catch(({ data, status }) => {
const params = { hdr: 'Error!', msg: `Call to copy failed. Return status: ${status}` };
ProcessErrors($scope, data, status, null, params);
})
.finally(() => Wait('stop'));
};
$scope.copyWorkflowJobTemplate = template => {
Wait('start');
new WorkflowJobTemplate('get', template.id)
.then(model => model.extend('GET', 'copy'))
.then(model => {
const action = () => {
model.copy()
.then(({ id }) => {
const params = { workflow_job_template_id: id };
$state.go('templates.editWorkflowJobTemplate', params, { reload: true });
})
.catch(({ data, status }) => {
const params = { hdr: 'Error!', msg: `Call to copy failed. Return status: ${status}` };
ProcessErrors($scope, data, status, null, params);
});
};
if (model.get('related.copy.can_copy_without_user_input')) {
action();
} else if (model.get('related.copy.can_copy')) {
Prompt({
hdr: 'Copy Workflow',
action,
actionText: 'COPY',
class: 'Modal-primaryButton',
body: buildWorkflowCopyPromptHTML(model.get('related.copy'))
});
} else {
Alert(ERROR_WORKFLOW_COPY, ALERT_NO_PERMISSION);
}
})
.catch(({ data, status }) => {
const params = { hdr: 'Error!', msg: `Call to copy failed. Return status: ${status}` };
ProcessErrors($scope, data, status, null, params);
})
.finally(() => Wait('stop'));
};
$scope.displayTemplateDeletePrompt = (template, body) => {
const action = () => {
function handleSuccessfulDelete (isWorkflow) {
let reloadListStateParams = null;
let stateParamID;
if (isWorkflow) {
stateParamID = $state.params.workflow_job_template_id;
} else {
stateParamID = $state.params.job_template_id;
}
const templateSearch = _.get($state.params, 'template_search');
const { page } = templateSearch;
if ($scope.templates.length === 1 && !_.isEmpty(page) && page !== '1') {
reloadListStateParams = _.cloneDeep($state.params);
const pageNum = (parseInt(reloadListStateParams.template_search.page, 0) - 1);
reloadListStateParams.template_search.page = pageNum.toString();
}
if (parseInt(stateParamID, 0) === template.id) {
$state.go('templates', reloadListStateParams, { reload: true });
} else {
$state.go('.', reloadListStateParams, { reload: true });
}
Wait('stop');
} // end handler
let deleteServiceMethod;
let failMsg;
if (isWorkflowTemplate(template)) {
deleteServiceMethod = TemplatesService.deleteWorkflowJobTemplate;
failMsg = 'Call to delete workflow job template failed. DELETE returned status: ';
} else if (isJobTemplate(template)) {
deleteServiceMethod = TemplatesService.deleteJobTemplate;
failMsg = 'Call to delete job template failed. DELETE returned status: ';
} else {
Alert(ERROR_UNKNOWN, ALERT_UNKNOWN_DELETE);
return;
}
$('#prompt-modal').modal('hide');
Wait('start');
deleteServiceMethod(template.id)
.then(() => handleSuccessfulDelete(isWorkflowTemplate(template)))
.catch(res => {
ProcessErrors($scope, res.data, res.status, null, {
hdr: 'Error!',
msg: `${failMsg} ${res.status}.`
});
});
}; // end action
Prompt({
action,
actionText: 'DELETE',
body,
hdr: i18n._('Delete'),
resourceName: $filter('sanitize')(template.name)
});
};
function buildTemplateDeletePromptHTML (dependentResourceCounts) {
const invalidateRelatedLines = [];
let bodyHTML = `
<div class="Prompt-bodyQuery">
${TemplatesStrings.get('deleteResource.CONFIRM', 'job template')}
</div>`;
dependentResourceCounts.forEach(countObj => {
if (countObj.count && countObj.count > 0) {
invalidateRelatedLines.push(`<div>
<span class="Prompt-warningResourceTitle">
${countObj.label}
</span>
<span class="badge List-titleBadge">
${countObj.count}
</span>
</div>`);
}
});
if (invalidateRelatedLines && invalidateRelatedLines.length > 0) {
bodyHTML = `
<div class="Prompt-bodyQuery">
${TemplatesStrings.get('deleteResource.USED_BY', 'job template')}
${TemplatesStrings.get('deleteResource.CONFIRM', 'job template')}
</div>`;
invalidateRelatedLines.forEach(invalidateRelatedLine => {
bodyHTML += invalidateRelatedLine;
});
}
return bodyHTML;
}
function buildWorkflowCopyPromptHTML (data) {
const {
credentials_unable_to_copy,
inventories_unable_to_copy,
job_templates_unable_to_copy
} = data;
let itemsHTML = '';
if (job_templates_unable_to_copy.length > 0) {
itemsHTML += '<div>Unified Job Templates that cannot be copied<ul>';
_.forOwn(job_templates_unable_to_copy, ujt => {
if (ujt) {
itemsHTML += `<li>'${ujt}</li>`;
}
});
itemsHTML += '</ul></div>';
}
if (inventories_unable_to_copy.length > 0) {
itemsHTML += '<div>Node prompted inventories that cannot be copied<ul>';
_.forOwn(inventories_unable_to_copy, inv => {
if (inv) {
itemsHTML += `<li>'${inv}</li>`;
}
});
itemsHTML += '</ul></div>';
}
if (credentials_unable_to_copy.length > 0) {
itemsHTML += '<div>Node prompted credentials that cannot be copied<ul>';
_.forOwn(credentials_unable_to_copy, cred => {
if (cred) {
itemsHTML += `<li>'${cred}</li>`;
}
});
itemsHTML += '</ul></div>';
}
const bodyHTML = `
<div class="Prompt-bodyQuery">
You do not have access to all resources used by this workflow.
Resources that you don't have access to will not be copied
and will result in an incomplete workflow.
</div>
<div class="Prompt-bodyTarget">
${itemsHTML}
</div>
`;
return bodyHTML;
}
function submitJobTemplate(template) {
if(template) {
if(template.type && (template.type === 'Job Template' || template.type === 'job_template')) {
let jobTemplate = new JobTemplate();
@ -149,7 +461,7 @@ function ListTemplatesController (model, JobTemplate, WorkflowJobTemplate, strin
else {
Alert('Error: Unable to launch template', 'Template parameter is missing');
}
};
}
$scope.launchJob = () => {
@ -219,255 +531,32 @@ function ListTemplatesController (model, JobTemplate, WorkflowJobTemplate, strin
msg: i18n.sprintf(i18n._('Failed to launch job template. POST returned: %d'), $scope.promptData.template, status) });
});
};
vm.scheduleJob = (template) => {
if(template) {
if(template.type && (template.type === 'Job Template' || template.type === 'job_template')) {
$state.go('jobTemplateSchedules', {id: template.id});
}
else if(template.type && (template.type === 'Workflow Job Template' || template.type === 'workflow_job_template')) {
$state.go('workflowJobTemplateSchedules', {id: template.id});
}
else {
// Something went wrong Let the user know that we're unable to redirect to schedule because we don't know
// what type of job template this is
Alert('Error: Unable to determine template type', 'We were unable to determine this template\'s type while routing to schedule.');
}
}
else {
Alert('Error: Unable to schedule job', 'Template parameter is missing');
}
};
vm.scheduleJob = (template) => {
if(template) {
if(template.type && (template.type === 'Job Template' || template.type === 'job_template')) {
$state.go('jobTemplateSchedules', {id: template.id});
}
else if(template.type && (template.type === 'Workflow Job Template' || template.type === 'workflow_job_template')) {
$state.go('workflowJobTemplateSchedules', {id: template.id});
}
else {
// Something went wrong Let the user know that we're unable to redirect to schedule because we don't know
// what type of job template this is
Alert('Error: Unable to determine template type', 'We were unable to determine this template\'s type while routing to schedule.');
}
}
else {
Alert('Error: Unable to schedule job', 'Template parameter is missing');
}
};
vm.copyTemplate = function(template) {
if(template) {
if(template.type && template.type === 'job_template') {
Wait('start');
TemplateCopyService.get(template.id)
.then(function(response){
TemplateCopyService.set(response.data.results)
.then(function(results){
Wait('stop');
if(results.type && results.type === 'job_template') {
$state.go('templates.editJobTemplate', {job_template_id: results.id}, {reload: true});
}
})
.catch(({data, status}) => {
ProcessErrors($scope, data, status, null, {
hdr: 'Error!',
msg: 'Call failed. Return status: ' + status
});
});
})
.catch(({data, status}) => {
ProcessErrors($scope, data, status, null, {hdr: 'Error!',
msg: 'Call failed. Return status: '+ status});
});
}
else if(template.type && template.type === 'workflow_job_template') {
TemplateCopyService.getWorkflowCopy(template.id)
.then(function(result) {
if(result.data.can_copy) {
if(result.data.can_copy_without_user_input) {
// Go ahead and copy the workflow - the user has full priveleges on all the resources
TemplateCopyService.copyWorkflow(template.id)
.then(function(result) {
$state.go('templates.editWorkflowJobTemplate', {workflow_job_template_id: result.data.id}, {reload: true});
})
.catch(function (response) {
Wait('stop');
ProcessErrors($scope, response.data, response.status, null, { hdr: 'Error!',
msg: 'Call to copy workflow job template failed. Return status: ' + response.status + '.'});
});
}
else {
let bodyHtml = `
<div class="Prompt-bodyQuery">
You do not have access to all resources used by this workflow. Resources that you don\'t have access to will not be copied and will result in an incomplete workflow.
</div>
<div class="Prompt-bodyTarget">`;
// List the unified job templates user can not access
if (result.data.templates_unable_to_copy.length > 0) {
bodyHtml += '<div>Unified Job Templates that can not be copied<ul>';
_.forOwn(result.data.templates_unable_to_copy, function(ujt) {
if(ujt) {
bodyHtml += '<li>' + ujt + '</li>';
}
});
bodyHtml += '</ul></div>';
}
// List the prompted inventories user can not access
if (result.data.inventories_unable_to_copy.length > 0) {
bodyHtml += '<div>Node prompted inventories that can not be copied<ul>';
_.forOwn(result.data.inventories_unable_to_copy, function(inv) {
if(inv) {
bodyHtml += '<li>' + inv + '</li>';
}
});
bodyHtml += '</ul></div>';
}
// List the prompted credentials user can not access
if (result.data.credentials_unable_to_copy.length > 0) {
bodyHtml += '<div>Node prompted credentials that can not be copied<ul>';
_.forOwn(result.data.credentials_unable_to_copy, function(cred) {
if(cred) {
bodyHtml += '<li>' + cred + '</li>';
}
});
bodyHtml += '</ul></div>';
}
bodyHtml += '</div>';
Prompt({
hdr: 'Copy Workflow',
body: bodyHtml,
action: function() {
$('#prompt-modal').modal('hide');
Wait('start');
TemplateCopyService.copyWorkflow(template.id)
.then(function(result) {
Wait('stop');
$state.go('templates.editWorkflowJobTemplate', {workflow_job_template_id: result.data.id}, {reload: true});
}, function (data) {
Wait('stop');
ProcessErrors($scope, data, status, null, { hdr: 'Error!',
msg: 'Call to copy template failed. POST returned status: ' + status });
});
},
actionText: 'COPY',
class: 'Modal-primaryButton'
});
}
}
else {
Alert('Error: Unable to copy workflow job template', 'You do not have permission to perform this action.');
}
}, function (data) {
Wait('stop');
ProcessErrors($scope, data, status, null, { hdr: 'Error!',
msg: 'Call to copy template failed. GET returned status: ' + status });
});
}
else {
// Something went wrong - Let the user know that we're unable to copy because we don't know
// what type of job template this is
Alert('Error: Unable to determine template type', 'We were unable to determine this template\'s type while copying.');
}
}
else {
Alert('Error: Unable to copy job', 'Template parameter is missing');
}
};
vm.deleteTemplate = function(template) {
var action = function() {
$('#prompt-modal').modal('hide');
Wait('start');
let deleteComplete = () => {
let reloadListStateParams = null;
if($scope.templates.length === 1 && $state.params.template_search && !_.isEmpty($state.params.template_search.page) && $state.params.template_search.page !== '1') {
reloadListStateParams = _.cloneDeep($state.params);
reloadListStateParams.template_search.page = (parseInt(reloadListStateParams.template_search.page)-1).toString();
}
if (parseInt($state.params.template_id) === template.id) {
$state.go("^", reloadListStateParams, { reload: true });
} else {
$state.go('.', reloadListStateParams, {reload: true});
}
};
if(template.type === "job_template") {
jobTemplate.request('delete', template.id)
.then(() => {
deleteComplete();
})
.catch(({data, status}) => {
ProcessErrors($scope, data, status, null, {
hdr: strings.get('error.HEADER'),
msg: strings.get('error.CALL', {path: "" + unifiedJobTemplate.path + template.id, status})
});
})
.finally(function() {
Wait('stop');
});
} else if(template.type === "workflow_job_template") {
workflowJobTemplate.request('delete', template.id)
.then(() => {
deleteComplete();
})
.catch(({data, status}) => {
ProcessErrors($scope, data, status, null, {
hdr: strings.get('error.HEADER'),
msg: strings.get('error.CALL', {path: "" + unifiedJobTemplate.path + template.id, status})
});
})
.finally(function() {
Wait('stop');
});
}
};
let deleteModalBody = `<div class="Prompt-bodyQuery">${strings.get('deleteResource.CONFIRM', 'template')}</div>`;
Prompt({
hdr: strings.get('deleteResource.HEADER'),
resourceName: $filter('sanitize')(template.name),
body: deleteModalBody,
action: action,
actionText: 'DELETE'
});
};
}
ListTemplatesController.$inject = [
'resolvedModels',
TemplatesListController.$inject = [
'$scope',
'$rootScope',
'Alert',
'TemplateList',
'Prompt',
'ProcessErrors',
'GetBasePath',
'InitiatePlaybookRun',
'Wait',
'$state',
'$filter',
'Dataset',
'rbacUiControlService',
'TemplatesService',
'QuerySet',
'i18n',
'JobTemplateModel',
'WorkflowJobTemplateModel',
'TemplatesStrings',
'$state',
'$scope',
'rbacUiControlService',
'Dataset',
'$filter',
'Alert',
'InitiatePlaybookRun',
'Prompt',
'Wait',
'ProcessErrors',
'TemplateCopyService',
'$q',
'$q,'
'Empty',
'i18n',
'PromptService'
'PromptService',
];
export default ListTemplatesController;
export default TemplatesListController;