module HammerCLIForeman

Constants

CONNECTION_NAME
RESOURCE_NAME_MAPPING

Public Class Methods

collection_to_common_format(data) click to toggle source
# File lib/hammer_cli_foreman/commands.rb, line 71
def self.collection_to_common_format(data)
  if data.class <= Hash && data.has_key?('total') && data.has_key?('results')
    col = HammerCLI::Output::RecordCollection.new(data['results'],
      :total => data['total'],
      :subtotal => data['subtotal'],
      :page => data['page'],
      :per_page => data['per_page'],
      :search => data['search'],
      :sort_by => data['sort']['by'],
      :sort_order => data['sort']['order'])
  elsif data.class <= Hash
    col = HammerCLI::Output::RecordCollection.new(data)
  elsif data.class <= Array
    # remove object types. From [ { 'type' => { 'attr' => val } }, ... ]
    # produce [ { 'attr' => 'val' }, ... ]
    col = HammerCLI::Output::RecordCollection.new(data.map { |r| r.keys.length == 1 ? r[r.keys[0]] : r })
  else
    raise RuntimeError.new(_("Received data of unknown format"))
  end
  col
end
credentials() click to toggle source
# File lib/hammer_cli_foreman/commands.rb, line 14
def self.credentials
  @credentials ||= BasicCredentials.new(
    :username => (HammerCLI::Settings.get(:_params, :username) || ENV['FOREMAN_USERNAME'] || HammerCLI::Settings.get(:foreman, :username)),
    :password => (HammerCLI::Settings.get(:_params, :password) || ENV['FOREMAN_PASSWORD'] || HammerCLI::Settings.get(:foreman, :password))
  )
  @credentials
end
exception_handler_class() click to toggle source
# File lib/hammer_cli_foreman.rb, line 7
def self.exception_handler_class
  HammerCLIForeman::ExceptionHandler
end
foreman_api() click to toggle source
# File lib/hammer_cli_foreman/commands.rb, line 46
def self.foreman_api
  foreman_api_connection.api
end
foreman_api_connection() click to toggle source
# File lib/hammer_cli_foreman/commands.rb, line 38
def self.foreman_api_connection
  HammerCLI::Connection.create(
    CONNECTION_NAME,
    HammerCLI::Apipie::Command.resource_config.merge(resource_config),
    HammerCLI::Apipie::Command.connection_options
  )
end
foreman_resource(resource_name, options={}) click to toggle source
# File lib/hammer_cli_foreman/commands.rb, line 59
def self.foreman_resource(resource_name, options={})
  begin
    foreman_resource!(resource_name, options)
  rescue NameError
    nil
  end
end
foreman_resource!(resource_name, options={}) click to toggle source
# File lib/hammer_cli_foreman/commands.rb, line 50
def self.foreman_resource!(resource_name, options={})
  if options[:singular]
    resource_name = ApipieBindings::Inflector.pluralize(resource_name.to_s).to_sym
  else
    resource_name = resource_name.to_sym
  end
  foreman_api.resource(resource_name)
end
param_to_resource(param_name) click to toggle source
# File lib/hammer_cli_foreman/commands.rb, line 67
def self.param_to_resource(param_name)
  HammerCLIForeman.foreman_resource(param_name.gsub(/_id[s]?$/, ""), :singular => true)
end
record_to_common_format(data) click to toggle source
# File lib/hammer_cli_foreman/commands.rb, line 93
def self.record_to_common_format(data)
    data.class <= Hash && data.keys.length == 1 ? data[data.keys[0]] : data
end
resource_config() click to toggle source
# File lib/hammer_cli_foreman/commands.rb, line 22
def self.resource_config
  config = {}
  config[:uri] = HammerCLI::Settings.get(:_params, :host) || HammerCLI::Settings.get(:foreman, :host)
  config[:credentials] = credentials
  config[:logger] = Logging.logger['API']
  config[:api_version] = 2
  config[:follow_redirects] = HammerCLI::Settings.get(:foreman, :follow_redirects) || :never
  config[:aggressive_cache_checking] = HammerCLI::Settings.get(:foreman, :refresh_cache) || false
  config[:headers] = { "Accept-Language" => HammerCLI::I18n.locale }
  config[:language] = HammerCLI::I18n.locale
  config[:timeout] = HammerCLI::Settings.get(:foreman, :request_timeout)
  config[:timeout] = -1 if (config[:timeout] && config[:timeout].to_i < 0)
  config[:apidoc_authenticated] = false
  config
end
version() click to toggle source
# File lib/hammer_cli_foreman/version.rb, line 2
def self.version
  @version ||= Gem::Version.new '0.7.0'
end