Class: HammerCLIForeman::AssociatedListSearchCommand

Inherits:
ListCommand
  • Object
show all
Defined in:
lib/hammer_cli_foreman/commands.rb

Constant Summary

Constants inherited from ListCommand

ListCommand::DEFAULT_PER_PAGE, ListCommand::RETRIEVE_ALL_PER_PAGE

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ListCommand

#adapter, command_names, #execute, #extend_data, #help, output, #send_request, #transform_format

Methods inherited from Command

alias_name_for_resource, build_options, connection_name, create_option_builder, #customized_options, #dependency_resolver, dependency_resolver, #exception_handler_class, #get_identifier, #get_resource_id, #get_resource_ids, resolver, #resolver, resource_alias_name_mapping, resource_config, resource_name_mapping, #searchables, searchables, #send_request, #transform_format

Class Method Details

.default_search_optionsObject



365
366
367
368
# File 'lib/hammer_cli_foreman/commands.rb', line 365

def self.default_search_options
  option("--id", "ID", _("%s Id") % module_resource.singular_name)
  option("--name", "NAME", _("%s name") % module_resource.singular_name)
end

.search_options_mapping(mapping = {}) ⇒ Object



370
371
372
373
374
# File 'lib/hammer_cli_foreman/commands.rb', line 370

def self.search_options_mapping(mapping = {})
  { "name" => module_resource.singular_name,
    "id" => "#{module_resource.singular_name}_id"
  }.merge mapping
end

.search_resource(res, action = :index) ⇒ Object



360
361
362
363
# File 'lib/hammer_cli_foreman/commands.rb', line 360

def self.search_resource(res, action = :index)
  resource res, action
  default_search_options
end

Instance Method Details

#id_attr(resource_name) ⇒ Object



411
412
413
# File 'lib/hammer_cli_foreman/commands.rb', line 411

def id_attr(resource_name)
  "#{resource_name}_id"
end

#name_attr(resource_name) ⇒ Object



407
408
409
# File 'lib/hammer_cli_foreman/commands.rb', line 407

def name_attr(resource_name)
  "#{resource_name}_name"
end

#option_sourcesObject



376
377
378
379
380
381
382
383
384
# File 'lib/hammer_cli_foreman/commands.rb', line 376

def option_sources
  sources = super
  sources.find_by_name('IdResolution').insert_relative(
    :replace,
    'SelfParam',
     HammerCLI::Options::Sources::Base.new
  )
  sources
end

#parent_resource_id_attrObject



403
404
405
# File 'lib/hammer_cli_foreman/commands.rb', line 403

def parent_resource_id_attr
  "id"
end

#parent_resource_nameObject



391
392
393
# File 'lib/hammer_cli_foreman/commands.rb', line 391

def parent_resource_name
  self.class.module_resource.singular_name
end

#parent_resource_name_attrObject



399
400
401
# File 'lib/hammer_cli_foreman/commands.rb', line 399

def parent_resource_name_attr
  "name"
end

#request_paramsObject



415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/hammer_cli_foreman/commands.rb', line 415

def request_params
  params = super
  search = []
  search << params['search'] if params['search']

  resource_name = get_option_value(parent_resource_name_attr)
  search << %Q(#{search_mapping parent_resource_name_attr}="#{resource_name}") if resource_name

  resource_id = get_option_value(parent_resource_id_attr)
  search << "#{search_mapping parent_resource_id_attr}=#{resource_id}" if resource_id

  search += taxonomy_request_params('organization', params)
  search += taxonomy_request_params('location', params)

  params['search'] = search.join(' and ') unless search.empty?
  params
end

#search_mapping(key) ⇒ Object



395
396
397
# File 'lib/hammer_cli_foreman/commands.rb', line 395

def search_mapping(key)
  self.class.search_options_mapping[key]
end

#taxonomy_request_params(taxonomy, params) ⇒ Object



433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'lib/hammer_cli_foreman/commands.rb', line 433

def taxonomy_request_params(taxonomy, params)
  res = []
  tax_name = get_option_value(name_attr taxonomy)
  if tax_name
    res << "#{name_attr taxonomy}=#{tax_name}"
    params.delete(name_attr taxonomy)
  end

  tax_id = get_option_value(id_attr taxonomy)
  if tax_id
    res << "#{id_attr taxonomy}=#{tax_id}"
    params.delete(id_attr taxonomy)
  end
  res
end

#validate_optionsObject



386
387
388
389
# File 'lib/hammer_cli_foreman/commands.rb', line 386

def validate_options
  super
  validator.any("option_name".to_sym, "option_id".to_sym).required
end