-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #600 from billfitzgerald0120/refactor_infra_vm_ret…
…irement_start_retirement Refactor start_retirement method for Infra VM Retirement.
- Loading branch information
Showing
2 changed files
with
91 additions
and
20 deletions.
There are no files selected for viewing
73 changes: 53 additions & 20 deletions
73
.../Infrastructure/VM/Retirement/StateMachines/Methods.class/__methods__/start_retirement.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 |
---|---|---|
@@ -1,30 +1,63 @@ | ||
# | ||
# Description: This method sets the retirement_state to retiring | ||
# | ||
module ManageIQ | ||
module Automate | ||
module Infrastructure | ||
module VM | ||
module Retirement | ||
module StateMachines | ||
module Methods | ||
class StartRetirement | ||
def initialize(handle = $evm) | ||
@handle = handle | ||
end | ||
|
||
$evm.log("info", "Listing Root Object Attributes:") | ||
$evm.root.attributes.sort.each { |k, v| $evm.log("info", "\t#{k}: #{v}") } | ||
$evm.log("info", "===========================================") | ||
def main | ||
log_info | ||
start_retirement(vm) | ||
end | ||
|
||
vm = $evm.root['vm'] | ||
if vm.nil? | ||
$evm.log('error', "VM Object not found") | ||
exit MIQ_ABORT | ||
end | ||
private | ||
|
||
if vm.retired? | ||
$evm.log('error', "VM is already retired. Aborting current State Machine.") | ||
exit MIQ_ABORT | ||
end | ||
def vm | ||
@handle.root["vm"].tap do |vm| | ||
raise 'VM Object not found' if vm.nil? | ||
end | ||
end | ||
|
||
if vm.retiring? | ||
$evm.log('error', "VM is in the process of being retired. Aborting current State Machine.") | ||
exit MIQ_ABORT | ||
end | ||
def log_info | ||
@handle.log("info", "Listing Root Object Attributes:") | ||
@handle.root.attributes.sort.each { |k, v| @handle.log("info", "\t#{k}: #{v}") } | ||
@handle.log("info", "===========================================") | ||
end | ||
|
||
def vm_validation(vm) | ||
if vm.retired? | ||
raise 'VM is already retired' | ||
end | ||
|
||
$evm.log('info', "VM before start_retirement: #{vm.inspect} ") | ||
if vm.retiring? | ||
raise 'VM is already in the process of being retired' | ||
end | ||
end | ||
|
||
$evm.create_notification(:type => :vm_retiring, :subject => vm) | ||
vm.start_retirement | ||
def start_retirement(vm) | ||
vm_validation(vm) | ||
@handle.log('info', "VM before start_retirement: #{vm.inspect} ") | ||
@handle.create_notification(:type => :vm_retiring, :subject => vm) | ||
|
||
vm.start_retirement | ||
|
||
@handle.log('info', "VM after start_retirement: #{vm.inspect} ") | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
$evm.log('info', "VM after start_retirement: #{vm.inspect} ") | ||
ManageIQ::Automate::Infrastructure::VM::Retirement::StateMachines::Methods::StartRetirement.new.main |
38 changes: 38 additions & 0 deletions
38
...astructure/VM/Retirement/StateMachines/Methods.class/__methods__/start_retirement_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,38 @@ | ||
require_domain_file | ||
|
||
describe ManageIQ::Automate::Infrastructure::VM::Retirement::StateMachines::Methods::StartRetirement do | ||
let(:svc_vm) { MiqAeMethodService::MiqAeServiceVm.find(vm.id) } | ||
let(:ems) { FactoryBot.create(:ems_vmware) } | ||
let(:vm) { FactoryBot.create(:vm_vmware, :ems_id => ems.id) } | ||
let(:root_object) { Spec::Support::MiqAeMockObject.new(root_hash) } | ||
let(:root_hash) { { 'vm' => svc_vm } } | ||
|
||
let(:ae_service) do | ||
Spec::Support::MiqAeMockService.new(root_object).tap do |service| | ||
current_object = Spec::Support::MiqAeMockObject.new | ||
current_object.parent = root_object | ||
service.object = current_object | ||
end | ||
end | ||
|
||
it "without vm" do | ||
ae_service.root['vm'] = nil | ||
expect { described_class.new(ae_service).main }.to raise_error('VM Object not found') | ||
end | ||
|
||
it "with retired vm" do | ||
svc_vm.finish_retirement | ||
expect { described_class.new(ae_service).main }.to raise_error('VM is already retired') | ||
end | ||
|
||
it "with retiring vm" do | ||
svc_vm.start_retirement | ||
expect { described_class.new(ae_service).main }.to raise_error('VM is already in the process of being retired') | ||
end | ||
|
||
it "starts retirement" do | ||
expect(ae_service).to receive(:create_notification).with(:type => :vm_retiring, :subject => svc_vm) | ||
described_class.new(ae_service).main | ||
expect(svc_vm.retirement_state).to eq('retiring') | ||
end | ||
end |