VMware Cloud Community
GarTomlon
Enthusiast
Enthusiast
Jump to solution

vRO and tagging

I have a workflow that allows me to select tags for VMs.  It is fairly simple.  It has a couple of inputs.  First input is the Tag Category.    In the presentation, I have a action that populates the Predefined list of elements that list the Categories.  The second input feeds off the first.  It simply lists the tags that are part of the selected category.  All fine and good, except I need to be able to select multiple tags.  That will be multiple tags and categories.  Probably need to step back to step one.  I provided the input for category, because I thought it would make it cleaner, however, I am inclined to think that needing to select multiple selections, I create a drop down list with all tags, and not break down by category.  I suppose I could organize the list by category.  Am I right in my thinking?  Any other suggestions?

Reply
0 Kudos
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

Yes, something like the following should work (the outer loops iterates over categories, the inner loop iterates over the tags in the current category from the outer loop):

var client = endpoint.client();     

var category = new com_vmware_cis_tagging_category(client);     

var tagging = new com_vmware_cis_tagging_tag(client);     

var outputTags = [];

var categories = category.list();

for each (var c in categories) {

  if (Cat.indexOf(category.get(c).name) < 0) {

    // the category name is not in the list of selected categories

    continue;

  }

  var tagsInCategory = tagging.list_tags_for_category(c);

  for each (var t in tagsInCategory) {

    outputTags.push(tagging.get(t).name);

  }

}

client.close();

System.log(outputTags);

return outputTags;

View solution in original post

Reply
0 Kudos
14 Replies
daphnissov
Immortal
Immortal
Jump to solution

Are you only doing this through naked vRO, or is this being run through vRA in a catalog request form?

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

To be able to select multiple categories and multiple tags, you'll need to change the type of the corresponding workflow input fields from string to Array/string

Of course, this will also require to change the type of input taken by the second scripting action bound to 'Predefined list of elements' that populate the content of the tag element field to Array/string in order to receive all selected tag categories from the first input. Inside this scripting action, you can group the tags any way you want - eg. you can populate the return array first with tags belonging to the first category, then with tags from the second category, etc. Or you can use some naming convention here, for example, instead of returning an array with tag names, you can prepend each tag name with its category name.

Reply
0 Kudos
GarTomlon
Enthusiast
Enthusiast
Jump to solution

Well the thought was to create an XAAS from this workflow that selects the tags ,so as to include it in vRA blueprint.  Then feed the array back into a workflow (called from the EBS after the VM is provisioned) that will set the tags.  

Reply
0 Kudos
GarTomlon
Enthusiast
Enthusiast
Jump to solution

Well, here is my current setup that is allowing me to select single tag category/ tag.  It is working as designed.  But, like I stated, I am wanting to be able to select multiple categories, then have that feed the available Tags selection. 

Action - getCategoriesByEndpoint 

Return type : Array/String

// Set the VAPI endpoint to the first endpoint returned

var endpoints = VAPIManager.getAllEndpoints(); 

var endpoint = endpoints[2]

//Fetch Categories

var client = endpoint.client();

var category = new com_vmware_cis_tagging_category(client);

var categories = category.list();

var outputCats = [];

for (var i in categories)

     {

         outputCats.push(category.get(categories[i]).name)

     }

System.log(outputCats);

return outputCats;

Action - getTagsByCategory

Return Type - Array/String

Input - Cat Type:String

// Modify with the name of the desired category

var categoryName = Cat

System.log(Cat)

// Set the VAPI endpoint to the first endpoint returned

var endpoints = VAPIManager.getAllEndpoints(); 

var endpoint = endpoints[2]

if (endpoint == null) { 

       throw "'endpoint' parameter should not be null";

     }

// Fetch tags and tag categories

var client = endpoint.client(); 

var category = new com_vmware_cis_tagging_category(client); 

var categories = category.list(); 

var tagging = new com_vmware_cis_tagging_tag(client); 

var tags = tagging.list(); 

var outputTags = [];

// Iterate through tag categories to find the category specified in categoryName

for (var i in categories) 

     {

       if (category.get(categories[i]).name == categoryName) {

              categoryId = categories[i];

              System.log("Name: " + category.get(categories[i]).name + " Id: " + categories[i]);

            }

     }

Inputs

Cat - Type:String

Tag - Type:String

Presentation

Cat - Predefined List of Elements

GetAction(".com.env.core","getCategoriesByEndpoint").call()

Tag - Predefined List of Elements

GetAction(".com.env.core","getTagsByCategory").call( #Cat)

I change the input Cat (for starters) to type array/string.  I go to my Presentation.  I go to add the Predefined List of Elements, and it is not available.

Cat.JPG

What would need to be changed?  As I said, the only thing I have changed is the Cat input to Array/String.  Thanks

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

  • The type of the input Tag should be also changed to Array/string (as you want to be able to select multiple tags)
  • The type of the input Cat in action getTagsByCategory should be changed to Array/string (as you want it to compute tags for multiple categories selected in the first workflow input). And the scripting code of this action should be changed also to iterate over these multiple categories, and for each category to retrieve the tags in this category)
  • In the presentation, for the inputs of type Array/string you can use the property Predefined answers to bind the corrsponding action
GarTomlon
Enthusiast
Enthusiast
Jump to solution

THanks for that.  I changed the additional inputs and set the properties to Predefined answers.  I beleive that will work.  However, my Tags are not populating.  I would think that it would have to do with the action   getTagsByCategory.  The action was setup for a single category.   I imagine that I would need to set the Category (Cat) to an array.  I am looking into that.  Would I be correct in that assumption?

// Modify with the name of the desired category

var categoryName = Cat

System.log(Cat)

// Set the VAPI endpoint to the first endpoint returned

var endpoints = VAPIManager.getAllEndpoints(); 

var endpoint = endpoints[2]

if (endpoint == null) { 

       throw "'endpoint' parameter should not be null";

     }

// Fetch tags and tag categories

var client = endpoint.client(); 

var category = new com_vmware_cis_tagging_category(client); 

var categories = category.list(); 

var tagging = new com_vmware_cis_tagging_tag(client); 

var tags = tagging.list(); 

var outputTags = [];

// Iterate through tag categories to find the category specified in categoryName

for (var i in categories) 

{

       if (category.get(categories[i]).name == categoryName) {

         categoryId = categories[i];

         System.log("Name: " + category.get(categories[i]).name + " Id: " + categories[i]);

       }

}

// Iterate through tags to find the tags that belong to the categoryName and add them to the outputTags array

for (var i in tags) 

{

  if (tagging.get(tags[i]).category_id.toString() == categoryId.toString()) {

    outputTags.push(tagging.get(tags[i]).name);

  }

}

System.log(outputTags); 

return outputTags;

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

Yes, 'Cat' should be an array, and you should have a loop that will iterate over categories in Cat, and for each such category you'll need to have code that retrieves tags in this category and push them to the output tags collection.

GarTomlon
Enthusiast
Enthusiast
Jump to solution

Getting close.  I set the items to array.  I have a the code to iterate thru the array of selected Categories.  However, the action for the Tags are not returning the appropriate tags to choose from based on the selected Categories.  Now, it does work if I only select one category.  But more than one category, and the Tags list is empty.  Here is a copy of the action.

Again, on the action, return tyoe is Array/String and I have an input 'Cat' of Array/String Type

// Modify with the name of the desired category

var categoryName = Cat

//System.log(Cat)

for (var i in categoryName) {

System.log (i);

}

// Set the VAPI endpoint to the first endpoint returned

var endpoints = VAPIManager.getAllEndpoints(); 

var endpoint = endpoints[2]

if (endpoint == null) { 

  throw "'endpoint' parameter should not be null";

}

// Fetch tags and tag categories

var client = endpoint.client(); 

var category = new com_vmware_cis_tagging_category(client); 

var categories = category.list(); 

var tagging = new com_vmware_cis_tagging_tag(client); 

var tags = tagging.list(); 

var outputTags = [];

// Iterate through tag categories to find the category specified in categoryName

for (var i in categories) 

{

  if (category.get(categories[i]).name == categoryName) {

    categoryId = categories[i];

    //System.log("Name: " + category.get(categories[i]).name + " Id: " + categories[i]);

  }

}

// Iterate through tags to find the tags that belong to the categoryName and add them to the outputTags array

for (var i in tags) 

{

  if (tagging.get(tags[i]).category_id.toString() == categoryId.toString()) {

    outputTags.push(tagging.get(tags[i]).name);

  }

}

System.log(outputTags); 

return outputTags;

Reply
0 Kudos
GarTomlon
Enthusiast
Enthusiast
Jump to solution

I have tried multiple changes.  Not sure if I am close.  I am still getting the tags by categories to select when I only select a single Category.  However, if I select multiple categories, I am not getting any tags.  Here is the code  for the Action (Predefined Answers) I have in my Tags input on the PResentation Tab.

// Modify with the name of the desired category 

var categoryName = Cat 

//System.log(Cat) 

 

for (var i in categoryName) { 

System.log (i); 

 

// Set the VAPI endpoint to the first endpoint returned 

var endpoints = VAPIManager.getAllEndpoints();   

var endpoint = endpoints[2] 

 

if (endpoint == null) {   

  throw "'endpoint' parameter should not be null"; 

 

// Fetch tags and tag categories 

var client = endpoint.client();   

var category = new com_vmware_cis_tagging_category(client);   

var categories = category.list();   

var tagging = new com_vmware_cis_tagging_tag(client);   

var tags = tagging.list();   

var outputTags = []; 

 

 

// Iterate through tag categories to find the category specified in categoryName 

 

 

for (var i in categories)   

  if (category.get(categories[i]).name == categoryName) { 

    categoryId = categories[i]; 

    //System.log("Name: " + category.get(categories[i]).name + " Id: " + categories[i]); 

  } 

 

// Iterate through tags to find the tags that belong to the categoryName and add them to the outputTags array 

for (var i in tags)   

  if (tagging.get(tags[i]).category_id.toString() == categoryId.toString()) { 

    outputTags.push(tagging.get(tags[i]).name); 

  } 

 

System.log(outputTags);   

return outputTags;

Any heklp is appreciated.

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

It doesn't work for multiple categories, because in the for loop on line 38 you check for a matching with a single category ID.

You need to use a for loop loop will iterate over array of categories passed as an input, so on each loop iteration you will have the corresponding category ID, and then you can use this ID to call taging.list_tags_for_category(category_id); to enumerate all tags for this category ID

GarTomlon
Enthusiast
Enthusiast
Jump to solution

Thanks for the reply.  So, you are saying I will need a for loop nested inside of the look at line 38?

Reply
0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

Yes, something like the following should work (the outer loops iterates over categories, the inner loop iterates over the tags in the current category from the outer loop):

var client = endpoint.client();     

var category = new com_vmware_cis_tagging_category(client);     

var tagging = new com_vmware_cis_tagging_tag(client);     

var outputTags = [];

var categories = category.list();

for each (var c in categories) {

  if (Cat.indexOf(category.get(c).name) < 0) {

    // the category name is not in the list of selected categories

    continue;

  }

  var tagsInCategory = tagging.list_tags_for_category(c);

  for each (var t in tagsInCategory) {

    outputTags.push(tagging.get(t).name);

  }

}

client.close();

System.log(outputTags);

return outputTags;

Reply
0 Kudos
GarTomlon
Enthusiast
Enthusiast
Jump to solution

Thanks so much.  At first, the filed was blank, not displaying any tags.  Got to looking at I simply had to add my endpoint.  I just added these two lines at the beginning and all was good.

var endpoints = VAPIManager.getAllEndpoints();     

var endpoint = endpoints[2]

I will either add the possiblity of selecting an endpoint, or setting the value as an attribute later.  But it works now like this.  Thanks for your help again.

Reply
0 Kudos
SasidharKE
Contributor
Contributor
Jump to solution

Please share the inputs, output , attributes information of this workflow.

Please share if you have any existing workflow to remove the Tags of VMs based on category

Reply
0 Kudos