Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert console_supported? to supports? #864

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module ManageIQ::Providers::Vmware::CloudManager::Vm::RemoteConsole
def console_supported?(type)
%w(WEBMKS).include?(type.upcase)
extend ActiveSupport::Concern

included do
supports :console
supports :html5_console
supports :webmks_console
end

def validate_remote_console_acquire_ticket(protocol, options = {})
Expand Down
22 changes: 9 additions & 13 deletions app/models/manageiq/providers/vmware/infra_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ class Vmware::InfraManager < InfraManager
supports :label_mapping
supports :metrics
supports :native_console
supports :vmrc_console do
if api_version.blank? || hostname.blank? || uid_ems.blank?
"vCenter needs to be refreshed to determine remote console support."
elsif ext_management_system.authentication_type(:console).nil?
"remote console requires console credentials"
end
end
supports :webmks_console

supports :provisioning
supports :smartstate_analysis
supports :streaming_refresh do
Expand Down Expand Up @@ -332,19 +341,6 @@ def remote_console_vmrc_acquire_ticket
ticket
end

def remote_console_vmrc_support_known?
!api_version.blank? && !hostname.blank? && !uid_ems.blank?
end

def validate_remote_console_vmrc_support
raise(MiqException::RemoteConsoleNotSupportedError, "vCenter needs to be refreshed to determine VMRC remote console support.") unless self.remote_console_vmrc_support_known?
true
end

def validate_remote_console_webmks_support
true
end

def after_update_authentication
super
stop_refresh_worker_queue_on_credential_change
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
module ManageIQ::Providers::Vmware::InfraManager::Vm::RemoteConsole
def console_supported?(type)
%w(VMRC VNC WEBMKS).include?(type.upcase)
end

def validate_remote_console_acquire_ticket(protocol, options = {})
raise(MiqException::RemoteConsoleNotSupportedError, "#{protocol} remote console requires the vm to be registered with a management system.") if ext_management_system.nil?

raise(MiqException::RemoteConsoleNotSupportedError, "remote console requires console credentials") if ext_management_system.authentication_type(:console).nil? && protocol == "vmrc"

options[:check_if_running] = true unless options.key?(:check_if_running)
raise(MiqException::RemoteConsoleNotSupportedError, "#{protocol} remote console requires the vm to be running.") if options[:check_if_running] && state != "on"
extend ActiveSupport::Concern

included do
supports :console do
if ext_management_system.nil?
"VM must be registered with a management system."
elsif state != "on"
"VM must be running."
end
end
supports(:html5_console) { unsupported_reason(:console) }
supports :vmrc_console do
unsupported_reason(:console) ||
ext_management_system.unsupported_reason(:vmrc_console)
end
supports :vnc_console { unsupported_reason(:console) }
supports :webmks_console do
unsupported_reason(:console) ||
ext_management_system.unsupported_reason(:webmks_console)
end
end

def remote_console_acquire_ticket(userid, originating_server, protocol)
Expand Down Expand Up @@ -41,7 +50,7 @@ def remote_console_acquire_ticket_queue(protocol, userid)
#

def remote_console_vmrc_acquire_ticket(_userid = nil, _originating_server = nil)
validate_remote_console_acquire_ticket("vmrc")
validate_supports(:vmrc_console)
ticket = ext_management_system.remote_console_vmrc_acquire_ticket

{
Expand All @@ -51,18 +60,12 @@ def remote_console_vmrc_acquire_ticket(_userid = nil, _originating_server = nil)
}
end

def validate_remote_console_vmrc_support
validate_remote_console_acquire_ticket("vmrc")
ext_management_system.validate_remote_console_vmrc_support
true
end

#
# WebMKS
#

def remote_console_webmks_acquire_ticket(userid, originating_server = nil)
validate_remote_console_acquire_ticket("webmks")
validate_supports(:webmks_console)
ticket = ext_management_system.vm_remote_console_webmks_acquire_ticket(self)

SystemConsole.force_vm_invalid_token(id)
Expand All @@ -79,12 +82,6 @@ def remote_console_webmks_acquire_ticket(userid, originating_server = nil)
SystemConsole.launch_proxy_if_not_local(console_args, originating_server, ticket['host'].to_s, ticket['port'].to_i)
end

def validate_remote_console_webmks_support
validate_remote_console_acquire_ticket("webmks")
ext_management_system.validate_remote_console_webmks_support
true
end

#
# HTML5 selects the best available console type (VNC or WebMKS)
#
Expand All @@ -99,7 +96,7 @@ def remote_console_html5_acquire_ticket(userid, originating_server = nil)
def remote_console_vnc_acquire_ticket(userid, originating_server)
require 'securerandom'

validate_remote_console_acquire_ticket("vnc")
validate_supports(:vnc_console)

password = SecureRandom.base64[0, 8] # Random password from the Base64 character set
host_port = host.reserve_next_available_vnc_port
Expand Down Expand Up @@ -138,6 +135,12 @@ def remote_console_vnc_acquire_ticket(userid, originating_server)

private

def validate_supports(feature)
if (unsupported_reason = unsupported_reason(feature))
raise(MiqException::RemoteConsoleNotSupportedError, unsupported_reason)
end
end

# Method to generate the remote URI for the VMRC console
def build_vmrc_url(ticket)
url = URI::Generic.build(:scheme => "vmrc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,25 +126,24 @@
end
end

context '#validate_remote_console_webmks_support' do
context '#supports?(:webmks_console)' do
before do
ems.authentications << FactoryBot.create(:authentication, :authtype => :console, :userid => "root", :password => "vmware")
end

it 'normal case' do
ems.update_attribute(:api_version, '6.0')
expect(vm.validate_remote_console_webmks_support).to be_truthy
expect(vm.supports?(:webmks_console)).to be_truthy
end

it 'with vm with no ems' do
vm.ext_management_system = nil
vm.save!
expect { vm.validate_remote_console_webmks_support }.to raise_error MiqException::RemoteConsoleNotSupportedError
vm.update!(:ext_management_system => nil)
expect(vm.supports?(:webmks_console)).to be_falsey
end

it 'with vm off' do
vm.update_attribute(:raw_power_state, 'poweredOff')
expect { vm.validate_remote_console_webmks_support }.to raise_error MiqException::RemoteConsoleNotSupportedError
vm.update(:raw_power_state => 'poweredOff')
expect(vm.supports?(:webmks_console)).to be_falsey
end
end

Expand Down Expand Up @@ -180,24 +179,24 @@
end
end

context '#validate_remote_console_vmrc_support' do
context '#supports?(:vmrc_console)' do
before do
ems.authentications << FactoryBot.create(:authentication, :authtype => :console, :userid => "root", :password => "vmware")
end

it 'normal case' do
expect(vm.validate_remote_console_vmrc_support).to be_truthy
expect(vm.supports?(:vmrc_console)).to be_truthy
end

it 'with vm with no ems' do
vm.ext_management_system = nil
vm.save!
expect { vm.validate_remote_console_vmrc_support }.to raise_error MiqException::RemoteConsoleNotSupportedError
expect(vm.supports?(:vmrc_console)).to be_falsey
end

it 'with vm off' do
vm.update_attribute(:raw_power_state, 'poweredOff')
expect { vm.validate_remote_console_vmrc_support }.to raise_error MiqException::RemoteConsoleNotSupportedError
expect(vm.supports?(:vmrc_console)).to be_falsey
end
end

Expand Down
20 changes: 10 additions & 10 deletions spec/models/manageiq/providers/vmware/infra_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,50 +155,50 @@
end
end

context "#validate_remote_console_vmrc_support" do
context "#supports?(:vmrc_console)" do
before(:each) do
@ems = FactoryBot.create(:ems_vmware)
end

it "raise for missing/blank values" do
@ems.update(:api_version => "", :uid_ems => "2E1C1E82-BD83-4E54-9271-630C6DFAD4D1")
expect { @ems.validate_remote_console_vmrc_support }.to raise_error MiqException::RemoteConsoleNotSupportedError
expect(@ems.supports?(:vmrc_console)).to be_falsey
end
end

context "#remote_console_vmrc_support_known?" do
context "#supports?(:vmrc_console)" do
before(:each) do
@ems = FactoryBot.create(:ems_vmware)
@ems = FactoryBot.create(:ems_vmware_with_authentication, :authtype => 'console')
end

it "true with nothing missing/blank" do
@ems.update(:api_version => "5.0", :uid_ems => "2E1C1E82-BD83-4E54-9271-630C6DFAD4D1")
expect(@ems.remote_console_vmrc_support_known?).to be_truthy
expect(@ems.supports?(:vmrc_console)).to be_truthy
end

it "false for blank hostname" do
@ems.update(:hostname => "", :api_version => "5.0", :uid_ems => "2E1C1E82-BD83-4E54-9271-630C6DFAD4D1")
expect(@ems.remote_console_vmrc_support_known?).not_to be_truthy
expect(@ems.supports?(:vmrc_console)).not_to be_truthy
end

it "false for missing api_version" do
@ems.update(:api_version => nil, :uid_ems => "2E1C1E82-BD83-4E54-9271-630C6DFAD4D1")
expect(@ems.remote_console_vmrc_support_known?).not_to be_truthy
expect(@ems.supports?(:vmrc_console)).not_to be_truthy
end

it "false for blank api_version" do
@ems.update(:api_version => "", :uid_ems => "2E1C1E82-BD83-4E54-9271-630C6DFAD4D1")
expect(@ems.remote_console_vmrc_support_known?).not_to be_truthy
expect(@ems.supports?(:vmrc_console)).not_to be_truthy
end

it "false for missing uid_ems" do
@ems.update(:api_version => "5.0", :uid_ems => nil)
expect(@ems.remote_console_vmrc_support_known?).not_to be_truthy
expect(@ems.supports?(:vmrc_console)).not_to be_truthy
end

it "false for blank uid_ems" do
@ems.update(:api_version => "5.0", :uid_ems => "")
expect(@ems.remote_console_vmrc_support_known?).not_to be_truthy
expect(@ems.supports?(:vmrc_console)).not_to be_truthy
end
end

Expand Down
Loading