diff --git a/client/app/core/dialog-field-refresh.service.js b/client/app/core/dialog-field-refresh.service.js index 1d783bc92..b0801e668 100644 --- a/client/app/core/dialog-field-refresh.service.js +++ b/client/app/core/dialog-field-refresh.service.js @@ -10,17 +10,20 @@ export function DialogFieldRefreshFactory (CollectionsApi) { return service - function refreshDialogField (dialogData, dialogField, url, resourceId) { + function refreshDialogField (dialogData, dialogField, url, idList) { return new Promise((resolve, reject) => { CollectionsApi.post( url, - resourceId, + idList.dialogId, {}, angular.toJson({ action: 'refresh_dialog_fields', resource: { dialog_fields: dialogData, - fields: dialogField + fields: dialogField, + resource_action_id: idList.resourceActionId, + target_id: idList.targetId, + target_type: idList.targetType } }) ).then((response) => { diff --git a/client/app/core/dialog-field-refresh.service.spec.js b/client/app/core/dialog-field-refresh.service.spec.js index 7fe08a1f8..886779c52 100644 --- a/client/app/core/dialog-field-refresh.service.spec.js +++ b/client/app/core/dialog-field-refresh.service.spec.js @@ -5,76 +5,89 @@ describe('DialogFieldRefresh', () => { bard.inject('CollectionsApi', 'Notifications', 'DialogFieldRefresh') }) - it('allows a dialog field to be refreshed', () => { - const successResponse = { - result: { - dialog1: { - data_type: 'string', - options: 'options', - read_only: false, - required: false, - values: 'Text' + describe('#refreshDialogField', () => { + it('allows a dialog field to be refreshed', () => { + const successResponse = { + result: { + dialog1: { + data_type: 'string', + options: 'options', + read_only: false, + required: false, + values: 'Text' + } } } - } - const collectionsApiSpy = sinon.stub(CollectionsApi, 'post').returns(Promise.resolve(successResponse)) - const dialogData = { - 'dialog1': 'value1', - 'dialog2': 'value2' - } - DialogFieldRefresh.refreshDialogField(dialogData, ['dialog1'], '/test/1', 1234).then((response) => { - expect(collectionsApiSpy).to.have.been.calledWith('/test/1', - 1234, - {}, - '{"action":"refresh_dialog_fields","resource":{"dialog_fields":{"dialog1":"value1","dialog2":"value2"},"fields":["dialog1"]}}') + const collectionsApiSpy = sinon.stub(CollectionsApi, 'post').returns(Promise.resolve(successResponse)) + const dialogData = { + 'dialog1': 'value1', + 'dialog2': 'value2' + } + const idList = { + dialogId: 1234, + resourceActionId: 4321, + targetId: 3241, + targetType: 'targetType' + } + DialogFieldRefresh.refreshDialogField(dialogData, ['dialog1'], '/test/1', idList).then((response) => { + expect(collectionsApiSpy).to.have.been.calledWith('/test/1', + 1234, + {}, + '{"action":"refresh_dialog_fields","resource":{"dialog_fields":{"dialog1":"value1","dialog2":"value2"},"fields":["dialog1"], "resource_action_id": "4321", "target_id": "3241", "target_type": "targetType"}}') + }) }) - }) - it('reports back when a field fails to refresh', () => { - const dialogData = { - 'dialog1': 'value1', - 'dialog2': 'value2' - } - const failureResponse = {'status': 'failed'} - sinon.stub(CollectionsApi, 'post').returns(Promise.reject(failureResponse)) - DialogFieldRefresh.refreshDialogField(dialogData, ['dialog1'], '/test/1', 1234).then((response) => { + it('reports back when a field fails to refresh', () => { + const dialogData = { + 'dialog1': 'value1', + 'dialog2': 'value2' + } + const idList = { + dialogId: 1234, + resourceActionId: 4321, + targetId: 3241, + targetType: 'targetType' + } + const failureResponse = {'status': 'failed'} + sinon.stub(CollectionsApi, 'post').returns(Promise.reject(failureResponse)) + DialogFieldRefresh.refreshDialogField(dialogData, ['dialog1'], '/test/1', idList).then((response) => { - }).catch((err) => { - expect(err).to.eq(failureResponse) + }).catch((err) => { + expect(err).to.eq(failureResponse) + }) }) }) - it('can override defaults values for a dialog', () => { - const testDialog = { - dialog_tabs: [ - { - dialog_groups: [ - { - dialog_fields: [ - {'name': 'test1', 'default_value': 'test1'}, - {'name': 'test2', 'default_value': 'test2'} - ] - } - ] - } - ] - } - const testValues = { - dialog_test1: 'modifiedTest1', - dialog_test2: 'modifiedTest2' - } + describe('#setFieldValueDefaults', () => { + it('can override defaults values for a dialog', () => { + const testDialog = { + dialog_tabs: [{ + dialog_groups: [{ + dialog_fields: [ + {'name': 'test1', 'default_value': 'test1'}, + {'name': 'test2', 'default_value': 'test2'} + ] + }] + }] + } + + const testValues = { + dialog_test1: 'modifiedTest1', + dialog_test2: 'modifiedTest2' + } - const expectedDialog = { - 'dialog_tabs': [{ - 'dialog_groups': [{ - 'dialog_fields': [{ - 'name': 'test1', - 'default_value': 'modifiedTest1' - }, {'name': 'test2', 'default_value': 'modifiedTest2'}] + const expectedDialog = { + 'dialog_tabs': [{ + 'dialog_groups': [{ + 'dialog_fields': [{ + 'name': 'test1', + 'default_value': 'modifiedTest1' + }, {'name': 'test2', 'default_value': 'modifiedTest2'}] + }] }] - }] - } - const modifiedDialog = DialogFieldRefresh.setFieldValueDefaults(testDialog, testValues) - expect(modifiedDialog).to.deep.eq(expectedDialog) + } + const modifiedDialog = DialogFieldRefresh.setFieldValueDefaults(testDialog, testValues) + expect(modifiedDialog).to.deep.eq(expectedDialog) + }) }) }) diff --git a/client/app/states/services/custom_button_details/custom_button_details.state.js b/client/app/states/services/custom_button_details/custom_button_details.state.js index 0e5f6f9e7..3587621dc 100644 --- a/client/app/states/services/custom_button_details/custom_button_details.state.js +++ b/client/app/states/services/custom_button_details/custom_button_details.state.js @@ -32,12 +32,13 @@ function getStates () { function StateController ($state, $stateParams, CollectionsApi, EventNotifications, DialogFieldRefresh) { var vm = this vm.title = __('Custom button action') - vm.dialogId = '' + vm.dialogId = $stateParams.dialogId || '' vm.dialogs = {} vm.service = {} vm.serviceId = $stateParams.serviceId vm.vmId = $stateParams.vmId || null vm.button = $stateParams.button + vm.resourceAction = vm.button.resource_action vm.submitCustomButton = submitCustomButton vm.submitButtonEnabled = false vm.dialogUrl = 'service_dialogs/' @@ -48,7 +49,7 @@ function StateController ($state, $stateParams, CollectionsApi, EventNotificatio function init () { const options = {expand: 'resources', attributes: 'content'} - const dialogId = vm.button.resource_action.dialog_id + const dialogId = vm.resourceAction.dialog_id const resolveDialogs = CollectionsApi.query('service_dialogs/' + dialogId, options) const resolveService = CollectionsApi.get('services', $stateParams.serviceId, {attributes: ['picture', 'picture.image_href']}) @@ -63,7 +64,21 @@ function StateController ($state, $stateParams, CollectionsApi, EventNotificatio } init() function refreshField (field) { - return DialogFieldRefresh.refreshDialogField(vm.dialogData, [field.name], vm.dialogUrl, vm.dialogId) + let targetType = 'service' + let targetId = vm.serviceId + if (vm.vmId) { + targetType = 'vm' + targetId = vm.vmId + } + + let idList = { + dialogId: vm.dialogId, + resourceActionId: vm.resourceAction.id, + targetId: targetId, + targetType: targetType + } + + return DialogFieldRefresh.refreshDialogField(vm.dialogData, [field.name], vm.dialogUrl, idList) } function setDialogData (data) { diff --git a/client/app/states/services/custom_button_details/custom_button_details.state.spec.js b/client/app/states/services/custom_button_details/custom_button_details.state.spec.js index 0489b819e..a623facd4 100644 --- a/client/app/states/services/custom_button_details/custom_button_details.state.spec.js +++ b/client/app/states/services/custom_button_details/custom_button_details.state.spec.js @@ -1,35 +1,51 @@ /* global $state, $controller, CollectionsApi, Notifications, DialogFieldRefresh */ /* eslint-disable no-unused-expressions */ describe('State: services.custom_button_details', () => { + let dialog, dialogData, dialogFields, button + beforeEach(() => { module('app.states') - }) - describe('controller', () => { - let collectionsApiSpy, controller, notificationsErrorSpy, notificationsSuccessSpy - const dialogFields = [{ + dialogFields = [{ name: 'dialogField1', default_value: '1' }, { name: 'dialogField2', default_value: '2' }] - const dialog = { + + dialog = { dialog_tabs: [{ dialog_groups: [{ dialog_fields: dialogFields }] }] } - const button = { + + button = { name: 'buttonName', applies_to_id: 456, applies_to_class: 'servicetemplate', resource_action: { - dialog_id: 1 + dialog_id: 1, + id: 789 } } + dialogData = { + 'validations': { + 'isValid': true + }, + 'data': { + 'dialogField1': 1, + 'dialogField2': 2 + } + } + }) + + describe('controller', () => { + let collectionsApiSpy, controller, notificationsErrorSpy, notificationsSuccessSpy + beforeEach(() => { bard.inject('$controller', '$log', '$state', '$stateParams', '$rootScope', 'CollectionsApi', 'Notifications', 'DialogFieldRefresh') @@ -46,15 +62,6 @@ describe('State: services.custom_button_details', () => { serviceTemplateCatalogId: 321 } }) - const dialogData = { - 'validations': { - 'isValid': true - }, - 'data': { - 'dialogField1': 1, - 'dialogField2': 2 - } - } controller.setDialogData(dialogData) }) @@ -147,7 +154,8 @@ describe('State: services.custom_button_details', () => { applies_to_id: 456, applies_to_class: 'vm', resource_action: { - dialog_id: 1 + dialog_id: 1, + id: 789 } } @@ -168,15 +176,7 @@ describe('State: services.custom_button_details', () => { serviceTemplateCatalogId: 321 } }) - const dialogData = { - 'validations': { - 'isValid': true - }, - 'data': { - 'dialogField1': 1, - 'dialogField2': 2 - } - } + controller.setDialogData(dialogData) }) it('POSTs to the vms API', (done) => { @@ -203,4 +203,69 @@ describe('State: services.custom_button_details', () => { expect($state.is('services.resource-details')).to.be.true }) }) + + describe('controller#refreshField', () => { + let controller + + describe('when the vmId does not exist', () => { + beforeEach(() => { + bard.inject('$controller', '$state', '$stateParams', 'CollectionsApi', 'Notifications', 'DialogFieldRefresh') + 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, + serviceTemplateCatalogId: 321 + } + }) + + controller.setDialogData(dialogData) + }) + + it('delegates to DialogFieldRefresh with the right id list', () => { + controller.refreshField({name: 'fieldName'}) + expect(DialogFieldRefresh.refreshDialogField).to.have.been.calledWith( + dialogData.data, + ['fieldName'], + 'service_dialogs/', + {dialogId: 213, resourceActionId: 789, targetId: 123, targetType: 'service'} + ) + }) + }) + + describe('when the vmId exists', () => { + beforeEach(() => { + bard.inject('$controller', '$state', '$stateParams', 'CollectionsApi', 'Notifications', 'DialogFieldRefresh') + 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 + } + }) + + controller.setDialogData(dialogData) + }) + + it('delegates to DialogFieldRefresh with the right id list', () => { + controller.refreshField({name: 'fieldName'}) + expect(DialogFieldRefresh.refreshDialogField).to.have.been.calledWith( + dialogData.data, + ['fieldName'], + 'service_dialogs/', + {dialogId: 213, resourceActionId: 789, targetId: 456, targetType: 'vm'} + ) + }) + }) + }) })