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

Run a control action to order Ansible Playbook Service #13874

Merged
merged 5 commits into from
Feb 28, 2017
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
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