Skip to content

Commit

Permalink
Merge pull request #18277 from djberg96/conversion_host_validations2
Browse files Browse the repository at this point in the history
Add ConversionHost validations

(cherry picked from commit c617468)

https://bugzilla.redhat.com/show_bug.cgi?id=1694229
  • Loading branch information
agrare authored and simaishi committed Mar 29, 2019
1 parent 48dbd21 commit 373368e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
22 changes: 17 additions & 5 deletions app/models/conversion_host.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
class ConversionHost < ApplicationRecord
include NewWithTypeStiMixin

VALID_RESOURCE_TYPES = %w(VmOrTemplate Host).freeze

acts_as_miq_taggable

belongs_to :resource, :polymorphic => true
has_many :service_template_transformation_plan_tasks, :dependent => :nullify
has_many :active_tasks, -> { where(:state => ['active', 'migrate']) }, :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
Expand Down Expand Up @@ -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) }
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions spec/models/conversion_host_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) { FactoryBot.create(:service_template_transformation_plan_task, :state => 'migrate', :conversion_host => conversion_host_2) }
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,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 "#valid_states" do
it "contains 'migrate'" do
Expand Down

0 comments on commit 373368e

Please sign in to comment.