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

Model change for Ansible Tower Credential #13773

Merged
merged 3 commits into from
Feb 16, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -1,2 +1,3 @@
class ManageIQ::Providers::AnsibleTower::AutomationManager::MachineCredential < ManageIQ::Providers::AutomationManager::Authentication
extend ApiCreate
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class ManageIQ::Providers::AnsibleTower::AutomationManager::MachineCredential
module ApiCreate
def create_in_provider(manager_id, params)
manager = ExtManagementSystem.find(manager_id)
credential = manager.with_provider_connection do |connection|
connection.api.credentials.create!(params)
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 verify that the kind is ssh? Or merge that in as a default option?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, that was the same thing I am not sure about. Actually not just kind, my question is do we want to validate the input? and how much we want to validate? Or let Tower's validation does its work?

Copy link
Member

Choose a reason for hiding this comment

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

My concern about kind is that this is a create on a particular leaf class. If you want to move this logic up to the superclass so that it is shared by all AutomationManager::Authentications, then I would be okay with letting Tower do all of the data validation. Is there a reason for this method to be on the leaf class rather than the superclass? (I don't think there's anything specific to this leaf class here)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't like this at all and that's why I have only done this to MachineCredential. I prefer this to be done at a higher level class. The existing AutomationManager::Authentication is not the right place because this particular behavior is Tower specific.
We need to insert another Authentication in the name space of Tower. Are you onboard with that?

Copy link
Member

Choose a reason for hiding this comment

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

I agree, this is Tower specific logic and should live in ManageIQ::Providers::AnsibleTower::AutomationManager::Authentication

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great. It's in now

end

# Get the record in our database
# TODO: This needs to be targeted refresh so it doesn't take too long
EmsRefresh.queue_refresh(manager, nil, true) if !manager.missing_credentials? && manager.authentication_status_ok?

find_by(:resource_id => manager.id, :manager_ref => credential.id)
end

def create_in_provider_queue(manager_id, params)
task_opts = {
:action => "Creating Ansible Tower MachineCredential",
:userid => "system"
}

manager = ExtManagementSystem.find(manager_id)

queue_opts = {
:args => [manager_id, params],
:class_name => "ManageIQ::Providers::AnsibleTower::AutomationManager::MachineCredential",
:method_name => "create_in_provider",
:priority => MiqQueue::HIGH_PRIORITY,
:role => "ems_operations",
:zone => manager.my_zone
}

MiqTask.generic_action_with_callback(task_opts, queue_opts)
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require 'ansible_tower_client'

describe ManageIQ::Providers::AnsibleTower::AutomationManager::MachineCredential do
context "::ApiCreate" do
let(:provider) { FactoryGirl.create(:provider_ansible_tower, :with_authentication) }
let(:manager) { provider.managers.first }
let(:atc) { double("AnsibleTowerClient::Connection", :api => api) }
let(:api) { double("AnsibleTowerClient::Api", :credentials => credentials) }
let(:credentials) { double("AnsibleTowerClient::Collection", :create! => credential) }
let(:credential) { AnsibleTowerClient::Credential.new(nil, credential_json) }

let(:credential_json) do
params.merge(
:id => 10,
).stringify_keys.to_json
end

let(:params) do
{
:description => "Description",
:name => "My Credential",
:related => {}
Copy link
Member

Choose a reason for hiding this comment

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

I would expect to see :username here and maybe some other fields too.

}
end

it ".create_in_provider" do
expect(AnsibleTowerClient::Connection).to receive(:new).and_return(atc)
expect(EmsRefresh).to receive(:queue_refresh).and_return(store_new_credential(credential, manager))
expect(ExtManagementSystem).to receive(:find).with(manager.id).and_return(manager)

expect(described_class.create_in_provider(manager.id, params)).to be_a(described_class)
end

it ".create_in_provider_queue" do
EvmSpecHelper.local_miq_server
task_id = described_class.create_in_provider_queue(manager.id, params)
expect(MiqTask.find(task_id)).to have_attributes(:name => "Creating Ansible Tower MachineCredential")
expect(MiqQueue.first).to have_attributes(
:args => [manager.id, params],
:class_name => "ManageIQ::Providers::AnsibleTower::AutomationManager::MachineCredential",
:method_name => "create_in_provider",
:priority => MiqQueue::HIGH_PRIORITY,
:role => "ems_operations",
:zone => manager.my_zone
)
end

def store_new_credential(credential, manager)
described_class.create!(
:resource => manager,
:manager_ref => credential.id.to_s,
:name => credential.name,
)
end
end
end