diff --git a/app/models/mixins/retirement_mixin.rb b/app/models/mixins/retirement_mixin.rb index 3c894398a1f..013b8753b27 100644 --- a/app/models/mixins/retirement_mixin.rb +++ b/app/models/mixins/retirement_mixin.rb @@ -128,10 +128,8 @@ def retire_now(requester = nil) _log.info("Creating request for #{self.class.name} #{id}") begin options = {:src_ids => id} - VmRetireRequest.make_request(@request_id, options, User.current_user.try(:userid)) - - # still need the next line for orchestration and services - # raise_retirement_event(event_name, requester) + request_type = (self.class.name.demodulize + "RetireRequest").constantize + request_type.make_request(@request_id, options, User.current_user.try(:userid)) rescue => err _log.log_backtrace(err) end diff --git a/app/models/service_retire_request.rb b/app/models/service_retire_request.rb new file mode 100644 index 00000000000..d8ceb5509a0 --- /dev/null +++ b/app/models/service_retire_request.rb @@ -0,0 +1,19 @@ +class ServiceRetireRequest < MiqRequest + 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 diff --git a/app/models/service_retire_task.rb b/app/models/service_retire_task.rb new file mode 100644 index 00000000000..fb17573d48a --- /dev/null +++ b/app/models/service_retire_task.rb @@ -0,0 +1,104 @@ +class ServiceRetireTask < MiqRequestTask + validate :validate_request_type, :validate_state + + AUTOMATE_DRIVES = true + + 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(", ")}" + 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 + end +end diff --git a/spec/models/service/retirement_management_spec.rb b/spec/models/service/retirement_management_spec.rb index 0917eef7a3f..6e1338416b8 100644 --- a/spec/models/service/retirement_management_spec.rb +++ b/spec/models/service/retirement_management_spec.rb @@ -24,7 +24,7 @@ it "#retire_now" do expect(@service.retirement_state).to be_nil - expect(MiqEvent).to receive(:raise_evm_event).once + expect(ServiceRetireRequest).to receive(:make_request).once @service.retire_now @service.reload end @@ -35,7 +35,7 @@ event_hash = {:service => @service, :type => "Service", :retirement_initiator => "user", :userid => "freddy"} - expect(MiqEvent).to receive(:raise_evm_event).with(@service, event_name, event_hash, {}).once + expect(ServiceRetireRequest).to receive(:make_request).once @service.retire_now('freddy') @service.reload @@ -47,7 +47,7 @@ event_hash = {:service => @service, :type => "Service", :retirement_initiator => "system"} - expect(MiqEvent).to receive(:raise_evm_event).with(@service, event_name, event_hash, {}).once + expect(ServiceRetireRequest).to receive(:make_request).once @service.retire_now @service.reload diff --git a/spec/models/vm/retirement_management_spec.rb b/spec/models/vm/retirement_management_spec.rb index a9d2d702a30..b3c422267bb 100644 --- a/spec/models/vm/retirement_management_spec.rb +++ b/spec/models/vm/retirement_management_spec.rb @@ -26,7 +26,7 @@ end it "#retire_now" do - expect(MiqEvent).to receive(:raise_evm_event).once + expect(VmRetireRequest).to receive(:make_request).once @vm.retire_now end @@ -37,7 +37,7 @@ :retirement_initiator => "user", :userid => 'freddy'} options = {:zone => @zone.name} - expect(MiqEvent).to receive(:raise_evm_event).with(@vm, event_name, event_hash, options).once + expect(VmRetireRequest).to receive(:make_request).once @vm.retire_now('freddy') end @@ -48,7 +48,7 @@ :retirement_initiator => "system"} options = {:zone => @zone.name} - expect(MiqEvent).to receive(:raise_evm_event).with(@vm, event_name, event_hash, options).once + expect(VmRetireRequest).to receive(:make_request).once @vm.retire_now end