Skip to content

Commit

Permalink
Add dialog locals for many objects and refactor to a service
Browse files Browse the repository at this point in the history
https://www.pivotaltracker.com/story/show/152449649

Custom buttons supported in this commit:
CloudTenant
CloudVolume
ContainerNode
EmsCluster
Host
InfraManager (Provider)
MiqTemplate
Storage
  • Loading branch information
eclarizio committed Nov 30, 2017
1 parent 4a66843 commit 5032ed2
Show file tree
Hide file tree
Showing 3 changed files with 225 additions and 32 deletions.
37 changes: 5 additions & 32 deletions app/controllers/application_controller/buttons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ 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] = custom_button_service.determine_dialog_locals_for_custom_button(obj, button.name)

dialog_initialize(button.resource_action, options)

Expand All @@ -356,37 +356,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
when /Host/
api_collection_name = "hosts"
cancel_endpoint = "/host"
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 Expand Up @@ -1309,4 +1278,8 @@ def build_filter_exp_table
@visibility_expression_table = @custom_button.visibility_expression.kind_of?(MiqExpression) ? exp_build_table(@custom_button.visibility_expression.exp) : nil
@enablement_expression_table = @custom_button.enablement_expression.kind_of?(MiqExpression) ? exp_build_table(@custom_button.enablement_expression.exp) : nil
end

def custom_button_service
@custom_button_service ||= CustomButtonService.new
end
end
75 changes: 75 additions & 0 deletions app/services/custom_button_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
class CustomButtonService
NEW_DIALOG_USERS = %w(
CloudTenant
CloudVolume
ContainerNode
EmsCluster
GenericObject
Host
InfraManager
MiqTemplate
Service
Storage
Vm
).freeze

def determine_dialog_locals_for_custom_button(obj, button_name)
dialog_locals = {:force_old_dialog_use => true}

return dialog_locals unless NEW_DIALOG_USERS.include?(obj.class.name.demodulize)

submit_endpoint, cancel_endpoint = determine_api_endpoints(obj)

dialog_locals[:force_old_dialog_use] = false
dialog_locals[:api_submit_endpoint] = submit_endpoint
dialog_locals[:api_action] = button_name
dialog_locals[:finish_submit_endpoint] = cancel_endpoint
dialog_locals[:cancel_endpoint] = cancel_endpoint

dialog_locals
end

private

def determine_api_endpoints(obj)
case obj.class.name.demodulize
when /CloudTenant/
api_collection_name = "cloud_tenants"
cancel_endpoint = "/cloud_tenant"
when /CloudVolume/
api_collection_name = "cloud_volumes"
cancel_endpoint = "/cloud_volume"
when /ContainerNode/
api_collection_name = "container_nodes"
cancel_endpoint = "/container_node"
when /EmsCluster/
api_collection_name = "clusters"
cancel_endpoint = "/ems_cluster"
when /GenericObject/
api_collection_name = "generic_objects"
cancel_endpoint = "/generic_object/show_list"
when /Host/
api_collection_name = "hosts"
cancel_endpoint = "/host"
when /InfraManager/
api_collection_name = "providers"
cancel_endpoint = "/ems_infra"
when /MiqTemplate/
api_collection_name = "templates"
cancel_endpoint = "/vm_or_template/explorer"
when /Service/
api_collection_name = "services"
cancel_endpoint = "/service/explorer"
when /Storage/
api_collection_name = "datastores"
cancel_endpoint = "/storage/explorer"
when /Vm/
api_collection_name = "vms"
cancel_endpoint = "/vm_infra/explorer"
end

submit_endpoint = "/api/#{api_collection_name}/#{obj.id}"

return submit_endpoint, cancel_endpoint
end
end
145 changes: 145 additions & 0 deletions spec/services/custom_button_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
describe CustomButtonService do
let(:service) { described_class.new }

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

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

it "returns a hash" do
expect(service.determine_dialog_locals_for_custom_button(obj, button_name)).to eq(
:force_old_dialog_use => false,
:api_submit_endpoint => "/api/cloud_tenants/123",
:api_action => "custom-button-name",
:cancel_endpoint => "/cloud_tenant"
)
end
end

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

it "returns a hash" do
expect(service.determine_dialog_locals_for_custom_button(obj, button_name)).to eq(
:force_old_dialog_use => false,
:api_submit_endpoint => "/api/container_nodes/123",
:api_action => "custom-button-name",
:cancel_endpoint => "/container_node"
)
end
end

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

it "returns a hash" do
expect(service.determine_dialog_locals_for_custom_button(obj, button_name)).to eq(
:force_old_dialog_use => false,
:api_submit_endpoint => "/api/clusters/123",
:api_action => "custom-button-name",
:cancel_endpoint => "/ems_cluster"
)
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)).to eq(
:force_old_dialog_use => false,
:api_submit_endpoint => "/api/generic_objects/123",
:api_action => "custom-button-name",
:cancel_endpoint => "/generic_object/show_list"
)
end
end

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

it "returns a hash" do
expect(service.determine_dialog_locals_for_custom_button(obj, button_name)).to eq(
:force_old_dialog_use => false,
:api_submit_endpoint => "/api/hosts/123",
:api_action => "custom-button-name",
:cancel_endpoint => "/host"
)
end
end

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

it "returns a hash" do
expect(service.determine_dialog_locals_for_custom_button(obj, button_name)).to eq(
:force_old_dialog_use => false,
:api_submit_endpoint => "/api/providers/123",
:api_action => "custom-button-name",
:cancel_endpoint => "/ems_infra"
)
end
end

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

it "returns a hash" do
expect(service.determine_dialog_locals_for_custom_button(obj, button_name)).to eq(
:force_old_dialog_use => false,
:api_submit_endpoint => "/api/templates/123",
:api_action => "custom-button-name",
:cancel_endpoint => "/vm_or_template/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)).to eq(
:force_old_dialog_use => false,
:api_submit_endpoint => "/api/services/123",
:api_action => "custom-button-name",
:cancel_endpoint => "/service/explorer"
)
end
end

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

it "returns a hash" do
expect(service.determine_dialog_locals_for_custom_button(obj, button_name)).to eq(
:force_old_dialog_use => false,
:api_submit_endpoint => "/api/datastores/123",
:api_action => "custom-button-name",
:cancel_endpoint => "/storage/explorer"
)
end
end

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)).to eq(
:force_old_dialog_use => false,
:api_submit_endpoint => "/api/vms/123",
:api_action => "custom-button-name",
:cancel_endpoint => "/vm_infra/explorer"
)
end
end

context "when the object does not support new dialogs" do
let(:obj) { double(:id => 123) }

it "returns a hash with 'force_old_dialog_use' set to true" do
expect(service.determine_dialog_locals_for_custom_button(obj, button_name)).to eq(:force_old_dialog_use => true)
end
end
end
end

0 comments on commit 5032ed2

Please sign in to comment.