diff --git a/app/models/manageiq/providers/ansible_tower/automation_manager.rb b/app/models/manageiq/providers/ansible_tower/automation_manager.rb index c9547a6d..44923eae 100644 --- a/app/models/manageiq/providers/ansible_tower/automation_manager.rb +++ b/app/models/manageiq/providers/ansible_tower/automation_manager.rb @@ -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, diff --git a/app/models/manageiq/providers/ansible_tower/provider.rb b/app/models/manageiq/providers/ansible_tower/provider.rb index d7274318..c2a00e64 100644 --- a/app/models/manageiq/providers/ansible_tower/provider.rb +++ b/app/models/manageiq/providers/ansible_tower/provider.rb @@ -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 @@ -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