Class: HammerCLIForeman::AssociatedCommand

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

alias_name_for_resource, build_options, connection_name, #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

.associated_resource(name = nil) ⇒ Object



575
576
577
578
579
# File 'lib/hammer_cli_foreman/commands.rb', line 575

def self.associated_resource(name=nil)
  @associated_api_resource = HammerCLIForeman.foreman_resource!(name) unless name.nil?
  return @associated_api_resource if @associated_api_resource
  return superclass.associated_resource if superclass.respond_to? :associated_resource
end

.create_option_builderObject



551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
# File 'lib/hammer_cli_foreman/commands.rb', line 551

def self.create_option_builder
  configurator = BuilderConfigurator.new(searchables, dependency_resolver)

  builder = ForemanOptionBuilder.new(searchables)
  builder.builders = [
    SearchablesOptionBuilder.new(resource, searchables),
    DependentSearchablesOptionBuilder.new(associated_resource, searchables)
  ]

  resources = []
  resources += dependency_resolver.resource_dependencies(resource, :only_required => true, :recursive => true)
  resources += dependency_resolver.resource_dependencies(associated_resource, :only_required => true, :recursive => true)
  resources.each do |r|
    builder.builders << DependentSearchablesOptionBuilder.new(r, searchables)
  end
  builder.builders << IdOptionBuilder.new(resource)

  builder
end

.default_message(format) ⇒ Object



581
582
583
584
# File 'lib/hammer_cli_foreman/commands.rb', line 581

def self.default_message(format)
  name = associated_resource ? associated_resource.singular_name.to_s : nil
  format % { :resource_name => name.gsub(/_|-/, ' ') } unless name.nil?
end

Instance Method Details

#associated_resourceObject



571
572
573
# File 'lib/hammer_cli_foreman/commands.rb', line 571

def associated_resource
  self.class.associated_resource
end

#association_name(plural = false) ⇒ Object



614
615
616
# File 'lib/hammer_cli_foreman/commands.rb', line 614

def association_name(plural = false)
  plural ? associated_resource.name.to_s : associated_resource.singular_name.to_s
end

#get_associated_identifierObject



586
587
588
# File 'lib/hammer_cli_foreman/commands.rb', line 586

def get_associated_identifier
  get_resource_id(associated_resource, :scoped => true)
end

#get_current_idsObject



594
595
596
597
598
599
600
601
# File 'lib/hammer_cli_foreman/commands.rb', line 594

def get_current_ids
  item = HammerCLIForeman.record_to_common_format(resource.call(:show, {:id => get_identifier}))
  if item.has_key?(association_name(true))
    item[association_name(true)].map { |assoc| assoc['id'] }
  else
    item[association_name+'_ids'] || []
  end
end

#get_new_idsObject



590
591
592
# File 'lib/hammer_cli_foreman/commands.rb', line 590

def get_new_ids
  []
end

#request_paramsObject



603
604
605
606
607
608
609
610
611
612
# File 'lib/hammer_cli_foreman/commands.rb', line 603

def request_params
  params = super
  if params.key?(resource.singular_name)
    params[resource.singular_name] = {"#{association_name}_ids" => get_new_ids }
  else
    params["#{association_name}_ids"] = get_new_ids
  end
  params['id'] = get_identifier
  params
end