Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BZ 1490103 - Fixed issue for VM custom button actions #1022

Merged
merged 3 commits into from
Sep 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions client/app/services/custom-button/custom-button.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ function CustomButtonController ($state, EventNotifications, CollectionsApi, RBA

function invokeCustomAction (button) {
if (button.resource_action && button.resource_action.dialog_id) {
$state.go('services.custom_button_details', {
const options = {
button: button,
serviceId: vm.serviceId
})
}
if (vm.vmId) {
options.vmId = vm.vmId
}
$state.go('services.custom_button_details', options)
} else if (vm.vmId) {
const data = {action: button.name}
CollectionsApi.post('vms', vm.vmId, {}, data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ function getStates () {
templateUrl,
controller: StateController,
controllerAs: 'vm',
title: __('Service Custom Button Details'),
title: __('Custom Button Details'),
params: {
button: {
value: null
},
serviceId: {
value: null
},
vmId: {
value: null
}
},
resolve: {
Expand Down Expand Up @@ -51,6 +54,7 @@ function StateController ($state, $stateParams, dialog, service, CollectionsApi,
vm.dialogs = dialog.content
vm.service = service
vm.serviceId = $stateParams.serviceId
vm.vmId = $stateParams.vmId || null
vm.button = $stateParams.button
vm.submitCustomButton = submitCustomButton
vm.submitButtonEnabled = false
Expand All @@ -69,19 +73,31 @@ function StateController ($state, $stateParams, dialog, service, CollectionsApi,
}

function submitCustomButton () {
const buttonClass = vm.button.applies_to_class.toLowerCase()
const collection = buttonClass === 'servicetemplate' ? 'services' : buttonClass === 'vm' ? 'vms' : null
let collection = 'services'
let itemId = vm.serviceId

if (vm.vmId) {
collection = 'vms'
itemId = vm.vmId
}

CollectionsApi.post(
collection,
$stateParams.serviceId,
itemId,
{},
angular.toJson({action: $stateParams.button.name, resource: vm.dialogData})
).then(submitSuccess, submitFailure)

function submitSuccess (result) {
EventNotifications.success(result.message)
$state.go('services.details', {serviceId: $stateParams.serviceId})
let stateName = 'services.details'
let parameters = {serviceId: vm.serviceId}

if (vm.vmId) {
stateName = 'services.resource-details'
parameters = {serviceId: vm.serviceId, vmId: vm.vmId}
}
$state.go(stateName, parameters)
}

function submitFailure (result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,80 @@ describe('services.custom_button_details', function() {
});
});
});

describe('Custom button actions for a VM', () => {
let collectionsApiSpy;
let controller;
let notificationsErrorSpy;
let notificationsSuccessSpy;
let refreshSingleFieldSpy;
const dialogFields = [{
name: 'dialogField1',
default_value: '1'
}, {
name: 'dialogField2',
default_value: '2'
}];
const dialog = {
dialog_tabs: [{
dialog_groups: [{
dialog_fields: dialogFields
}]
}]
};
const button = {
name: 'buttonName',
applies_to_id: 456,
applies_to_class: 'vm'
};

beforeEach(function () {
bard.inject('$controller', '$log', '$state', '$stateParams', '$rootScope', 'CollectionsApi', 'Notifications', 'DialogFieldRefresh');

refreshSingleFieldSpy = sinon.stub(DialogFieldRefresh, 'refreshDialogField');

controller = $controller($state.get('services.custom_button_details').controller, {
dialog: { content: [dialog], id: 213 },
service: {},
$stateParams: {
dialogId: 213,
button: button,
serviceId: 123,
vmId: 456,
serviceTemplateCatalogId: 321
}
});
const dialogData = {
"validations": {
"isValid": true
},
"data": {
"dialogField1": 1,
"dialogField2": 2
}
};
controller.setDialogData(dialogData);
});
it('POSTs to the vms API', () => {
const successResponse = {
message: 'Great Success!'
};

collectionsApiSpy = sinon.stub(CollectionsApi, 'post').returns(Promise.resolve(successResponse));
notificationsSuccessSpy = sinon.spy(Notifications, 'success');

controller.submitCustomButton();
expect(collectionsApiSpy).to.have.been.calledWith(
'vms',
456,
{},
'{"action":"buttonName","resource":{"dialogField1":1,"dialogField2":2}}'
);
});
it('goes to the resource details', function(done) {
controller.submitCustomButton();
done();
expect($state.is('services.resource-details')).to.be.true;
});
})
});