From 6cfc8e8e440d755b41d8bc1c05356641f12e3efb Mon Sep 17 00:00:00 2001 From: d-m-u Date: Thu, 6 Sep 2018 11:46:51 -0400 Subject: [PATCH] Add retirement initiator context --- app/models/mixins/process_tasks_mixin.rb | 2 +- app/models/mixins/retirement_mixin.rb | 55 +++++++++++++------ .../retirement_management_spec.rb | 3 +- 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/app/models/mixins/process_tasks_mixin.rb b/app/models/mixins/process_tasks_mixin.rb index 61e37036e499..34297b7f0958 100644 --- a/app/models/mixins/process_tasks_mixin.rb +++ b/app/models/mixins/process_tasks_mixin.rb @@ -7,7 +7,7 @@ module ClassMethods def process_tasks(options) raise _("No ids given to process_tasks") if options[:ids].blank? if options[:task] == 'retire_now' - name.constantize.make_retire_request(*options[:ids]) + name.constantize.make_retire_request(*options[:ids], User.find_by(:userid => User.current_user.try(:userid))) elsif options[:task] == "refresh_ems" && respond_to?("refresh_ems") refresh_ems(options[:ids]) msg = "'#{options[:task]}' initiated for #{options[:ids].length} #{ui_lookup(:table => base_class.name).pluralize}" diff --git a/app/models/mixins/retirement_mixin.rb b/app/models/mixins/retirement_mixin.rb index b683991cd31a..ead5fe00db37 100644 --- a/app/models/mixins/retirement_mixin.rb +++ b/app/models/mixins/retirement_mixin.rb @@ -10,10 +10,10 @@ module RetirementMixin end module ClassMethods - def make_retire_request(*src_ids) + def make_retire_request(*src_ids, requester) klass = (name.demodulize + "RetireRequest").constantize - options = {:src_ids => src_ids.presence || id, :__request_type__ => klass.request_types.first} - klass.make_request(nil, options, User.current_user, true) + options = {:src_ids => src_ids.presence, :__request_type__ => klass.request_types.first} + klass.make_request(nil, options, requester, true) end def retire(ids, options = {}) @@ -112,18 +112,19 @@ def raise_retire_audit_event(message) def retirement_check return if retired? || retiring? || retirement_initialized? + requester = system_context_requester if !retirement_warned? && retirement_warning_due? begin self.retirement_last_warn = Time.now.utc save - raise_retirement_event(retire_warn_event_name) + raise_retirement_event(retire_warn_event_name, requester) rescue => err _log.log_backtrace(err) end end - self.class.make_retire_request(self.id) if retirement_due? + self.class.make_retire_request(self.id, *requester) if retirement_due? end def retire_now(requester = nil) @@ -140,7 +141,7 @@ def retire_now(requester = nil) event_name = "request_#{retirement_event_prefix}_retire" _log.info("calling #{event_name}") begin - raise_retirement_event(event_name, requester) + raise_retirement_event(event_name, requester ||= current_user) rescue => err _log.log_backtrace(err) end @@ -201,14 +202,12 @@ def retired_event_name end def raise_retirement_event(event_name, requester = nil) - requester ||= User.current_user.try(:userid) - q_options = retire_queue_options - $log.info("Requester [#{requester}] raising Retirement Event for [#{name}] with queue options: #{q_options.inspect}") - MiqEvent.raise_evm_event(self, event_name, setup_event_hash(requester), q_options) + q_options = q_user_info(retire_queue_options, requester) + $log.info("Raising Retirement Event for [#{name}] with queue options: #{q_options.inspect}") + MiqEvent.raise_evm_event(self, event_name, setup_event_hash(requester, initiator), q_options) end def raise_audit_event(event_name, message, requester = nil) - requester ||= User.current_user.try(:userid) event_hash = { :target_class => retirement_base_model_name, :target_id => id.to_s, @@ -237,14 +236,34 @@ def valid_zone? respond_to?(:my_zone) && my_zone.present? end - def setup_event_hash(requester) - event_hash = {:retirement_initiator => "system"} - event_hash[retirement_base_model_name.underscore.to_sym] = self - event_hash[:host] = host if self.respond_to?(:host) - if requester - event_hash[:userid] = requester - event_hash[:retirement_initiator] = "user" + def system_context_requester + if respond_to?(:evm_owner) && evm_owner.present? + evm_owner.id + elsif respond_to?(:tenant_identity) + tenant_identity + else + $log.info("Cannot retire without a valid owner, you moron.") + raise # raise something here end + end + + def current_user + User.find_by(:userid => User.current_user.try(:userid)) + end + + def q_user_info(q_options, requester) + q_options[:user_id] = requester.id + q_options[:group_id] = requester.current_group.id + q_options[:tenant_id] = requester.current_tenant.id + q_options + end + + def setup_event_hash(requester, initiator) + event_hash = {} + event_hash[:retirement_initiator] = initiator + event_hash[:userid] = requester + event_hash[retirement_base_model_name.underscore.to_sym] = self + event_hash[:host] = host if respond_to?(:host) event_hash[:type] ||= self.class.name event_hash end diff --git a/spec/models/orchestration_stack/retirement_management_spec.rb b/spec/models/orchestration_stack/retirement_management_spec.rb index 07db310ded3b..913f126107a3 100644 --- a/spec/models/orchestration_stack/retirement_management_spec.rb +++ b/spec/models/orchestration_stack/retirement_management_spec.rb @@ -131,7 +131,8 @@ event_hash = { :orchestration_stack => @stack, :type => "OrchestrationStack", - :retirement_initiator => "system" + :retirement_initiator => "system", + :userid => nil } expect(MiqEvent).to receive(:raise_evm_event).with(@stack, event_name, event_hash, {})