-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Merge pull request #573 from fdupont-redhat/v2v_state_machine…
…_restore_vm_attributes" This reverts commit 0cf5131. https://bugzilla.redhat.com/show_bug.cgi?id=1768524
- Loading branch information
Showing
4 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
...ManageIQ/Transformation/Infrastructure/VM/Common.class/__methods__/restorevmattributes.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
15 changes: 15 additions & 0 deletions
15
...nageIQ/Transformation/Infrastructure/VM/Common.class/__methods__/restorevmattributes.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
...eIQ/Transformation/Infrastructure/VM/Common.class/__methods__/restorevmattributes_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |