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

Adjust VM validity correctly while editing a ServiceTemplate record #18065

Merged
merged 3 commits into from
Oct 24, 2018
Merged
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
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