Skip to content

Commit

Permalink
Merge pull request ManageIQ#174 from vecerek/miq_pr_13111
Browse files Browse the repository at this point in the history
Toolbar refactoring: VM buttons
  • Loading branch information
mzazrivec authored Jan 17, 2017
2 parents 87a5fa1 + d3d6ea9 commit 19ec890
Show file tree
Hide file tree
Showing 24 changed files with 431 additions and 391 deletions.
5 changes: 5 additions & 0 deletions app/helpers/application_helper/button/vm_check_compliance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ApplicationHelper::Button::VmCheckCompliance < ApplicationHelper::Button::InstanceCheckCompare
def visible?
true
end
end
38 changes: 38 additions & 0 deletions app/helpers/application_helper/button/vm_console.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class ApplicationHelper::Button::VmConsole < ApplicationHelper::Button::Basic
needs :@record

def visible?
console_supports_type?('MKS')
end

def disabled?
unless supported_browser? && supported_os?
@error_message = _('The web-based console is only available on IE, Firefox or Chrome (Windows/Linux)')
return true
end

yield if block_given?
return true if @error_message.present?

@error_message = _('The web-based console is not available because the VM is not powered on') unless on?
@error_message.present?
end

private

def console_supports_type?(supported_type)
::Settings.server.remote_console_type == supported_type ? @record.console_supported?(supported_type) : false
end

def on?
@record.current_state == 'on'
end

def supported_browser?
@view_context.is_browser?(%w(explorer firefox mozilla chrome))
end

def supported_os?
@view_context.is_browser_os?(%w(windows linux))
end
end
16 changes: 16 additions & 0 deletions app/helpers/application_helper/button/vm_snapshot_add.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class ApplicationHelper::Button::VmSnapshotAdd < ApplicationHelper::Button::Basic
def disabled?
@error_message = if records_and_role_allows? && !@active
_('Select the Active snapshot to create a new snapshot for this VM')
else
@record.is_available_now_error_message(:create_snapshot)
end
@error_message.present?
end

private

def records_and_role_allows?
@record.is_available?(:create_snapshot)
end
end
9 changes: 9 additions & 0 deletions app/helpers/application_helper/button/vm_snapshot_revert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,13 @@ def visible?
return false if @record.kind_of?(ManageIQ::Providers::Openstack::CloudManager::Vm)
super
end

def disabled?
@error_message = if @active
_('Select a snapshot that is not the active one')
elsif !@record.is_available?(:revert_to_snapshot)
@record.is_available_now_error_message(:revert_to_snapshot)
end
@error_message.present?
end
end
13 changes: 13 additions & 0 deletions app/helpers/application_helper/button/vm_timeline.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class ApplicationHelper::Button::VmTimeline < ApplicationHelper::Button::Basic
needs :@record

def disabled?
@error_message = _('No Timeline data has been collected for this VM') unless proper_events?
end

private

def proper_events?
@record.has_events? || @record.has_events?(:policy_events)
end
end
19 changes: 19 additions & 0 deletions app/helpers/application_helper/button/vm_vmrc_console.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class ApplicationHelper::Button::VmVmrcConsole < ApplicationHelper::Button::VmConsole
needs :@record

def visible?
console_supports_type?('VMRC')
end

def disabled?
super { remote_control_supported? }
end

private

def remote_control_supported?
@record.validate_remote_console_vmrc_support
rescue MiqException::RemoteConsoleNotSupportedError => err
@error_message = _('VM VMRC Console error: %{error}') % {:error => err}
end
end
13 changes: 13 additions & 0 deletions app/helpers/application_helper/button/vm_vnc_console.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class ApplicationHelper::Button::VmVncConsole < ApplicationHelper::Button::VmConsole
needs :@record

def visible?
return console_supports_type?('VNC') if @record.vendor == 'vmware'
@record.console_supported?('vnc')
end

def disabled?
@error_message = _('The web-based VNC console is not available because the VM is not powered on') unless on?
@error_message.present?
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ def self.included(included_class)
'pficon pficon-screen fa-lg',
N_('Open a web-based VNC or SPICE console for this VM'),
N_('VM Console'),
:url => "html5_console"),
:url => "html5_console",
:klass => ApplicationHelper::Button::VmVncConsole),
]
),
])
Expand Down
3 changes: 2 additions & 1 deletion app/helpers/application_helper/toolbar/vm_infras_center.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ class ApplicationHelper::Toolbar::VmInfrasCenter < ApplicationHelper::Toolbar::B
:url_parms => "main_div",
:confirm => N_("Initiate Check Compliance of the last known configuration for the selected items?"),
:enabled => false,
:onwhen => "1+"),
:onwhen => "1+",
:klass => ApplicationHelper::Button::VmCheckCompliance),
]
),
])
Expand Down
3 changes: 2 additions & 1 deletion app/helpers/application_helper/toolbar/vms_center.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ class ApplicationHelper::Toolbar::VmsCenter < ApplicationHelper::Toolbar::Basic
:url_parms => "main_div",
:confirm => N_("Initiate Check Compliance of the last known configuration for the selected items?"),
:enabled => false,
:onwhen => "1+"),
:onwhen => "1+",
:klass => ApplicationHelper::Button::VmCheckCompliance),
]
),
])
Expand Down
26 changes: 18 additions & 8 deletions app/helpers/application_helper/toolbar/x_vm_center.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ class ApplicationHelper::Toolbar::XVmCenter < ApplicationHelper::Toolbar::Basic
'fa fa-search fa-lg',
N_('Check Compliance of the last known configuration for this VM'),
N_('Check Compliance of Last Known Configuration'),
:confirm => N_("Initiate Check Compliance of the last known configuration for this VM?")),
:confirm => N_("Initiate Check Compliance of the last known configuration for this VM?"),
:klass => ApplicationHelper::Button::VmCheckCompliance),
]
),
])
Expand Down Expand Up @@ -161,7 +162,8 @@ class ApplicationHelper::Toolbar::XVmCenter < ApplicationHelper::Toolbar::Basic
'product product-timeline fa-lg',
N_('Show Timelines for this VM'),
N_('Timelines'),
:url_parms => "?display=timeline"),
:url_parms => "?display=timeline",
:klass => ApplicationHelper::Button::VmTimeline),
]
),
])
Expand Down Expand Up @@ -243,20 +245,23 @@ class ApplicationHelper::Toolbar::XVmCenter < ApplicationHelper::Toolbar::Basic
N_('Open a web-based console for this VM'),
N_('VM Console'),
:url => "console",
:confirm => N_("Opening a VM web-based console can take a while and requires that the VMware MKS plugin version configured for Management Engine already be installed and working. Are you sure?")),
:confirm => N_("Opening a VM web-based console can take a while and requires that the VMware MKS plugin version configured for Management Engine already be installed and working. Are you sure?"),
:klass => ApplicationHelper::Button::VmConsole),
button(
:vm_vnc_console,
'pficon pficon-screen fa-lg',
N_('Open a web-based VNC or SPICE console for this VM'),
N_('VM Console'),
:url => "html5_console"),
:url => "html5_console",
:klass => ApplicationHelper::Button::VmVncConsole),
button(
:vm_vmrc_console,
'pficon pficon-screen fa-lg',
N_('Open a web-based VMRC console for this VM. This requires that VMRC is pre-configured to work in your browser.'),
N_('VM Console'),
:url => "vmrc_console",
:confirm => N_("Opening a VM web-based VMRC console requires that VMRC is pre-configured to work in your browser. Are you sure?")),
:confirm => N_("Opening a VM web-based VMRC console requires that VMRC is pre-configured to work in your browser. Are you sure?"),
:klass => ApplicationHelper::Button::VmVmrcConsole),
button(
:cockpit_console,
'pficon pficon-screen fa-lg',
Expand All @@ -273,7 +278,8 @@ class ApplicationHelper::Toolbar::XVmCenter < ApplicationHelper::Toolbar::Basic
'pficon pficon-add-circle-o fa-lg',
N_('Create a new snapshot for this VM'),
nil,
:onwhen => "1"),
:onwhen => "1",
:klass => ApplicationHelper::Button::VmSnapshotAdd),
select(
:vm_delete_snap_choice,
'pficon pficon-delete fa-lg',
Expand All @@ -287,13 +293,17 @@ class ApplicationHelper::Toolbar::XVmCenter < ApplicationHelper::Toolbar::Basic
t,
:confirm => N_("The selected snapshot will be permanently deleted. Are you sure you want to delete the selected snapshot?"),
:url_parms => "main_div",
:onwhen => "1"),
:onwhen => "1",
:klass => ApplicationHelper::Button::GenericFeatureButtonWithDisable,
:options => {:feature => :remove_snapshot}),
button(
:vm_snapshot_delete_all,
'pficon pficon-delete fa-lg',
t = N_('Delete All Existing Snapshots'),
t,
:confirm => N_("Delete all of this VMs existing snapshots?")),
:confirm => N_("Delete all of this VMs existing snapshots?"),
:klass => ApplicationHelper::Button::GenericFeatureButtonWithDisable,
:options => {:feature => :remove_all_snapshots}),
]
),
button(
Expand Down
59 changes: 0 additions & 59 deletions app/helpers/application_helper/toolbar_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -443,19 +443,6 @@ def hide_button?(id)
case id
when "miq_task_canceljob"
return true unless ["all_tasks", "all_ui_tasks"].include?(@layout)
when "vm_console"
type = ::Settings.server.remote_console_type
return type != 'MKS' || !@record.console_supported?(type)
when "vm_vnc_console"
if @record.vendor == 'vmware'
# remote_console_type is only useful for vmware consoles
type = ::Settings.server.remote_console_type
return type != 'VNC' || !@record.console_supported?(type)
end
return !@record.console_supported?('vnc')
when "vm_vmrc_console"
type = ::Settings.server.remote_console_type
return type != 'VMRC' || !@record.console_supported?(type)
# Check buttons behind SMIS setting
when "ontap_storage_system_statistics", "ontap_logical_disk_statistics", "ontap_storage_volume_statistics",
"ontap_file_share_statistics"
Expand Down Expand Up @@ -665,52 +652,6 @@ def disable_button(id)
case id
when "instance_perf", "vm_perf", "container_perf"
return N_("No Capacity & Utilization data has been collected for this VM") unless @record.has_perf_data?
when "vm_check_compliance"
unless @record.has_compliance_policies?
return N_("No Compliance Policies assigned to this virtual machine")
end
when "vm_console", "vm_vmrc_console"
if !is_browser?(%w(explorer firefox mozilla chrome)) ||
!is_browser_os?(%w(windows linux))
return N_("The web-based console is only available on IE, Firefox or Chrome (Windows/Linux)")
end

if id.in?(["vm_vmrc_console"])
begin
@record.validate_remote_console_vmrc_support
rescue MiqException::RemoteConsoleNotSupportedError => err
return N_("VM VMRC Console error: %{error}") % {:error => err}
end
end

if @record.current_state != "on"
return N_("The web-based console is not available because the VM is not powered on")
end
when "vm_vnc_console"
if @record.current_state != "on"
return N_("The web-based VNC console is not available because the VM is not powered on")
end
when "vm_timeline"
unless @record.has_events? || @record.has_events?(:policy_events)
return N_("No Timeline data has been collected for this VM")
end
when "vm_snapshot_add"
if @record.number_of(:snapshots) <= 0
return @record.is_available_now_error_message(:create_snapshot) unless @record.is_available?(:create_snapshot)
else
unless @record.is_available?(:create_snapshot)
return @record.is_available_now_error_message(:create_snapshot)
else
return N_("Select the Active snapshot to create a new snapshot for this VM") unless @active
end
end
when "vm_snapshot_delete"
return @record.is_available_now_error_message(:remove_snapshot) unless @record.is_available?(:remove_snapshot)
when "vm_snapshot_delete_all"
return @record.is_available_now_error_message(:remove_all_snapshots) unless @record.is_available?(:remove_all_snapshots)
when "vm_snapshot_revert"
return N_("Select a snapshot that is not the active one") if @active
return @record.is_available_now_error_message(:revert_to_snapshot) unless @record.is_available?(:revert_to_snapshot)
end
when "MiqTemplate"
case id
Expand Down
17 changes: 3 additions & 14 deletions spec/helpers/application_helper/buttons/container_timeline_spec.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
describe ApplicationHelper::Button::ContainerTimeline do
let(:record) { FactoryGirl.create(:container) }
subject do
let(:button) do
described_class.new(setup_view_context_with_sandbox({}), {}, {'record' => record},
{:options => {:entity => 'Container'}})
end

before { allow(record).to receive(:has_events?).and_return(has_events) }

describe '#disabled?' do
%i(ems_events policy_events).each do |event_type|
context "record has #{event_type}" do
let(:has_events) { true }
it { expect(subject.disabled?).to be_falsey }
end
end
context 'record has no ems_events or policy_events' do
let(:has_events) { false }
it { expect(subject.disabled?).to be_truthy }
end
describe '#calculate_properties?' do
it_behaves_like 'timeline#calculate_properties', 'No Timeline data has been collected for this Container'
end
end
13 changes: 13 additions & 0 deletions spec/helpers/application_helper/buttons/vm_console_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
describe ApplicationHelper::Button::VmConsole do
let(:view_context) { setup_view_context_with_sandbox({}) }
let(:record) { FactoryGirl.create(:vm) }
let(:button) { described_class.new(view_context, {}, {'record' => record}, {}) }

describe '#visible?' do
it_behaves_like 'vm_console_visible?', 'MKS'
end

describe '#calculate_properties' do
it_behaves_like 'vm_console_calculate_properties'
end
end
48 changes: 48 additions & 0 deletions spec/helpers/application_helper/buttons/vm_snapshot_add_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
describe ApplicationHelper::Button::VmSnapshotAdd do
let(:view_context) { setup_view_context_with_sandbox({}) }
let(:zone) { EvmSpecHelper.local_miq_server(:is_master => true).zone }
let(:ems) { FactoryGirl.create(:ems_vmware, :zone => zone, :name => 'Test EMS') }
let(:host) { FactoryGirl.create(:host) }
let(:record) { FactoryGirl.create(:vm_vmware, :ems_id => ems.id, :host_id => host.id) }
let(:active) { true }
let(:button) { described_class.new(view_context, {}, {'record' => record, 'active' => active}, {}) }

describe '#calculate_properties' do
before { button.calculate_properties }
context 'when creating snapshots is available' do
let(:current) { 1 }
let(:record) do
record = FactoryGirl.create(:vm_vmware, :ems_id => ems.id, :host_id => host.id)
record.snapshots = [FactoryGirl.create(:snapshot,
:create_time => 1.minute.ago,
:vm_or_template_id => record.id,
:name => 'EvmSnapshot',
:description => "Some Description",
:current => current)]
record
end
context 'and the selected snapshot is not active' do
let(:active) { false }
it_behaves_like 'a disabled button', 'Select the Active snapshot to create a new snapshot for this VM'
end
context 'and the selected snapshot may be active but the vm is not connected to a host' do
let(:record) { FactoryGirl.create(:vm_vmware) }
it_behaves_like 'a disabled button', 'The VM is not connected to a Host'
end
context 'and the selected snapshot is active and current' do
context 'and current' do
it_behaves_like 'an enabled button'
end
context 'but not current' do
let(:current) { 0 }
it_behaves_like 'a disabled button',
'At least one snapshot has to be active to create a new snapshot for this VM'
end
end
end
context 'when creating snapshots is not available' do
let(:record) { FactoryGirl.create(:vm_amazon) }
it_behaves_like 'a disabled button', 'Create Snapshot operation not supported for Amazon VM'
end
end
end
Loading

0 comments on commit 19ec890

Please sign in to comment.