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

Add ConversionHost validations #18277

Merged
merged 7 commits into from
Dec 11, 2018
Merged
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
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') }, :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}?")
djberg96 marked this conversation as resolved.
Show resolved Hide resolved
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) { FactoryGirl.create(:service_template_transformation_plan_task, :state => 'active', :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 @@ -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) }
Expand Down