Skip to content

Commit

Permalink
Add v2v transformation validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
lfu committed Apr 4, 2018
1 parent c7a5c17 commit e773c9f
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 9 deletions.
44 changes: 44 additions & 0 deletions app/models/transformation_mapping.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,53 @@
class TransformationMapping < ApplicationRecord
VM_VALID = N_("OK").freeze

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 = []

Vm.each do |vm|
reason = validate_vm(vm)
valid_list << describe_vm(vm, reason) if reason == VM_VALID
end

{"valid_vms" => valid_list}
end

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

invalid_storages = validate_storages(vm)
invalid_list << "storages: #{invalid_storages.join(", ")}" if invalid_storages.present?

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

return N_("Not defined source for this migration - #{invalid_list.join('. ')}") if invalid_list.present?

vm.v2v_migrated? ? N_('VM has been migrated') : VM_VALID
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
6 changes: 6 additions & 0 deletions app/models/vm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ def supported_consoles
}
end

def v2v_migrated?
ServiceResource.where(:resource => self, :status => 'complete').any? do |resource|
ServiceTemplate.find(resource.service_template_id).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

0 comments on commit e773c9f

Please sign in to comment.