From 7d390c38f0bcd935ac1770d1fd440c4b0cd9db08 Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Mon, 1 Jul 2024 18:06:11 -0400 Subject: [PATCH 1/3] change order of supports to avoid call to supports? and unsupports? --- app/models/host.rb | 12 ++++++------ app/models/vm/operations/power.rb | 18 +++++++++--------- app/models/vm_or_template/operations.rb | 6 +++--- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/app/models/host.rb b/app/models/host.rb index eb1da4b6464..b201058ac7c 100644 --- a/app/models/host.rb +++ b/app/models/host.rb @@ -197,18 +197,18 @@ class Host < ApplicationRecord # if you change this, please check in on VmWare#start supports :start do - if !supports?(:ipmi) - unsupported_reason(:ipmi) - elsif power_state != "off" + if power_state != "off" _("The Host is not in power state off") + else + unsupported_reason(:ipmi) end end supports :stop do - if !supports?(:ipmi) - unsupported_reason(:ipmi) - elsif power_state != "on" + if power_state != "on" _("The Host is not in powered on") + else + unsupported_reason(:ipmi) end end diff --git a/app/models/vm/operations/power.rb b/app/models/vm/operations/power.rb index 043ac64e26c..57838d5a916 100644 --- a/app/models/vm/operations/power.rb +++ b/app/models/vm/operations/power.rb @@ -7,26 +7,26 @@ module Vm::Operations::Power api_relay_method :suspend supports :suspend do - if !supports?(:control) - unsupported_reason(:control) - elsif !vm_powered_on? + if !vm_powered_on? _('The VM is not powered on') + else + unsupported_reason(:control) end end supports :start do - if !supports?(:control) - unsupported_reason(:control) - elsif vm_powered_on? + if vm_powered_on? _('The VM is powered on') + else + unsupported_reason(:control) end end supports :stop do - if !supports?(:control) - unsupported_reason(:control) - elsif !vm_powered_on? + if !vm_powered_on? _('The VM is not powered on') + else + unsupported_reason(:control) end end end diff --git a/app/models/vm_or_template/operations.rb b/app/models/vm_or_template/operations.rb index 5f28460c496..05554541a54 100644 --- a/app/models/vm_or_template/operations.rb +++ b/app/models/vm_or_template/operations.rb @@ -148,10 +148,10 @@ def log_user_event(user_event) end supports :vm_control_powered_on do - if !supports?(:control) - unsupported_reason(:control) - elsif current_state != "on" + if current_state != "on" "The VM is not powered on" + else + unsupported_reason(:control) end end end From 23c8f55159d8fa1b5f6b6d6c1a22d5e1b71681d6 Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Mon, 1 Jul 2024 18:06:57 -0400 Subject: [PATCH 2/3] extract Vm#supports_hash This condenses the logic and avoids calling supports and unsupports for the same key --- app/models/vm.rb | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/app/models/vm.rb b/app/models/vm.rb index a7b1f6afe7c..bbdb5fce1e4 100644 --- a/app/models/vm.rb +++ b/app/models/vm.rb @@ -118,9 +118,9 @@ def remote_console_url=(url, user_id) def supported_consoles { - :html5 => html5_support, - :vmrc => vmrc_support, - :native => native_support + :html5 => support_hash(:html5_console, :launch_html5_console), + :vmrc => support_hash(:vmrc_console, :launch_vmrc_console), + :native => support_hash(:native_console, :launch_native_console) } end @@ -144,27 +144,12 @@ def self.display_name(number = 1) private - def html5_support + def support_hash(visible, launch) + reason = unsupported_reason(launch) { - :visible => supports?(:html5_console), - :enabled => supports?(:launch_html5_console), - :message => unsupported_reason(:launch_html5_console) - } - end - - def vmrc_support - { - :visible => supports?(:vmrc_console), - :enabled => supports?(:launch_vmrc_console), - :message => unsupported_reason(:launch_vmrc_console) - } - end - - def native_support - { - :visible => supports?(:native_console), - :enabled => supports?(:launch_native_console), - :message => unsupported_reason(:launch_native_console) + :visible => supports?(visible), + :enabled => !reason, + :message => reason } end From c0f9c8ebf79a258294dcedcbc2d03403f7f91ee7 Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Mon, 1 Jul 2024 18:07:41 -0400 Subject: [PATCH 3/3] remove unsupported_reason and supports? call Use a variable to avoid calling supports and unsupported reason on the same code --- app/models/storage.rb | 4 ++-- app/models/vm.rb | 6 +++--- app/models/vm_scan/dispatcher.rb | 3 +-- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/app/models/storage.rb b/app/models/storage.rb index 9b8f79d1ea9..f781b8218d2 100644 --- a/app/models/storage.rb +++ b/app/models/storage.rb @@ -344,10 +344,10 @@ def self.scan_timer(zone_name = nil) end def scan(userid = "system", _role = "ems_operations") - unless supports?(:smartstate_analysis) + if (reason = unsupported_reason(:smartstate_analysis)) raise(MiqException::MiqUnsupportedStorage, _("Action not supported for Datastore type [%{store_type}], [%{name}] with id: [%{id}] %{error}") % - {:store_type => store_type, :name => name, :id => id, :error => unsupported_reason(:smartstate_analysis)}) + {:store_type => store_type, :name => name, :id => id, :error => reason}) end task_name = "SmartState Analysis for [#{name}]" diff --git a/app/models/vm.rb b/app/models/vm.rb index bbdb5fce1e4..e50b80b1ca1 100644 --- a/app/models/vm.rb +++ b/app/models/vm.rb @@ -78,9 +78,9 @@ def self.find_all_by_mac_address_and_hostname_and_ipaddress(mac_address, hostnam def running_processes pl = {} - unless supports?(:collect_running_processes) - _log.warn(unsupported_reason(:collect_running_processes)) - raise unsupported_reason(:collect_running_processes) + if (reason = unsupported_reason(:collect_running_processes)) + _log.warn(reason) + raise reason end begin diff --git a/app/models/vm_scan/dispatcher.rb b/app/models/vm_scan/dispatcher.rb index 3d05719f0ee..1d924725d25 100644 --- a/app/models/vm_scan/dispatcher.rb +++ b/app/models/vm_scan/dispatcher.rb @@ -246,8 +246,7 @@ def get_eligible_proxies_for_job(job) return [] end - unless @vm.supports?(:smartstate_analysis) - msg = @vm.unsupported_reason(:smartstate_analysis) + if (msg = @vm.unsupported_reason(:smartstate_analysis)) queue_signal(job, {:args => [:abort, msg, "error"]}) return [] end