diff --git a/magnum_ui/static/dashboard/container-infra/magnum.service.js b/magnum_ui/static/dashboard/container-infra/magnum.service.js index 1264846e..756c2a50 100644 --- a/magnum_ui/static/dashboard/container-infra/magnum.service.js +++ b/magnum_ui/static/dashboard/container-infra/magnum.service.js @@ -220,7 +220,8 @@ } function deleteQuota(projectId, resource, suppressError) { - var promise = apiService.delete('/api/container_infra/quotas/' + projectId + '/' + resource); + var promise = apiService.delete('/api/container_infra/quotas/' + projectId + '/' + resource, + {project_id: projectId, resource: resource}); return suppressError ? promise : promise.error(function() { var msg = gettext('Unable to delete the quota with project id: %(projectId)s and ' + 'resource: %(resource)s.'); diff --git a/magnum_ui/static/dashboard/container-infra/magnum.service.spec.js b/magnum_ui/static/dashboard/container-infra/magnum.service.spec.js index 59ffb764..ff6bd805 100644 --- a/magnum_ui/static/dashboard/container-infra/magnum.service.spec.js +++ b/magnum_ui/static/dashboard/container-infra/magnum.service.spec.js @@ -214,6 +214,10 @@ { "func": "deleteQuota", "method": "delete", + "data": { + "project_id": "123", + "resource": "Cluster" + }, "path": "/api/container_infra/quotas/123/Cluster", "error": "Unable to delete the quota with project id: 123 and resource: Cluster.", "testInput": ["123", "Cluster"] diff --git a/magnum_ui/static/dashboard/container-infra/quotas/actions.module.js b/magnum_ui/static/dashboard/container-infra/quotas/actions.module.js index 7e3b7df3..8ba4f946 100644 --- a/magnum_ui/static/dashboard/container-infra/quotas/actions.module.js +++ b/magnum_ui/static/dashboard/container-infra/quotas/actions.module.js @@ -33,6 +33,7 @@ 'horizon.framework.conf.resource-type-registry.service', 'horizon.framework.util.i18n.gettext', 'horizon.dashboard.container-infra.quotas.create.service', + 'horizon.dashboard.container-infra.quotas.delete.service', 'horizon.dashboard.container-infra.quotas.resourceType' ]; @@ -40,6 +41,7 @@ registry, gettext, createQuotaService, + deleteQuotaService, resourceType) { var quotaResourceType = registry.getResourceType(resourceType); @@ -52,6 +54,26 @@ text: gettext('Create Quota') } }); + + quotaResourceType.batchActions + .append({ + id: 'batchDeleteQuotaService', + service: deleteQuotaService, + template: { + type: 'delete-selected', + text: gettext('Delete Quotas') + } + }); + + quotaResourceType.itemActions + .append({ + id: 'deleteQuotaService', + service: deleteQuotaService, + template: { + type: 'delete', + text: gettext('Delete Quota') + } + }); } })(); diff --git a/magnum_ui/static/dashboard/container-infra/quotas/delete/delete.service.js b/magnum_ui/static/dashboard/container-infra/quotas/delete/delete.service.js new file mode 100644 index 00000000..319dd323 --- /dev/null +++ b/magnum_ui/static/dashboard/container-infra/quotas/delete/delete.service.js @@ -0,0 +1,167 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use self file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +(function() { + 'use strict'; + + angular + .module('horizon.dashboard.container-infra.quotas') + .factory('horizon.dashboard.container-infra.quotas.delete.service', deleteService); + + deleteService.$inject = [ + '$location', + '$q', + 'horizon.app.core.openstack-service-api.magnum', + 'horizon.app.core.openstack-service-api.policy', + 'horizon.framework.util.actions.action-result.service', + 'horizon.framework.util.i18n.gettext', + 'horizon.framework.util.q.extensions', + 'horizon.framework.widgets.modal.deleteModalService', + 'horizon.framework.widgets.toast.service', + 'horizon.dashboard.container-infra.quotas.resourceType', + 'horizon.dashboard.container-infra.quotas.events' + ]; + + /** + * @ngDoc factory + * @name quotas.delete.service + * @param {Object} $location + * @param {Object} $q + * @param {Object} magnum + * @param {Object} policy + * @param {Object} actionResult + * @param {Object} gettext + * @param {Object} $qExtensions + * @param {Object} deleteModal + * @param {Object} toast + * @param {Object} resourceType + * @param {Object} events + * @returns {Object} delete service + * @description + * Brings up the delete quotas confirmation modal dialog. + * On submit, delete selected resources. + * On cancel, do nothing. + */ + function deleteService( + $location, $q, magnum, policy, actionResult, gettext, $qExtensions, + deleteModal, toast, resourceType, events + ) { + var scope; + var context = { + labels: null, + deleteEntity: deleteEntity, + successEvent: events.DELETE_SUCCESS + }; + var service = { + initAction: initAction, + allowed: allowed, + perform: perform + }; + var notAllowedMessage = gettext("You are not allowed to delete quotas: %s"); + + return service; + + ////////////// + + // include this function in your service + // if you plan to emit events to the parent controller + function initAction() { + } + + function allowed() { + return $qExtensions.booleanAsPromise(true); + } + + // delete selected resource objects + function perform(selected, $scope) { + scope = $scope; + selected = angular.isArray(selected) ? selected : [selected]; + context.labels = labelize(selected.length); + return $qExtensions.allSettled(selected.map(checkPermission)).then(afterCheck); + } + + function labelize(count) { + return { + title: ngettext('Confirm Delete Quota', + 'Confirm Delete Quotas', count), + /* eslint-disable max-len */ + message: ngettext('You have selected "%s". Please confirm your selection. Deleted quota is not recoverable.', + 'You have selected "%s". Please confirm your selection. Deleted quotas are not recoverable.', count), + /* eslint-enable max-len */ + submit: ngettext('Delete Quota', + 'Delete Quotas', count), + success: ngettext('Deleted quota: %s.', + 'Deleted quotas: %s.', count), + error: ngettext('Unable to delete quota: %s.', + 'Unable to delete quotas: %s.', count) + }; + } + + // for batch delete + function checkPermission(selected) { + return {promise: allowed(selected), context: selected}; + } + + // for batch delete + function afterCheck(result) { + var outcome = $q.reject(); // Reject the promise by default + if (result.fail.length > 0) { + toast.add('error', getMessage(notAllowedMessage, result.fail)); + outcome = $q.reject(result.fail); + } + if (result.pass.length > 0) { + outcome = deleteModal.open(scope, result.pass.map(getEntity), context).then(createResult); + } + return outcome; + } + + function createResult(deleteModalResult) { + // To make the result of this action generically useful, reformat the return + // from the deleteModal into a standard form + var result = actionResult.getActionResult(); + deleteModalResult.pass.forEach(function markDeleted(item) { + result.deleted(resourceType, getEntity(item).project_id + "/" + getEntity(item).resource); + }); + deleteModalResult.fail.forEach(function markFailed(item) { + result.failed(resourceType, getEntity(item).project_id + "/" + getEntity(item).resource); + }); + var indexPath = "/admin/container_infra/quotas"; + var currentPath = $location.path(); + if (result.result.failed.length === 0 && result.result.deleted.length > 0 && + currentPath !== indexPath) { + $location.path(indexPath); + } else { + return result.result; + } + } + + function getMessage(message, entities) { + return interpolate(message, [entities.map(getName).join(", ")]); + } + + function getName(result) { + return getEntity(result).project_id + "/" + getEntity(result).resource; + } + + // for batch delete + function getEntity(result) { + return result.context; + } + + // call delete REST API + function deleteEntity(id, item) { + return magnum.deleteQuota(item.project_id, item.resource, false); + } + } +})(); diff --git a/magnum_ui/static/dashboard/container-infra/quotas/delete/delete.service.spec.js b/magnum_ui/static/dashboard/container-infra/quotas/delete/delete.service.spec.js new file mode 100644 index 00000000..c270fdcb --- /dev/null +++ b/magnum_ui/static/dashboard/container-infra/quotas/delete/delete.service.spec.js @@ -0,0 +1,138 @@ +/** + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. You may obtain + * a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +(function() { + 'use strict'; + + describe('horizon.dashboard.container-infra.quotas.delete.service', function() { + + var service, $scope, deferredModal; + + var deleteModalService = { + open: function () { + deferredModal.resolve({ + pass: [{context: {id: 'a'}}], + fail: [{context: {id: 'b'}}] + }); + return deferredModal.promise; + } + }; + + var magnumAPI = { + deleteQuota: function() { + return; + } + }; + + var policyAPI = { + ifAllowed: function() { + return { + success: function(callback) { + callback({allowed: true}); + } + }; + } + }; + + beforeEach(module('horizon.dashboard.container-infra.quotas')); + + beforeEach(module('horizon.app.core')); + beforeEach(module('horizon.framework')); + + beforeEach(module('horizon.framework.widgets.modal', function($provide) { + $provide.value('horizon.framework.widgets.modal.deleteModalService', deleteModalService); + })); + + beforeEach(module('horizon.app.core.openstack-service-api', function($provide) { + $provide.value('horizon.app.core.openstack-service-api.magnum', magnumAPI); + $provide.value('horizon.app.core.openstack-service-api.policy', policyAPI); + spyOn(policyAPI, 'ifAllowed').and.callThrough(); + })); + + beforeEach(inject(function($injector, _$rootScope_, $q) { + $scope = _$rootScope_.$new(); + service = $injector.get('horizon.dashboard.container-infra.quotas.delete.service'); + deferredModal = $q.defer(); + })); + + function generateQuota(count) { + var Quota = []; + var data = { + id: '1', + project_id: 'delete_test', + resource: 'Cluster', + hard_limit: 2 + }; + + for (var index = 0; index < count; index++) { + var quotas = angular.copy(data); + quotas.id = index + 1; + Quota.push(quotas); + } + return Quota; + } + + describe('perform method', function() { + + beforeEach(function() { + spyOn(deleteModalService, 'open').and.callThrough(); + service.initAction(labelize); + }); + + function labelize(count) { + return { + title: ngettext('title', 'titles', count), + message: ngettext('message', 'messages', count), + submit: ngettext('submit', 'submits', count), + success: ngettext('success', 'successes', count), + error: ngettext('error', 'errors', count) + }; + } + + it('should open the delete modal and show correct labels', testSingleObject); + + function testSingleObject() { + var quotas = generateQuota(1); + service.perform(quotas[0], $scope); + $scope.$apply(); + + expect(deleteModalService.open).toHaveBeenCalled(); + } + + it('should open the delete modal and show correct labels', testDoubleObject); + + function testDoubleObject() { + var quotas = generateQuota(2); + service.perform(quotas, $scope); + $scope.$apply(); + + expect(deleteModalService.open).toHaveBeenCalled(); + } + + it('should pass in a function that deletes a quota', testMagnum); + + function testMagnum() { + spyOn(magnumAPI, 'deleteQuota'); + var quotas = generateQuota(1); + var quota = quotas[0]; + service.perform(quotas, $scope); + $scope.$apply(); + + var contextArg = deleteModalService.open.calls.argsFor(0)[2]; + var deleteFunction = contextArg.deleteEntity; + deleteFunction(quota.id, quota); + } + }); + }); +})();