class HammerCLIForeman::Api::Connection

Attributes

authenticator[R]

Public Class Methods

new(settings, logger = nil, locale = nil, auth_type = nil) click to toggle source
Calls superclass method
# File lib/hammer_cli_foreman/api/connection.rb, line 23
def initialize(settings, logger = nil, locale = nil, auth_type = nil)
  auth_type ||= default_auth_type(settings)
  default_params = build_default_params(settings, logger, locale, auth_type)
  super(default_params,
    :logger => logger,
    :reload_cache => settings.get(:_params, :reload_cache) || settings.get(:reload_cache)
  )
end

Public Instance Methods

login() click to toggle source
# File lib/hammer_cli_foreman/api/connection.rb, line 32
def login
  # Call some api entry point to trigger the successful connection
  @api.resource(:home).action(:status).call
end
login_status() click to toggle source
# File lib/hammer_cli_foreman/api/connection.rb, line 41
def login_status
  @authenticator.status
end
logout() click to toggle source
# File lib/hammer_cli_foreman/api/connection.rb, line 37
def logout
  @authenticator.clear if @authenticator.respond_to?(:clear)
end

Protected Instance Methods

build_default_params(settings, logger, locale, auth_type) click to toggle source
# File lib/hammer_cli_foreman/api/connection.rb, line 58
def build_default_params(settings, logger, locale, auth_type)
  config = {}
  config[:uri] = settings.get(:_params, :host) || settings.get(:foreman, :host)
  config[:logger] = logger unless logger.nil?
  config[:api_version] = 2
  config[:follow_redirects] = settings.get(:foreman, :follow_redirects) || :never
  config[:aggressive_cache_checking] = settings.get(:foreman, :refresh_cache) || false
  unless locale.nil?
    config[:headers] = { "Accept-Language" => locale }
    config[:language] = locale
  end
  config[:timeout] = settings.get(:foreman, :request_timeout)
  config[:timeout] = -1 if (config[:timeout] && config[:timeout].to_i < 0)
  config[:apidoc_authenticated] = false
  config[:authenticator] = create_authenticator(config[:uri], settings, auth_type)
  config
end
create_authenticator(uri, settings, auth_type) click to toggle source
# File lib/hammer_cli_foreman/api/connection.rb, line 52
def create_authenticator(uri, settings, auth_type)
  return @authenticator if @authenticator

  @authenticator = HammerCLIForeman::Api::Authenticator.new(auth_type, uri, settings).fetch
end
default_auth_type(settings) click to toggle source
# File lib/hammer_cli_foreman/api/connection.rb, line 47
def default_auth_type(settings)
  return AUTH_TYPES[:basic_auth] unless HammerCLIForeman::Sessions.enabled?
  HammerCLI::Settings.get(:foreman, :default_auth_type) || AUTH_TYPES[:basic_auth]
end