Skip to content

Commit

Permalink
Changes to catch the retirement requester in automate.
Browse files Browse the repository at this point in the history
  • Loading branch information
lfu committed Feb 23, 2018
1 parent 7c7f922 commit 037c427
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
13 changes: 10 additions & 3 deletions app/models/mixins/retirement_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ def retire(ids, options = {})
object = find_by(:id => id)
object.retire(options) if object.respond_to?(:retire)
end
MiqQueue.put(:class_name => 'RetirementManager', :method_name => 'check')
q_options = {:class_name => 'RetirementManager', :method_name => 'check'}
user = User.current_user
q_options.merge!(:user_id => user.id, :group_id => user.current_group.id, :tenant_id => user.current_tenant.id) if user
MiqQueue.put(q_options)
end
end

Expand Down Expand Up @@ -136,10 +139,11 @@ def retire_now(requester = nil)
def finish_retirement
raise _("%{name} already retired") % {:name => name} if retired?
$log.info("Finishing Retirement for [#{name}]")
requester = retirement_requester
update_attributes(:retires_on => Time.zone.now, :retired => true, :retirement_state => "retired")
message = "#{self.class.base_model.name}: [#{name}], Retires On: [#{retires_on.strftime("%x %R %Z")}], has been retired"
$log.info("Calling audit event for: #{message} ")
raise_audit_event(retired_event_name, message)
raise_audit_event(retired_event_name, message, requester)
$log.info("Called audit event for: #{message} ")
end

Expand Down Expand Up @@ -178,18 +182,21 @@ 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("Raising Retirement Event for [#{name}] with queue options: #{q_options.inspect}")
MiqEvent.raise_evm_event(self, event_name, setup_event_hash(requester), q_options)
end

def raise_audit_event(event_name, message)
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,
:event => event_name,
:message => message
}
event_hash[:userid] = requester if requester.present?
AuditEvent.success(event_hash)
end

Expand Down
15 changes: 14 additions & 1 deletion spec/models/vm/retirement_management_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
expect(vm.retirement_due?).to be_truthy
end

it "#raise_retirement_event" do
it "#raise_retirement_event without current user" do
event_name = 'foo'
event_hash = {:vm => @vm, :host => @vm.host, :type => "ManageIQ::Providers::Vmware::InfraManager::Vm",
:retirement_initiator => "system"}
Expand All @@ -143,6 +143,19 @@
@vm.raise_retirement_event(event_name)
end

it "#raise_retirement_event with current user" do
user = FactoryGirl.create(:user_with_group, :userid => 'freddy')
event_name = 'foo'
event_hash = {:vm => @vm, :host => @vm.host, :type => "ManageIQ::Providers::Vmware::InfraManager::Vm",
:retirement_initiator => "user", :userid => 'freddy'}
options = {:zone => @vm.my_zone}

User.with_user(user) do
expect(MiqEvent).to receive(:raise_evm_event).with(@vm, event_name, event_hash, options).once
@vm.raise_retirement_event(event_name)
end
end

it "#raise_audit_event" do
event_name = 'foo'
message = 'bar'
Expand Down

0 comments on commit 037c427

Please sign in to comment.