Skip to content

Commit

Permalink
Merge pull request #11715 from romanblanco/toolbar_button_refactoring…
Browse files Browse the repository at this point in the history
…_server_role

Toolbar button refactoring server role
  • Loading branch information
martinpovolny authored Nov 2, 2016
2 parents 0be7ad8 + eadc3fc commit 716e611
Show file tree
Hide file tree
Showing 14 changed files with 408 additions and 159 deletions.
10 changes: 10 additions & 0 deletions app/helpers/application_helper/button/role_power_options.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class ApplicationHelper::Button::RolePowerOptions < ApplicationHelper::Button::Basic
needs :@record, :@sb

def visible?
@view_context.x_active_tree == :diagnostics_tree &&
%w(diagnostics_roles_servers diagnostics_servers_roles).include?(@sb[:active_tab]) &&
@record.class == AssignedServerRole &&
@record.miq_server.started?
end
end
21 changes: 21 additions & 0 deletions app/helpers/application_helper/button/role_start.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class ApplicationHelper::Button::RoleStart < ApplicationHelper::Button::RolePowerOptions
needs :@record, :@sb

def calculate_properties
super
self[:title] = @error_message if disabled?
end

def disabled?
@error_message = if @record.class == AssignedServerRole
if @record.active
N_("This Role is already active on this Server")
elsif !@record.miq_server.started?
N_("Only available Roles on active Servers can be started")
elsif @view_context.x_node != "root" && @record.server_role.regional_role?
N_("This role can only be managed at the Region level")
end
end
@error_message.present?
end
end
26 changes: 26 additions & 0 deletions app/helpers/application_helper/button/role_suspend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class ApplicationHelper::Button::RoleSuspend < ApplicationHelper::Button::RolePowerOptions
needs :@record, :@sb

def calculate_properties
super
self[:title] = @error_message if disabled?
end

def disabled?
@error_message = if @view_context.x_node != "root" && @record.server_role.regional_role?
N_("This role can only be managed at the Region level")
else
if @record.active
unless @record.server_role.max_concurrent != 1
N_("Activate the %{server_role_description} Role on another Server to suspend it on %{server_name} [%{server_id}]") %
{:server_role_description => @record.server_role.description,
:server_name => @record.miq_server.name,
:server_id => @record.miq_server.id}
end
else
N_("Only active Roles on active Servers can be suspended")
end
end
@error_message.present?
end
end
19 changes: 19 additions & 0 deletions app/helpers/application_helper/button/server_demote.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class ApplicationHelper::Button::ServerDemote < ApplicationHelper::Button::ServerLevelOptions
needs :@record, :@sb

def calculate_properties
super
self[:title] = @error_message if disabled?
end

def disabled?
@error_message = if @record.master_supported?
if @record.priority == 1 || @record.priority == 2
if @view_context.x_node != "root" && @record.server_role.regional_role?
N_("This role can only be managed at the Region level")
end
end
end
@error_message.present?
end
end
10 changes: 10 additions & 0 deletions app/helpers/application_helper/button/server_level_options.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class ApplicationHelper::Button::ServerLevelOptions < ApplicationHelper::Button::Basic
needs :@record, :@sb

def visible?
@view_context.x_active_tree == :diagnostics_tree &&
%w(diagnostics_roles_servers diagnostics_servers_roles).include?(@sb[:active_tab]) &&
@record.class == AssignedServerRole &&
@record.master_supported?
end
end
19 changes: 19 additions & 0 deletions app/helpers/application_helper/button/server_promote.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class ApplicationHelper::Button::ServerPromote < ApplicationHelper::Button::ServerLevelOptions
needs :@record, :@sb

def calculate_properties
super
self[:title] = @error_message if disabled?
end

def disabled?
@error_message = if @record.master_supported?
if (@record.priority != 1 && @record.priority != 2) || @record.priority == 2
if @view_context.x_node != "root" && @record.server_role.regional_role?
N_("This role can only be managed at the Region level")
end
end
end
@error_message.present?
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ class ApplicationHelper::Toolbar::DiagnosticsRegionCenter < ApplicationHelper::T
:server_role_description => @record.server_role.description,
:server_name => @record.miq_server.name,
:server_id => @record.miq_server.id}
end),
end,
:klass => ApplicationHelper::Button::RoleStart
),
button(
:role_suspend,
'fa fa-pause-circle-o fa-lg',
Expand All @@ -56,7 +58,8 @@ class ApplicationHelper::Toolbar::DiagnosticsRegionCenter < ApplicationHelper::T
:server_role_description => @record.server_role.description,
:server_name => @record.miq_server.name,
:server_id => @record.miq_server.id}
end
end,
:klass => ApplicationHelper::Button::RoleSuspend
),
button(
:demote_server,
Expand All @@ -68,7 +71,8 @@ class ApplicationHelper::Toolbar::DiagnosticsRegionCenter < ApplicationHelper::T
:server_id => @record.miq_server.id}
end,
N_('Demote Server'),
:confirm => N_("Do you want to demote this Server to secondary? This will leave no primary Server for this Role.")),
:confirm => N_("Do you want to demote this Server to secondary? This will leave no primary Server for this Role."),
:klass => ApplicationHelper::Button::ServerDemote),
button(
:promote_server,
'product product-migrate fa-lg',
Expand All @@ -79,7 +83,8 @@ class ApplicationHelper::Toolbar::DiagnosticsRegionCenter < ApplicationHelper::T
:server_id => @record.miq_server.id}
end,
N_('Promote Server'),
:confirm => N_("Do you want to promote this Server to primary? This will replace any existing primary Server for this Role.")),
:confirm => N_("Do you want to promote this Server to primary? This will replace any existing primary Server for this Role."),
:klass => ApplicationHelper::Button::ServerPromote),
]
),
])
Expand Down
14 changes: 10 additions & 4 deletions app/helpers/application_helper/toolbar/diagnostics_zone_center.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class ApplicationHelper::Toolbar::DiagnosticsZoneCenter < ApplicationHelper::Too
:server_name => @record.miq_server.name,
:server_id => @record.miq_server.id
}
end
end,
:klass => ApplicationHelper::Button::RolePowerOptions
),
button(
:zone_role_suspend,
Expand All @@ -63,7 +64,8 @@ class ApplicationHelper::Toolbar::DiagnosticsZoneCenter < ApplicationHelper::Too
:server_name => @record.miq_server.name,
:server_id => @record.miq_server.id
}
end
end,
:klass => ApplicationHelper::Button::RolePowerOptions
),
button(
:zone_demote_server,
Expand All @@ -76,7 +78,9 @@ class ApplicationHelper::Toolbar::DiagnosticsZoneCenter < ApplicationHelper::Too
}
end,
N_('Demote Server'),
:confirm => N_("Do you want to demote this Server to secondary? This will leave no primary Server for this Role.")),
:confirm => N_("Do you want to demote this Server to secondary? This will leave no primary Server for this Role."),
:klass => ApplicationHelper::Button::ServerLevelOptions
),
button(
:zone_promote_server,
'product product-migrate fa-lg',
Expand All @@ -88,7 +92,9 @@ class ApplicationHelper::Toolbar::DiagnosticsZoneCenter < ApplicationHelper::Too
}
end,
N_('Promote Server'),
:confirm => N_("Do you want to promote this Server to primary? This will replace any existing primary Server for this Role.")),
:confirm => N_("Do you want to promote this Server to primary? This will replace any existing primary Server for this Role."),
:klass => ApplicationHelper::Button::ServerLevelOptions
),
]
),
select(
Expand Down
60 changes: 3 additions & 57 deletions app/helpers/application_helper/toolbar_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -425,12 +425,10 @@ def hide_button_ops(id)
return false
when "delete_server", "zone_delete_server"
return @record.class != MiqServer
when "role_start", "role_suspend", "zone_role_start", "zone_role_suspend"
return !(@record.class == AssignedServerRole && @record.miq_server.started?)
when "demote_server", "promote_server", "zone_demote_server", "zone_promote_server"
return !(@record.class == AssignedServerRole && @record.master_supported?)
when "zone_collect_current_logs", "zone_collect_logs", "zone_log_depot_edit"
return true
end
return true
return false
when "diagnostics_summary"
return !["refresh_server_summary", "restart_server"].include?(id)
when "diagnostics_workers"
Expand Down Expand Up @@ -705,16 +703,6 @@ def hide_button?(id)
else
return true unless editable_domain?(@record)
end
when "MiqServer", "MiqRegion"
case id
when "role_start", "role_suspend", "promote_server", "demote_server"
return true
end
when "ServerRole"
case id
when "server_delete", "role_start", "role_suspend", "promote_server", "demote_server"
return true
end
when "ManageIQ::Providers::AnsibleTower::ConfigurationManager::ConfiguredSystem", "ManageIQ::Providers::Foreman::ConfigurationManager::ConfiguredSystem"
case id
when "configured_system_provision"
Expand Down Expand Up @@ -752,48 +740,6 @@ def disable_button(id)
end

case get_record_cls(@record)
when "AssignedServerRole"
case id
when "role_start"
if x_node != "root" && @record.server_role.regional_role?
return N_("This role can only be managed at the Region level")
elsif @record.active
return N_("This Role is already active on this Server")
elsif !@record.miq_server.started? && !@record.active
return N_("Only available Roles on active Servers can be started")
end
when "role_suspend"
if x_node != "root" && @record.server_role.regional_role?
return N_("This role can only be managed at the Region level")
else
if @record.active
unless @record.server_role.max_concurrent != 1
return N_("Activate the %{server_role_description} Role on another Server to suspend it on %{server_name} [%{server_id}]") %
{:server_role_description => @record.server_role.description,
:server_name => @record.miq_server.name,
:server_id => @record.miq_server.id}
end
else
return N_("Only active Roles on active Servers can be suspended")
end
end
when "demote_server"
if @record.master_supported?
if @record.priority == 1 || @record.priority == 2
if x_node != "root" && @record.server_role.regional_role?
return N_("This role can only be managed at the Region level")
end
end
end
when "promote_server"
if @record.master_supported?
if (@record.priority != 1 && @record.priority != 2) || @record.priority == 2
if x_node != "root" && @record.server_role.regional_role?
return N_("This role can only be managed at the Region level")
end
end
end
end
when "AvailabilityZone"
case id
when "availability_zone_perf"
Expand Down
110 changes: 110 additions & 0 deletions spec/helpers/application_helper/buttons/role_start_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
describe ApplicationHelper::Button::RoleStart do
describe '#visible?' do
context "record is assigned server role and miq server is started" do
before do
@record = FactoryGirl.create(
:assigned_server_role,
:miq_server => FactoryGirl.create(:miq_server)
)
allow(@record.miq_server).to receive(:started?).and_return(true)
end

it "will not be skipped for this record" do
view_context = setup_view_context_with_sandbox({})
button = described_class.new(view_context, {}, {'record' => @record}, {})
button.instance_variable_set(:@sb, {:active_tab => "diagnostics_roles_servers"})
allow(view_context).to receive(:x_active_tree).and_return(:diagnostics_tree)
expect(button.visible?).to be_truthy
end
end

context "record is server role" do
before do
@record = FactoryGirl.create(:server_role, :name => "biggus_dickus")
end

it "will be skipped for this record" do
view_context = setup_view_context_with_sandbox({})
button = described_class.new(view_context, {}, {'record' => @record}, {})
button.instance_variable_set(:@sb, {:active_tab => "diagnostics_roles_servers"})
allow(view_context).to receive(:x_active_tree).and_return(:diagnostics_tree)
expect(button.visible?).to be_falsey
end
end

context "record is miq server" do
before do
@record = FactoryGirl.create(:miq_server)
end

it "will be skipped for this record" do
view_context = setup_view_context_with_sandbox({})
button = described_class.new(view_context, {}, {'record' => @record}, {})
button.instance_variable_set(:@sb, {:active_tab => "diagnostics_roles_servers"})
allow(view_context).to receive(:x_active_tree).and_return(:diagnostics_tree)
expect(button.visible?).to be_falsey
end
end
end

describe '#disabled?' do
context "record is active assigned server role" do
before do
@record = FactoryGirl.create(:assigned_server_role)
allow(@record).to receive(:active?).and_return(true)
end

it "disables the button and returns the error message" do
view_context = setup_view_context_with_sandbox({})
button = described_class.new(view_context, {}, {'record' => @record}, {})
expect(button.disabled?).to be_truthy
button.calculate_properties
expect(button[:title]).to eq("This Role is already active on this Server")
end
end

context "record is inactive assigned server role" do
before do
@record = FactoryGirl.create(
:assigned_server_role,
:active => false,
:miq_server => FactoryGirl.create(:miq_server)
)
allow(@record.miq_server).to receive(:started?).and_return(false)
end

it "disables the button and returns the error message" do
view_context = setup_view_context_with_sandbox({})
button = described_class.new(view_context, {}, {'record' => @record}, {})
expect(button.disabled?).to be_truthy
button.calculate_properties
expect(button[:title]).to eq("Only available Roles on active Servers can be started")
end
end

context "record is inactive assigned server role" do
before do
@record = FactoryGirl.create(
:assigned_server_role,
:active => false,
:miq_server => FactoryGirl.create(:miq_server),
:server_role => FactoryGirl.create(
:server_role,
:name => "dr_fetus"
)
)
allow(@record.server_role).to receive(:regional_role?).and_return(true)
allow(@record.miq_server).to receive(:started?).and_return(true)
end

it "disables the button and returns the error message" do
view_context = setup_view_context_with_sandbox({})
button = described_class.new(view_context, {}, {'record' => @record}, {})
allow(view_context).to receive(:x_node).and_return('z-1r23')
expect(button.disabled?).to be_truthy
button.calculate_properties
expect(button[:title]).to eq("This role can only be managed at the Region level")
end
end
end
end
Loading

0 comments on commit 716e611

Please sign in to comment.