-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dialog locals for many objects and refactor to a service
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
Showing
3 changed files
with
225 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |