Skip to content

Commit

Permalink
Merge pull request ManageIQ#19 from pkomanek/refactoring_update_servi…
Browse files Browse the repository at this point in the history
…ceprovision_status

Refactoring update_serviceprovision_status method
  • Loading branch information
mkanoor authored Jan 27, 2017
2 parents 3a268de + 236a9df commit 6d2f13a
Show file tree
Hide file tree
Showing 7 changed files with 360 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,50 @@
# Description: This method updates the service provision status.
# Required inputs: status
#
module ManageIQ
module Automate
module Cloud
module Orchestration
module Provisioning
module StateMachines
class UpdateServiceProvisionStatus
def initialize(handle = $evm)
@handle = handle
end

prov = $evm.root['service_template_provision_task']
def main
prov = @handle.root['service_template_provision_task']

unless prov
$evm.log(:error, "Service Template Provision Task not provided")
exit(MIQ_STOP)
end
if prov.nil?
@handle.log(:error, "Service Template Provision Task not provided")
raise "Service Template Provision Task not provided"
end

update_status_message(prov, @handle.inputs['status'])
end

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

# Update Status Message
updated_message = "Server [#{$evm.root['miq_server'].name}] "
updated_message += "Service [#{prov.destination.name}] "
updated_message += "Step [#{$evm.root['ae_state']}] "
updated_message += "Status [#{status}] "
updated_message += "Message [#{prov.message}] "
updated_message += "Current Retry Number [#{$evm.root['ae_state_retries']}]"\
if $evm.root['ae_result'] == 'retry'
prov.miq_request.user_message = updated_message
prov.message = status
def update_status_message(prov, status)
updated_message = "Server [#{@handle.root['miq_server'].name}] "
updated_message += "Service [#{prov.destination.name}] "
updated_message += "Step [#{@handle.root['ae_state']}] "
updated_message += "Status [#{status}] "
updated_message += "Message [#{prov.message}] "
updated_message += "Current Retry Number [#{@handle.root['ae_state_retries']}]"\
if @handle.root['ae_result'] == 'retry'
prov.miq_request.user_message = updated_message
prov.message = status
end
end
end
end
end
end
end
end

if __FILE__ == $PROGRAM_NAME
ManageIQ::Automate::Cloud::Orchestration::Provisioning::
StateMachines::UpdateServiceProvisionStatus.new.main
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,52 @@
# Description: This method updates the service provision status.
# Required inputs: status
#
module ManageIQ
module Automate
module ConfigurationManagement
module AnsibleTower
module Service
module Provisioning
module StateMachines
class UpdateServiceProvisionStatus
def initialize(handle = $evm)
@handle = handle
end

prov = $evm.root['service_template_provision_task']
def main
prov = @handle.root['service_template_provision_task']

unless prov
$evm.log(:error, "Service Template Provision Task not provided")
exit(MIQ_STOP)
end
if prov.nil?
@handle.log(:error, "Service Template Provision Task not provided")
raise "Service Template Provision Task not provided"
end

update_status_message(prov, @handle.inputs['status'])
end

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

# Update Status Message
updated_message = "Server [#{$evm.root['miq_server'].name}] "
updated_message += "Service [#{prov.destination.name}] "
updated_message += "Step [#{$evm.root['ae_state']}] "
updated_message += "Status [#{status}] "
updated_message += "Message [#{prov.message}] "
updated_message += "Current Retry Number [#{$evm.root['ae_state_retries']}]"\
if $evm.root['ae_result'] == 'retry'
prov.miq_request.user_message = updated_message
prov.message = status
def update_status_message(prov, status)
updated_message = "Server [#{@handle.root['miq_server'].name}] "
updated_message += "Service [#{prov.destination.name}] "
updated_message += "Step [#{@handle.root['ae_state']}] "
updated_message += "Status [#{status}] "
updated_message += "Message [#{prov.message}] "
updated_message += "Current Retry Number [#{@handle.root['ae_state_retries']}]"\
if @handle.root['ae_result'] == 'retry'
prov.miq_request.user_message = updated_message
prov.message = status
end
end
end
end
end
end
end
end
end

if __FILE__ == $PROGRAM_NAME
ManageIQ::Automate::ConfigurationManagement::AnsibleTower::Service::
Provisioning::StateMachines::UpdateServiceProvisionStatus.new.main
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,57 @@
# Description: This method updates the service provision status.
# Required inputs: status
#
module ManageIQ
module Automate
module Service
module Provisioning
module StateMachines
module ServiceProvision_Template
class UpdateServiceProvisionStatus
def initialize(handle = $evm)
@handle = handle
end

prov = $evm.root['service_template_provision_task']
def main
prov = @handle.root['service_template_provision_task']

unless prov
$evm.log(:error, "service_template_provision_task object not provided")
exit(MIQ_STOP)
end
if prov.nil?
@handle.log(:error, "service_template_provision_task object not provided")
raise "service_template_provision_task object not provided"
end

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

# Get status from input field status
status = $evm.inputs['status']
if @handle.root['ae_result'] == "error"
@handle.create_notification(:level => "error",
:subject => prov.miq_request,
:message => "Service Provision Error: #{updated_message}")
end
end

# Update Status Message
updated_message = "Server [#{$evm.root['miq_server'].name}] "
updated_message += "Service [#{prov.destination.name}] "
updated_message += "Step [#{$evm.root['ae_state']}] "
updated_message += "Status [#{status}] "
updated_message += "Message [#{prov.message}] "
updated_message += "Current Retry Number [#{$evm.root['ae_state_retries']}]" if $evm.root['ae_result'] == 'retry'
prov.miq_request.user_message = updated_message
prov.message = status
private

if $evm.root['ae_result'] == "error"
$evm.create_notification(:level => "error", :subject => prov.miq_request, \
:message => "Service Provision Error: #{updated_message}")
def update_status_message(prov, status)
updated_message = "Server [#{@handle.root['miq_server'].name}] "
updated_message += "Service [#{prov.destination.name}] "
updated_message += "Step [#{@handle.root['ae_state']}] "
updated_message += "Status [#{status}] "
updated_message += "Message [#{prov.message}] "
updated_message += "Current Retry Number [#{@handle.root['ae_state_retries']}]"\
if @handle.root['ae_result'] == 'retry'
prov.miq_request.user_message = updated_message
prov.message = status

updated_message
end
end
end
end
end
end
end
end
if __FILE__ == $PROGRAM_NAME
ManageIQ::Automate::Service::Provisioning::StateMachines::
ServiceProvision_Template::UpdateServiceProvisionStatus.new.main
end

This file was deleted.

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

describe ManageIQ::Automate::Cloud::Orchestration::Provisioning::StateMachines::UpdateServiceProvisionStatus do
let(:user) { FactoryGirl.create(:user_with_group) }
let(:miq_server) { EvmSpecHelper.local_miq_server }
let(:service_orchestration) { FactoryGirl.create(:service_orchestration) }

let(:miq_request_task) do
FactoryGirl.create(:miq_request_task, :destination => service_orchestration,
:miq_request => request, :state => 'fred')
end

let(:request) do
FactoryGirl.create(:service_template_provision_request, :requester => user)
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_miq_request_task) { MiqAeMethodService::MiqAeServiceMiqRequestTask.find(miq_request_task.id) }
let(:svc_model_service) { MiqAeMethodService::MiqAeServiceService.find(service_orchestration.id) }
let(:svc_model_request) do
MiqAeMethodService::MiqAeServiceServiceTemplateProvisionRequest.find(request.id)
end

context "with a stp request object" do
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["service_template_provision_task"] = svc_model_miq_request_task
obj["miq_server"] = svc_model_miq_server
obj
end

before do
allow(ae_service).to receive(:inputs) { {'status' => "fred"} }
allow(svc_model_miq_request_task).to receive(:destination) { svc_model_service }
ae_service.root['ae_result'] = '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}] Service [#{service_orchestration.name}] Step [] Status [fred] Message [] "
expect(svc_model_request.reload.message).to eq(msg)
end
end

context "without a stp request object" do
let(:root_hash) { {} }
let(:root_object) { Spec::Support::MiqAeMockObject.new(root_hash) }

it "method fails" do
expect { described_class.new(ae_service).main }.to raise_error(
"Service Template Provision Task not provided"
)
end
end
end
Loading

0 comments on commit 6d2f13a

Please sign in to comment.