Skip to content

Commit

Permalink
[ansible_runner] Add VmwareCredential
Browse files Browse the repository at this point in the history
  • Loading branch information
NickLaMuro committed Jul 17, 2019
1 parent 61cde3e commit 4ecdab9
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/ansible/runner/credential/vmware_credential.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions spec/factories/authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
83 changes: 83 additions & 0 deletions spec/lib/ansible/runner/credential/vmware_credential_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4ecdab9

Please sign in to comment.