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

Pass credentials from ansible playbook automate methods to runner #18983

Merged
merged 1 commit into from
Jul 18, 2019
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
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we rescue ActiveRecord::RecordNotFound?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's necessary. At least it wasn't the first 3 times this method was written 😆

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