diff --git a/app/controllers/application_controller/buttons.rb b/app/controllers/application_controller/buttons.rb index 14dcadaa921..7932c40a1c4 100644 --- a/app/controllers/application_controller/buttons.rb +++ b/app/controllers/application_controller/buttons.rb @@ -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, button.resource_action.id) + options[:dialog_locals] = DialogLocalService.new.determine_dialog_locals_for_custom_button( + obj, button.name, button.resource_action.id + ) dialog_initialize(button.resource_action, options) @@ -356,36 +358,6 @@ def custom_buttons(ids = nil) end 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.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 - def get_available_dialogs @edit[:new][:available_dialogs] = {} Dialog.all.each do |d| diff --git a/app/controllers/catalog_controller.rb b/app/controllers/catalog_controller.rb index 9b8518d7730..a807e5559bb 100644 --- a/app/controllers/catalog_controller.rb +++ b/app/controllers/catalog_controller.rb @@ -543,16 +543,9 @@ def svc_catalog_provision options[:header] = @right_cell_text options[:target_id] = st.id options[:target_kls] = st.class.name - options[:dialog_locals] = { - :resource_action_id => ra.id, - :target_id => st.id, - :target_type => st.class.name.underscore, - :dialog_id => ra.dialog_id, - :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" - } + options[:dialog_locals] = DialogLocalService.new.determine_dialog_locals_for_svc_catalog_provision( + ra, st, svc_catalog_provision_finish_submit_endpoint + ) replace_right_cell(:action => "dialog_provision", :dialog_locals => options[:dialog_locals]) else diff --git a/app/services/dialog_local_service.rb b/app/services/dialog_local_service.rb new file mode 100644 index 00000000000..329e2e288f2 --- /dev/null +++ b/app/services/dialog_local_service.rb @@ -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 diff --git a/spec/services/dialog_local_service_spec.rb b/spec/services/dialog_local_service_spec.rb new file mode 100644 index 00000000000..0dd5a418fbb --- /dev/null +++ b/spec/services/dialog_local_service_spec.rb @@ -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