From 386c470a3b120153cc0278699113008e730a5421 Mon Sep 17 00:00:00 2001 From: Madhu Kanoor Date: Thu, 2 Feb 2017 11:58:37 -0500 Subject: [PATCH 1/5] WIP: Run a action to order a service --- app/models/miq_action.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/models/miq_action.rb b/app/models/miq_action.rb index 20f4d4e308a..5cbe1f370cd 100644 --- a/app/models/miq_action.rb +++ b/app/models/miq_action.rb @@ -222,6 +222,11 @@ 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]) + Api::ServiceTemplateWorkflow.create(service_template, {}).submit_request + end + def action_snmp_trap(action, rec, inputs) # Validate SNMP Version snmp_version = action.options[:snmp_version] From a1c7050bd052c8ac6a73014a347ba158ec5aa7f0 Mon Sep 17 00:00:00 2001 From: Madhu Kanoor Date: Fri, 17 Feb 2017 12:41:34 -0500 Subject: [PATCH 2/5] Temporary commit --- app/models/miq_action.rb | 20 ++++++++++++++++-- spec/models/miq_action_spec.rb | 38 ++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/app/models/miq_action.rb b/app/models/miq_action.rb index 5cbe1f370cd..72a37977337 100644 --- a/app/models/miq_action.rb +++ b/app/models/miq_action.rb @@ -222,9 +222,11 @@ 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]) - Api::ServiceTemplateWorkflow.create(service_template, {}).submit_request + options = { :dialog_hosts => target_hosts(action, rec) } + + Api::ServiceTemplateWorkflow.create(service_template, options).submit_request end def action_snmp_trap(action, rec, inputs) @@ -1023,4 +1025,18 @@ 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 end diff --git a/spec/models/miq_action_spec.rb b/spec/models/miq_action_spec.rb index ec6b48aa329..68692cde23d 100644 --- a/spec/models/miq_action_spec.rb +++ b/spec/models/miq_action_spec.rb @@ -387,4 +387,42 @@ 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 + wf = instance_double(ResourceActionWorkflow) + allow(wf).to receive(:submit_request) + expect(ServiceTemplate).to receive(:find).with(stap.id).and_return(stap) + expect(Api::ServiceTemplateWorkflow).to receive(:create).with(stap, options).and_return(wf) + 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 + end end From 1a734514d45a63a78ca7b4124a008e2f485bd68b Mon Sep 17 00:00:00 2001 From: Madhu Kanoor Date: Wed, 22 Feb 2017 14:57:05 -0500 Subject: [PATCH 3/5] Call provision_request in service_template --- app/models/miq_action.rb | 9 ++++++--- spec/models/miq_action_spec.rb | 29 ++++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/app/models/miq_action.rb b/app/models/miq_action.rb index 72a37977337..835b5e18746 100644 --- a/app/models/miq_action.rb +++ b/app/models/miq_action.rb @@ -225,8 +225,7 @@ def action_audit(_action, rec, inputs) def action_run_ansible_playbook(action, rec, _inputs) service_template = ServiceTemplate.find(action.options[:service_template_id]) options = { :dialog_hosts => target_hosts(action, rec) } - - Api::ServiceTemplateWorkflow.create(service_template, options).submit_request + service_template.provision_request(target_user(rec), options) end def action_snmp_trap(action, rec, inputs) @@ -1032,11 +1031,15 @@ def target_hosts(action, rec) elsif action.options[:use_localhost] 'localhost' else - action.options['hosts'] + 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 : User.find("admin") + end end diff --git a/spec/models/miq_action_spec.rb b/spec/models/miq_action_spec.rb index 68692cde23d..f253d2c82f5 100644 --- a/spec/models/miq_action_spec.rb +++ b/spec/models/miq_action_spec.rb @@ -406,10 +406,11 @@ def stub_csv(data) shared_examples_for "#workflow check" do it "run playbook" do - wf = instance_double(ResourceActionWorkflow) - allow(wf).to receive(:submit_request) + 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(Api::ServiceTemplateWorkflow).to receive(:create).with(stap, options).and_return(wf) + expect(stap).to receive(:provision_request).with(user, options).and_return(miq_request) + action.action_run_ansible_playbook(action, vm, {}) end end @@ -424,5 +425,27 @@ def stub_csv(data) 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 From 7c19f6499b1b799bcf71d2ecc5e54dc86b9399ff Mon Sep 17 00:00:00 2001 From: Madhu Kanoor Date: Wed, 22 Feb 2017 15:18:06 -0500 Subject: [PATCH 4/5] Fixed rubocop warnings in spec --- spec/models/miq_action_spec.rb | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/spec/models/miq_action_spec.rb b/spec/models/miq_action_spec.rb index f253d2c82f5..0ea10a15c7b 100644 --- a/spec/models/miq_action_spec.rb +++ b/spec/models/miq_action_spec.rb @@ -418,8 +418,7 @@ def stub_csv(data) context "use event target" do let(:action_options) do { :service_template_id => stap.id, - :use_event_target => true - } + :use_event_target => true } end let(:options) { {:dialog_hosts => ip1 } } @@ -429,8 +428,7 @@ def stub_csv(data) context "use localhost" do let(:action_options) do { :service_template_id => stap.id, - :use_localhost => true - } + :use_localhost => true } end let(:options) { {:dialog_hosts => 'localhost' } } @@ -440,8 +438,7 @@ def stub_csv(data) context "use hosts" do let(:action_options) do { :service_template_id => stap.id, - :hosts => "ip1, ip2" - } + :hosts => "ip1, ip2" } end let(:options) { {:dialog_hosts => 'ip1, ip2' } } From 5a4e3b59411dfc3aa1e2fc4d36c276dcadb66ac5 Mon Sep 17 00:00:00 2001 From: Madhu Kanoor Date: Mon, 27 Feb 2017 16:59:40 -0500 Subject: [PATCH 5/5] If the tenant_identity is not definef use the admin --- app/models/miq_action.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/models/miq_action.rb b/app/models/miq_action.rb index 835b5e18746..82c288c5625 100644 --- a/app/models/miq_action.rb +++ b/app/models/miq_action.rb @@ -1040,6 +1040,10 @@ def ipaddress(record) end def target_user(record) - record.respond_to?(:tenant_identity) ? record.tenant_identity : User.find("admin") + 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