Skip to content

Commit

Permalink
Merge pull request ManageIQ#13874 from mkanoor/miq_action_run_playbook
Browse files Browse the repository at this point in the history
Run a control action to order Ansible Playbook Service
  • Loading branch information
gmcculloug authored Feb 28, 2017
2 parents 8036eda + 5a4e3b5 commit 8710eed
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
28 changes: 28 additions & 0 deletions app/models/miq_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ def action_audit(_action, rec, inputs)
:message => "Policy #{msg}: policy: [#{inputs[:policy].description}], event: [#{inputs[:event].description}]")
end

def action_run_ansible_playbook(action, rec, _inputs)
service_template = ServiceTemplate.find(action.options[:service_template_id])
options = { :dialog_hosts => target_hosts(action, rec) }
service_template.provision_request(target_user(rec), options)
end

def action_snmp_trap(action, rec, inputs)
# Validate SNMP Version
snmp_version = action.options[:snmp_version]
Expand Down Expand Up @@ -1018,4 +1024,26 @@ def invoke_or_queue(
MiqQueue.put(args)
end
end

def target_hosts(action, rec)
if action.options[:use_event_target]
ipaddress(rec)
elsif action.options[:use_localhost]
'localhost'
else
action.options[:hosts]
end
end

def ipaddress(record)
record.ipaddresses[0] if record.respond_to?(:ipaddresses)
end

def target_user(record)
record.respond_to?(:tenant_identity) ? record.tenant_identity : default_user
end

def default_user
User.super_admin.tap { |u| u.current_group = Tenant.root_tenant.default_miq_group }
end
end
58 changes: 58 additions & 0 deletions spec/models/miq_action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -387,4 +387,62 @@ def stub_csv(data)
action.action_email(action, nil, inputs)
end
end

context 'run_ansible_playbook' do
let(:tenant) { FactoryGirl.create(:tenant) }
let(:group) { FactoryGirl.create(:miq_group, :tenant => tenant) }
let(:user) { FactoryGirl.create(:user, :userid => "test", :miq_groups => [group]) }
let(:vm) { FactoryGirl.create(:vm_vmware, :evm_owner => user, :miq_group => group, :hardware => hardware) }
let(:action) { FactoryGirl.create(:miq_action, :name => "run_ansible_playbook", :options => action_options) }
let(:stap) { FactoryGirl.create(:service_template_ansible_playbook) }
let(:ip1) { "1.1.1.94" }
let(:ip2) { "1.1.1.96" }
let(:hardware) do
FactoryGirl.create(:hardware).tap do |h|
h.ipaddresses << ip1
h.ipaddresses << ip2
end
end

shared_examples_for "#workflow check" do
it "run playbook" do
miq_request = instance_double(MiqRequest)
allow(vm).to receive(:tenant_identity).and_return(user)
expect(ServiceTemplate).to receive(:find).with(stap.id).and_return(stap)
expect(stap).to receive(:provision_request).with(user, options).and_return(miq_request)

action.action_run_ansible_playbook(action, vm, {})
end
end

context "use event target" do
let(:action_options) do
{ :service_template_id => stap.id,
:use_event_target => true }
end
let(:options) { {:dialog_hosts => ip1 } }

it_behaves_like "#workflow check"
end

context "use localhost" do
let(:action_options) do
{ :service_template_id => stap.id,
:use_localhost => true }
end
let(:options) { {:dialog_hosts => 'localhost' } }

it_behaves_like "#workflow check"
end

context "use hosts" do
let(:action_options) do
{ :service_template_id => stap.id,
:hosts => "ip1, ip2" }
end
let(:options) { {:dialog_hosts => 'ip1, ip2' } }

it_behaves_like "#workflow check"
end
end
end

0 comments on commit 8710eed

Please sign in to comment.