Giuseppe08
Contributor
Contributor

Add tags to multiple VMs

Hello to everyone,

i have to run a workflow where the user can pass different set of machine and for all of this machine we have to run operation like tagging (add a tag i.e. tagName)

how can I make it possibile? We must use vRO 7.6, no other solutions are possible.

Many thanks for who can be helpful

0 Kudos
1 Reply
KThorlund
Enthusiast
Enthusiast

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);
}

 

 

0 Kudos