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 1 commit
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
16 changes: 12 additions & 4 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,18 @@ function CustomButtonController ($state, EventNotifications, CollectionsApi, RBA

function invokeCustomAction (button) {
if (button.resource_action && button.resource_action.dialog_id) {
$state.go('services.custom_button_details', {
button: button,
serviceId: vm.serviceId
})
if (vm.vmId) {
$state.go('services.vm_custom_button_details', {
button: button,
vmId: vm.vmId,
serviceId: vm.serviceId
})
} else {
$state.go('services.custom_button_details', {
button: button,
serviceId: vm.serviceId
})
}
} 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 @@ -25,6 +25,28 @@ function getStates () {
dialog: resolveDialog,
service: resolveService
}
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these two states appear very similar, instead of duplicating the code with a mild name change, why not make the vmId an optional param on the existing state?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought about it. It is really just a judgement call. Initially I wanted to make it this way so we could refresh the page successfully but it looks like we are passing button data and that isn't reproducible so we can't refresh either way. I am kinda fine either way.

'services.vm_custom_button_details': {
url: '/:serviceId/vm/:vmId/custom_button_details',
templateUrl,
controller: StateController,
controllerAs: 'vm',
title: __('VM Custom Button Details'),
params: {
button: {
value: null
},
serviceId: {
value: null
},
vmId: {
value: null
}
},
resolve: {
dialog: resolveDialog,
service: resolveService
}
}
}
}
Expand All @@ -51,6 +73,7 @@ function StateController ($state, $stateParams, dialog, service, CollectionsApi,
vm.dialogs = dialog.content
vm.service = service
vm.serviceId = $stateParams.serviceId
vm.vmId = ($stateParams.vmId ? $stateParams.vmId : null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$stateParams.vmId || null ternary might be a bit of overkill

vm.button = $stateParams.button
vm.submitCustomButton = submitCustomButton
vm.submitButtonEnabled = false
Expand All @@ -69,19 +92,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', () => {
var collectionsApiSpy;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer, const or let to var. consider stacking all the ones without assignments on the same line

var controller;
var notificationsErrorSpy;
var notificationsSuccessSpy;
var refreshSingleFieldSpy;
var dialogFields = [{
name: 'dialogField1',
default_value: '1'
}, {
name: 'dialogField2',
default_value: '2'
}];
var dialog = {
dialog_tabs: [{
dialog_groups: [{
dialog_fields: dialogFields
}]
}]
};
var 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.vm_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;
});
})
});