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

Restore VM ownership and retirement when migrating #397

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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down