Skip to content

Commit

Permalink
Merge pull request #2885 from eclarizio/BZ1518390
Browse files Browse the repository at this point in the history
Pass additional information to the API when ordering a service catalog
  • Loading branch information
Dan Clarizio authored Dec 4, 2017
2 parents 3f552a2 + cad3a39 commit 6e2fb56
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
ManageIQ.angular.app.controller('dialogUserController', ['API', 'dialogFieldRefreshService', 'miqService', 'dialogId', 'apiSubmitEndpoint', 'apiAction', 'finishSubmitEndpoint', 'cancelEndpoint', function(API, dialogFieldRefreshService, miqService, dialogId, apiSubmitEndpoint, apiAction, finishSubmitEndpoint, cancelEndpoint) {
ManageIQ.angular.app.controller('dialogUserController', ['API', 'dialogFieldRefreshService', 'miqService', 'dialogId', 'apiSubmitEndpoint', 'apiAction', 'finishSubmitEndpoint', 'cancelEndpoint', 'resourceActionId', 'targetId', 'targetType', function(API, dialogFieldRefreshService, miqService, dialogId, apiSubmitEndpoint, apiAction, finishSubmitEndpoint, cancelEndpoint, resourceActionId, targetId, targetType) {
var vm = this;

vm.$onInit = function() {
return new Promise(function(resolve) {
resolve(API.get('/api/service_dialogs/' + dialogId, {expand: 'resources', attributes: 'content'}).then(init));
var url = '/api/service_dialogs/' + dialogId +
'?resource_action_id=' + resourceActionId +
'&target_id=' + targetId +
'&target_type=' + targetType;

resolve(API.get(url, {expand: 'resources', attributes: 'content'}).then(init));
});
};

Expand All @@ -21,7 +26,14 @@ ManageIQ.angular.app.controller('dialogUserController', ['API', 'dialogFieldRefr
vm.saveable = saveable;

function refreshField(field) {
return dialogFieldRefreshService.refreshField(vm.dialogData, [field.name], vm.refreshUrl, dialogId);
var idList = {
dialogId: dialogId,
resourceActionId: resourceActionId,
targetId: targetId,
targetType: targetType
};

return dialogFieldRefreshService.refreshField(vm.dialogData, [field.name], vm.refreshUrl, idList);
}

function setDialogData(data) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
ManageIQ.angular.app.service('dialogFieldRefreshService', ['miqService', function(miqService) {
this.areFieldsBeingRefreshed = false;

this.refreshField = function(dialogData, dialogField, url, resourceId) {
this.refreshField = function(dialogData, dialogField, url, idList) {
this.areFieldsBeingRefreshed = true;
var data = angular.toJson({
action: 'refresh_dialog_fields',
resource: {
dialog_fields: dialogData,
fields: dialogField,
resource_action_id: idList.resourceActionId,
target_id: idList.targetId,
target_type: idList.targetType,
},
});

return new Promise(function(resolve) {
miqService.jqueryRequest(url + resourceId, {data: data, dataType: 'json'}).then(function(response) {
miqService.jqueryRequest(url + idList.dialogId, {data: data, dataType: 'json'}).then(function(response) {
resolve(response.result[dialogField]);
if ($.active < 1) {
this.areFieldsBeingRefreshed = false;
Expand Down
31 changes: 3 additions & 28 deletions app/controllers/application_controller/buttons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,9 @@ def custom_buttons(ids = nil)
:target_kls => obj.class.name,
}

options[:dialog_locals] = determine_dialog_locals_for_custom_button(obj, button.name)
options[:dialog_locals] = DialogLocalService.new.determine_dialog_locals_for_custom_button(
obj, button.name, button.resource_action.id
)

dialog_initialize(button.resource_action, options)

Expand All @@ -356,33 +358,6 @@ def custom_buttons(ids = nil)
end
end

def determine_dialog_locals_for_custom_button(obj, button_name)
case obj.class.name.demodulize
when /Vm/
api_collection_name = "vms"
cancel_endpoint = "/vm_infra/explorer"
force_old_dialog_use = false
when /Service/
api_collection_name = "services"
cancel_endpoint = "/service/explorer"
force_old_dialog_use = false
when /GenericObject/
api_collection_name = "generic_objects"
cancel_endpoint = "/generic_object/show_list"
force_old_dialog_use = false
else
force_old_dialog_use = true
end

{
:force_old_dialog_use => force_old_dialog_use,
:api_submit_endpoint => "/api/#{api_collection_name}/#{obj.id}",
:api_action => button_name,
:finish_submit_endpoint => cancel_endpoint,
:cancel_endpoint => cancel_endpoint
}
end

def get_available_dialogs
@edit[:new][:available_dialogs] = {}
Dialog.all.each do |d|
Expand Down
16 changes: 9 additions & 7 deletions app/controllers/catalog_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -543,13 +543,15 @@ def svc_catalog_provision
options[:header] = @right_cell_text
options[:target_id] = st.id
options[:target_kls] = st.class.name
options[:dialog_locals] = {
:api_submit_endpoint => "/api/service_catalogs/#{st.service_template_catalog_id}/service_templates/#{st.id}",
:api_action => "order",
:finish_submit_endpoint => svc_catalog_provision_finish_submit_endpoint,
:cancel_endpoint => "/catalog/explorer"
}
dialog_initialize(ra, options)
options[:dialog_locals] = DialogLocalService.new.determine_dialog_locals_for_svc_catalog_provision(
ra, st, svc_catalog_provision_finish_submit_endpoint
)

if Settings.product.old_dialog_user_ui
dialog_initialize(ra, options)
else
replace_right_cell(:action => "dialog_provision", :dialog_locals => options[:dialog_locals])
end
else
# if catalog item has no dialog and provision button was pressed from list view
add_flash(_("No Ordering Dialog is available"), :warning)
Expand Down
47 changes: 47 additions & 0 deletions app/services/dialog_local_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
class DialogLocalService
def determine_dialog_locals_for_svc_catalog_provision(resource_action, target, finish_submit_endpoint)
api_submit_endpoint = "/api/service_catalogs/#{target.service_template_catalog_id}/service_templates/#{target.id}"

{
:resource_action_id => resource_action.id,
:target_id => target.id,
:target_type => target.class.name.underscore,
:dialog_id => resource_action.dialog_id,
:force_old_dialog_use => false,
:api_submit_endpoint => api_submit_endpoint,
:api_action => "order",
:finish_submit_endpoint => finish_submit_endpoint,
:cancel_endpoint => "/catalog/explorer"
}
end

def determine_dialog_locals_for_custom_button(obj, button_name, resource_action_id)
case obj.class.name.demodulize
when /Vm/
api_collection_name = "vms"
cancel_endpoint = "/vm_infra/explorer"
force_old_dialog_use = false
when /Service/
api_collection_name = "services"
cancel_endpoint = "/service/explorer"
force_old_dialog_use = false
when /GenericObject/
api_collection_name = "generic_objects"
cancel_endpoint = "/generic_object/show_list"
force_old_dialog_use = false
else
force_old_dialog_use = true
end

{
:resource_action_id => resource_action_id,
:target_id => obj.id,
:target_type => obj.class.name.demodulize.underscore,
:force_old_dialog_use => force_old_dialog_use,
:api_submit_endpoint => "/api/#{api_collection_name}/#{obj.id}",
:api_action => button_name,
:finish_submit_endpoint => cancel_endpoint,
:cancel_endpoint => cancel_endpoint
}
end
end
9 changes: 8 additions & 1 deletion app/views/shared/dialogs/_dialog_provision.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
- api_action ||= @dialog_locals[:api_action]
- finish_submit_endpoint ||= @dialog_locals[:finish_submit_endpoint]
- cancel_endpoint ||= @dialog_locals[:cancel_endpoint]
- target_id ||= @dialog_locals[:target_id]
- target_type ||= @dialog_locals[:target_type]
- dialog_id ||= @dialog_locals[:dialog_id]
- resource_action_id ||= @dialog_locals[:resource_action_id]
- force_old_dialog_use = @dialog_locals[:force_old_dialog_use].to_s == "true"

- force_old_dialog_use ||= true if api_submit_endpoint.nil?
Expand Down Expand Up @@ -39,7 +43,10 @@

- else
= render :partial => "shared/dialogs/dialog_user",
:locals => {:wf => wf,
:locals => {:dialog_id => dialog_id,
:resource_action_id => resource_action_id,
:target_id => target_id,
:target_type => target_type,
:finish_submit_endpoint => finish_submit_endpoint,
:api_submit_endpoint => api_submit_endpoint,
:api_action => api_action,
Expand Down
5 changes: 4 additions & 1 deletion app/views/shared/dialogs/_dialog_user.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
'on-click' => "vm.cancelClicked($event)"}
:javascript
ManageIQ.angular.app.value('dialogId', '#{wf.dialog.id}');
ManageIQ.angular.app.value('resourceActionId', '#{resource_action_id}');
ManageIQ.angular.app.value('targetId', '#{target_id}');
ManageIQ.angular.app.value('targetType', '#{target_type}');
ManageIQ.angular.app.value('dialogId', '#{dialog_id}');
ManageIQ.angular.app.value('finishSubmitEndpoint', '#{finish_submit_endpoint}');
ManageIQ.angular.app.value('apiSubmitEndpoint', '#{api_submit_endpoint}');
ManageIQ.angular.app.value('apiAction', '#{api_action}');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ describe('dialogUserController', function() {

spyOn(dialogFieldRefreshService, 'refreshField');
spyOn(miqService, 'miqAjaxButton');
spyOn(miqService, 'redirectBack');
spyOn(miqService, 'sparkleOn');
spyOn(miqService, 'sparkleOff');

Expand All @@ -29,6 +28,9 @@ describe('dialogUserController', function() {
apiAction: 'order',
cancelEndpoint: 'cancel endpoint',
finishSubmitEndpoint: 'finish submit endpoint',
resourceActionId: '789',
targetId: '987',
targetType: 'targettype',
});
}));

Expand All @@ -38,7 +40,10 @@ describe('dialogUserController', function() {
});

it('requests the current dialog based on the service template', function() {
expect(API.get).toHaveBeenCalledWith('/api/service_dialogs/1234', {expand: 'resources', attributes: 'content'});
expect(API.get).toHaveBeenCalledWith(
'/api/service_dialogs/1234?resource_action_id=789&target_id=987&target_type=targettype',
{expand: 'resources', attributes: 'content'}
);
});

it('resolves the request and stores the information in the dialog property', function() {
Expand All @@ -63,7 +68,7 @@ describe('dialogUserController', function() {
'dialogData',
['dialogName'],
'/api/service_dialogs/',
'1234'
{dialogId: '1234', resourceActionId: '789', targetId: '987', targetType: 'targettype'}
);
});
});
Expand All @@ -85,6 +90,7 @@ describe('dialogUserController', function() {

context('when the API call succeeds', function() {
beforeEach(function() {
spyOn(miqService, 'redirectBack');
spyOn(API, 'post').and.returnValue(Promise.resolve('awesome'));
});

Expand Down Expand Up @@ -122,6 +128,7 @@ describe('dialogUserController', function() {

context('when the API call fails', function() {
beforeEach(function() {
spyOn(miqService, 'redirectBack');
spyOn(API, 'post').and.returnValue(Promise.reject('not awesome'));
spyOn(window, 'add_flash');
});
Expand Down
14 changes: 11 additions & 3 deletions spec/javascripts/services/dialog_field_refresh_service_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ describe('dialogFieldRefreshService', function() {
var data = 'the data';
var field = 'the field';
var url = 'url';
var resourceId = '123';
var idList = {
dialogId: '123',
resourceActionId: '321',
targetId: '456',
targetType: 'service_template',
};

var refreshPromise;
var resolvedValue;

beforeEach(function(done) {
refreshPromise = testDialogFieldRefreshService.refreshField(data, field, url, resourceId);
refreshPromise = testDialogFieldRefreshService.refreshField(data, field, url, idList);

refreshPromise.then(function(value) {
resolvedValue = value;
Expand All @@ -45,7 +50,10 @@ describe('dialogFieldRefreshService', function() {
action: 'refresh_dialog_fields',
resource: {
dialog_fields: 'the data',
fields: 'the field'
fields: 'the field',
resource_action_id: '321',
target_id: '456',
target_type: 'service_template',
}
};

Expand Down
81 changes: 81 additions & 0 deletions spec/services/dialog_local_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
describe DialogLocalService do
let(:service) { described_class.new }

describe "#determine_dialog_locals_for_svc_catalog_provision" do
let(:resource_action) { instance_double("ResourceAction", :id => 456, :dialog_id => 654) }
let(:target) { instance_double("ServiceTemplate", :class => ServiceTemplate, :id => 321, :service_template_catalog_id => 123) }
let(:finish_submit_endpoint) { "finishsubmitendpoint" }

it "returns a hash" do
expect(service.determine_dialog_locals_for_svc_catalog_provision(
resource_action, target, finish_submit_endpoint
)).to eq(
:resource_action_id => 456,
:target_id => 321,
:target_type => 'service_template',
:dialog_id => 654,
:force_old_dialog_use => false,
:api_submit_endpoint => "/api/service_catalogs/123/service_templates/321",
:api_action => "order",
:finish_submit_endpoint => "finishsubmitendpoint",
:cancel_endpoint => "/catalog/explorer"
)
end
end

describe "#determine_dialog_locals_for_custom_button" do
let(:button_name) { "custom-button-name" }
let(:resource_action_id) { 321 }

context "when the object is a Vm" do
let(:obj) { double(:class => ManageIQ::Providers::Vmware::InfraManager::Vm, :id => 123) }

it "returns a hash" do
expect(service.determine_dialog_locals_for_custom_button(obj, button_name, resource_action_id)).to eq(
:resource_action_id => 321,
:target_id => 123,
:target_type => 'vm',
:force_old_dialog_use => false,
:api_submit_endpoint => "/api/vms/123",
:api_action => "custom-button-name",
:finish_submit_endpoint => "/vm_infra/explorer",
:cancel_endpoint => "/vm_infra/explorer"
)
end
end

context "when the object is a Service" do
let(:obj) { double(:class => ManageIQ::Providers::Vmware::InfraManager::Service, :id => 123) }

it "returns a hash" do
expect(service.determine_dialog_locals_for_custom_button(obj, button_name, resource_action_id)).to eq(
:resource_action_id => 321,
:target_id => 123,
:target_type => 'service',
:force_old_dialog_use => false,
:api_submit_endpoint => "/api/services/123",
:api_action => "custom-button-name",
:finish_submit_endpoint => "/service/explorer",
:cancel_endpoint => "/service/explorer"
)
end
end

context "when the object is a GenericObject" do
let(:obj) { double(:class => ManageIQ::Providers::Vmware::InfraManager::GenericObject, :id => 123) }

it "returns a hash" do
expect(service.determine_dialog_locals_for_custom_button(obj, button_name, resource_action_id)).to eq(
:resource_action_id => 321,
:target_id => 123,
:target_type => 'generic_object',
:force_old_dialog_use => false,
:api_submit_endpoint => "/api/generic_objects/123",
:api_action => "custom-button-name",
:finish_submit_endpoint => "/generic_object/show_list",
:cancel_endpoint => "/generic_object/show_list"
)
end
end
end
end

0 comments on commit 6e2fb56

Please sign in to comment.