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



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

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



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

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



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

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

Instance Method Details

#id_attr(resource_name) ⇒ Object



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

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

#name_attr(resource_name) ⇒ Object



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

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

#option_sourcesObject



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

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



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

def parent_resource_id_attr
  "id"
end

#parent_resource_nameObject



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

def parent_resource_name
  self.class.module_resource.singular_name
end

#parent_resource_name_attrObject



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

def parent_resource_name_attr
  "name"
end

#request_paramsObject



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

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



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

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

#taxonomy_request_params(taxonomy, params) ⇒ Object



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

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



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

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