Skip to content

Commit

Permalink
Refactor start_retirement method for Cloud Orch Retirement.
Browse files Browse the repository at this point in the history
This PR is based on the issue below.
ManageIQ/manageiq#12038

@miq-bot add_label refactoring
@miq-bot assign @tinaafitz
  • Loading branch information
billfitzgerald0120 committed Dec 10, 2019
1 parent f3b8a03 commit 5c926f2
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,60 @@
# Description: This method sets the retirement_state to retiring
#

$evm.log("info", "Listing Root Object Attributes:")
$evm.root.attributes.sort.each { |k, v| $evm.log("info", "\t#{k}: #{v}") }
$evm.log("info", "===========================================")

stack = $evm.root['orchestration_stack']
if stack.nil?
$evm.log('error', "OrchestrationStack Object not found")
exit MIQ_ABORT
end
module ManageIQ
module Automate
module Cloud
module Orchestration
module Retirement
module StateMachines
module Methods
class StartRetirement
def initialize(handle = $evm)
@handle = handle
end

if stack.retired?
$evm.log('error', "Stack is already retired. Aborting current State Machine.")
exit MIQ_ABORT
end
def main
log_info
start_retirement(stack)
end

if stack.retiring?
$evm.log('error', "Stack is in the process of being retired. Aborting current State Machine.")
exit MIQ_ABORT
end
private

def stack
@handle.root["orchestration_stack"].tap do |stack|
raise 'OrchestrationStack Object not found' if stack.nil?
end
end

$evm.log('info', "Stack before start_retirement: #{stack.inspect} ")
$evm.create_notification(:type => :vm_retiring, :subject => stack)
def stack_validation(stack)
if stack.retired?
raise 'Stack is already retired. Aborting current State Machine.'
end
if stack.retiring?
raise 'Stack is in the process of being retired. Aborting current State Machine.'
end
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 start_retirement(stack)
stack_validation(stack)
@handle.log('info', "Stack before start_retirement: #{stack.inspect} ")
@handle.create_notification(:type => :vm_retiring, :subject => stack)

stack.start_retirement
end
end
end
end
end
end
end
end
end

stack.start_retirement
ManageIQ::Automate::Cloud::Orchestration::Retirement::StateMachines::Methods::StartRetirement.new.main
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require_domain_file

describe ManageIQ::Automate::Cloud::Orchestration::Retirement::StateMachines::Methods::StartRetirement do
let(:stack) { FactoryBot.create(:orchestration_stack_amazon) }
let(:service_orchestration) { FactoryBot.create(:service_orchestration) }
let(:svc_model_service) { MiqAeMethodService::MiqAeServiceService.find(service_orchestration.id) }
let(:svc_model_stack) { MiqAeMethodService::MiqAeServiceOrchestrationStack.find(stack.id) }

let(:root_hash) do
{ 'service_template' => MiqAeMethodService::MiqAeServiceService.find(service_orchestration.id) }
end

let(:root_object) do
obj = Spec::Support::MiqAeMockObject.new(root_hash)
obj["orchestration_stack"] = svc_model_stack
obj
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 "without stack" do
ae_service.root['orchestration_stack'] = nil
expect { described_class.new(ae_service).main }.to raise_error('OrchestrationStack Object not found')
end

it "with retired stack" do
svc_model_stack.finish_retirement
expect { described_class.new(ae_service).main }.to raise_error('Stack is already retired. Aborting current State Machine.')
end

it "with retiring stack" do
svc_model_stack.start_retirement
expect { described_class.new(ae_service).main }.to raise_error('Stack is in the process of being retired. Aborting current State Machine.')
end

it "starts retirement" do
expect(ae_service).to receive(:create_notification).with(:type => :vm_retiring, :subject => svc_model_stack)
expect(svc_model_stack).to receive(:start_retirement)
expect { described_class.new(ae_service).main }.to_not raise_error
end
end

0 comments on commit 5c926f2

Please sign in to comment.