diff --git a/app/models/conversion_host.rb b/app/models/conversion_host.rb index b856c101174..1bf76a1e9c0 100644 --- a/app/models/conversion_host.rb +++ b/app/models/conversion_host.rb @@ -4,11 +4,10 @@ class ConversionHost < ApplicationRecord acts_as_miq_taggable belongs_to :resource, :polymorphic => true + has_many :service_template_transformation_plan_tasks, :dependent => :nullify def active_tasks - ServiceTemplateTransformationPlanTask.where(:state => 'active').select do |task| - task.conversion_host == self - end + service_template_transformation_plan_tasks { active } end def eligible? diff --git a/app/models/service_template_transformation_plan_task.rb b/app/models/service_template_transformation_plan_task.rb index 28a105c2ff4..d647531b274 100644 --- a/app/models/service_template_transformation_plan_task.rb +++ b/app/models/service_template_transformation_plan_task.rb @@ -1,4 +1,6 @@ class ServiceTemplateTransformationPlanTask < ServiceTemplateProvisionTask + belongs_to :conversion_host + def self.base_model ServiceTemplateTransformationPlanTask end @@ -46,10 +48,6 @@ def task_active vm_resource.update_attributes(:status => ServiceResource::STATUS_ACTIVE) end - def conversion_host - ConversionHost.find_by(:id => options[:transformation_host_id]) - end - def source_ems options[:source_ems_id] ||= source.ext_management_system.id source.ext_management_system @@ -110,7 +108,7 @@ def destination_security_group def transformation_log host = conversion_host if host.nil? - msg = "Conversion host was not found: ID [#{options[:transformation_host_id]}]. Download of transformation log aborted." + msg = "Conversion host was not found. Download of transformation log aborted." _log.error(msg) raise MiqException::Error, msg end @@ -144,7 +142,7 @@ def transformation_log_queue(userid = nil) userid ||= User.current_userid || 'system' host = conversion_host if host.nil? - msg = "Conversion host was not found: ID [#{options[:transformation_host_id]}]. Cannot queue the download of transformation log." + msg = "Conversion host was not found. Cannot queue the download of transformation log." return create_error_status_task(userid, msg).id end diff --git a/spec/models/conversion_host_spec.rb b/spec/models/conversion_host_spec.rb index 7c5a00a063e..d9d250fea90 100644 --- a/spec/models/conversion_host_spec.rb +++ b/spec/models/conversion_host_spec.rb @@ -1,23 +1,19 @@ describe ConversionHost do let(:apst) { FactoryGirl.create(:service_template_ansible_playbook) } - let(:conversion_host) { FactoryGirl.create(:conversion_host) } context "provider independent methods" do - let(:task_1) { FactoryGirl.create(:service_template_transformation_plan_task) } - let(:task_2) { FactoryGirl.create(:service_template_transformation_plan_task) } - let(:task_3) { FactoryGirl.create(:service_template_transformation_plan_task) } - - let(:conversion_host_1) { FactoryGirl.create(:conversion_host) } - let(:conversion_host_2) { FactoryGirl.create(:conversion_host) } + 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(: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) } before do conversion_host_1.concurrent_transformation_limit = "2" conversion_host_2.concurrent_transformation_limit = "1" - task_1.options[:transformation_host_id] = conversion_host_1.id - task_2.options[:transformation_host_id] = conversion_host_1.id - task_3.options[:transformation_host_id] = conversion_host_2.id - allow(ServiceTemplateTransformationPlanTask).to receive(:where).with(:state => 'active').and_return([task_1, task_3]) end @@ -85,6 +81,7 @@ let(:dst_storage) { FactoryGirl.create(:storage) } let(:dst_lan_1) { FactoryGirl.create(:lan) } let(:dst_lan_2) { FactoryGirl.create(:lan) } + let(:conversion_host) { FactoryGirl.create(:conversion_host, :resource => FactoryGirl.create(:host, :ext_management_system => dst_ems)) } let(:mapping) do FactoryGirl.create( @@ -115,11 +112,11 @@ let(:plan) { ServiceTemplateTransformationPlan.create_catalog_item(catalog_item_options) } let(:request) { FactoryGirl.create(:service_template_transformation_plan_request, :source => plan) } - let(:task) { FactoryGirl.create(:service_template_transformation_plan_task, :miq_request => request, :request_type => 'transformation_plan', :source => src_vm) } + let(:task) { FactoryGirl.create(:service_template_transformation_plan_task, :miq_request => request, :request_type => 'transformation_plan', :source => src_vm, :conversion_host => conversion_host) } before do - task.options[:transformation_host_id] = conversion_host.id allow(task).to receive(:source_disks).and_return(source_disks) + allow(conversion_host).to receive(:ipaddress).and_return('10.0.1.1') end context "transport method is vddk" do @@ -177,6 +174,7 @@ let(:dst_cloud_network_2) { FactoryGirl.create(:cloud_network) } let(:dst_flavor) { FactoryGirl.create(:flavor) } let(:dst_security_group) { FactoryGirl.create(:security_group) } + let(:conversion_host) { FactoryGirl.create(:conversion_host, :resource => FactoryGirl.create(:vm_or_template, :ext_management_system => dst_ems)) } let(:mapping) do FactoryGirl.create( @@ -209,10 +207,9 @@ let(:plan) { ServiceTemplateTransformationPlan.create_catalog_item(catalog_item_options) } let(:request) { FactoryGirl.create(:service_template_transformation_plan_request, :source => plan) } - let(:task) { FactoryGirl.create(:service_template_transformation_plan_task, :miq_request => request, :request_type => 'transformation_plan', :source => src_vm) } + let(:task) { FactoryGirl.create(:service_template_transformation_plan_task, :miq_request => request, :request_type => 'transformation_plan', :source => src_vm, :conversion_host => conversion_host) } before do - task.options[:transformation_host_id] = conversion_host.id allow(task).to receive(:source_disks).and_return(source_disks) end diff --git a/spec/models/service_template_transformation_plan_task_spec.rb b/spec/models/service_template_transformation_plan_task_spec.rb index 42913ff2711..13d9619b59e 100644 --- a/spec/models/service_template_transformation_plan_task_spec.rb +++ b/spec/models/service_template_transformation_plan_task_spec.rb @@ -19,6 +19,8 @@ let(:vm) { FactoryGirl.create(:vm_or_template) } let(:vm2) { FactoryGirl.create(:vm_or_template) } let(:apst) { FactoryGirl.create(:service_template_ansible_playbook) } + let(:conversion_host) { FactoryGirl.create(:conversion_host, :resource => host) } + let(:mapping) do FactoryGirl.create( :transformation_mapping, @@ -103,16 +105,9 @@ end describe '#transformation_log_queue' do - let(:conversion_host_id) { 22 } - - before do - task.options[:transformation_host_id] = conversion_host_id - task.save! - end - context 'when conversion host exists' do before do - FactoryGirl.create(:conversion_host, :id => conversion_host_id, :resource => host) + task.conversion_host = conversion_host allow(described_class).to receive(:find).and_return(task) @@ -147,7 +142,7 @@ it 'returns an error message' do taskid = task.transformation_log_queue('user') expect(MiqTask.find(taskid)).to have_attributes( - :message => "Conversion host was not found: ID [#{conversion_host_id}]. Cannot queue the download of transformation log.", + :message => "Conversion host was not found. Cannot queue the download of transformation log.", :status => 'Error' ) end @@ -155,11 +150,9 @@ end describe '#transformation_log' do - let(:conversion_host) { FactoryGirl.create(:conversion_host, :id => 9, :resource => host) } - before do EvmSpecHelper.create_guid_miq_server_zone - task.options[:transformation_host_id] = conversion_host.id + task.conversion_host = conversion_host task.options.store_path(:virtv2v_wrapper, "v2v_log", "/path/to/log.file") task.save!