Skip to content

Commit

Permalink
Add factories for service and orchestration stack retire task and req…
Browse files Browse the repository at this point in the history
…uest
  • Loading branch information
d-m-u committed Mar 22, 2018
1 parent 787b796 commit 0d789ad
Show file tree
Hide file tree
Showing 19 changed files with 184 additions and 173 deletions.
6 changes: 6 additions & 0 deletions app/models/miq_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ class MiqRequest < ApplicationRecord
:VmRetireRequest => {
:vm_retire => N_("VM Retire")
},
:ServiceRetireRequest => {
:service_retire => N_("Service Retire")
},
:OrchestrationStackRetireRequest => {
:orchestration_stack_retire => N_("Orchestration Stack Retire")
},
:ServiceTemplateProvisionRequest => {
:clone_to_service => N_("Service Provision")
},
Expand Down
13 changes: 13 additions & 0 deletions app/models/miq_retire_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class MiqRetireRequest < MiqRequest
# subclasses must set this
SOURCE_CLASS_NAME = nil

validates :request_state, :inclusion => { :in => %w(pending finished) + ACTIVE_STATES, :message => "should be pending, #{ACTIVE_STATES.join(", ")} or finished" }
validate :must_have_user

default_value_for(:source_id) { |r| r.get_option(:src_id) }
default_value_for :source_type, SOURCE_CLASS_NAME

def my_zone
end
end
90 changes: 90 additions & 0 deletions app/models/miq_retire_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
class MiqRetireTask < MiqRequestTask
validate :validate_request_type, :validate_state

AUTOMATE_DRIVES = true

def self.get_description(req_obj)
name = nil
if req_obj.source.nil?
# Single source has not been selected yet
if req_obj.options[:src_ids].length == 1
m = model_being_retired.find_by(:id => req_obj.options[:src_ids].first)
name = m.nil? ? "" : m.name
else
name = "Multiple " + model_being_retired.to_s.pluralize
end
else
name = req_obj.source.name
end

new_settings = []
"#{request_class::TASK_DESCRIPTION} for: #{name} - #{new_settings.join(", ")}"
end

def deliver_to_automate(req_type = request_type, zone = nil)
task_check_on_delivery

_log.info("Queuing #{request_class::TASK_DESCRIPTION}: [#{description}]...")
if self.class::AUTOMATE_DRIVES
args = {
:object_type => self.class.name,
:object_id => id,
:attrs => {"request" => req_type},
:instance_name => "AUTOMATION",
:user_id => 1,
:miq_group_id => 2,
:tenant_id => 1,
}

args[:attrs].merge!(MiqAeEngine.create_automation_attributes(source.class.base_model.name => source))

zone ||= source.respond_to?(:my_zone) ? source.my_zone : MiqServer.my_zone
MiqQueue.put(
:class_name => 'MiqAeEngine',
:method_name => 'deliver',
:args => [args],
:role => 'automate',
:zone => options.fetch(:miq_zone, zone),
:tracking_label => my_task_id,
)
update_and_notify_parent(:state => "pending", :status => "Ok", :message => "Automation Starting")
else
execute_queue
end
end

def after_request_task_create
update_attribute(:description, get_description)
end

def after_ae_delivery(ae_result)
_log.info("ae_result=#{ae_result.inspect}")
reload

return if ae_result == 'retry'
return if miq_request.state == 'finished'

if ae_result == 'ok'
update_and_notify_parent(:state => "finished", :status => "Ok", :message => display_message("#{request_class::TASK_DESCRIPTION} completed"))
else
mark_pending_items_as_finished
update_and_notify_parent(:state => "finished", :status => "Error", :message => display_message("#{request_class::TASK_DESCRIPTION} failed"))
end
end

def before_ae_starts(_options)
reload
if state.to_s.downcase.in?(%w(pending queued))
_log.info("Executing #{request_class::TASK_DESCRIPTION} request: [#{description}]")
update_and_notify_parent(:state => "active", :status => "Ok", :message => "In Process")
end
end

def mark_pending_items_as_finished
miq_request.miq_request_tasks.each do |s|
if s.state == 'pending'
s.update_and_notify_parent(:state => "finished", :status => "Warn", :message => "Error in Request: #{miq_request.id}. Setting pending Task: #{id} to finished.") unless id == s.id
end
end
end
end
15 changes: 10 additions & 5 deletions app/models/mixins/retirement_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ module RetirementMixin
end

module ClassMethods
def make_retire_request
options = {:task => task, :userid => current_user, :ids => objs, :src_ids => objs}
(klass.to_s + "RetireRequest").constantize.make_request(@request_id, options, current_user)
end

def retire(ids, options = {})
ids.each do |id|
object = find_by(:id => id)
Expand Down Expand Up @@ -117,19 +122,19 @@ def retirement_check
end
end

retire_now if retirement_due?
make_retire_request if retirement_due?
end

def retire_now(requester = nil)
if retired
return if retired_validated?
_log.info("#{retirement_object_title}: [#{name}], Retires On: [#{retires_on.strftime("%x %R %Z")}], was previously retired, but currently #{retired_invalid_reason}")
else
_log.info("Creating request for #{self.class.name} #{id}")
update_attributes(:retirement_requester => requester)
event_name = "request_#{retirement_event_prefix}_retire"
_log.info("calling #{event_name}")
begin
options = {:src_ids => id}
request_type = (self.class.name.demodulize + "RetireRequest").constantize
request_type.make_request(@request_id, options, User.current_user.try(:userid))
raise_retirement_event(event_name, requester)
rescue => err
_log.log_backtrace(err)
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/orchestration_stack_retire_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class OrchestrationStackRetireRequest < MiqRetireRequest
TASK_DESCRIPTION = 'OrchestrationStack Retire'.freeze
SOURCE_CLASS_NAME = 'OrchestrationStack'.freeze
end
9 changes: 9 additions & 0 deletions app/models/orchestration_stack_retire_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class OrchestrationStackRetireTask < MiqRetireTask
def self.base_model
OrchestrationStackRetireTask
end

def self.model_being_retired
OrchestrationStack
end
end
2 changes: 2 additions & 0 deletions app/models/service/retirement_management.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ def before_retirement
end

def retire_service_resources
# TODO: delete me per https://github.com/ManageIQ/manageiq/pull/16933#discussion_r175805070
return
direct_service_children.each(&:retire_service_resources)

service_resources.each do |sr|
Expand Down
15 changes: 1 addition & 14 deletions app/models/service_retire_request.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
class ServiceRetireRequest < MiqRequest
class ServiceRetireRequest < MiqRetireRequest
TASK_DESCRIPTION = 'Service Retire'.freeze
SOURCE_CLASS_NAME = 'Service'.freeze

validates :request_state, :inclusion => { :in => %w(pending finished) + ACTIVE_STATES, :message => "should be pending, #{ACTIVE_STATES.join(", ")} or finished" }

validate :must_have_user
delegate :service_template, :to => :source, :allow_nil => true

default_value_for(:source_id) { |r| r.get_option(:src_id) }
default_value_for :source_type, SOURCE_CLASS_NAME

def my_role
'ems_operations'
end

def my_zone
end
end
24 changes: 3 additions & 21 deletions app/models/service_retire_task.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,10 @@
class ServiceRetireTask < MiqRequestTask
validate :validate_request_type, :validate_state

AUTOMATE_DRIVES = true

class ServiceRetireTask < MiqRetireTask
def self.base_model
ServiceRetireTask
end

def self.get_description(req_obj)
name = nil
if req_obj.source.nil?
# Single source has not been selected yet
if req_obj.options[:src_ids].length == 1
s = Service.find_by(:id => req_obj.options[:src_ids].first)
name = s.nil? ? "" : s.name
else
name = "Multiple Services"
end
else
name = req_obj.source.name
end

new_settings = []
"#{request_class::TASK_DESCRIPTION} for: #{name} - #{new_settings.join(", ")}"
def self.model_being_retired
Service
end

def after_request_task_create
Expand Down
7 changes: 0 additions & 7 deletions app/models/vm_retire_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,8 @@ class VmRetireRequest < MiqRequest
SOURCE_CLASS_NAME = 'Vm'.freeze
ACTIVE_STATES = %w(retired) + base_class::ACTIVE_STATES

validates :request_state, :inclusion => { :in => %w(pending finished) + ACTIVE_STATES, :message => "should be pending, #{ACTIVE_STATES.join(", ")} or finished" }
validate :must_have_user

def my_zone
vm = Vm.find_by(:id => options[:src_ids])
vm.nil? ? super : vm.my_zone
end

def my_role
'ems_operations'
end
end
101 changes: 3 additions & 98 deletions app/models/vm_retire_task.rb
Original file line number Diff line number Diff line change
@@ -1,106 +1,11 @@
class VmRetireTask < MiqRequestTask
class VmRetireTask < MiqRetireTask
alias_attribute :vm, :source

validate :validate_request_type, :validate_state

AUTOMATE_DRIVES = true

def self.base_model
VmRetireTask
end

def self.get_description(req_obj)
name = nil
if req_obj.source.nil?
# Single source has not been selected yet
if req_obj.options[:src_ids].length == 1
v = Vm.find_by(:id => req_obj.options[:src_ids].first)
name = v.nil? ? "" : v.name
else
name = "Multiple VMs"
end
else
name = req_obj.source.name
end

new_settings = []
"#{request_class::TASK_DESCRIPTION} for: #{name} - #{new_settings.join(", ")}"
end

def after_request_task_create
update_attribute(:description, get_description)
end

def after_ae_delivery(ae_result)
_log.info("ae_result=#{ae_result.inspect}")
reload

return if ae_result == 'retry'
return if miq_request.state == 'finished'

if ae_result == 'ok'
update_and_notify_parent(:state => "finished", :status => "Ok", :message => display_message("#{request_class::TASK_DESCRIPTION} completed"))
else
mark_pending_items_as_finished
update_and_notify_parent(:state => "finished", :status => "Error", :message => display_message("#{request_class::TASK_DESCRIPTION} failed"))
end
end

def deliver_to_automate(req_type = request_type, zone = nil)
task_check_on_delivery

_log.info("Queuing #{request_class::TASK_DESCRIPTION}: [#{description}]...")

if self.class::AUTOMATE_DRIVES
args = {
:object_type => self.class.name,
:object_id => id,
:attrs => {"request" => req_type},
:instance_name => "AUTOMATION",
:user_id => get_user.id,
:miq_group_id => get_user.current_group.id,
:tenant_id => get_user.current_tenant.id,
}

args[:attrs].merge!(MiqAeEngine.create_automation_attributes(source.class.base_model.name => source))

zone ||= source.respond_to?(:my_zone) ? source.my_zone : MiqServer.my_zone
MiqQueue.put(
:class_name => 'MiqAeEngine',
:method_name => 'deliver',
:args => [args],
:role => 'automate',
:zone => options.fetch(:miq_zone, zone),
:tracking_label => my_task_id,
)
update_and_notify_parent(:state => "pending", :status => "Ok", :message => "Automation Starting")
else
execute_queue
end
end

def before_ae_starts(_options)
reload
if state.to_s.downcase.in?(%w(pending queued))
_log.info("Executing #{request_class::TASK_DESCRIPTION} request: [#{description}]")
update_and_notify_parent(:state => "active", :status => "Ok", :message => "In Process")
end
end

def update_and_notify_parent(*args)
prev_state = state
super
task_finished if state == "finished" && prev_state != "finished"
end

def task_finished
end

def mark_pending_items_as_finished
miq_request.miq_request_tasks.each do |s|
if s.state == 'pending'
s.update_and_notify_parent(:state => "finished", :status => "Warn", :message => "Error in Request: #{miq_request.id}. Setting pending Task: #{id} to finished.") unless id == s.id
end
end
def self.model_being_retired
Vm
end
end
3 changes: 3 additions & 0 deletions spec/factories/orchestration_stack_retire_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FactoryGirl.define do
factory :orchestration_stack_retire_request
end
3 changes: 3 additions & 0 deletions spec/factories/orchestration_stack_retire_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FactoryGirl.define do
factory :orchestration_stack_retire_task
end
3 changes: 3 additions & 0 deletions spec/factories/service_retire_request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FactoryGirl.define do
factory :service_retire_request
end
3 changes: 3 additions & 0 deletions spec/factories/service_retire_task.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FactoryGirl.define do
factory :service_retire_task
end
Loading

0 comments on commit 0d789ad

Please sign in to comment.