Skip to content

Commit

Permalink
Pass credentials from ansible playbook automate methods to runner
Browse files Browse the repository at this point in the history
  • Loading branch information
carbonin committed Jul 17, 2019
1 parent e120e56 commit 99ff901
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,30 @@ def create_job_template
my_signal(minimize_indirect, :post_ansible_run, err.message, 'error')
end

def translate_credentials!(launch_options)
%i[credential vault_credential cloud_credential network_credential].each do |cred_type|
credential_id = launch_options.delete("#{cred_type}_id".to_sym)
next if credential_id.blank?

launch_options[cred_type] = Authentication.find(credential_id).native_ref
end
end

LAUNCH_OPTIONS_KEYS = %i[
cloud_credential_id
credential_id
extra_vars
limit
network_credential_id
vault_credential_id
].freeze

def launch_ansible_tower_job
set_status('launching tower job')

launch_options = options.slice(:extra_vars, :limit)
launch_options = options.slice(*LAUNCH_OPTIONS_KEYS)
launch_options[:hosts] = hosts_array(options[:hosts])
translate_credentials!(launch_options)
tower_job = ManageIQ::Providers::EmbeddedAnsible::AutomationManager::Job.create_job(temp_configuration_script, launch_options)
options[:tower_job_id] = tower_job.id
self.name = "#{name}, Job ID: #{tower_job.id}"
Expand Down
4 changes: 4 additions & 0 deletions spec/factories/authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@
:parent => :embedded_ansible_credential,
:class => "ManageIQ::Providers::EmbeddedAnsible::AutomationManager::VmwareCredential"

factory :embedded_ansible_network_credential,
:parent => :embedded_ansible_credential,
:class => "ManageIQ::Providers::EmbeddedAnsible::AutomationManager::NetworkCredential"

factory :auth_key_pair_cloud, :class => "ManageIQ::Providers::CloudManager::AuthKeyPair"
factory :auth_key_pair_amazon, :class => "ManageIQ::Providers::Amazon::CloudManager::AuthKeyPair"
factory :auth_key_pair_openstack, :class => "ManageIQ::Providers::Openstack::CloudManager::AuthKeyPair"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,37 @@
subject.launch_ansible_tower_job
end
end

context 'with credentials' do
let(:cloud_credential) { FactoryBot.create(:embedded_ansible_amazon_credential) }
let(:machine_credential) { FactoryBot.create(:embedded_ansible_machine_credential) }
let(:network_credential) { FactoryBot.create(:embedded_ansible_network_credential) }
let(:vault_credential) { FactoryBot.create(:embedded_ansible_vault_credential) }

let(:options) do
{
:cloud_credential_id => cloud_credential.id,
:credential_id => machine_credential.id,
:network_credential_id => network_credential.id,
:vault_credential_id => vault_credential.id
}
end

it 'passes them to the job' do
expected_options = {
:hosts => ["localhost"],
:cloud_credential => cloud_credential.native_ref,
:credential => machine_credential.native_ref,
:network_credential => network_credential.native_ref,
:vault_credential => vault_credential.native_ref
}
expect(ManageIQ::Providers::EmbeddedAnsible::AutomationManager::Job).to receive(:create_job)
.with(an_instance_of(ManageIQ::Providers::EmbeddedAnsible::AutomationManager::ConfigurationScript), expected_options)
.and_return(double(:id => 'jb1'))
expect(subject).to receive(:queue_signal)
subject.launch_ansible_tower_job
end
end
end

describe '#poll_ansible_tower_job_status' do
Expand Down

0 comments on commit 99ff901

Please sign in to comment.