diff --git a/app/models/transformation_mapping.rb b/app/models/transformation_mapping.rb index 4e351de50e2..00eed0c531f 100644 --- a/app/models/transformation_mapping.rb +++ b/app/models/transformation_mapping.rb @@ -5,6 +5,7 @@ class TransformationMapping < ApplicationRecord VM_MIGRATED = "migrated".freeze VM_NOT_EXIST = "not_exist".freeze VM_VALID = "ok".freeze + VM_INACTIVE = "inactive".freeze has_many :transformation_mapping_items, :dependent => :destroy has_many :service_resources, :as => :resource, :dependent => :nullify @@ -82,8 +83,8 @@ def describe_non_vm(vm_name) def describe_vm(vm, reason) { "name" => vm.name, - "cluster" => vm.ems_cluster.name, - "path" => "#{vm.ext_management_system.name}/#{vm.parent_blue_folder_path(:exclude_non_display_folders => true)}", + "cluster" => vm.ems_cluster.try(:name) || '', + "path" => vm.ext_management_system ? "#{vm.ext_management_system.name}/#{vm.parent_blue_folder_path(:exclude_non_display_folders => true)}" : '', "allocated_size" => vm.allocated_disk_storage, "id" => vm.id, "reason" => reason @@ -91,6 +92,9 @@ def describe_vm(vm, reason) end def validate_vm(vm, quick = true) + validate_result = vm.validate_v2v_migration + return validate_result unless validate_result == VM_VALID + # a valid vm must find all resources in the mapping and has never been migrated invalid_list = [] @@ -111,8 +115,7 @@ def validate_vm(vm, quick = true) return no_mapping_msg(invalid_list) if quick end - return no_mapping_msg(invalid_list) if invalid_list.present? - vm.validate_v2v_migration + invalid_list.present? ? no_mapping_msg(invalid_list) : VM_VALID end def no_mapping_msg(list) diff --git a/app/models/vm.rb b/app/models/vm.rb index 562f6815bac..5332406a036 100644 --- a/app/models/vm.rb +++ b/app/models/vm.rb @@ -122,6 +122,8 @@ def supported_consoles end def validate_v2v_migration + return TransformationMapping::VM_INACTIVE unless active? + vm_as_resources = ServiceResource.where(:resource => self).includes(:service_template).where(:service_templates => {:type => "ServiceTemplateTransformationPlan"}) # VM has not been migrated before diff --git a/spec/models/transformation_mapping_spec.rb b/spec/models/transformation_mapping_spec.rb index b63d15afe89..ca3534d5f52 100644 --- a/spec/models/transformation_mapping_spec.rb +++ b/spec/models/transformation_mapping_spec.rb @@ -31,6 +31,7 @@ describe '#validate_vms' do let(:vm) { FactoryGirl.create(:vm_vmware, :name => 'test_vm', :ems_cluster => src, :ext_management_system => FactoryGirl.create(:ext_management_system)) } + let(:inactive_vm) { FactoryGirl.create(:vm_vmware, :name => 'test_vm_inactive', :ems_cluster => src, :ext_management_system => nil) } let(:storage) { FactoryGirl.create(:storage) } let(:lan) { FactoryGirl.create(:lan) } let(:nic) { FactoryGirl.create(:guest_device_nic, :lan => lan) } @@ -49,6 +50,12 @@ expect(result['invalid_vms'].first).to match(hash_including('reason' => TransformationMapping::VM_NOT_EXIST)) end + it 'if VM is inactive' do + inactive_vm.storages << FactoryGirl.create(:storage, :name => 'storage_for_inactive_vm') + result = mapping.validate_vms(['name' => 'test_vm_inactive']) + expect(result['invalid_vms'].first).to match(hash_including('reason' => TransformationMapping::VM_INACTIVE)) + end + it "if VM's cluster is not in the mapping" do FactoryGirl.create( :vm_vmware,