From 36de1718345bdb7aed145b5029f388c72cc919ec Mon Sep 17 00:00:00 2001 From: Aparna Karve Date: Fri, 5 Oct 2018 11:33:09 -0700 Subject: [PATCH 1/3] adjust VM validity during plan edit appropriately VMs belonging to a Plan are valid VMs and should not be categorized as `in_other_plan` --- app/models/transformation_mapping.rb | 4 ++-- app/models/transformation_mapping/vm_migration_validator.rb | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/app/models/transformation_mapping.rb b/app/models/transformation_mapping.rb index b1562860190..1c86fd6388c 100644 --- a/app/models/transformation_mapping.rb +++ b/app/models/transformation_mapping.rb @@ -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 diff --git a/app/models/transformation_mapping/vm_migration_validator.rb b/app/models/transformation_mapping/vm_migration_validator.rb index 62c33fe63f3..592c9854679 100644 --- a/app/models/transformation_mapping/vm_migration_validator.rb +++ b/app/models/transformation_mapping/vm_migration_validator.rb @@ -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 end def validate @@ -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.to_s == @service_template_id } ? VM_VALID : VM_IN_OTHER_PLAN end def no_mapping_list(invalid_list, data_type, new_records) From c3a331db747cb4de1430a6e6e5a14f59db8274bf Mon Sep 17 00:00:00 2001 From: Aparna Karve Date: Fri, 5 Oct 2018 11:33:45 -0700 Subject: [PATCH 2/3] spec that tests for VM validity when a plan is being edited --- spec/models/transformation_mapping_spec.rb | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/spec/models/transformation_mapping_spec.rb b/spec/models/transformation_mapping_spec.rb index de7be5c90a6..a62a78017fb 100644 --- a/spec/models/transformation_mapping_spec.rb +++ b/spec/models/transformation_mapping_spec.rb @@ -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) } @@ -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 From d41949dc7cba7e18f190b6046a2cf48d846217c2 Mon Sep 17 00:00:00 2001 From: Aparna Karve Date: Mon, 8 Oct 2018 09:54:47 -0700 Subject: [PATCH 3/3] ensure that `service_template_id` is a bigint in the model --- app/models/transformation_mapping/vm_migration_validator.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/transformation_mapping/vm_migration_validator.rb b/app/models/transformation_mapping/vm_migration_validator.rb index 592c9854679..c1d80d58877 100644 --- a/app/models/transformation_mapping/vm_migration_validator.rb +++ b/app/models/transformation_mapping/vm_migration_validator.rb @@ -13,7 +13,7 @@ class TransformationMapping::VmMigrationValidator def initialize(mapping, vm_list = nil, service_template_id = nil) @mapping = mapping @vm_list = vm_list - @service_template_id = service_template_id + @service_template_id = service_template_id.try(:to_i) end def validate @@ -99,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 || rsc.service_template_id.to_s == @service_template_id } ? 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)