Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to catch the retirement requester in automate. #17033

Merged
merged 1 commit into from
Feb 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lfu How do we ensure that the User.current_user is set? Should we be raising an error if its not set

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mkanoor We only take and store the user info into the queue only when the user is set. The retirement requester would default to system if no user is specified.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The system account would be used when retirement is initiated from the scheduler.

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The raise_retirement_event method gets call from two places: retirement_check and retire_now.

The retirement_check method is called from the scheduler and I do not believe it gets called anywhere else, so I would expect User.current_user to be nil.

The retire_now method can get called directly by a user and on line 128 sets update_attributes(:retirement_requester => requester). With your current code change the User.current_user would be passed to the event, but not set for the retirement_requester on the object, so this is only a partial fix.

Moving requester ||= User.current_user.try(:userid) into retire_now before the update_attributes call would resolve this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

retirement_check gets called when running Set Retirement Date.

When retire_now gets called from UI or API, requester is already set with this change.

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