Class: HammerCLIForeman::AssociatedCommand

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from WriteCommand

#send_request, #success_message_params

Constructor Details

#initialize(*args) ⇒ AssociatedCommand

Returns a new instance of AssociatedCommand.



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

def initialize(*args)
  super
  setup_associated_identifier_options
end

Class Method Details

.associated_identifiers(*keys) ⇒ Object



205
206
207
# File 'lib/hammer_cli_foreman/commands.rb', line 205

def self.associated_identifiers(*keys)
  @associated_identifiers = keys
end

.associated_resource(resource_class = nil) ⇒ Object



198
199
200
201
# File 'lib/hammer_cli_foreman/commands.rb', line 198

def self.associated_resource(resource_class=nil)
  @associated_api_resource = HammerCLI::Apipie::ResourceDefinition.new(resource_class) unless resource_class.nil?
  return @associated_api_resource
end

.declared_associated_identifiersObject



209
210
211
212
213
214
215
216
217
# File 'lib/hammer_cli_foreman/commands.rb', line 209

def self.declared_associated_identifiers
  if @associated_identifiers
    return @associated_identifiers
  elsif superclass.respond_to?(:declared_associated_identifiers, true)
    superclass.declared_associated_identifiers
  else
    []
  end
end

Instance Method Details

#associated_resourceObject



194
195
196
# File 'lib/hammer_cli_foreman/commands.rb', line 194

def associated_resource
  HammerCLI::Apipie::ResourceInstance.from_definition(self.class.associated_resource, resource_config)
end

#get_current_idsObject



225
226
227
228
# File 'lib/hammer_cli_foreman/commands.rb', line 225

def get_current_ids
  item = resource.call('show', {'id' => get_identifier[0]})[0]
  item[resource.name][associated_resource.name+'_ids'] || []
end

#get_new_idsObject



221
222
223
# File 'lib/hammer_cli_foreman/commands.rb', line 221

def get_new_ids
  []
end

#get_required_idObject



230
231
232
233
# File 'lib/hammer_cli_foreman/commands.rb', line 230

def get_required_id
  item = associated_resource.call('show', {'id' => associated_id || associated_name})[0]
  item[associated_resource.name]['id']
end

#request_paramsObject



235
236
237
238
239
240
# File 'lib/hammer_cli_foreman/commands.rb', line 235

def request_params
  params = method_options
  params[resource.name][associated_resource.name+'_ids'] = get_new_ids
  params['id'] = get_identifier[0]
  params
end

#setup_associated_identifier_optionsObject



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

def setup_associated_identifier_options
  name = associated_resource.name.to_s
  option_switch = "--"+name.gsub('_', '-')
  self.class.option option_switch, name.upcase, " ", :attribute_name => :associated_name if self.class.declared_associated_identifiers.include? :name
  self.class.option option_switch+"-id", name.upcase+"_ID", " ", :attribute_name => :associated_id if self.class.declared_associated_identifiers.include? :id
end

#validate_optionsObject



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

def validate_options
  associated_ids = self.class.declared_associated_identifiers.collect {|id_name| "associated_#{id_name}" }
  validator.any(*associated_ids).required
  validator.any(*self.class.declared_identifiers.values).required
end