jeudi 9 décembre 2021

Ruby loop through and print out available methods/attributes

How can I list out all the attributes or methods available for the provider below?

Other scripts make use of providers method/attribute like below:

if provider
  provider.miq_templates.each do |template|
  ....

if provider
  provider.security_groups.each do |security_group|
  ....

So I want to print out everything it has like miq_templates, security_groups etc.,

I tried the following but it only prints provider is : AWS. How can I print everything it has?

if provider
  log(:info, "provider is : #{provider}")
end

Here is the code

def get_provider(provider_id=nil)
  $evm.root.attributes.detect { |k,v| provider_id = v if k.end_with?('provider_id') } rescue nil
  provider = $evm.vmdb(:ManageIQ_Providers_Amazon_CloudManager).find_by_id(provider_id)
  log(:info, "Found provider: #{provider.name} via provider_id: #{provider.id}") if provider

  if !provider
    provider = $evm.vmdb(:ManageIQ_Providers_Amazon_CloudManager).first
    if provider
      log(:info, "Found provider: #{provider.name} via default method")
    else
      bail_out('< No providers found, check RBAC tags >')
    end
  end
  provider ? (return provider) : (return nil)
end


provider = get_provider(query_catalogitem(:src_ems_id)) || get_provider_from_template()


if provider
  log(:info, "provider is : #{provider}")
end

if provider
  provider.miq_templates.each do |template|
    log(:info, "miq_templates is : #{template.id}")
    next if template.archived || template.orphaned  
    #dialog_hash[template.guid] = "#{template.name} on #{provider.name}"
    dialog_hash[template.id] = "#{template.name} on #{provider.name}"
  end
else
  # no provider so list everything
  $evm.vmdb(:ManageIQ_Providers_Amazon_CloudManager_Template).all.each do |template|
    next if template.archived || template.orphaned
     #dialog_hash[template[:guid]] = "#{template.name} on #{template.ext_management_system.name}"
      dialog_hash[template[:id]] = "#{template.name} on #{template.ext_management_system.name}"
  end
end

Aucun commentaire:

Enregistrer un commentaire