Listing Tags in a Dialog


#1

Hey guys,

I got my first bit of code to work!

Using https://pemcg.gitbooks.io/introduction-to-cloudforms-automation/content/chapter7/using_tags_from_automate.html

I creates a class, instance and method to get dialogs to lookup tags for a user to choose.

I cloned Cloudforms_Essential/Integration/RedHat/Cloudforms/DynamicDialogs/list_cloudforms_tenant_ids, and adjusted from there.

I thought I’d share.

David

=begin
 list_tags.rb

 Author: David Thornton <david.thornton@scalar.ca>

 Description: This method lists tags for a given category, for using dialogs.

=end

dialog_hash = {}

chosencategory = $evm.inputs['category']

$evm.log(:info, "Chosen Category is #{chosencategory}")

tag_classification = $evm.vmdb('classification').find_by_name(chosencategory)

list_of_tags = {}

$evm.vmdb('classification').find_all_by_parent_id(tag_classification.id).each do |tag|
  # $evm.log(:info, "list_tags Found #{chosencategory} tag #{tag.name}")
  # list_of_tags[tag.name] = tag.description
  dialog_hash[tag.name] = tag.description
end


current = dialog_hash.detect {|k,v| v.include?('CURRENT') }
if current
  $evm.object['default_value'] = current[0]
else
  choose = {''=>'< choose a #{chosencategory} id >'}
  dialog_hash = choose.merge!(dialog_hash)
end

unless $evm.execute('category_exists?', chosencategory)
  dialog_hash[0] = "Category #{chosencategory} does not exist"
end

$evm.object["values"]     = dialog_hash
$evm.log(:info, "$evm.object['values']: #{$evm.object['values'].inspect}")

#2

next question: How to set the tag category at dialog build time.


#3

and I figured this out, I just make multiple instances of the class each passing it’s own category value .

in the method field where before I had

 list_tags

I changed that to

list_tags( category => 'Environment')

Now $evm.root(inputs) has {category=>“environment”}

Score!


#4

Awesome @dthornton! Thanks for sharing your positive experience using @pemcg material as reference.


#5

quick follow up.

Now I have multiple dynamic drop downs in my dialog, each points to a different “instance” where the category is specified.

The method remains unchanged.

David


#6

update!

I just upgraded from 4.0 to 4.2

“find_all_by_parent_id” is gone and so my work is broken.

you can learn more about why and how to fix here:

David


#7

Hi David

The ‘Euwe’ version of the Automate book should have removed any reference to the ‘find_all_*’ finders.

The tags chapter is here: https://pemcg.gitbooks.io/mastering-automation-in-cloudforms-4-2-and-manage/content/using_tags_from_automate/chapter.html

Hope this helps,

pemcg