Skip to content

Commit

Permalink
Merge pull request ManageIQ#266 from d-m-u/adding_vm_infra_update_ret…
Browse files Browse the repository at this point in the history
…irement_status_spec

Add namespace and task status message to vm retirement
  • Loading branch information
mkanoor authored Mar 29, 2018
2 parents dd599b7 + 3776d8d commit 2fc22ea
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,48 +1,64 @@
#
# Description: This method updates the vm retirement status.
# Required inputs: status
#

# Get variables from Server object
server = $evm.root['miq_server']

# Get State Machine
state = $evm.current_object.class_name

# Get current step
step = $evm.root['ae_state']

# Get status from input field status
status = $evm.inputs['status']

# Get status_state ['on_entry', 'on_exit', 'on_error']
status_state = $evm.root['ae_status_state']

vm = $evm.root['vm']

$evm.log("info", "Server:<#{server.name}> Ae_Result:<#{$evm.root['ae_result']}> State:<#{state}> Step:<#{step}>")
$evm.log("info", "Status_State:<#{status_state}> Status:<#{status}>")

# Update Status Message
updated_message = "Server [#{server.name}] "
updated_message += "VM [#{vm.name}] " if vm
updated_message += "Step [#{step}] "
updated_message += "Status [#{status}] "
updated_message += "Current Retry Number [#{$evm.root['ae_state_retries']}]" if $evm.root['ae_result'] == 'retry'

# Update Status for on_error for all states other than the first state which is startretirement
# in the retirement state machine.
if $evm.root['ae_result'] == 'error'
if step.downcase == 'startretirement'
msg = 'Cannot continue because VM is '
msg += vm ? "#{vm.retirement_state}." : 'nil.'
$evm.log('info', msg)
updated_message += msg
$evm.create_notification(:level => 'warning', :message => "VM Retirement Warning: #{updated_message}")
$evm.log(:warn, "VM Retirement Warning: #{updated_message}")
else
$evm.create_notification(:level => 'error', :message => "VM Retirement Error: #{updated_message}")
$evm.log(:error, "VM Retirement Error: #{updated_message}")
vm.retirement_state = 'error' if vm
module ManageIQ
module Automate
module Infrastructure
module VM
module Retirement
module StateMachines
module VMRetirement
class UpdateRetirementStatus
def initialize(handle = $evm)
@handle = handle
end

def main
vm = @handle.root['vm']

updated_message = update_status_message(vm, @handle.inputs['status'])

if @handle.root['ae_result'] == "error"
@handle.create_notification(:level => "error",
:subject => "vm",
:message => "VM Retirement Error: #{updated_message}")
@handle.log(:error, "VM Retirement Error: #{updated_message}")
end
end

private

def update_status_message(vm, status)
updated_message = "Server [#{@handle.root['miq_server'].name}] "
updated_message += "Step [#{@handle.root['ae_state']}] "
updated_message += "Status [#{status}] "
updated_message += "Current Retry Number [#{@handle.root['ae_state_retries']}]" if @handle.root['ae_result'] == 'retry'

if @handle.root['ae_result'] == 'error'
if @handle.root['ae_state'].downcase == 'startretirement'
msg = 'Cannot continue because VM is '
msg += vm ? "#{vm.retirement_state}." : 'nil.'
@handle.log('info', msg)
updated_message += msg
elsif vm
vm.retirement_state = 'error'
end
end

if @handle.root['vm_retire_task']
task = @handle.root['vm_retire_task']
task.miq_request.user_message = updated_message
task.message = status

updated_message
end
end
end
end
end
end
end
end
end
end

if $PROGRAM_NAME == __FILE__
ManageIQ::Automate::Infrastructure::VM::Retirement::StateMachines::VMRetirement::UpdateRetirementStatus.new.main
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
require_domain_file

describe ManageIQ::Automate::Infrastructure::VM::Retirement::StateMachines::
VMRetirement::UpdateRetirementStatus do
let(:user) { FactoryGirl.create(:user_with_group) }
let(:miq_server) { EvmSpecHelper.local_miq_server }
let(:vm) { FactoryGirl.create(:vm_vmware, :ems_id => FactoryGirl.create(:ems_vmware).id, :evm_owner => user) }
let(:root_hash) do
{ }
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

let(:svc_model_miq_server) { MiqAeMethodService::MiqAeServiceMiqServer.find(miq_server.id) }
let(:svc_model_vm) { MiqAeMethodService::MiqAeServiceVm.find(vm.id) }

context "with a stp request object" do
let(:miq_request_task) do
FactoryGirl.create(:miq_request_task,
:miq_request => request, :state => 'fred')
end
let(:request) do
FactoryGirl.create(:vm_retire_request, :requester => user)
end
let(:svc_model_miq_request_task) { MiqAeMethodService::MiqAeServiceMiqRequestTask.find(miq_request_task.id) }
let(:svc_model_request) do
MiqAeMethodService::MiqAeServiceVmRetireRequest.find(request.id)
end

let(:root_object) do
obj = Spec::Support::MiqAeMockObject.new(root_hash)
obj["vm_retire_task"] = svc_model_miq_request_task
obj["miq_server"] = svc_model_miq_server
obj["vm"] = svc_model_vm
obj
end

before do
allow(ae_service).to receive(:inputs) { {'status' => "fred"} }
ae_service.root['ae_result'] = 'ok'
ae_service.root['ae_status'] = 'ok'
end

it "method succeeds" do
described_class.new(ae_service).main
expect(svc_model_request.reload.status).to eq('Ok')
end

it "request message set properly" do
described_class.new(ae_service).main
msg = "Server [#{miq_server.name}] Step [] Status [fred] "
expect(svc_model_request.reload.message).to eq(msg)
end

it "creates notification due to ae_result is 'error'" do
ae_service.root['ae_result'] = "error"
ae_service.root['ae_state'] = "thingotherthanstartretirement"
expect(ae_service).to receive(:create_notification)
described_class.new(ae_service).main
expect(svc_model_vm.retirement_state).to eq('error')
end

it "creates notification due to ae_result is 'error'" do
ae_service.root['ae_result'] = "error"
ae_service.root['ae_state'] = "startretirement"
expect(ae_service).to receive(:create_notification)
described_class.new(ae_service).main
end
end

context "without a stp request object" do
let(:root_object) do
obj = Spec::Support::MiqAeMockObject.new(root_hash)
obj["miq_server"] = svc_model_miq_server
obj["vm"] = svc_model_vm
obj
end

before do
allow(ae_service).to receive(:inputs) { {'status' => "fred"} }
ae_service.root['ae_result'] = 'ok'
ae_service.root['ae_status'] = 'ok'
end

it "creates notification due to ae_result is 'error'" do
ae_service.root['ae_result'] = "error"
ae_service.root['ae_state'] = "thingotherthanstartretirement"
expect(ae_service).to receive(:create_notification)
described_class.new(ae_service).main
expect(svc_model_vm.retirement_state).to eq('error')
end

it "creates notification due to ae_result is 'error'" do
ae_service.root['ae_result'] = "error"
ae_service.root['ae_state'] = "startretirement"
expect(ae_service).to receive(:create_notification)
described_class.new(ae_service).main
end
end
end

0 comments on commit 2fc22ea

Please sign in to comment.