This is one way to add a tag to a VM. And then you just have to loop it on your list of VMs.
You will need to add the VAPI endpoint to the vRO.
var client = vApiEndpoint.client();
var taggingCat = new com_vmware_cis_tagging_category(client);
var tagCats = taggingCat.list();
tagCategory = "Test-Dev-Backup";
tagName = "Daily Backup 1";
System.debug(tagCategory);
for each (var tagCat in tagCats)
{
var obj = taggingCat.get(tagCat);
System.debug(obj.name);
if (tagCategory == obj.name)
{
System.debug("Tag category ID found " + obj.id + " for tag " + tagCategory);
tagCategoryId = obj.id;
break;
}
}
var tagging = new com_vmware_cis_tagging_tag(client);
var tags = tagging.list();
for each (var tag in tags)
{
var obj = tagging.get(tag);
System.debug(obj.name);
if (tagName == obj.name)
{
System.debug("Tag ID found " + obj.id + " for tag " + tagName);
var tagId = obj.id;
break;
}
}
try
{
var taggingAss = new com_vmware_cis_tagging_tag__association(client);
var enumerationId = new com_vmware_vapi_std_dynamic__ID() ;
enumerationId.id = vm.id;
enumerationId.type = vm.vimType;
System.log("enumerationId.type: " + enumerationId.type);
System.log("enumerationId.id: " + enumerationId.id);
taggingAss.attach(tagId, enumerationId);
System.debug("Tag ID " + tagId + " assigned to VC VM " + vm.name);
}
catch(e)
{
System.debug("Associating " + tagId + " failed");
System.debug(e);
}