VMware Cloud Community
DexG83
Enthusiast
Enthusiast
Jump to solution

Get all Tags associated to a VM using vRO 8.3

Hi Community,

I am looking for a way to get all tags associated with a specific VM. With this information, I can check if a specific tag is associated, and if not I can associate it.

Is there a good way to get that information with vAPI? I don't want to use PowerCLI...

Many Thanks for any idea,

Dex

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
omrsafetyo
Enthusiast
Enthusiast
Jump to solution

This quick demo code should show most everything you might need to get what you're asking.

 

var client = VAPIEndpoint.client();
// Set up a dynamic ID for querying 
var objId = new com_vmware_vapi_std_dynamic__ID();
objId.id = vm.id;
objId.type = vm.vimType;

// set up connections for tag associations, tags, and tag categories
var tagging = new com_vmware_cis_tagging_tag__association(client);
var tagMgr = new com_vmware_cis_tagging_tag(client);
var catMgr = new com_vmware_cis_tagging_category(client);

var tagList = tagging.list_attached_tags(objId);  //returns the IDs of each tag
for each (var tagId in tagList) {
    var theTag = tagMgr.get(tagId);  // get the tag itself
    var tagName=theTag.name;         // name/value of the tag
    var tagCategoryId=theTag.category_id;  // the ID of the category
    var Category = catMgr.get(tagCategoryId); // obtain the tag category
    var catName = Category.name;     // tag category name
    // do more things
}

 

 This works on 7.5, but I see no reason it should stop working on 8.3. Very likely, you just flip a boolean for the category name, and if its still set to false at the end of the loop, then its not associated to that category. 

View solution in original post

3 Replies
jimmyvandermast
Hot Shot
Hot Shot
Jump to solution

Not for getting the tags, but to attach tags, a snippet from our code is:

 

var client = vapiEndpoint.client();
var tagging = new com_vmware_cis_tagging_tag__association(client);
var enumerationId = new com_vmware_vapi_std_dynamic__ID() ;
enumerationId.id = vcVm.id;
enumerationId.type = vcVm.vimType;
tagging.attach(tagId, enumerationId);

 

Perhaps this is enough to get you started with the vapi and find more about your specific question?

0 Kudos
omrsafetyo
Enthusiast
Enthusiast
Jump to solution

This quick demo code should show most everything you might need to get what you're asking.

 

var client = VAPIEndpoint.client();
// Set up a dynamic ID for querying 
var objId = new com_vmware_vapi_std_dynamic__ID();
objId.id = vm.id;
objId.type = vm.vimType;

// set up connections for tag associations, tags, and tag categories
var tagging = new com_vmware_cis_tagging_tag__association(client);
var tagMgr = new com_vmware_cis_tagging_tag(client);
var catMgr = new com_vmware_cis_tagging_category(client);

var tagList = tagging.list_attached_tags(objId);  //returns the IDs of each tag
for each (var tagId in tagList) {
    var theTag = tagMgr.get(tagId);  // get the tag itself
    var tagName=theTag.name;         // name/value of the tag
    var tagCategoryId=theTag.category_id;  // the ID of the category
    var Category = catMgr.get(tagCategoryId); // obtain the tag category
    var catName = Category.name;     // tag category name
    // do more things
}

 

 This works on 7.5, but I see no reason it should stop working on 8.3. Very likely, you just flip a boolean for the category name, and if its still set to false at the end of the loop, then its not associated to that category. 

DexG83
Enthusiast
Enthusiast
Jump to solution

This really helped me out. Thanks alot for the code!

0 Kudos