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

Refactoring cloud vm retirement pre_retirement method #259

Merged
merged 2 commits into from
Jun 5, 2018
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
@@ -1,10 +1,37 @@
#
# Description: This method stops a cloud Instance
#
module ManageIQ
module Automate
module Cloud
module VM
module Retirement
module StateMachines
module Methods
class PreRetirement
def initialize(handle = $evm)
@handle = handle
end

vm = $evm.root['vm']
if vm && vm.power_state == 'on'
ems = vm.ext_management_system
$evm.log('info', "Stopping Instance <#{vm.name}> in EMS <#{ems.try(:name)}>")
vm.stop if ems
def main
vm = @handle.root['vm']
if vm && vm.power_state == 'on'
ems = vm.ext_management_system
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tinaafitz if the ems is missing and the vm is powered on, should we be raising an exception and stopping the workflow or should we keep continuing. I remember we had some corner cases that we were trying to solve where a failed retirement is restarted. If there is no ems should we log the fact that its an orphaned vm.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mkanoor We should continue processing if the ems is nil and the VM will have the power state of "unknown" in that case

if ems
@handle.log('info', "Stopping Instance <#{vm.name}> in EMS <#{ems.name}>")
vm.stop
end
end
end
end
end
end
end
end
end
end
end

if $PROGRAM_NAME == __FILE__
ManageIQ::Automate::Cloud::VM::Retirement::StateMachines::Methods::PreRetirement.new.main
end
30 changes: 0 additions & 30 deletions spec/automation/unit/method_validation/pre_retirement_spec.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require_domain_file

describe ManageIQ::Automate::Cloud::VM::Retirement::StateMachines::Methods::PreRetirement do
let(:svc_vm) { MiqAeMethodService::MiqAeServiceVm.find(vm.id) }
let(:ems) { FactoryGirl.create(:ems_microsoft) }
let(:root_object) { Spec::Support::MiqAeMockObject.new(root_hash) }
let(:root_hash) { { 'vm' => svc_vm } }
let(:vm) do
FactoryGirl.create(:vm_microsoft, :ems_id => ems.id)
end

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 'powers off a vm in a \'powered_on\' state' do
expect(svc_vm).to receive(:stop)
described_class.new(ae_service).main
end

it 'does not stop a vm in \'powered_off\' state' do
vm.update_attributes(:raw_power_state => "PowerOff")
expect(svc_vm).to_not receive(:stop)
described_class.new(ae_service).main
end

context 'nil ems' do
let(:vm) { FactoryGirl.create(:vm_microsoft) }

it 'does not stop a vm without ems' do
expect(svc_vm).to_not receive(:stop)
described_class.new(ae_service).main
end
end
end