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

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

Yields:

  • (builder_params)


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

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



95
96
97
# File 'lib/hammer_cli_foreman/commands.rb', line 95

def self.connection_name(resource_class)
  CONNECTION_NAME
end

.create_option_builderObject



115
116
117
118
119
120
121
122
123
# File 'lib/hammer_cli_foreman/commands.rb', line 115

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



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

def self.dependency_resolver
  HammerCLIForeman::DependencyResolver.new
end

.resolverObject



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

def self.resolver
  api = HammerCLI::Connection.get("foreman").api
  HammerCLIForeman::IdResolver.new(api, HammerCLIForeman::Searchables.new)
end

.resource_configObject



99
100
101
# File 'lib/hammer_cli_foreman/commands.rb', line 99

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

.resource_name_mappingObject



125
126
127
# File 'lib/hammer_cli_foreman/commands.rb', line 125

def self.resource_name_mapping
  HammerCLIForeman::RESOURCE_NAME_MAPPING
end

.searchablesObject



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

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

Instance Method Details

#customized_optionsObject



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/hammer_cli_foreman/commands.rb', line 211

def customized_options
  params = options
  # resolve all '<resource_name>_id' parameters if they are defined as options
  # (they can be skipped using .without or .expand.except)
  IdParamsFilter.new(:only_required => false).for_action(resource.action(action)).each do |api_param|
    param_resource = HammerCLIForeman.param_to_resource(api_param.name)
    if param_resource && respond_to?(HammerCLI.option_accessor_name("#{param_resource.singular_name}_id"))
      resource_id = get_resource_id(param_resource, :scoped => true, :required => api_param.required?)
      params[HammerCLI.option_accessor_name(api_param.name)] = resource_id if resource_id
    end
  end

  # resolve all '<resource_name>_ids' parameters if they are defined as options
  IdArrayParamsFilter.new(:only_required => false).for_action(resource.action(action)).each do |api_param|
    param_resource = HammerCLIForeman.param_to_resource(api_param.name)
    if param_resource && respond_to?(HammerCLI.option_accessor_name("#{param_resource.singular_name}_ids"))
      resource_ids = get_resource_ids(param_resource, :scoped => true, :required => api_param.required?)
      params[HammerCLI.option_accessor_name(api_param.name)] = resource_ids if resource_ids
    end
  end

  # resolve 'id' parameter if it's defined as an option
  id_option_name = HammerCLI.option_accessor_name('id')
  params[id_option_name] ||= get_identifier if respond_to?(id_option_name)
  params
end

#dependency_resolverObject



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

def dependency_resolver
  self.class.dependency_resolver
end

#get_identifierObject



137
138
139
140
# File 'lib/hammer_cli_foreman/commands.rb', line 137

def get_identifier
  @identifier ||= get_resource_id(resource)
  @identifier
end

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



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/hammer_cli_foreman/commands.rb', line 142

def get_resource_id(resource, options={})
  if options[:scoped]
    opts = resolver.scoped_options(resource.singular_name, all_options)
  else
    opts = all_options
  end
  begin
    resolver.send("#{resource.singular_name}_id", opts)
  rescue HammerCLIForeman::MissingSeachOptions => 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



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/hammer_cli_foreman/commands.rb', line 158

def get_resource_ids(resource, options={})
  opts = resolver.scoped_options(resource.singular_name, all_options)
  begin
    resolver.send("#{resource.singular_name}_ids", opts)
  rescue HammerCLIForeman::MissingSeachOptions => 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



238
239
240
241
242
243
244
245
246
247
# File 'lib/hammer_cli_foreman/commands.rb', line 238

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



103
104
105
# File 'lib/hammer_cli_foreman/commands.rb', line 103

def resolver
  self.class.resolver
end

#searchablesObject



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

def searchables
  self.class.searchables
end

#send_requestObject



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/hammer_cli_foreman/commands.rb', line 184

def send_request
  transform_format(super)
rescue HammerCLIForeman::MissingSeachOptions => e

  switches = self.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 MissingSeachOptions.new(
    error_message % {
      :resource => e.resource.singular_name,
      :switches => switches.join(", ")
    },
    e.resource
  )
end

#transform_format(data) ⇒ Object



207
208
209
# File 'lib/hammer_cli_foreman/commands.rb', line 207

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