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

Refactor start_retirement method for Cloud Orchestration Retirement. #615

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
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
Expand Up @@ -13,25 +13,6 @@

let(:stack_in_provider) { true }

describe "#start_retirement" do
let(:method_name) { "StartRetirement" }

it "starts a retirement request" do
ws
expect(OrchestrationStack.where(:id => stack.id).first.retirement_state).to eq('retiring')
end

it "aborts if stack is already retired" do
stack.update(:retired => true)
expect { ws }.to raise_error(MiqAeException::AbortInstantiation, 'Method exited with rc=MIQ_ABORT')
end

it "aborts if stack is retiring" do
stack.update(:retirement_state => 'retiring')
expect { ws }.to raise_error(MiqAeException::AbortInstantiation, 'Method exited with rc=MIQ_ABORT')
end
end

describe "#remove_from_provider" do
let(:method_name) { "RemoveFromProvider" }

Expand Down
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