Skip to content

Commit

Permalink
Pass event_source and event_name to Ansible
Browse files Browse the repository at this point in the history
  • Loading branch information
mkanoor committed Mar 27, 2017
1 parent be47178 commit e7f0305
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 26 deletions.
10 changes: 6 additions & 4 deletions app/models/miq_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,13 @@ 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)
def action_run_ansible_playbook(action, rec, inputs)
service_template = ServiceTemplate.find(action.options[:service_template_id])
options = { :hosts => target_hosts(action, rec),
:initiator => 'control' }
service_template.provision_request(target_user(rec), options)
dialog_options = { :hosts => target_hosts(action, rec) }
request_options = { :manageiq_extra_vars => { 'event_source' => rec.href_slug,
'event_name' => inputs[:event].try(:name) },
:initiator => 'control' }
service_template.provision_request(target_user(rec), dialog_options, request_options)
end

def action_snmp_trap(action, rec, inputs)
Expand Down
6 changes: 5 additions & 1 deletion app/models/resource_action_workflow.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class ResourceActionWorkflow < MiqRequestWorkflow
attr_accessor :dialog
attr_accessor :request_options

def self.base_model
ResourceActionWorkflow
Expand Down Expand Up @@ -55,7 +56,10 @@ def validate_dialog
end

def create_values
create_values_hash.tap { |value| value[:src_id] = @target.id }
create_values_hash.tap do |value|
value[:src_id] = @target.id
value[:request_options] = request_options unless request_options.blank?
end
end

def request_class
Expand Down
6 changes: 5 additions & 1 deletion app/models/service_ansible_playbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ def manageiq_extra_vars(action)
'service' => href_slug,
'user' => evm_owner.href_slug,
'action' => action
}
}.merge(request_options_extra_vars)
end

def request_options_extra_vars
miq_request_task.options.fetch_path(:request_options, :manageiq_extra_vars) || {}
end

def get_job_options(action)
Expand Down
24 changes: 13 additions & 11 deletions app/models/service_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,19 @@ def self.create_from_options(options)
end
private_class_method :create_from_options

def provision_request(user, options = nil)
provision_workflow(user, options).submit_request
def provision_request(user, options = nil, request_options = nil)
provision_workflow(user, options, request_options).submit_request
end

def provision_workflow(user, dialog_options = nil, request_options = nil)
dialog_options ||= {}
request_options ||= {}
ra_options = { :target => self, :initiator => request_options[:initiator] }
ResourceActionWorkflow.new({}, user,
provision_action, ra_options).tap do |wf|
wf.request_options = request_options
dialog_options.each { |key, value| wf.set_value(key, value) }
end
end

private
Expand Down Expand Up @@ -463,15 +474,6 @@ def update_from_options(params)
update_attributes!(params.except(:config_info))
end

def provision_workflow(user, options = nil)
options ||= {}
ra_options = { :target => self, :initiator => options[:initiator] }
ResourceActionWorkflow.new({}, user,
provision_action, ra_options).tap do |wf|
options.each { |key, value| wf.set_value(key, value) }
end
end

def construct_config_info
config_info = {}
if service_resources.where(:resource_type => 'MiqRequest').exists?
Expand Down
20 changes: 15 additions & 5 deletions spec/models/miq_action_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -397,21 +397,31 @@ def stub_csv(data)
let(:stap) { FactoryGirl.create(:service_template_ansible_playbook) }
let(:ip1) { "1.1.1.94" }
let(:ip2) { "1.1.1.96" }
let(:event_name) { "Fred" }

let(:miq_event_def) do
FactoryGirl.create(:miq_event_definition, :name => event_name)
end
let(:hardware) do
FactoryGirl.create(:hardware).tap do |h|
h.ipaddresses << ip1
h.ipaddresses << ip2
end
end

let(:request_options) do
{ :manageiq_extra_vars => { "event_source" => vm.href_slug, "event_name" => event_name },
:initiator => 'control' }
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)
expect(stap).to receive(:provision_request).with(user, dialog_options, request_options).and_return(miq_request)

action.action_run_ansible_playbook(action, vm, {})
action.action_run_ansible_playbook(action, vm, :event => miq_event_def)
end
end

Expand All @@ -420,7 +430,7 @@ def stub_csv(data)
{ :service_template_id => stap.id,
:use_event_target => true }
end
let(:options) { {:hosts => ip1, :initiator => 'control' } }
let(:dialog_options) { {:hosts => ip1 } }

it_behaves_like "#workflow check"
end
Expand All @@ -430,7 +440,7 @@ def stub_csv(data)
{ :service_template_id => stap.id,
:use_localhost => true }
end
let(:options) { {:hosts => 'localhost', :initiator => 'control' } }
let(:dialog_options) { {:hosts => 'localhost' } }

it_behaves_like "#workflow check"
end
Expand All @@ -440,7 +450,7 @@ def stub_csv(data)
{ :service_template_id => stap.id,
:hosts => "ip1, ip2" }
end
let(:options) { {:hosts => 'ip1, ip2', :initiator => 'control' } }
let(:dialog_options) { {:hosts => 'ip1, ip2' } }

it_behaves_like "#workflow check"
end
Expand Down
7 changes: 5 additions & 2 deletions spec/models/service_ansible_playbook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,17 @@
describe '#execute' do
before do
FactoryGirl.create(:miq_region, :region => ApplicationRecord.my_region_number)
miq_request_task = FactoryGirl.create(:miq_request_task)
@control_extras = { 'a' => 'A', 'b' => 'B', 'c' => 'C'}
miq_request_task.update_attributes(:options => {:request_options => {:manageiq_extra_vars => @control_extras}})
loaded_service.update_attributes(:evm_owner => FactoryGirl.create(:user),
:miq_request_task => FactoryGirl.create(:miq_request_task))
:miq_request_task => miq_request_task)
end

it 'creates an Ansible Tower job' do
expect(ManageIQ::Providers::EmbeddedAnsible::AutomationManager::Job).to receive(:create_job) do |jobtemp, opts|
expect(jobtemp).to eq(tower_job_temp)
exposed_miq = %w(api_url api_token service user action)
exposed_miq = %w(api_url api_token service user) + @control_extras.keys
expect(opts[:extra_vars].delete('manageiq').keys).to include(*exposed_miq)
expect(opts).to include(provision_options[:provision_job_options])
tower_job
Expand Down
5 changes: 3 additions & 2 deletions spec/models/service_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -674,9 +674,10 @@
.with({}, user, resource_action, hash).and_return(workflow))
expect(workflow).to receive(:submit_request)
expect(workflow).to receive(:set_value).with('ordered_by', 'fred')
expect(workflow).to receive(:set_value).with(:initiator, 'control')
expect(workflow).to receive(:request_options=).with(:initiator => 'control')

service_template.provision_request(user, 'ordered_by' => 'fred', :initiator => 'control')
service_template.provision_request(user, {'ordered_by' => 'fred'},
{:initiator => 'control'})
end
end
end
Expand Down

0 comments on commit e7f0305

Please sign in to comment.