diff --git a/app/models/conversion_host.rb b/app/models/conversion_host.rb index 4928688f546..b4144275a10 100644 --- a/app/models/conversion_host.rb +++ b/app/models/conversion_host.rb @@ -1,6 +1,8 @@ class ConversionHost < ApplicationRecord include NewWithTypeStiMixin + VALID_RESOURCE_TYPES = %w(VmOrTemplate Host).freeze + acts_as_miq_taggable belongs_to :resource, :polymorphic => true @@ -8,6 +10,16 @@ class ConversionHost < ApplicationRecord has_many :active_tasks, -> { where(:state => 'active') }, :class_name => ServiceTemplateTransformationPlanTask, :inverse_of => :conversion_host delegate :ext_management_system, :hostname, :ems_ref, :to => :resource, :allow_nil => true + validates :name, :presence => true + validates :resource_id, :presence => true + validates :resource_type, :presence => true, :inclusion => { :in => VALID_RESOURCE_TYPES } + + validates :address, + :uniqueness => true, + :format => { :with => Resolv::AddressRegex }, + :inclusion => { :in => -> { resource.ipaddresses } }, + :unless => -> { resource.blank? || resource.ipaddresses.blank? } + include_concern 'Configurations' # To be eligible, a conversion host must have the following properties @@ -36,9 +48,9 @@ def source_transport_method end def ipaddress(family = 'ipv4') - return address if address && IPAddr.new(address).send("#{family}?") + return address if address.present? && IPAddr.new(address).send("#{family}?") resource.ipaddresses.detect { |ip| IPAddr.new(ip).send("#{family}?") } - end + end def run_conversion(conversion_options) result = connect_ssh { |ssu| ssu.shell_exec('/usr/bin/virt-v2v-wrapper.py', nil, nil, conversion_options.to_json) } @@ -65,7 +77,7 @@ def get_conversion_log(path) connect_ssh { |ssu| ssu.get_file(path, nil) } rescue => e raise "Could not get conversion log '#{path}' from '#{resource.name}' with [#{e.class}: #{e}" - end + end def check_conversion_host_role install_conversion_host_module @@ -127,7 +139,7 @@ def connect_ssh require 'MiqSshUtil' MiqSshUtil.shell_with_su(*miq_ssh_util_args) do |ssu, _shell| yield(ssu) - end + end rescue Exception => e _log.error("SSH connection failed for [#{ipaddress}] with [#{e.class}: #{e}]") raise e @@ -139,7 +151,7 @@ def miq_ssh_util_args def miq_ssh_util_args_manageiq_providers_redhat_inframanager_host [hostname || ipaddress, resource.authentication_userid, resource.authentication_password, nil, nil] - end + end def miq_ssh_util_args_manageiq_providers_openstack_cloudmanager_vm authentication = resource.ext_management_system.authentications diff --git a/spec/models/conversion_host_spec.rb b/spec/models/conversion_host_spec.rb index a5e98a95042..34501c47444 100644 --- a/spec/models/conversion_host_spec.rb +++ b/spec/models/conversion_host_spec.rb @@ -5,9 +5,9 @@ context "provider independent methods" do let(:host) { FactoryGirl.create(:host) } - let(:vm) { FactoryGirl.create(:vm_or_template) } - let(:conversion_host_1) { FactoryGirl.create(:conversion_host, :resource => host) } - let(:conversion_host_2) { FactoryGirl.create(:conversion_host, :resource => vm) } + let(:vm) { FactoryGirl.create(:vm) } + let(:conversion_host_1) { FactoryGirl.create(:conversion_host, :resource => host, :address => '10.0.0.1') } + let(:conversion_host_2) { FactoryGirl.create(:conversion_host, :resource => vm, :address => '10.0.1.1') } let(:task_1) { FactoryGirl.create(:service_template_transformation_plan_task, :state => 'active', :conversion_host => conversion_host_1) } let(:task_2) { FactoryGirl.create(:service_template_transformation_plan_task, :conversion_host => conversion_host_1) } let(:task_3) { FactoryGirl.create(:service_template_transformation_plan_task, :state => 'active', :conversion_host => conversion_host_2) } @@ -118,12 +118,12 @@ it "returns false if if kill command failed" do allow(conversion_host_1).to receive(:connect_ssh).and_raise('Unexpected failure') expect(conversion_host_1.kill_process('1234', 'KILL')).to eq(false) - end + end it "returns true if if kill command succeeded" do allow(conversion_host_1).to receive(:connect_ssh) expect(conversion_host_1.kill_process('1234', 'KILL')).to eq(true) - end + end end end diff --git a/spec/models/service_template_transformation_plan_task_spec.rb b/spec/models/service_template_transformation_plan_task_spec.rb index 0429c7c3dd3..dabdfb111a1 100644 --- a/spec/models/service_template_transformation_plan_task_spec.rb +++ b/spec/models/service_template_transformation_plan_task_spec.rb @@ -248,7 +248,7 @@ let(:task_1) { FactoryGirl.create(:service_template_transformation_plan_task, :miq_request => request, :request_type => 'transformation_plan', :source => src_vm_1) } let(:task_2) { FactoryGirl.create(:service_template_transformation_plan_task, :miq_request => request, :request_type => 'transformation_plan', :source => src_vm_2) } - let(:conversion_host) { FactoryGirl.create(:conversion_host) } + let(:conversion_host) { FactoryGirl.create(:conversion_host, :resource => src_vm_1) } describe '#transformation_destination' do it { expect(task_1.transformation_destination(src_cluster)).to eq(dst_cluster) }