From 13339f218010766261955697f04b50cd3dc45346 Mon Sep 17 00:00:00 2001 From: Matt Borland Date: Mon, 23 May 2016 11:57:31 -0600 Subject: [PATCH] Fixes issue where metadata is always removed on edit This patch modifies the way in which the metadata is saved so that: * it responds to metadata-changed events properly * it saves the metadata * it saves the image record Change-Id: If21a7170e7a33f61619e1fa20faa32adc4bb3a90 Closes-Bug: 1584218 --- .../images/actions/edit.action.service.js | 58 +++++++++---------- .../actions/edit.action.service.spec.js | 49 +--------------- 2 files changed, 27 insertions(+), 80 deletions(-) diff --git a/openstack_dashboard/static/app/core/images/actions/edit.action.service.js b/openstack_dashboard/static/app/core/images/actions/edit.action.service.js index 1ea0a6ae2..d6a05cc01 100644 --- a/openstack_dashboard/static/app/core/images/actions/edit.action.service.js +++ b/openstack_dashboard/static/app/core/images/actions/edit.action.service.js @@ -23,6 +23,7 @@ editService.$inject = [ '$q', + 'horizon.app.core.images.events', 'horizon.app.core.images.resourceType', 'horizon.app.core.images.actions.editWorkflow', 'horizon.app.core.metadata.service', @@ -42,6 +43,7 @@ */ function editService( $q, + events, imageResourceType, editWorkflow, metadataService, @@ -54,10 +56,9 @@ toast ) { var message = { - success: gettext('Image %s was successfully updated.'), - successMetadata: gettext('Image metadata %s was successfully updated.') + success: gettext('Image %s was successfully updated.') }; - var modifyImagePolicyCheck, scope, saveDeferred; + var modifyImagePolicyCheck, scope; var model = { image: {}, @@ -76,6 +77,7 @@ function initScope($scope) { scope = $scope; + $scope.$on(events.IMAGE_METADATA_CHANGED, onMetadataChange); modifyImagePolicyCheck = policy.ifAllowed({rules: [['image', 'modify_image']]}); } @@ -97,64 +99,56 @@ model.image = localImage; } - wizardModalService.modal({ + return wizardModalService.modal({ scope: scope, workflow: editWorkflow, submit: submit - }).result.catch(cancel); - - saveDeferred = $q.defer(); - return saveDeferred.promise; + }).result; } - function cancel() { - saveDeferred.reject(); + function onMetadataChange(e, metadata) { + model.metadata = metadata; + e.stopPropagation(); } function submit() { - return saveMetadata().then(onSaveMetadata, onFailMetadata); + return updateMetadata().then(updateImage, onUpdateImageFail); } - function onFailMetadata() { - glance.updateImage(model.image).then(onUpdateImageSuccess, onUpdateImageFail); - } - - function onSaveMetadata() { - toast.add('success', interpolate(message.successMetadata, [model.image.name])); - glance.updateImage(model.image).then(onUpdateImageSuccess, onUpdateImageFail); + function updateImage() { + var finalModel = model.image; + return glance.updateImage(finalModel).then(onUpdateImageSuccess, onUpdateImageFail); } function onUpdateImageSuccess() { toast.add('success', interpolate(message.success, [model.image.name])); - saveDeferred.resolve(actionResultService.getActionResult() + return actionResultService.getActionResult() .updated(imageResourceType, model.image.id) - .result); + .result; } function onUpdateImageFail() { - saveDeferred.reject(actionResultService.getActionResult() + return actionResultService.getActionResult() .failed(imageResourceType, model.image.id) - .result); + .result; } - function saveMetadata() { - var imageId = model.image.id; - var deferred = $q.defer(); + function updateMetadata() { - metadataService.getMetadata('image', imageId).then(onMetadataGet); + return metadataService + .getMetadata('image', model.image.id) + .then(onMetadataGet); function onMetadataGet(response) { + var updated = model.metadata; var removed = angular.copy(response.data); - angular.forEach(model.metadata, function(value, key) { + angular.forEach(updated, function(value, key) { delete removed[key]; }); - deferred.resolve( - metadataService.editMetadata('image', imageId, model.metadata, Object.keys(removed)) - ); + return metadataService + .editMetadata('image', model.image.id, updated, Object.keys(removed)); } - - return deferred.promise; } function isActive(image) { diff --git a/openstack_dashboard/static/app/core/images/actions/edit.action.service.spec.js b/openstack_dashboard/static/app/core/images/actions/edit.action.service.spec.js index 93ba33756..146728f96 100644 --- a/openstack_dashboard/static/app/core/images/actions/edit.action.service.spec.js +++ b/openstack_dashboard/static/app/core/images/actions/edit.action.service.spec.js @@ -77,7 +77,7 @@ } }; - var service, $scope, $q, toast, deferred, testImage, $timeout; + var service, $scope, $q, deferred, testImage, $timeout; /////////////////////// @@ -96,7 +96,6 @@ $scope = _$rootScope_.$new(); $q = _$q_; service = $injector.get('horizon.app.core.images.actions.edit.service'); - toast = $injector.get('horizon.framework.widgets.toast.service'); service.initScope($scope); deferred = $q.defer(); $timeout = _$timeout_; @@ -119,42 +118,6 @@ expect(modalArgs.workflow).toBeDefined(); }); - it('should raise event even if update meta data fails', function() { - var image = { name: 'Test', id: '2' }; - - var failedPromise = function() { - return { - then: function(callback, errorCallback) { - errorCallback(); - } - }; - }; - - spyOn(wizardModalService, 'modal').and.callThrough(); - spyOn(glanceAPI, 'updateImage').and.callThrough(); - spyOn(metadataService, 'editMetadata').and.callFake(failedPromise); - spyOn(toast, 'add').and.callThrough(); - - service.initScope($scope); - service.perform(image); - $scope.$apply(); - - var modalArgs = wizardModalService.modal.calls.argsFor(0)[0]; - modalArgs.submit(); - $scope.$apply(); - - expect(toast.add.calls.count()).toBe(1); - }); - }); - - describe('edit', function() { - it('should allow edit if image can be edited', function() { - var image = {owner: 'project', status: 'active'}; - var allowed = service.allowed(image); - permissionShouldPass(allowed); - $scope.$apply(); - }); - it('should not allow edit if image is not owned by user', function() { deferred.reject(); var image = {owner: 'doesnt_matter', status: 'active'}; @@ -180,16 +143,6 @@ }); } - function permissionShouldPass(permissions) { - permissions.then( - function() { - expect(true).toBe(true); - }, - function() { - expect(false).toBe(true); - }); - } - }); });