VMware Cloud Community
TechNoMo
Contributor
Contributor
Jump to solution

Find VMs with a tag and perform an action(s) for each one

Hello all, looking for a little help, I have some tags in vCenter that I would like vRO (8.0.1) to find and perform a set of actions on. To make it simple the actions are guest shutdown (and poll till off), snapshot, and power back on. I have a working workflow for the actions, but I'm having an issues getting vRO to find the VMs tagged with my "Test" tag. Any help would be welcomed! Once I get the tagged part figured out I need a little help on the for each vm found do this.

Reply
0 Kudos
1 Solution

Accepted Solutions
TechNoMo
Contributor
Contributor
Jump to solution

Thank you, the code runs, no errors however I don't see in the log the list of VMs with the tag name. I know it sees/finds something as it doesn't give the error "unable to find tag id". I need it to output the VM names so I can pass it to the next task in the flow. I thought about making an output and using "attachedObjects" as the string item.

 

if (endpoint == null) {
throw "'endpoint' parameter should not be null";
}
var tagName = MyTagName;
var client = endpoint.client();
var tagsvc = new com_vmware_cis_tagging_tag(client);
var tagAssoc = new com_vmware_cis_tagging_tag__association(client);
var tagsID = tagsvc.list();
var myTagID = null;
for(i in tagsID){
var objTag = tagsvc.get(tagsID[i]);
if (objTag.name == tagName){
myTagID = tagsID[i];
break;
}
}
if(myTagID != null)
var attachedObjects = tagAssoc.list_attached_objects(myTagID);
else
System.log("Unable to find tag ID");

View solution in original post

Reply
0 Kudos
11 Replies
nef_user
Enthusiast
Enthusiast
Jump to solution

Hi,

You can use vAPI endpoint.

You need to configure de vAPI endpoint. In the code below endpoint is of type VAPI:VAPIEndpoint. You can also use the vAPI to get de tag ID that is needed to use the method to list associated objects. 

 

var endpointClient = endpoint.client();

var tagAssoc = new com_vmware_cis_tagging_tag_association(endpointClient);

var attachedObjects = tagAssoc.list_attached_objects(tagID);

TechNoMo
Contributor
Contributor
Jump to solution

when I attempted to run this I got the following error, it did let me select the vAPI endpoint, and I created the string input for "tagID" and supplied the tag name I was looking for.

 

ReferenceError: "com_vmware_cis_tagging_tag_association" is not defined

Reply
0 Kudos
TechNoMo
Contributor
Contributor
Jump to solution

I also tried this one from another post, and got a different error. https://communities.vmware.com/t5/vRealize-Orchestrator/vAPI-list-all-tagged-objects-doesn-t-seem-to...

 

 

ERRORWorkflow execution stack: *** item: 'Copy of List all tags/item2', state: 'failed', business state: 'null', exception: 'com.vmware.vapi.std.errors.not_found => {data=<unset>, messages=[]} (Workflow:Copy of List all tags / Scriptable task (item1)#6)' workflow: 'Copy of List all tags' (cdd000db-87a9-435a-9da8-f2bec70dc788) | 'attribute': name=errorCode type=string value=com.vmware.vapi.std.errors.not_found => {data=<unset>, messages=[]} (Workflow:Copy of List all tags / Scriptable task (item1)#6) | 'input': name=endpoint type=VAPI:VAPIEndpoint value=dunes://service.dunes.ch/CustomSDKObject?id='ENDPOINT--https___*SERVER*_api'&dunesName='VAPI:VAPIEndpoint' | 'input': name=tagid type=string value=Window_A | 'no outputs' *** End of execution stack.

Reply
0 Kudos
nef_user
Enthusiast
Enthusiast
Jump to solution

Hi,

Sorry, 

typing error. the method is com_vmware_cis_tagging_tag__association. There are two underscores before association.

 

 

 

 

Reply
0 Kudos
TechNoMo
Contributor
Contributor
Jump to solution

thank you! I figured it that out by looking at another example and the api explorer and realized it was missing another _. Since then I get the following error, again I'm sorry if these seem dumb, I haven't directly worked vRO in just the JavaScript part before. Do I need an output for the VMs found, and should it be a string/array? Here is a shot of the new error.

 

ERRORWorkflow execution stack: *** item: 'List tags Association/item2', state: 'failed', business state: 'null', exception: 'com.vmware.vapi.std.errors.not_found => {data=<unset>, messages=[]} (Workflow:List tags Association / Scriptable task (item1)#6)' workflow: 'List tags Association' (cdd000db-87a9-435a-9da8-f2bec70dc788) | 'attribute': name=errorCode type=string value=com.vmware.vapi.std.errors.not_found => {data=<unset>, messages=[]} (Workflow:List tags Association / Scriptable task (item1)#6) | 'input': name=endpoint type=VAPI:VAPIEndpoint value=dunes://service.dunes.ch/CustomSDKObject?id='ENDPOINT--https___vro.local.com_api'&dunesName='VAPI:VAPIEndpoint' | 'input': name=tagid type=string value=Window_A | 'no outputs' *** End of execution stack.

Reply
0 Kudos
nef_user
Enthusiast
Enthusiast
Jump to solution

The tagId used in this method is not the tag name that appears at vCenter. You need to get the tag ID first.

ps: I wrote the code below, I did not copy from vRO, be aware of typing errors.

 

var tagName = "MyTagName";

var client = endpoint.client();

var tagsvc = new com_vmware_cis_tagging_tag(client);

var tagAssoc = new com_vmware_cis_tagging__association(client);

var tagsID = tagsvc.list();

var myTagID = null;

for(i in tagsID){

     var objTag = tagsvc.get(tagsID[i]);

     if (objTag.name == tagName){

       myTagID = tagsID[i];

       break;

   }

}

if(myTagID != null)

  var attachedObjects = tagAssoc.list_attached_objects(myTagID);

else

   System.log("Unable to find tag ID");

 

 

Reply
0 Kudos
TechNoMo
Contributor
Contributor
Jump to solution

Thank you, the code runs, no errors however I don't see in the log the list of VMs with the tag name. I know it sees/finds something as it doesn't give the error "unable to find tag id". I need it to output the VM names so I can pass it to the next task in the flow. I thought about making an output and using "attachedObjects" as the string item.

 

if (endpoint == null) {
throw "'endpoint' parameter should not be null";
}
var tagName = MyTagName;
var client = endpoint.client();
var tagsvc = new com_vmware_cis_tagging_tag(client);
var tagAssoc = new com_vmware_cis_tagging_tag__association(client);
var tagsID = tagsvc.list();
var myTagID = null;
for(i in tagsID){
var objTag = tagsvc.get(tagsID[i]);
if (objTag.name == tagName){
myTagID = tagsID[i];
break;
}
}
if(myTagID != null)
var attachedObjects = tagAssoc.list_attached_objects(myTagID);
else
System.log("Unable to find tag ID");
Reply
0 Kudos
nef_user
Enthusiast
Enthusiast
Jump to solution

The var attachedObjects will be an array of the objects with the tag. If you have other objects with the same tag or more than one vCenter at your inventory you need to validate, because at some time this code will not work properly. I don't know if there is a better way to do, but the code below will work.

If you add:

var arrayVMs = [];

for(i in attachedObjects){

   var attObj = attachedObjects[i];

   var vmID = attObj.id;  // Now you have the virtual machine id, for example: vm-39

   var vm = VcPlugin.getAllVirtualMachines(null, "xpath:id='" + vmID + "'")[0];  //now you have the virtual machine Object - VcVirtualMachine

   System.log(vm.name); // Log the vm Name

   arrayVMs.push(vm); //Add the VM to the array

}

//arrayVMs will be an array of VcVirtualMachine, you can output this array to the next flow.

Reply
0 Kudos
TechNoMo
Contributor
Contributor
Jump to solution

that worked, and it logs the VMs with the tag. Thank you very much, truly appreciate it!

 

Now to figure out how to make the next one perform a set of actions against each VM.

Reply
0 Kudos
TechNoMo
Contributor
Contributor
Jump to solution

coming back to say thank you again, I was able to complete my overall workflow with your help on with the VM retrieve from tag name/tagid! Truly appreciate it!!

Reply
0 Kudos
rkuechler
Enthusiast
Enthusiast
Jump to solution

May I ask a general question about vCenter Tags and Categories?

Do I understand correctly that you can only access vCenter tags and categories if you configure a "vAPI Endpoint"? And it works not with the already configured "vSphere vCenter Plug-in"?

Reply
0 Kudos