From 4ecdab9355486b3b7cf5fd62c9c5e5e4a336f3ef Mon Sep 17 00:00:00 2001 From: Nick LaMuro Date: Tue, 16 Jul 2019 21:30:05 -0500 Subject: [PATCH] [ansible_runner] Add VmwareCredential --- .../runner/credential/vmware_credential.rb | 23 +++++ spec/factories/authentication.rb | 4 + .../credential/vmware_credential_spec.rb | 83 +++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 lib/ansible/runner/credential/vmware_credential.rb create mode 100644 spec/lib/ansible/runner/credential/vmware_credential_spec.rb diff --git a/lib/ansible/runner/credential/vmware_credential.rb b/lib/ansible/runner/credential/vmware_credential.rb new file mode 100644 index 000000000000..9d581618287b --- /dev/null +++ b/lib/ansible/runner/credential/vmware_credential.rb @@ -0,0 +1,23 @@ +module Ansible + class Runner + class VmwareCredential < Credential + def self.auth_type + "ManageIQ::Providers::EmbeddedAnsible::AutomationManager::VmwareCredential" + end + + # Modeled off of vmware injectors for awx: + # + # https://github.com/ansible/awx/blob/1242ee2b/awx/main/models/credential/injectors.py#L63-L67 + # + # TODO: NickL: Should we add support for the VMWARE_VALIDATE_CERTS var? + # + def env_vars + { + "VMWARE_USER" => auth.userid || "", + "VMWARE_PASSWORD" => auth.password || "", + "VMWARE_HOST" => auth.host || "" + } + end + end + end +end diff --git a/spec/factories/authentication.rb b/spec/factories/authentication.rb index 1a0a98ab296c..17ed0508a551 100644 --- a/spec/factories/authentication.rb +++ b/spec/factories/authentication.rb @@ -115,6 +115,10 @@ :parent => :embedded_ansible_credential, :class => "ManageIQ::Providers::EmbeddedAnsible::AutomationManager::ScmCredential" + factory :embedded_ansible_vmware_credential, + :parent => :embedded_ansible_credential, + :class => "ManageIQ::Providers::EmbeddedAnsible::AutomationManager::VmwareCredential" + 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" diff --git a/spec/lib/ansible/runner/credential/vmware_credential_spec.rb b/spec/lib/ansible/runner/credential/vmware_credential_spec.rb new file mode 100644 index 000000000000..2eb5090d3189 --- /dev/null +++ b/spec/lib/ansible/runner/credential/vmware_credential_spec.rb @@ -0,0 +1,83 @@ +require 'ansible/runner' +require 'ansible/runner/credential' + +RSpec.describe Ansible::Runner::VmwareCredential do + it ".auth_type is the correct Authentication sub-class" do + expect(described_class.auth_type).to eq("ManageIQ::Providers::EmbeddedAnsible::AutomationManager::VmwareCredential") + end + + context "with a credential object" do + around do |example| + Dir.mktmpdir("ansible-runner-credential-test") do |dir| + @base_dir = dir + example.run + end + end + + let(:auth) { FactoryBot.create(:embedded_ansible_vmware_credential, auth_attributes) } + let(:auth_attributes) do + { + :userid => "manageiq-vmware", + :password => "vmware_secret", + :options => { + :host => "vmware_host" + } + } + end + + let(:cred) { described_class.new(auth.id, @base_dir) } + + describe "#command_line" do + it "returns an empty hash" do + expect(cred.command_line).to eq({}) + end + end + + # Modeled off of vmware injectors for awx: + # + # https://github.com/ansible/awx/blob/1242ee2b/awx/main/models/credential/injectors.py#L63-L67 + # + describe "#env_vars" do + it "sets VMWARE_USER, VMWARE_PASSWORD, and VMWARE_HOST" do + expected = { + "VMWARE_USER" => "manageiq-vmware", + "VMWARE_PASSWORD" => "vmware_secret", + "VMWARE_HOST" => "vmware_host" + } + expect(cred.env_vars).to eq(expected) + end + + it "defaults VMWARE_USER, VMWARE_PASSWORD, and VMWARE_HOST to '' if missing" do + auth.update!(:userid => nil, :password => nil, :options => nil) + expected = { + "VMWARE_USER" => "", + "VMWARE_PASSWORD" => "", + "VMWARE_HOST" => "" + } + expect(cred.env_vars).to eq(expected) + end + + it "handles empty options hash" do + auth.update!(:options => {}) + expected = { + "VMWARE_USER" => "manageiq-vmware", + "VMWARE_PASSWORD" => "vmware_secret", + "VMWARE_HOST" => "" + } + expect(cred.env_vars).to eq(expected) + end + end + + describe "#extra_vars" do + it "returns an empty hash" do + expect(cred.extra_vars).to eq({}) + end + end + + describe "#write_password_file" do + it "no-ops" do + expect(cred.write_password_file).to be_nil + end + end + end +end