Class: HammerCLIForeman::OptionSources::ReferencedResourceIdParams
- Inherits:
-
IdParams
- Object
- HammerCLI::Options::Sources::Base
- IdParams
- HammerCLIForeman::OptionSources::ReferencedResourceIdParams
- Defined in:
- lib/hammer_cli_foreman/option_sources/referenced_resource_id_params.rb
Instance Method Summary collapse
Methods inherited from IdParams
#available_id_params, #initialize, #needs_resolving?
Constructor Details
This class inherits a constructor from HammerCLIForeman::OptionSources::IdParams
Instance Method Details
#get_options(defined_options, result) ⇒ Object
8 9 10 11 12 13 14 15 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 |
# File 'lib/hammer_cli_foreman/option_sources/referenced_resource_id_params.rb', line 8 def (, result) return result if @command.action.nil? available_id_params.each do |api_param| attr_name = HammerCLI.option_accessor_name(api_param.name.gsub('_id', '_name')) option = @command.class.(attribute_name: attr_name).first next unless option resource = HammerCLIForeman.foreman_resource(option.referenced_resource, singular: true) if result[HammerCLI.option_accessor_name(api_param.name)].nil? && param_updatable?(resource, api_param) scope = { HammerCLI.option_accessor_name("#{resource.singular_name}_name") => result[attr_name] } resource_id = @command.get_resource_id( resource, scoped: true, required: api_param.required?, all_options: result.merge(scope)) result[HammerCLI.option_accessor_name(api_param.name)] = resource_id if resource_id end end result rescue HammerCLIForeman::MissingSearchOptions => e switches = @command.class.(:referenced_resource => e.resource.singular_name).map(&:long_switch) if switches.empty? = _("Could not find %{resource}. Some search options were missing, please see --help.") elsif switches.length == 1 = _("Could not find %{resource}, please set option %{switches}.") else = _("Could not find %{resource}, please set one of options %{switches}.") end raise MissingSearchOptions.new( % { :resource => e.resource.singular_name, :switches => switches.join(", ") }, e.resource ) end |
#param_updatable?(resource, api_param) ⇒ Boolean
4 5 6 |
# File 'lib/hammer_cli_foreman/option_sources/referenced_resource_id_params.rb', line 4 def param_updatable?(resource, api_param) resource && @command.respond_to?(HammerCLI.option_accessor_name(api_param.name)) end |