Skip to content

Commit

Permalink
Add DDF based verify_credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
Fryguy committed Sep 30, 2019
1 parent 1a1a93c commit 1e457fb
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
class ManageIQ::Providers::AnsibleTower::AutomationManager < ManageIQ::Providers::ExternalAutomationManager
include ProcessTasksMixin

class << self
delegate :params_for_create,
:verify_credentials,
:to => ManageIQ::Providers::AnsibleTower::Provider
end

delegate :authentications,
:authentication_check,
:authentication_status,
Expand Down
77 changes: 71 additions & 6 deletions app/models/manageiq/providers/ansible_tower/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,75 @@ class ManageIQ::Providers::AnsibleTower::Provider < ::Provider
validates :name, :presence => true, :uniqueness => true
validates :url, :presence => true

PARAMS_FOR_CREATE = {
:title => "Configure Ansible Tower",
:fields => [
{
:component => "text-field",
:name => "endpoints.default.base_url",
:label => "URL",
:isRequired => true,
:validate => [{:type => "required-validator"}]
},
{
:component => "text-field",
:name => "endpoints.default.username",
:label => "Username",
:isRequired => true,
:validate => [{:type => "required-validator"}]
},
{
:component => "text-field",
:name => "endpoints.default.password",
:label => "Password",
:type => "password",
:isRequired => true,
:validate => [{:type => "required-validator"}]
},
{
:component => "checkbox",
:name => "endpoints.default.verify_ssl",
:label => "Verify SSL"
}
]
}.freeze

def self.params_for_create
PARAMS_FOR_CREATE
end

# Verify Credentials
# args:
# {
# "endpoints" => {
# "default" => {
# "base_url" => "",
# "username" => "",
# "password" => "",
# "verify_ssl" => ""
# }
# }
# }
def self.verify_credentials(args)
default_endpoint = args.dig("endpoints", "default")

base_url, username, password, verify_ssl = default_endpoint&.values_at(
"base_url", "username", "password", "verify_ssl"
)

!!verify_connection(raw_connect(base_url, username, password, verify_ssl))
end

def self.verify_connection(connection)
require 'ansible_tower_client'
begin
connection.api.verify_credentials ||
raise(MiqException::MiqInvalidCredentialsError, _("Username or password is not valid"))
rescue AnsibleTowerClient::ClientError => err
raise MiqException::MiqCommunicationsError, err.message, err.backtrace
end
end

def self.raw_connect(base_url, username, password, verify_ssl)
require 'ansible_tower_client'
AnsibleTowerClient.logger = $ansible_tower_log
Expand Down Expand Up @@ -38,12 +107,8 @@ def connect(options = {})
end

def verify_credentials(auth_type = nil, options = {})
require 'ansible_tower_client'
begin
with_provider_connection(options.merge(:auth_type => auth_type)) { |c| c.api.verify_credentials } ||
raise(MiqException::MiqInvalidCredentialsError, _("Username or password is not valid"))
rescue AnsibleTowerClient::ClientError => err
raise MiqException::MiqCommunicationsError, err.message, err.backtrace
with_provider_connection(options.merge(:auth_type => auth_type)) do |c|
self.class.verify_connection(c)
end
end

Expand Down

0 comments on commit 1e457fb

Please sign in to comment.