Skip to content

Commit

Permalink
Merge pull request #17764 from tumido/ansible_reporting
Browse files Browse the repository at this point in the history
Add CustomButton event emiter
  • Loading branch information
gmcculloug committed Aug 20, 2018
2 parents 5903757 + b9cc5a6 commit c08b60e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
24 changes: 22 additions & 2 deletions app/models/custom_button.rb
Expand Up @@ -88,11 +88,29 @@ def applies_to=(other)
end
end

def invoke(target)
def invoke(target, source = nil)
args = resource_action.automate_queue_hash(target, {}, User.current_user)

publish_event(source, target, args)
MiqQueue.put(queue_opts(target, args))
end

def publish_event(source, target, args)
CustomButtonEvent.create(
:event_type => 'button.trigger.start',
:message => 'Custom button launched',
:source => source,
:target => target,
:user_id => args[:user_id],
:group_id => args[:miq_group_id],
:tenant_id => args[:tenant_id],
:full_data => {
:args => args,
:automate_entry_point => resource_action.ae_path
}
)
end

def queue_opts(target, args)
{
:class_name => 'MiqAeEngine',
Expand All @@ -104,13 +122,15 @@ def queue_opts(target, args)
}
end

def invoke_async(target)
def invoke_async(target, source = nil)
task_opts = {
:action => "Calling automate for user #{userid}",
:userid => User.current_user
}

args = resource_action.automate_queue_hash(target, {}, User.current_user)

publish_event(source, target, args)
MiqTask.generic_action_with_callback(task_opts, queue_opts(target, args))
end

Expand Down
2 changes: 2 additions & 0 deletions app/models/custom_button_event.rb
@@ -0,0 +1,2 @@
class CustomButtonEvent < EventStream
end
32 changes: 32 additions & 0 deletions spec/models/custom_button_spec.rb
Expand Up @@ -292,4 +292,36 @@
button = FactoryGirl.create(:custom_button, :applies_to => service_template1)
expect { button.copy(:applies_to => service_template2) }.to change { CustomButton.count }.by(1)
end

context do
let(:vm) { FactoryGirl.create(:vm_vmware) }
let(:user) { FactoryGirl.create(:user_with_group) }
let(:resource_action) { FactoryGirl.create(:resource_action, :ae_namespace => 'SYSTEM', :ae_class => 'PROCESS', :ae_instance => 'Request') }
let(:custom_button) { FactoryGirl.create(:custom_button, :applies_to => vm.class, :resource_action => resource_action) }

before do
EvmSpecHelper.local_miq_server(:is_master => true, :zone => Zone.seed)
end

%i(invoke invoke_async).each do |method|
describe "##{method}" do
it "publishes CustomButtonEvent" do
User.with_user(user) { custom_button.send(method, vm, 'UI') }

expect(CustomButtonEvent.count).to eq(1)
expect(CustomButtonEvent.first).to have_attributes(
:source => 'UI',
:target_id => vm.id,
:target_type => 'VmOrTemplate',
:type => 'CustomButtonEvent',
:event_type => 'button.trigger.start',
:user_id => user.id
)
expect(CustomButtonEvent.first[:full_data]).to include(
:automate_entry_point => "/SYSTEM/PROCESS/Request"
)
end
end
end
end
end

0 comments on commit c08b60e

Please sign in to comment.