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

[WIP] Add v2v transformation validation. #17251

Closed
wants to merge 1 commit into from
Closed
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
46 changes: 46 additions & 0 deletions app/models/transformation_mapping.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,55 @@
class TransformationMapping < ApplicationRecord
VM_VALID = N_("OK").freeze
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You do not want to set this as a constant. It will get set once with whatever localization is in place when the model loads. You are trying to honor the current users local which is dynamic.


has_many :transformation_mapping_items, :dependent => :destroy

validates :name, :presence => true, :uniqueness => true

def destination(source)
transformation_mapping_items.find_by(:source => source).try(:destination)
end

def select_vms
valid_list = []

transformation_mapping_items.where(:source_type => EmsCluster).collect(&:source).each do |cluster|
cluster.vms.each do |vm|
reason = validate_vm(vm)
valid_list << describe_vm(vm, reason) if reason == VM_VALID
end
end

{"valid_vms" => valid_list}
end

def validate_vm(vm)
invalid_list = []
invalid_list << N_("cluster: %{name}") % {:name => vm.ems_cluster.name} unless valid_cluster?(vm)

invalid_storages = validate_storages(vm)
invalid_list << N_("storages: %{list}") % {:list => invalid_storages.join(", ")} if invalid_storages.present?

invalid_lans = validate_lans(vm)
invalid_list << N_("lans: %{list}") % {:list => invalid_lans.join(', ')} if invalid_lans.present?

return N_("Not defined source for this migration - %{list}") % {:list => invalid_list.join('. ')} if invalid_list.present?

vm.valid_for_v2v_migration? ? VM_VALID : N_('VM has been migrated')
end

def valid_cluster?(vm)
transformation_mapping_items.where(:source => vm.ems_cluster).present?
end

# return an empty array if all storages are valid for transformation
# otherwise return an array of invalid datastores
def validate_storages(vm)
vm.datastores - transformation_mapping_items.where(:source => vm.datastores).collect(&:source)
end

# return an empty array if all lans are valid for transformation
# otherwise return an array of invalid lans
def validate_lans(vm)
vm.lans - transformation_mapping_items.where(:source => vm.lans).collect(&:source)
end
end
7 changes: 7 additions & 0 deletions app/models/vm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ def supported_consoles
}
end

def valid_for_v2v_migration?
migrated_status = [ServiceResource::STATUS_QUEUED, ServiceResource::STATUS_APPROVED, ServiceResource::STATUS_ACTIVE, ServiceResource::STATUS_COMPLETED]
ServiceResource.where(:resource => self, :status => migrated_status).none? do |resource|
resource.service_template.kind_of?(ServiceTemplateTransformationPlan)
end
end

private

def vnc_support
Expand Down
69 changes: 60 additions & 9 deletions spec/models/transformation_mapping_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
describe TransformationMapping do
describe '#destination' do
let(:src) { FactoryGirl.create(:ems_cluster) }
let(:dst) { FactoryGirl.create(:ems_cluster) }
let(:src) { FactoryGirl.create(:ems_cluster) }
let(:dst) { FactoryGirl.create(:ems_cluster) }
let(:vm) { FactoryGirl.create(:vm_vmware, :ems_cluster => src) }

let(:mapping) do
FactoryGirl.create(
:transformation_mapping,
:transformation_mapping_items => [TransformationMappingItem.new(:source => src, :destination => dst)]
)
end
let(:mapping) do
FactoryGirl.create(
:transformation_mapping,
:transformation_mapping_items => [TransformationMappingItem.new(:source => src, :destination => dst)]
)
end

describe '#destination' do
it "finds the destination" do
expect(mapping.destination(src)).to eq(dst)
end
Expand All @@ -18,4 +19,54 @@
expect(mapping.destination(FactoryGirl.create(:ems_cluster))).to be_nil
end
end

describe '#valid_cluster?' do
it 'returns ture if cluster is listed' do
expect(mapping.valid_cluster?(vm)).to be true
end

it 'returns false if cluster is not listed' do
vm2 = FactoryGirl.create(:vm_vmware, :ems_cluster => FactoryGirl.create(:ems_cluster))
expect(mapping.valid_cluster?(vm2)).to be false
end
end

describe '#validate_storages' do
let(:storage1) { FactoryGirl.create(:storage) }
before do
mapping.transformation_mapping_items << TransformationMappingItem.new(:source => storage1, :destination => storage1)
vm.storages << storage1
end

it 'returns an empty array if all storages are listed' do
expect(mapping.validate_storages(vm)).to be_blank
end

it 'returns an array of invalid storages' do
storage2 = FactoryGirl.create(:storage)
vm.storages << storage2
expect(mapping.validate_storages(vm)).to eq([storage2])
end
end

describe '#validate_lans' do
let(:lan1) { FactoryGirl.create(:lan) }
let(:nic1) { FactoryGirl.create(:guest_device_nic, :lan => lan1) }

before do
mapping.transformation_mapping_items << TransformationMappingItem.new(:source => lan1, :destination => lan1)
vm.hardware = FactoryGirl.create(:hardware, :guest_devices => [nic1])
end

it 'returns an empty array if all lans are listed' do
expect(mapping.validate_lans(vm)).to be_blank
end

it 'returns an array of invalid lans' do
lan2 = FactoryGirl.create(:lan)
nic2 = FactoryGirl.create(:guest_device_nic, :lan => lan2)
vm.hardware.guest_devices << nic2
expect(mapping.validate_lans(vm)).to eq([lan2])
end
end
end