From 31383b0326cb5671065c7ca8f76a332226e34544 Mon Sep 17 00:00:00 2001 From: Bill Wei Date: Wed, 15 Mar 2017 17:51:59 -0400 Subject: [PATCH] Add manageiq to the extra_var before launching a job --- app/models/service_ansible_playbook.rb | 11 ++++++++++- spec/models/service_ansible_playbook_spec.rb | 15 +++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/app/models/service_ansible_playbook.rb b/app/models/service_ansible_playbook.rb index 6a92dd4e6fa..159f93eb935 100644 --- a/app/models/service_ansible_playbook.rb +++ b/app/models/service_ansible_playbook.rb @@ -9,7 +9,7 @@ def preprocess(action, add_options = {}) def execute(action) jt = job_template(action) - opts = get_job_options(action) + opts = get_job_options(action).deep_merge(:extra_vars => {'manageiq' => manageiq_extra_vars}) _log.info("Launching Ansible Tower job with options: #{opts}") new_job = ManageIQ::Providers::EmbeddedAnsible::AutomationManager::Job.create_job(jt, opts) @@ -47,6 +47,15 @@ def postprocess(action) private + def manageiq_extra_vars + { + 'api_url' => MiqRegion.my_region.remote_ws_url, + 'api_token' => Api::UserTokenService.new.generate_token(evm_owner.userid, 'api'), + 'service' => href_slug, + 'user' => evm_owner.href_slug + } + end + def get_job_options(action) job_opts = options[job_option_key(action)].deep_dup diff --git a/spec/models/service_ansible_playbook_spec.rb b/spec/models/service_ansible_playbook_spec.rb index 88bfcff0cb5..47607364833 100644 --- a/spec/models/service_ansible_playbook_spec.rb +++ b/spec/models/service_ansible_playbook_spec.rb @@ -109,9 +109,20 @@ end describe '#execute' do + before do + FactoryGirl.create(:miq_region, :region => ApplicationRecord.my_region_number) + loaded_service.update_attributes(:evm_owner => FactoryGirl.create(:user), + :miq_request_task => FactoryGirl.create(:miq_request_task)) + end + it 'creates an Ansible Tower job' do - expect(ManageIQ::Providers::EmbeddedAnsible::AutomationManager::Job) - .to receive(:create_job).with(tower_job_temp, provision_options[:provision_job_options]).and_return(tower_job) + 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) + expect(opts[:extra_vars].delete('manageiq').keys).to include(*exposed_miq) + expect(opts).to include(provision_options[:provision_job_options]) + tower_job + end loaded_service.execute(action) expected_job_attributes = { :id => tower_job.id,