Class: HammerCLIForeman::OptionSources::IdParams

Inherits:
HammerCLI::Options::Sources::Base
  • Object
show all
Defined in:
lib/hammer_cli_foreman/option_sources/id_params.rb

Direct Known Subclasses

ReferencedResourceIdParams

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ IdParams

Returns a new instance of IdParams.



4
5
6
# File 'lib/hammer_cli_foreman/option_sources/id_params.rb', line 4

def initialize(command)
  @command = command
end

Instance Method Details

#available_id_paramsObject



12
13
14
# File 'lib/hammer_cli_foreman/option_sources/id_params.rb', line 12

def available_id_params
  IdParamsFilter.new(:only_required => false).for_action(@command.resource.action(@command.action))
end

#get_options(defined_options, result) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/hammer_cli_foreman/option_sources/id_params.rb', line 16

def get_options(defined_options, result)
  # resolve all '<resource_name>_id' parameters if they are defined as options
  # (they can be skipped using .without or .expand.except)
  return result if @command.action.nil?
  available_id_params.each do |api_param|
    param_resource = HammerCLIForeman.param_to_resource(api_param.name)
    if result[HammerCLI.option_accessor_name(api_param.name)].nil? && param_updatable?(param_resource)
      resource_id = @command.get_resource_id(param_resource, :scoped => true, :required => api_param.required?, :all_options => result)
      result[HammerCLI.option_accessor_name(api_param.name)] = resource_id if resource_id
    end
  end
  result

rescue HammerCLIForeman::MissingSearchOptions => e

  switches = @command.class.find_options(:referenced_resource => e.resource.singular_name).map(&:long_switch)

  if switches.empty?
    error_message = _("Could not find %{resource}. Some search options were missing, please see --help.")
  elsif switches.length == 1
    error_message = _("Could not find %{resource}, please set option %{switches}.")
  else
    error_message = _("Could not find %{resource}, please set one of options %{switches}.")
  end

  raise MissingSearchOptions.new(
    error_message % {
      :resource => e.resource.singular_name,
      :switches => switches.join(", ")
    },
    e.resource
  )
end

#param_updatable?(param_resource) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/hammer_cli_foreman/option_sources/id_params.rb', line 8

def param_updatable?(param_resource)
  param_resource && @command.respond_to?(HammerCLI.option_accessor_name("#{param_resource.singular_name}_id"))
end