Skip to content

Commit

Permalink
Merge pull request #18065 from AparnaKarve/adjust_vm_validity_edit_plan
Browse files Browse the repository at this point in the history
Adjust VM validity correctly while editing a ServiceTemplate record
  • Loading branch information
roliveri authored Oct 24, 2018
2 parents 3ae9ce6 + d41949d commit 9caf12f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/models/transformation_mapping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def destination(source)
end

# vm_list: collection of hashes, each descriping a VM.
def search_vms_and_validate(vm_list = nil)
VmMigrationValidator.new(self, vm_list).validate
def search_vms_and_validate(vm_list = nil, service_template_id = nil)
VmMigrationValidator.new(self, vm_list, service_template_id).validate
end
end
5 changes: 3 additions & 2 deletions app/models/transformation_mapping/vm_migration_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ class TransformationMapping::VmMigrationValidator
VM_NOT_EXIST = "not_exist".freeze
VM_VALID = "ok".freeze

def initialize(mapping, vm_list = nil)
def initialize(mapping, vm_list = nil, service_template_id = nil)
@mapping = mapping
@vm_list = vm_list
@service_template_id = service_template_id.try(:to_i)
end

def validate
Expand Down Expand Up @@ -98,7 +99,7 @@ def vm_migration_status(vm)
return VM_MIGRATED unless vm_as_resources.where(:status => ServiceResource::STATUS_COMPLETED).empty?

# VM failed in previous migration
vm_as_resources.all? { |rsc| rsc.status == ServiceResource::STATUS_FAILED } ? VM_VALID : VM_IN_OTHER_PLAN
vm_as_resources.all? { |rsc| rsc.status == ServiceResource::STATUS_FAILED || rsc.service_template_id == @service_template_id } ? VM_VALID : VM_IN_OTHER_PLAN
end

def no_mapping_list(invalid_list, data_type, new_records)
Expand Down
30 changes: 30 additions & 0 deletions spec/models/transformation_mapping_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

describe '#search_vms_and_validate' do
let(:vm) { FactoryGirl.create(:vm_vmware, :name => 'test_vm', :ems_cluster => src, :ext_management_system => FactoryGirl.create(:ext_management_system)) }
let(:vm2) { FactoryGirl.create(:vm_vmware, :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) }
Expand Down Expand Up @@ -126,6 +127,35 @@
end
end

context 'with VM list and service_template_id' do
it 'returns valid vms when a ServiceTemplate record is edited with CSV containing the same VM already included in the ServiceTemplate record' do
service_template = FactoryGirl.create(:service_template_transformation_plan)

FactoryGirl.create(
:service_resource,
:resource => vm2,
:service_template => service_template,
:status => "Active"
)
result = mapping.search_vms_and_validate(['name' => vm2.name], service_template.id.to_s)
expect(result['valid'].first.reason).to match(/ok/)
end

it 'returns invalid vms when the Service Template record is edited with CSV containing a different VM that belongs to a different ServiceTemplate record' do
service_template = FactoryGirl.create(:service_template_transformation_plan)
service_template2 = FactoryGirl.create(:service_template_transformation_plan)

FactoryGirl.create(
:service_resource,
:resource => vm2,
:service_template => service_template,
:status => "Active"
)
result = mapping.search_vms_and_validate(['name' => vm2.name], service_template2.id.to_s)
expect(result['invalid'].first.reason).to match(/in_other_plan/)
end
end

context 'without VM list' do
it 'returns valid vms' do
result = mapping.search_vms_and_validate
Expand Down

0 comments on commit 9caf12f

Please sign in to comment.