From d104748ae4e546a90d5197ecda8af9b16b10e6af Mon Sep 17 00:00:00 2001 From: Greg McCullough Date: Mon, 13 Aug 2018 17:43:58 -0400 Subject: [PATCH] Merge pull request #397 from fdupont-redhat/v2v_preserve_ownership_and_retirement_date Restore VM ownership and retirement when migrating (cherry picked from commit 836d63c89816fa8d1cf57c3ea6974e46f7a34c2a) https://bugzilla.redhat.com/show_bug.cgi?id=1618807 --- .../__methods__/restorevmattributes.rb | 16 +++++++++- .../__methods__/restorevmattributes_spec.rb | 30 ++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/content/automate/ManageIQ/Transformation/Infrastructure/VM/Common.class/__methods__/restorevmattributes.rb b/content/automate/ManageIQ/Transformation/Infrastructure/VM/Common.class/__methods__/restorevmattributes.rb index 147330a23..1d36e742d 100644 --- a/content/automate/ManageIQ/Transformation/Infrastructure/VM/Common.class/__methods__/restorevmattributes.rb +++ b/content/automate/ManageIQ/Transformation/Infrastructure/VM/Common.class/__methods__/restorevmattributes.rb @@ -5,7 +5,7 @@ module Infrastructure module VM module Common class RestoreVmAttributes - IDENTITY_ITEMS = %w(service tags custom_attributes).freeze + IDENTITY_ITEMS = %w(service tags custom_attributes ownership retirement).freeze def initialize(handle = $evm) @handle = handle @@ -54,6 +54,20 @@ def vm_restore_custom_attributes(source_vm, destination_vm) end end + def vm_restore_ownership(source_vm, destination_vm) + owner = source_vm.owner + miq_group = @handle.vmdb(:miq_group).find_by(:id => source_vm.miq_group_id) + destination_vm.owner = owner if owner.present? + destination_vm.group = miq_group if miq_group.present? + end + + def vm_restore_retirement(source_vm, destination_vm) + retirement_datetime = source_vm.retires_on + retirement_warn = source_vm.retirement_warn + destination_vm.retires_on = retirement_datetime if retirement_datetime.present? + destination_vm.retirement_warn = retirement_warn if retirement_warn.present? + end + def main IDENTITY_ITEMS.each { |item| send("vm_restore_#{item}", source_vm, destination_vm) } rescue => e diff --git a/spec/content/automate/ManageIQ/Transformation/Infrastructure/VM/Common.class/__methods__/restorevmattributes_spec.rb b/spec/content/automate/ManageIQ/Transformation/Infrastructure/VM/Common.class/__methods__/restorevmattributes_spec.rb index f5b464f3c..1b6c76828 100644 --- a/spec/content/automate/ManageIQ/Transformation/Infrastructure/VM/Common.class/__methods__/restorevmattributes_spec.rb +++ b/spec/content/automate/ManageIQ/Transformation/Infrastructure/VM/Common.class/__methods__/restorevmattributes_spec.rb @@ -2,6 +2,7 @@ describe ManageIQ::Automate::Transformation::Infrastructure::VM::Common::RestoreVmAttributes do let(:user) { FactoryGirl.create(:user_with_email_and_group) } + let(:group) { FactoryGirl.create(:miq_group) } let(:task) { FactoryGirl.create(:service_template_transformation_plan_task) } let(:src_vm_vmware) { FactoryGirl.create(:vm_vmware) } let(:dst_vm_redhat) { FactoryGirl.create(:vm_redhat) } @@ -11,12 +12,15 @@ let!(:classification) { FactoryGirl.create(:classification, :name => "prod", :description => "Production", :parent => parent_classification) } let(:svc_model_user) { MiqAeMethodService::MiqAeServiceUser.find(user.id) } + let(:svc_model_group) { MiqAeMethodService::MiqAeServiceMiqGroup.find(group.id) } let(:svc_model_task) { MiqAeMethodService::MiqAeServiceServiceTemplateTransformationPlanTask.find(task.id) } let(:svc_model_src_vm_vmware) { MiqAeMethodService::MiqAeServiceManageIQ_Providers_Vmware_InfraManager_Vm.find(src_vm_vmware.id) } let(:svc_model_dst_vm_redhat) { MiqAeMethodService::MiqAeServiceManageIQ_Providers_Redhat_InfraManager_Vm.find(dst_vm_redhat.id) } let(:svc_model_dst_vm_openstack) { MiqAeMethodService::MiqAeServiceManageIQ_Providers_Openstack_CloudManager_Vm.find(dst_vm_openstack.id) } let(:svc_model_service) { MiqAeMethodService::MiqAeServiceService.find(service.id) } + let(:retirement_date) { Time.now.utc + 1.day } + let(:root) do Spec::Support::MiqAeMockObject.new( 'current' => current_object, @@ -36,6 +40,10 @@ def set_vm_identity svc_model_src_vm.add_to_service(svc_model_service) src_vm.tag_with("prod", :ns => "/managed", :cat => "environment") svc_model_src_vm.custom_set('attr', 'value') + svc_model_src_vm.owner = svc_model_user + svc_model_src_vm.group = svc_model_group + svc_model_src_vm.retires_on = retirement_date + svc_model_src_vm.retirement_warn = 7 end context "validate task" do @@ -109,7 +117,7 @@ def set_vm_identity end shared_examples_for "restore identity" do - let(:svc_vmdb_handle) { MiqAeMethodService::MiqAeServiceVm } + let(:svc_vmdb_handle) { MiqAeMethodService::MiqAeServiceMiqGroup } before do ae_service.root['service_template_transformation_plan_task'] = svc_model_task @@ -136,12 +144,32 @@ def set_vm_identity expect(svc_model_dst_vm.custom_get('attr')).to eq('value') end + it "restore ownership" do + allow(ae_service).to receive(:vmdb).with(:miq_group).and_return(svc_vmdb_handle) + allow(svc_vmdb_handle).to receive(:find_by).with(:id => svc_model_group.id).and_return(svc_model_group) + described_class.new(ae_service).vm_restore_ownership(svc_model_src_vm, svc_model_dst_vm) + expect(svc_model_dst_vm.owner.id).to eq(svc_model_user.id) + expect(svc_model_dst_vm.miq_group_id).to eq(svc_model_group.id) + end + + it "restore retirement" do + described_class.new(ae_service).vm_restore_retirement(svc_model_src_vm, svc_model_dst_vm) + expect(svc_model_dst_vm.retires_on.to_i).to be(retirement_date.to_i) + expect(svc_model_dst_vm.retirement_warn).to eq(7) + end + it "restore identity" do + allow(ae_service).to receive(:vmdb).with(:miq_group).and_return(svc_vmdb_handle) + allow(svc_vmdb_handle).to receive(:find_by).with(:id => svc_model_group.id).and_return(svc_model_group) described_class.new(ae_service).main expect(svc_model_src_vm.service).to be_nil expect(svc_model_dst_vm.service.id).to eq(svc_model_service.id) expect(svc_model_dst_vm.tags).to eq(["environment/prod"]) expect(svc_model_dst_vm.custom_get('attr')).to eq('value') + expect(svc_model_dst_vm.owner.id).to eq(svc_model_user.id) + expect(svc_model_dst_vm.miq_group_id).to eq(svc_model_group.id) + expect(svc_model_dst_vm.retires_on.to_i).to be(retirement_date.to_i) + expect(svc_model_dst_vm.retirement_warn).to eq(7) end end