Class: HammerCLIForeman::Command

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.alias_name_for_resource(resource, singular: true) ⇒ Object



114
115
116
117
118
119
# File 'lib/hammer_cli_foreman/commands.rb', line 114

def self.alias_name_for_resource(resource, singular: true)
  return nil if resource.nil?
  return resource_alias_name_mapping[resource.name.to_sym] unless singular

  resource_alias_name_mapping[resource.singular_name.to_sym]
end

.build_options(builder_params = {}) {|builder_params| ... } ⇒ Object

Yields:

  • (builder_params)


121
122
123
124
125
126
127
# File 'lib/hammer_cli_foreman/commands.rb', line 121

def self.build_options(builder_params={})
  builder_params[:resource_mapping] ||= resource_name_mapping
  builder_params = HammerCLIForeman::BuildParams.new(builder_params)
  yield(builder_params) if block_given?

  super(builder_params.to_hash, &nil)
end

.connection_name(resource_class) ⇒ Object



68
69
70
# File 'lib/hammer_cli_foreman/commands.rb', line 68

def self.connection_name(resource_class)
  CONNECTION_NAME
end

.create_option_builderObject



96
97
98
99
100
101
102
103
104
# File 'lib/hammer_cli_foreman/commands.rb', line 96

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

  builder = ForemanOptionBuilder.new(searchables)
  builder.builders = []
  builder.builders += configurator.builders_for(resource, resource.action(action)) if resource_defined?
  builder.builders += super.builders
  builder
end

.dependency_resolverObject



171
172
173
# File 'lib/hammer_cli_foreman/commands.rb', line 171

def self.dependency_resolver
  HammerCLIForeman::DependencyResolver.new
end

.resolverObject



166
167
168
169
# File 'lib/hammer_cli_foreman/commands.rb', line 166

def self.resolver
  api = HammerCLI.context[:api_connection].get("foreman")
  HammerCLIForeman::IdResolver.new(api, HammerCLIForeman::Searchables.new)
end

.resource_alias_name_mappingObject



110
111
112
# File 'lib/hammer_cli_foreman/commands.rb', line 110

def self.resource_alias_name_mapping
  HammerCLIForeman::RESOURCE_ALIAS_NAME_MAPPING
end

.resource_configObject



72
73
74
# File 'lib/hammer_cli_foreman/commands.rb', line 72

def self.resource_config
  super.merge(HammerCLIForeman.resource_config)
end

.resource_name_mappingObject



106
107
108
# File 'lib/hammer_cli_foreman/commands.rb', line 106

def self.resource_name_mapping
  HammerCLIForeman::RESOURCE_NAME_MAPPING
end

.searchablesObject



175
176
177
178
# File 'lib/hammer_cli_foreman/commands.rb', line 175

def self.searchables
  @searchables ||= HammerCLIForeman::Searchables.new
  @searchables
end

Instance Method Details

#customized_optionsObject



190
191
192
193
194
# File 'lib/hammer_cli_foreman/commands.rb', line 190

def customized_options
  # this method is deprecated and will be removed in future versions.
  # Check option_sources for custom tuning of options
  options
end

#dependency_resolverObject



80
81
82
# File 'lib/hammer_cli_foreman/commands.rb', line 80

def dependency_resolver
  self.class.dependency_resolver
end

#exception_handler_classObject



88
89
90
91
92
93
94
# File 'lib/hammer_cli_foreman/commands.rb', line 88

def exception_handler_class
  #search for exception handler class in parent modules/classes
  HammerCLI.constant_path(self.class.name.to_s).reverse.each do |mod|
    return mod.exception_handler_class if mod.respond_to? :exception_handler_class
  end
  HammerCLIForeman::ExceptionHandler
end

#get_identifier(all_opts = all_options) ⇒ Object



129
130
131
132
# File 'lib/hammer_cli_foreman/commands.rb', line 129

def get_identifier(all_opts=all_options)
  @identifier ||= get_resource_id(resource, :all_options => all_opts)
  @identifier
end

#get_resource_id(resource, options = {}) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/hammer_cli_foreman/commands.rb', line 134

def get_resource_id(resource, options={})
  resource_alias = self.class.alias_name_for_resource(resource)
  all_opts = options[:all_options] || all_options
  if options[:scoped]
    opts = resolver.scoped_options(resource.singular_name, all_opts, :single)
  else
    opts = all_opts
  end
  begin
    resolver.send("#{resource_alias || resource.singular_name}_id", opts)
  rescue HammerCLIForeman::MissingSearchOptions => e
    if (options[:required] == true || resource_search_requested(resource, opts))
      logger.info "Error occured while searching for #{resource.singular_name}"
      raise e
    end
  end
end

#get_resource_ids(resource, options = {}) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/hammer_cli_foreman/commands.rb', line 152

def get_resource_ids(resource, options={})
  resource_alias = self.class.alias_name_for_resource(resource)
  all_opts = options[:all_options] || all_options
  opts = resolver.scoped_options(resource.singular_name, all_opts, :multi)
  begin
    resolver.send("#{resource_alias || resource.singular_name}_ids", opts)
  rescue HammerCLIForeman::MissingSearchOptions => e
    if (options[:required] == true || resource_search_requested(resource, opts, true))
      logger.info "Error occured while searching for #{resource.name}"
      raise e
    end
  end
end

#request_paramsObject



196
197
198
199
200
201
202
203
204
# File 'lib/hammer_cli_foreman/commands.rb', line 196

def request_params
  params = customized_options
  params_pruned = method_options(params)
  # Options defined manualy in commands are removed in method_options.
  # Manual ids are common so its handling is covered here
  id_option_name = HammerCLI.option_accessor_name('id')
  params_pruned['id'] = params[id_option_name] if params[id_option_name]
  params_pruned
end

#resolverObject



76
77
78
# File 'lib/hammer_cli_foreman/commands.rb', line 76

def resolver
  self.class.resolver
end

#searchablesObject



84
85
86
# File 'lib/hammer_cli_foreman/commands.rb', line 84

def searchables
  self.class.searchables
end

#send_requestObject



180
181
182
183
184
# File 'lib/hammer_cli_foreman/commands.rb', line 180

def send_request
  data = super
  @meta = retrieve_meta(data)
  transform_format(data)
end

#transform_format(data) ⇒ Object



186
187
188
# File 'lib/hammer_cli_foreman/commands.rb', line 186

def transform_format(data)
  HammerCLIForeman.record_to_common_format(data)
end