-
Notifications
You must be signed in to change notification settings - Fork 91
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,28 @@ function getStates () { | |
dialog: resolveDialog, | ||
service: resolveService | ||
} | ||
}, | ||
'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 | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
vm.button = $stateParams.button | ||
vm.submitCustomButton = submitCustomButton | ||
vm.submitButtonEnabled = false | ||
|
@@ -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) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,4 +122,80 @@ describe('services.custom_button_details', function() { | |
}); | ||
}); | ||
}); | ||
|
||
describe('Custom button actions for a VM', () => { | ||
var collectionsApiSpy; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
}); | ||
}) | ||
}); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.