Skip to content

Commit

Permalink
Add retirement initiator context
Browse files Browse the repository at this point in the history
  • Loading branch information
d-m-u committed Sep 10, 2018
1 parent 85265cd commit 1d42b76
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
2 changes: 1 addition & 1 deletion app/models/mixins/process_tasks_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
61 changes: 46 additions & 15 deletions app/models/mixins/retirement_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {})
Expand Down Expand Up @@ -123,7 +123,9 @@ def retirement_check
end
end

self.class.make_retire_request(self.id) if retirement_due?
find_requester(initiator_context(retirement_requester))

self.class.make_retire_request(self.id, *requester) if retirement_due?
end

def retire_now(requester = nil)
Expand Down Expand Up @@ -201,14 +203,18 @@ 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)
initiator = initiator_context(requester)
if initiator == "system"
q_user_info(q_options)
requester = q_options[:user_id]
$log.info("System retirement initiator using id: [#{requester}] for: #{name}")
end
$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,
Expand All @@ -219,6 +225,10 @@ def raise_audit_event(event_name, message, requester = nil)
AuditEvent.success(event_hash)
end

def initiator_context(requester)
requester.present? ? user_initiation : system_initiation
end

def retiring?
retirement_state == RETIREMENT_RETIRING
end
Expand All @@ -229,6 +239,10 @@ def error_retiring?

private

def find_requester(requester_context, q_options = {})
requester_context == "system" ? q_options[:user_id] : User.current_user
end

def retire_queue_options
valid_zone? ? {:zone => my_zone} : {}
end
Expand All @@ -237,14 +251,31 @@ 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 q_user_info(q_options)
if respond_to?(:evm_owner)
q_options[:user_id] = evm_owner.id
q_options[:group_id] = evm_owner.current_group.id
q_options[:tenant_id] = evm_owner.current_tenant.id
q_options
else
# todo
end
end

def user_initiation
"user".freeze
end

def system_initiation
"system".freeze
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
Expand Down

0 comments on commit 1d42b76

Please sign in to comment.