Skip to content

Commit

Permalink
Merge pull request #259 from pkomanek/refactoring_cloud_vm_retirement…
Browse files Browse the repository at this point in the history
…_statemachines_methods_PreRetirement_method

Refactoring cloud vm retirement pre_retirement method
  • Loading branch information
mkanoor authored Jun 5, 2018
2 parents 6294b94 + cc615ec commit a91443d
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 35 deletions.
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
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

0 comments on commit a91443d

Please sign in to comment.