Skip to content

Commit

Permalink
Store the event initiator in MiqEvent object when user info is availa…
Browse files Browse the repository at this point in the history
  • Loading branch information
lfu committed Oct 27, 2017
1 parent ddc6393 commit 3e0e4fe
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 59 deletions.
13 changes: 6 additions & 7 deletions app/models/miq_event.rb
Expand Up @@ -79,16 +79,15 @@ def process_evm_event(inputs = {})
end

def self.build_evm_event(event, target)
user = User.current_user
MiqEvent.create(
options = {
:event_type => event,
:target => target,
:source => 'POLICY',
:timestamp => Time.now.utc,
:user_id => user.try(:id),
:group_id => user.try(:current_group).try(:id),
:tenant_id => user.try(:current_tenant).try(:id)
)
:timestamp => Time.now.utc
}
user = User.current_user
options.merge!(:user_id => user.id, :group_id => user.current_group.id, :tenant_id => user.current_tenant.id) if user
MiqEvent.create(options)
end

def update_with_policy_result(result = {})
Expand Down
40 changes: 19 additions & 21 deletions app/models/mixins/process_tasks_mixin.rb
Expand Up @@ -17,15 +17,14 @@ def process_tasks(options)
end

def invoke_tasks_queue(options)
user = User.current_user
MiqQueue.submit_job(
q_hash = {
:class_name => name,
:method_name => "invoke_tasks",
:args => [options],
:user_id => user.id,
:group_id => user.current_group.id,
:tenant_id => user.current_tenant.id
)
:args => [options]
}
user = User.current_user
q_hash.merge!(:user_id => user.id, :group_id => user.current_group.id, :tenant_id => user.current_tenant.id) if user
MiqQueue.submit_job(q_hash)
end

# Performs tasks received from the UI via the queue
Expand Down Expand Up @@ -78,16 +77,16 @@ def invoke_tasks_remote(options)

$log.error("An error occurred while invoking remote tasks...Requeueing for 1 minute from now.")
$log.log_backtrace(err)
user = User.current_user
MiqQueue.submit_job(

q_hash = {
:class_name => name,
:method_name => 'invoke_tasks_remote',
:args => [remote_options],
:deliver_on => Time.now.utc + 1.minute,
:user_id => user.id,
:group_id => user.current_group.id,
:tenant_id => user.current_tenant.id
)
:deliver_on => Time.now.utc + 1.minute
}
user = User.current_user
q_hash.merge!(:user_id => user.id, :group_id => user.current_group.id, :tenant_id => user.current_tenant.id) if user
MiqQueue.submit_job(q_hash)
next
end

Expand Down Expand Up @@ -139,24 +138,23 @@ def task_arguments(options)

# default implementation, can be overridden
def invoke_task_local(task, instance, options, args)
user = User.current_user
cb = {
:class_name => task.class.to_s,
:instance_id => task.id,
:method_name => :queue_callback,
:args => ["Finished"]
} if task

MiqQueue.submit_job(
q_hash = {
:class_name => name,
:instance_id => instance.id,
:method_name => options[:task],
:args => args,
:miq_callback => cb,
:user_id => user.id,
:group_id => user.current_group.id,
:tenant_id => user.current_tenant.id
)
:miq_callback => cb
}
user = User.current_user
q_hash.merge!(:user_id => user.id, :group_id => user.current_group.id, :tenant_id => user.current_tenant.id) if user
MiqQueue.submit_job(q_hash)
end

private
Expand Down
47 changes: 22 additions & 25 deletions app/models/vm_or_template.rb
Expand Up @@ -448,31 +448,28 @@ def self.invoke_task_local(task, vm, options, args)
end
end

if options[:task] == "destroy"
MiqQueue.submit_job(
:class_name => base_class.name,
:instance_id => vm.id,
:method_name => options[:task],
:args => args,
:miq_callback => cb,
:user_id => user.id,
:group_id => user.current_group.id,
:tenant_id => user.current_tenant.id
)
else
MiqQueue.submit_job(
:service => options[:invoke_by] == :job ? "smartstate" : "ems_operations",
:affinity => vm.ext_management_system,
:class_name => base_class.name,
:instance_id => vm.id,
:method_name => options[:task],
:args => args,
:miq_callback => cb,
:user_id => user.id,
:group_id => user.current_group.id,
:tenant_id => user.current_tenant.id
)
end
q_hash =
if options[:task] == "destroy"
{
:class_name => base_class.name,
:instance_id => vm.id,
:method_name => options[:task],
:args => args,
:miq_callback => cb,
}
else
{
:service => options[:invoke_by] == :job ? "smartstate" : "ems_operations",
:affinity => vm.ext_management_system,
:class_name => base_class.name,
:instance_id => vm.id,
:method_name => options[:task],
:args => args,
:miq_callback => cb,
}
end
q_hash.merge!(:user_id => user.id, :group_id => user.current_group.id, :tenant_id => user.current_tenant.id) if user
MiqQueue.submit_job(q_hash)
end

def self.action_for_task(task)
Expand Down
6 changes: 1 addition & 5 deletions spec/models/mixins/process_tasks_mixin_spec.rb
Expand Up @@ -28,7 +28,6 @@ def test_method

it "queues a message for the specified task" do
EvmSpecHelper.create_guid_miq_server_zone
allow(User).to receive(:current_user).and_return(FactoryGirl.create(:user_with_group, :userid => "admin"))
test_class.process_tasks(:task => "test_method", :ids => [5], :userid => "admin")

message = MiqQueue.first
Expand All @@ -43,7 +42,6 @@ def test_method

it "defaults the userid to system in the queue message" do
EvmSpecHelper.create_guid_miq_server_zone
allow(User).to receive(:current_user).and_return(FactoryGirl.create(:user_with_group, :userid => "admin"))
test_class.process_tasks(:task => "test_method", :ids => [5])

message = MiqQueue.first
Expand All @@ -52,7 +50,7 @@ def test_method
message.args.each do |h|
expect(h[:task]).to eq("test_method")
expect(h[:ids]).to eq([5])
expect(h[:userid]).to eq("admin")
expect(h[:userid]).to eq("system")
end
end

Expand Down Expand Up @@ -100,7 +98,6 @@ def test_method
end

it "requeues invoke_tasks_remote when invoke_api_tasks fails" do
allow(User).to receive(:current_user).and_return(FactoryGirl.create(:user_with_group, :userid => "admin"))
expect(InterRegionApiMethodRelay).to receive(:api_client_connection_for_region)
expect(test_class).to receive(:invoke_api_tasks).and_raise(RuntimeError)
test_class.invoke_tasks_remote(test_options)
Expand All @@ -121,7 +118,6 @@ def test_method
end

it "requeues if the server does not have an address" do
allow(User).to receive(:current_user).and_return(FactoryGirl.create(:user_with_group, :userid => "admin"))
test_class.invoke_tasks_remote(test_options)

expect(MiqQueue.first).to have_attributes(
Expand Down
1 change: 0 additions & 1 deletion spec/models/vm_spec.rb
Expand Up @@ -117,7 +117,6 @@
EvmSpecHelper.create_guid_miq_server_zone
@host = FactoryGirl.create(:host)
@vm = FactoryGirl.create(:vm_vmware, :host => @host)
allow(User).to receive(:current_user).and_return(FactoryGirl.create(:user_with_group, :userid => "Freddy"))
end

it "sets up standard callback for non Power Operations" do
Expand Down

0 comments on commit 3e0e4fe

Please sign in to comment.