From 707f98237bbb71059495ced997877ce03c8111f6 Mon Sep 17 00:00:00 2001 From: chouseknecht Date: Fri, 16 Aug 2013 17:01:18 -0400 Subject: [PATCH] AC-331 Delete one or many hosts now working. --- awx/ui/static/js/controllers/Inventories.js | 31 +++++++++- awx/ui/static/js/forms/Inventories.js | 20 ++---- awx/ui/static/js/helpers/Hosts.js | 68 +++++++++++++++------ awx/ui/static/js/helpers/refresh-related.js | 1 + awx/ui/static/lib/ansible/form-generator.js | 6 +- 5 files changed, 85 insertions(+), 41 deletions(-) diff --git a/awx/ui/static/js/controllers/Inventories.js b/awx/ui/static/js/controllers/Inventories.js index 266fe1150a..29966446d0 100644 --- a/awx/ui/static/js/controllers/Inventories.js +++ b/awx/ui/static/js/controllers/Inventories.js @@ -205,7 +205,7 @@ function InventoriesEdit ($scope, $rootScope, $compile, $location, $log, $routeP scope['inventory_id'] = id; scope['inventoryFailureFilter'] = false; - // Retrieve each related set and any lookups + // Retrieve each related sets and any lookups if (scope.inventoryLoadedRemove) { scope.inventoryLoadedRemove(); } @@ -217,6 +217,17 @@ function InventoriesEdit ($scope, $rootScope, $compile, $location, $log, $routeP } }); + // Add the selected flag to the hosts set. + if (scope.relatedHostsRemove) { + scope.relatedHostsRemove(); + } + scope.relatedHostsRemove = scope.$on('relatedhosts', function() { + scope.toggleAllFlag = false; + for (var i=0; i < scope.hosts.length; i++) { + scope.hosts[i].selected = 0; + } + }); + LoadInventory({ scope: scope, doPostSteps: true }); $('#inventory-tabs a[href="#inventory-hosts"]').on('show.bs.tab', function() { scope['hosts'] = null; @@ -460,6 +471,24 @@ function InventoriesEdit ($scope, $rootScope, $compile, $location, $log, $routeP { hdr: 'Error!', msg: 'Failed to lookup last job: ' + last_job + '. GET status: ' + status }); }); } + + scope.toggleAllHosts = function() { + scope.hostDeleteHide = (scope.toggleAllFlag) ? false : true; + for (var i=0; i < scope.hosts.length; i++) { + scope.hosts[i].selected = scope.toggleAllFlag; + } + } + + scope.toggleOneHost = function() { + var result = true; + for (var i=0; i < scope.hosts.length; i++) { + if (scope.hosts[i].selected) { + result = false; + break; + } + } + scope.hostDeleteHide = result; + } // Respond to the scope.$emit from awTree directive scope.$on('refreshHost', function(e, group, title) { diff --git a/awx/ui/static/js/forms/Inventories.js b/awx/ui/static/js/forms/Inventories.js index 5892e94819..1ffa95cb65 100644 --- a/awx/ui/static/js/forms/Inventories.js +++ b/awx/ui/static/js/forms/Inventories.js @@ -105,22 +105,6 @@ angular.module('InventoryFormDefinition', []) title: "groupTitle", iterator: 'host', actions: { - select: { - ngClick: "selectHost()", - icon: 'icon-check', - label: 'Add Existing', - awToolTip: 'Select existing host', - ngHide: 'createButtonShow == false', - "class": 'btn btn-default' - }, - create: { - ngClick: "createHost()", - icon: 'icon-plus', - label: 'Create New', - awToolTip: 'Create a new host', - ngHide: 'createButtonShow == false', - "class": 'btn-success' - } }, fields: { @@ -129,6 +113,10 @@ angular.module('InventoryFormDefinition', []) label: 'Host Name', ngClick: "editHost(\{\{ host.id \}\}, '\{\{ host.name \}\}')" }, + description: { + label: 'Description', + ngClick: "editHost(\{\{ host.id \}\}, '\{\{ host.name \}\}')" + }, has_active_failures: { label: 'Failed jobs?', showValue: false, diff --git a/awx/ui/static/js/helpers/Hosts.js b/awx/ui/static/js/helpers/Hosts.js index 1c18acd691..d690421115 100644 --- a/awx/ui/static/js/helpers/Hosts.js +++ b/awx/ui/static/js/helpers/Hosts.js @@ -98,6 +98,7 @@ angular.module('HostsHelper', [ 'RestServices', 'Utilities', 'ListGenerator', 'H scope.removeHostsReload(); } scope.removeHostsReload = scope.$on('hostsReload', function() { + console.log('here!'); HostsReload(params); }); @@ -345,7 +346,7 @@ angular.module('HostsHelper', [ 'RestServices', 'Utilities', 'ListGenerator', 'H function($rootScope, $location, $log, $routeParams, Rest, Alert, Prompt, ProcessErrors, GetBasePath, HostsReload) { return function(params) { - // Remove the selected host from the current group by dissaciating + // Remove the selected host from the current group by disassociating var scope = params.scope; var group_id = scope.group_id; @@ -356,39 +357,64 @@ angular.module('HostsHelper', [ 'RestServices', 'Utilities', 'ListGenerator', 'H var url = (scope.group_id == null || req == 'delete') ? GetBasePath('inventory') + inventory_id + '/hosts/' : GetBasePath('groups') + scope.group_id + '/hosts/'; + + if (scope.removeHostsReload) { + scope.removeHostsReload(); + } + scope.removeHostsReload = scope.$on('hostsReload', function() { + HostsReload(params); + }); var action_to_take = function() { - if (scope.removeHostsReload) { - scope.removeHostsReload(); + var errors = false; + var maxI; + + // Find index pointing to the last selected host + for (var i=0; i < scope.hosts.length; i++) { + if (scope.hosts[i].selected) { + maxI = i; + } } - scope.removeHostsReload = scope.$on('hostsReload', function() { - $('#prompt-modal').modal('hide'); - HostsReload(params); - }); + + function emit(i) { + // After we process the last selected host or after we hit a problem, refresh the host list + if (i >= maxI || errors) { + $('#prompt-modal').modal('hide'); + scope.$emit('hostsReload'); + } + } + Rest.setUrl(url); - Rest.post({ id: host_id, disassociate: 1 }) - .success( function(data, status, headers, config) { - scope.$emit('hostsReload'); - }) - .error( function(data, status, headers, config) { - scope.$emit('hostsReload'); - ProcessErrors(scope, data, status, null, - { hdr: 'Error!', msg: 'Call to ' + url + ' failed. POST returned status: ' + status }); - }); + for (var i=0; i < scope.hosts.length && !errors; i++) { + if (scope.hosts[i].selected) { + Rest.post({ id: scope.hosts[i].id, disassociate: 1 }) + .success( function(data, status, headers, config) { + // if this is the last selected host, clean up and exit + emit(i); + }) + .error( function(data, status, headers, config) { + errors = true; + emit(i); + ProcessErrors(scope, data, status, null, + { hdr: 'Error!', msg: 'Attempt to delete ' + scope.hosts[i].name + ' failed. POST returned status: ' + status }); + }); + } + } } - //Force binds to work. Not working usual way. + //Force binds to work (not working usual way), and launch the confirmation prompt if (scope.group_id == null || req == 'delete') { scope['promptHeader'] = 'Delete Host'; - scope['promptBody'] = 'Are you sure you want to permanently delete ' + host_name + ' from the inventory?'; + scope['promptBody'] = 'Are you sure you want to permanently delete the selected hosts?'; scope['promptActionBtnClass'] = 'btn-danger'; } - else { + + /*else { scope['promptHeader'] = 'Remove Host from Group'; scope['promptBody'] = 'Are you sure you want to remove ' + host_name + ' from the group? ' + host_name + ' will continue to be part of the inventory under All Hosts.'; scope['promptActionBtnClass'] = 'btn-success'; - } + }*/ scope.promptAction = action_to_take; @@ -414,6 +440,8 @@ angular.module('HostsHelper', [ 'RestServices', 'Utilities', 'ListGenerator', 'H var group_id = params.group_id; var scope = params.scope; scope['hosts'] = null; + scope['toggleAllFlag'] = false; + scope['hostDeleteHide'] = true; var url = (group_id !== null && group_id !== undefined) ? GetBasePath('groups') + group_id + '/all_hosts/' : GetBasePath('inventory') + params.inventory_id + '/hosts/'; diff --git a/awx/ui/static/js/helpers/refresh-related.js b/awx/ui/static/js/helpers/refresh-related.js index 6333e7c2b4..c6963cd17f 100644 --- a/awx/ui/static/js/helpers/refresh-related.js +++ b/awx/ui/static/js/helpers/refresh-related.js @@ -33,6 +33,7 @@ angular.module('RefreshRelatedHelper', ['RestServices', 'Utilities']) scope[iterator + 'PageCount'] = Math.ceil((data.count / scope[iterator + 'PageSize'])); scope[iterator + 'SearchSpin'] = false; scope[iterator + 'Loading'] = false; + scope.$emit('related' + set); if (!params.scope.$$phase) { params.scope.$digest(); } diff --git a/awx/ui/static/lib/ansible/form-generator.js b/awx/ui/static/lib/ansible/form-generator.js index b8574db6fd..8919c4e218 100644 --- a/awx/ui/static/lib/ansible/form-generator.js +++ b/awx/ui/static/lib/ansible/form-generator.js @@ -989,7 +989,7 @@ angular.module('FormGenerator', ['GeneratorHelpers', 'ngCookies']) html += "\n"; html += "\n"; html += "#\n"; - html += "\n"; + html += "\n"; for (var fld in form.related[itm].fields) { html += "\n"; // Select checkbox - html += ""; + html += ""; var cnt = 1; var rfield;