Merge "Add NG Flavor delete and update metadata actions"

This commit is contained in:
Jenkins 2017-03-02 11:19:57 +00:00 committed by Gerrit Code Review
commit 338dd392c5
6 changed files with 437 additions and 1 deletions

View File

@ -0,0 +1,75 @@
/**
* (c) Copyright 2016 Hewlett-Packard Development Company, L.P.
*
* 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';
/**
* @ngdoc overview
* @ngname horizon.app.core.flavors.actions
*
* @description
* Provides all of the actions for flavors.
*/
angular.module('horizon.app.core.flavors.actions', [
'horizon.framework.conf',
'horizon.app.core.flavors'
])
.run(registerFlavorActions);
registerFlavorActions.$inject = [
'horizon.framework.conf.resource-type-registry.service',
'horizon.app.core.flavors.actions.delete-flavor.service',
'horizon.app.core.flavors.actions.update-metadata.service',
'horizon.app.core.flavors.resourceType'
];
function registerFlavorActions(
registry,
deleteFlavorService,
updateMetadataService,
flavorResourceTypeCode
) {
var flavorResourceType = registry.getResourceType(flavorResourceTypeCode);
flavorResourceType.itemActions
.append({
id: 'updateMetadataService',
service: updateMetadataService,
template: {
text: gettext('Update Metadata')
}
})
.append({
id: 'deleteFlavorAction',
service: deleteFlavorService,
template: {
type: 'delete',
text: gettext('Delete Flavor')
}
});
flavorResourceType.batchActions
.append({
id: 'batchDeleteFlavorAction',
service: deleteFlavorService,
template: {
type: 'delete-selected',
text: gettext('Delete Flavors')
}
});
}
})();

View File

@ -0,0 +1,116 @@
/**
* 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.app.core.flavors')
.factory('horizon.app.core.flavors.actions.delete-flavor.service', deleteFlavorService);
deleteFlavorService.$inject = [
'horizon.app.core.openstack-service-api.nova',
'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.app.core.flavors.resourceType'
];
/*
* @ngdoc factory
* @name horizon.app.core.flavors.actions.delete-flavor.service
*
* @Description
* Brings up the delete flavors confirmation modal dialog.
* On submit, delete given flavors.
* On cancel, do nothing.
*/
function deleteFlavorService(
nova,
actionResultService,
gettext,
$qExtensions,
deleteModal,
toast,
flavorsResourceType
) {
var service = {
allowed: allowed,
perform: perform
};
return service;
//////////////
function allowed() {
return $qExtensions.booleanAsPromise(true);
}
function perform(items, scope) {
var flavors = angular.isArray(items) ? items : [items];
var context = {
labels: labelize(flavors.length),
deleteEntity: deleteFlavor
};
return deleteModal.open(scope, flavors, context).then(deleteResult);
}
function deleteResult(deleteModalResult) {
// To make the result of this action generically useful, reformat the return
// from the deleteModal into a standard form
var actionResult = actionResultService.getActionResult();
deleteModalResult.pass.forEach(function markDeleted(item) {
actionResult.deleted(flavorsResourceType, item.context.id);
});
deleteModalResult.fail.forEach(function markFailed(item) {
actionResult.failed(flavorsResourceType, item.context.id);
});
return actionResult.result;
}
function labelize(count) {
return {
title: ngettext(
'Confirm Delete Flavor',
'Confirm Delete Flavors', count),
message: ngettext(
'You have selected "%s". Deleted flavor is not recoverable.',
'You have selected "%s". Deleted flavors are not recoverable.', count),
submit: ngettext(
'Delete Flavor',
'Delete Flavors', count),
success: ngettext(
'Deleted Flavor: %s.',
'Deleted Flavors: %s.', count),
error: ngettext(
'Unable to delete Flavor: %s.',
'Unable to delete Flavors: %s.', count)
};
}
function deleteFlavor(flavor) {
return nova.deleteFlavor(flavor, true);
}
}
})();

View File

@ -0,0 +1,110 @@
/*
* (c) Copyright 2016 Hewlett Packard Enterprise Development LP
*
* 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';
describe('horizon.app.core.flavors.actions.delete-flavor.service', function() {
var service, deleteModalService, novaAPI;
///////////////////////
beforeEach(module('horizon.app.core'));
beforeEach(module('horizon.app.core.flavors'));
beforeEach(module('horizon.framework'));
beforeEach(inject(function($injector) {
service = $injector.get('horizon.app.core.flavors.actions.delete-flavor.service');
novaAPI = $injector.get('horizon.app.core.openstack-service-api.nova');
deleteModalService = $injector.get('horizon.framework.widgets.modal.deleteModalService');
}));
describe('perform method', function() {
beforeEach(function() {
spyOn(deleteModalService, 'open').and.returnValue({then: angular.noop});
});
////////////
it('should open the delete modal and show correct labels, single object', testSingleLabels);
it('should open the delete modal and show correct labels, plural objects', testPluralLabels);
it('should open the delete modal with correct entities', testEntities);
it('should only delete flavors that are valid', testValids);
it('should pass in a function that deletes an flavor', testNova);
////////////
function testSingleLabels() {
var flavors = {name: 'puffy'};
service.perform(flavors);
var labels = deleteModalService.open.calls.argsFor(0)[2].labels;
expect(deleteModalService.open).toHaveBeenCalled();
angular.forEach(labels, function eachLabel(label) {
expect(label.toLowerCase()).toContain('flavor');
});
}
function testPluralLabels() {
var flavors = [{name: 'puffy'}, {name: 'puffin'}];
service.perform(flavors);
var labels = deleteModalService.open.calls.argsFor(0)[2].labels;
expect(deleteModalService.open).toHaveBeenCalled();
angular.forEach(labels, function eachLabel(label) {
expect(label.toLowerCase()).toContain('flavors');
});
}
function testEntities() {
var flavors = [{name: 'puff'}, {name: 'puffy'}, {name: 'puffin'}];
service.perform(flavors);
var entities = deleteModalService.open.calls.argsFor(0)[1];
expect(deleteModalService.open).toHaveBeenCalled();
expect(entities.length).toEqual(flavors.length);
}
function testValids() {
var flavors = [{name: 'puff'}, {name: 'puffy'}, {name: 'puffin'}];
service.perform(flavors);
var entities = deleteModalService.open.calls.argsFor(0)[1];
expect(deleteModalService.open).toHaveBeenCalled();
expect(entities.length).toBe(flavors.length);
expect(entities[0].name).toEqual('puff');
expect(entities[1].name).toEqual('puffy');
expect(entities[2].name).toEqual('puffin');
}
function testNova() {
spyOn(novaAPI, 'deleteFlavor').and.callFake(angular.noop);
var flavors = [{id: 7, name: 'puff'}, {id: 14, name: 'cake'}];
service.perform(flavors);
var contextArg = deleteModalService.open.calls.argsFor(0)[2];
var deleteFunction = contextArg.deleteEntity;
deleteFunction(flavors[0].id);
expect(novaAPI.deleteFlavor).toHaveBeenCalledWith(flavors[0].id, true);
}
}); // end of delete modal
}); // end of delete-flavor
})();

View File

@ -0,0 +1,78 @@
/**
*
* 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.app.core.flavors')
.factory('horizon.app.core.flavors.actions.update-metadata.service', updateMetadataService);
updateMetadataService.$inject = [
'horizon.app.core.metadata.modal.service',
'horizon.framework.util.actions.action-result.service',
'horizon.framework.util.q.extensions',
'horizon.app.core.flavors.resourceType'
];
/**
* @ngDoc factory
* @name horizon.app.core.flavors.actions.updateMetadataService
*
* @param {Object} metadataModalService
* @param {Object} actionResultService
* @param {Object} $qExtensions
* @param {Object} flavorResourceType
* @Description
* Brings up the Update Metadata for flavor modal.
* On submit, update the metadata of selected flavor.
* On cancel, do nothing.
*
* @returns {Object} The service
*/
function updateMetadataService(
metadataModalService,
actionResultService,
$qExtensions,
flavorResourceType
) {
var service = {
perform: perform,
allowed: allowed
};
return service;
//////////////
function perform(flavor) {
return metadataModalService.open('flavor', flavor.id)
.result
.then(onSuccess);
function onSuccess() {
return actionResultService.getActionResult()
.updated(flavorResourceType, flavor.id)
.result;
}
}
function allowed() {
return $qExtensions.booleanAsPromise(true);
}
} // end of updateMetadataService
})(); // end of IIFE

View File

@ -0,0 +1,56 @@
/**
*
* 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';
describe('horizon.app.core.flavors.actions.update-metadata.service', function() {
var service;
var metadataModalMock = {
open: function () {}
};
///////////////////////
beforeEach(module('horizon.framework'));
beforeEach(module('horizon.app.core'));
beforeEach(module('horizon.app.core.metadata.modal', function($provide) {
$provide.value('horizon.app.core.metadata.modal.service', metadataModalMock);
}));
beforeEach(inject(function($injector) {
service = $injector.get('horizon.app.core.flavors.actions.update-metadata.service');
}));
it('should open the modal with correct message', function() {
var fakeModalService = {
result: {
then: function (callback) {
callback();
}
}
};
spyOn(metadataModalMock, 'open').and.returnValue(fakeModalService);
service.perform({id: '1', name: 'flavor1'});
expect(metadataModalMock.open).toHaveBeenCalled();
expect(metadataModalMock.open.calls.argsFor(0)).toEqual(['flavor', '1']);
});
});
})();

View File

@ -29,7 +29,8 @@
.module('horizon.app.core.flavors', [ .module('horizon.app.core.flavors', [
'ngRoute', 'ngRoute',
'horizon.framework.conf', 'horizon.framework.conf',
'horizon.app.core' 'horizon.app.core',
'horizon.app.core.flavors.actions'
]) ])
.constant('horizon.app.core.flavors.resourceType', 'OS::Nova::Flavor') .constant('horizon.app.core.flavors.resourceType', 'OS::Nova::Flavor')
.run(run) .run(run)