Skip to content

Commit

Permalink
Revert "Merge pull request #573 from fdupont-redhat/v2v_state_machine…
Browse files Browse the repository at this point in the history
…_restore_vm_attributes"

This reverts commit 0cf5131.

https://bugzilla.redhat.com/show_bug.cgi?id=1768524
  • Loading branch information
simaishi committed Dec 3, 2019
1 parent 33f1d27 commit 07d176c
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
module ManageIQ
module Automate
module Transformation
module Infrastructure
module VM
module Common
class RestoreVmAttributes
IDENTITY_ITEMS = %w(service tags custom_attributes ownership retirement).freeze

def initialize(handle = $evm)
@handle = handle
@task = ManageIQ::Automate::Transformation::Common::Utils.task(@handle)
@source_vm = ManageIQ::Automate::Transformation::Common::Utils.source_vm(@handle)
@destination_vm = ManageIQ::Automate::Transformation::Common::Utils.destination_vm(@handle)
end

def vm_restore_service
if @source_vm.service
@destination_vm.add_to_service(@source_vm.service)
@source_vm.remove_from_service
end
end

def vm_restore_tags
@source_vm.tags.each do |tag|
@destination_vm.tag_assign(tag) unless tag =~ /^folder_path_/
end
end

def vm_restore_custom_attributes
@source_vm.custom_keys.each do |ca|
@destination_vm.custom_set(ca, @source_vm.custom_get(ca))
end
end

def vm_restore_ownership
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
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}") }
rescue => e
@handle.set_state_var(:ae_state_progress, 'message' => e.message)
raise
end
end
end
end
end
end
end
end

ManageIQ::Automate::Transformation::Infrastructure::VM::Common::RestoreVmAttributes.new.main
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
object_type: method
version: 1.0
object:
attributes:
name: RestoreVmAttributes
display_name:
description:
scope: instance
language: ruby
location: inline
embedded_methods:
- "/Transformation/Common/Utils"
options: {}
inputs: []
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ object:
"Run post-migration playbook")
on_error: /System/CommonMethods/MiqAe.WeightedUpdateStatus(weight => 40, description =>
"Run post-migration playbook")
- State14:
value: "/Transformation/Infrastructure/VM/Common/RestoreVmAttributes"
on_entry: /System/CommonMethods/MiqAe.WeightedUpdateStatus(weight => 5, description
=> "Restore VM Attributes", task_message => "Migrating")
on_exit: /System/CommonMethods/MiqAe.WeightedUpdateStatus(weight => 5, description
=> "Restore VM Attributes", task_message => "Migrating")
on_error: /System/CommonMethods/MiqAe.WeightedUpdateStatus(weight => 5, description
=> "Restore VM Attributes", task_message => "Migrating")
- State17:
value: "/Transformation/Common/SetMigrated"
on_entry: /System/CommonMethods/MiqAe.WeightedUpdateStatus(weight => 5, description
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
require_domain_file
require File.join(ManageIQ::Content::Engine.root, 'content/automate/ManageIQ/Transformation/Common.class/__methods__/utils.rb')

describe ManageIQ::Automate::Transformation::Infrastructure::VM::Common::RestoreVmAttributes do
let(:user) { FactoryBot.create(:user_with_email_and_group) }
let(:group) { FactoryBot.create(:miq_group) }
let(:task) { FactoryBot.create(:service_template_transformation_plan_task) }
let(:src_vm_vmware) { FactoryBot.create(:vm_vmware) }
let(:dst_vm_redhat) { FactoryBot.create(:vm_redhat) }
let(:dst_vm_openstack) { FactoryBot.create(:vm_openstack) }
let(:service) { FactoryBot.create(:service) }
let!(:parent_classification) { FactoryBot.create(:classification, :name => "environment", :description => "Environment") }
let!(:classification) { FactoryBot.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,
'user' => svc_model_user,
)
end

let(:current_object) { Spec::Support::MiqAeMockObject.new }
let(:ae_service) do
Spec::Support::MiqAeMockService.new(root).tap do |service|
current_object.parent = root
service.current_object = current_object
end
end

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

shared_examples_for "restore identity" do
let(:svc_vmdb_handle) { MiqAeMethodService::MiqAeServiceMiqGroup }

before do
allow(ManageIQ::Automate::Transformation::Common::Utils).to receive(:task).and_return(svc_model_task)
allow(ManageIQ::Automate::Transformation::Common::Utils).to receive(:source_vm).and_return(svc_model_src_vm)
allow(ManageIQ::Automate::Transformation::Common::Utils).to receive(:destination_vm).and_return(svc_model_dst_vm)
set_vm_identity
end

it "restore service" do
described_class.new(ae_service).vm_restore_service
expect(svc_model_src_vm.service).to be_nil
expect(svc_model_dst_vm.service.id).to eq(svc_model_service.id)
end

it "restore tags" do
described_class.new(ae_service).vm_restore_tags
expect(svc_model_dst_vm.tags).to eq(["environment/prod"])
end

it "restore customer attributes" do
described_class.new(ae_service).vm_restore_custom_attributes
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
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
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

it "forcefully raise" do
allow(svc_model_src_vm).to receive(:service).and_raise('Unexpected error')
expect { described_class.new(ae_service).main }.to raise_error('Unexpected error')
expect(ae_service.get_state_var(:ae_state_progress)).to eq('message' => 'Unexpected error')
end
end

context "restore when source is vmware and destination redhat" do
let(:src_vm) { src_vm_vmware }
let(:dst_vm) { dst_vm_redhat }
let(:svc_model_src_vm) { svc_model_src_vm_vmware }
let(:svc_model_dst_vm) { svc_model_dst_vm_redhat }

it_behaves_like "restore identity"
end

context "restore when source is vmware and destination openstack" do
let(:src_vm) { src_vm_vmware }
let(:dst_vm) { dst_vm_openstack }
let(:svc_model_src_vm) { svc_model_src_vm_vmware }
let(:svc_model_dst_vm) { svc_model_dst_vm_openstack }

it_behaves_like "restore identity"
end
end

0 comments on commit 07d176c

Please sign in to comment.