VMware Cloud Community
bkgc
Contributor
Contributor
Jump to solution

Create snapshot of all servers in ressource pool with tag xyz

Hi,

i am using the workflow: Create snapshots of all virtual machines in a ressource pool.

now i want to check in that workflow if a virtual machine has the tag xyz. if so, then do a snapshot. if not, do no snapshot. i think the best way is to get all vms with this tag in initialize part of that workflow in variable "allVMs", but how can i achive this? Or does anyone have an better idea?

regards,

bkgc

0 Kudos
1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

What's not working with this code? Did you see any output from commented debug logs on lines 18, 21 and 23?

One problem here is with the code on line 24. I assume you want to collect all vms[k] objects into an array allVMs, right? The code as written won't do it.

So assuming at some point (before the 'for' loops) you have declared allVMs variable to be an array with something like

var allVMs = [];

Then you should replace the code on line 24 with something like that will add vms[k] to the array:

allVMs.push(vms[k]);

View solution in original post

0 Kudos
44 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

The problem is that vCenter API consumed by vCenter plug-in does not provide access to tagging functionality. To access tagging API, one option is to use vRO vAPI plug-in. Combining vCenter and vAPI plug-ins, you have two ways to solve your task:

1) Once you have the list of VMs in the resource pool in variable allVMs, iterate over it and for each VM check if it has the required tag using vAPI APIs

or 2) Using vAPI APIs, get a list of all objects associated with the required tag object, and then check if each object in this list is a VM belonging also to allVMs list

Not sure which of these 2 approaches is more effective, but the first one seems more straightforward to me. Let me know if you need help with vAPI plug-in.

0 Kudos
bkgc
Contributor
Contributor
Jump to solution

Hi,

ok, lets start with option one. Can you explain to me how to do that?

regards,

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

The first step is to import vAPI metamodel from your vCenter and add vAPI endpoint. To do so, run the workflow Import vAPI metamodel and provide endpoint URL in the form https://your-vcenter-fqdn/api. Once it completes, take a look at the example workflows under Library > VAPI > Examples > vSphere and make sure they work. Also, takea look at the scripting code inside these workflows to see how the vAPI look like. Then, check the vAPI documentation in vRO API Explorer, in particular the scripting class com_vmware_cis_tagging_tag__association and its methods.

Most of vAPI work with object IDs, so at this point you should try to write some code, for example, try to write workflow or action to get a tag ID given a tag name xyz, and also a workflow or action to find VM ID given a vCenter VM object. Once you have these 2 building blocks ready, you should be able to combine them with the workflow to create snapshots.

0 Kudos
bkgc
Contributor
Contributor
Jump to solution

Hi,

thanks for your answer but i am not a coding guy Smiley Sad is there an other way to modify my existing workflow to only use a set of vms in a ressource pool. "TAGGING" seems to be to complicated for me.

is it possible to create a ressource pool with vms in it and let the settings of the resource pool that there are no limitations or reservations? Just like a simple container were i can put VMs in it and the i chose this ressource pool for my workflow?

regards,

0 Kudos
bkgc
Contributor
Contributor
Jump to solution

hi,

ok, i will try it Smiley Happy

>> Once it completes, take a look at the example workflows under Library > VAPI > Examples > vSphere and make sure they work.

When i run "List all Tags" for example i am asked for the vapi endpoint. what is that? what do i have to enter in this search field? when i enter my https://fqdn/api it says "nothing found".

regards,

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

When you run Import vAPI metamodel workflow, there is an option (radio button) to also create an endpoint. Another option is after impoting the metamodel, to run the workflow Add vAPI endpoint to register endpoint. This workflow is useful also when you want to import one metamodel (eg. from vCenter 6.5) and then register multiple endpoints (eg. one endpoint per each vCenter 6.5 instance).

So when you run List tags workflow, in the endpoint input field you need to select one of the previously added endpoints; you cannot provide an arbitrary string there.

0 Kudos
bkgc
Contributor
Contributor
Jump to solution

Hi,

step by step i come closer to my needs Smiley Happy the workflows under VAPI Examples are working now.

>> try to write workflow or action to get a tag ID given a tag name xyz

can you help me with this?

regards,

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

You can use the example workflow List all tags as a starting point.

This workflow shows how to retrieve IDs of all existing tags. So you'll need to add some code to iterate over the array with found tag IDs, and for each ID call the method get(id) of the object com_vmware_cis_taggin_tag to retrieve the tag object behind this tag ID.

Once you have the tag object, you can compare its name property with the desired tag name you are looking for, eg. xyz, and if they match, then you have found the tag ID.

One possible complication here is that it is possible to have more than one tag with a given name xyz but in different tag categories, so the above loop may find multiple matches. You should decide how to handle this situation; for example, you can return all such tag IDs, or you can return just the first tag ID that matches.

0 Kudos
bkgc
Contributor
Contributor
Jump to solution

Hi,

i understand.

Lets go one step back. When i run the workflow "List all Tags" in Log Output in Orchestrator Designer i see only:

[2018-07-13 11:03:32.251] [I] urn:vmomi:InventoryServiceTag:096dcdfc-26c3-4d37-81ae-8b76fcdcc4dd:GLOBAL

But i have definetly a Tag created in VCenter named "Test".

Why is it not shown in Log Output?

regards,

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

Because urn:vmomi:InventoryServiceTag:096dcdfc-26c3-4d37-81ae-8b76fcdcc4dd:GLOBAL is a tag ID, not tag object.

To retrieve various tag properties like tag name, you need to fetch tag object by its tag ID. Check the sample workflow Get tag by ID that demonstrates how to do so.

0 Kudos
bkgc
Contributor
Contributor
Jump to solution

OK, what do you think about that code:

if (endpoint == null) {
  throw "'endpoint' parameter should not be null";
}

if (tagName == null) {
  throw "'tagName' parameter should not be null";
}

var client = endpoint.client();
try {
  var tagging = new com_vmware_cis_tagging_tag(client);
  var result = tagging.list();

  for (i = 0; i <= result.length; i++) {
result.get(id);

if (result.name == tagName) {
  System.log(result);
}
  }

} finally {
  client.close();
}

i have created an IN Parameter tagName as string.

regards,

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

The for loop and its body are not correct.

You need to replace:

for (i = 0; i <= result.length; i++) {

  result.get(id);

  if (result.name == tagName) {

    System.log(result);

  }

}

with:

for (i = 0; i < result.length; i++) {

  var tag = tagging.get(result[i]);

  if (tag.name == tagName) {

    System.log(tag);

  }

}

0 Kudos
bkgc
Contributor
Contributor
Jump to solution

Hi,

thx Smiley Happy

when i now run this workflow i use as vAPI Endpoint https://fqdn/api (i use my real fqdn) and for tagName "Test" (which i have definetly in my vCenter) but in Output Log there is no entry.

Any idea what could be wrong?

regards,

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

It works for me.

To troubleshoot it, add additional System.log() statements to dump various variables in the code.

0 Kudos
bkgc
Contributor
Contributor
Jump to solution

Hi,

very strange, on my other vcenter it is working as it should. never mind, lets go on.

now i get the following output:

[2018-07-13 13:44:24.682] [I] DynamicWrapper (Instance) : [com_vmware_cis_tagging_tag__model]-[class com.vmware.o11n.plugin.vapi.model.VapiObjectWrapper] -- VALUE : com.vmware.o11n.plugin.vapi.model.VapiObjectWrapper@6ac8344b[{category_id=urn:vmomi:InventoryService..., name=Test, description=, id=urn:vmomi:InventoryServiceTag:6448419a-0963-414a-bf0a-f62ff38218e7:GLOBAL, used_by=[]}]

i have now added a output: tag as string

is this correct so far?

>> workflow or action to find VM ID given a vCenter VM object

Can you point me to the right way in this case to?

regards,

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

Here is a snippet showing how to get the list of VMs via vAPI whose ID matches the ID of the vCenter VM (represented in the below code with variable vm of type VC:VirtualMachine)

var vmsvc = new com_vmware_vcenter_VM(client);

var filter = new com_vmware_vcenter_VM_filter__spec();

filter.vms = [ vm.id ];

var result = vmsvc.list(filter);

0 Kudos
bkgc
Contributor
Contributor
Jump to solution

like that?

if (endpoint == null) {
  throw "'endpoint' parameter should not be null";
}

if (tagName == null) {
  throw "'tagName' parameter should not be null";
}

var client = endpoint.client();
try {
  var tagging = new com_vmware_cis_tagging_tag(client);
  var result = tagging.list();

  for (i = 0; i < result.length; i++) {
var tag = tagging.get(result[i])
if (tag.name == tagName) {
  System.log(tag);
}
  }

  var vmsvc = new com_vmware_vcenter_VM(client); 
  var filter = new com_vmware_vcenter_VM_filter__spec(); 
  filter.vms = [ vm.id ]; 
  var result = vmsvc.list(filter); 
  System.log(result);

} finally {
  client.close();
}

sorry, how do i format my code in this forum as code?

regards,

0 Kudos
iiliev
VMware Employee
VMware Employee
Jump to solution

Yes, something like that.

To format the code as javascript code, just select it, then select the 'Insert' icon (the rightmost icon on the second line on the editing toolbar above the edit text window; looks like  '>>' ). From the menu that will open select  Syntax Highlighting > javascript

0 Kudos
bkgc
Contributor
Contributor
Jump to solution

Hi,

this is my code right now but it thorws an error:

if (endpoint == null) {
  throw "'endpoint' parameter should not be null";
}

if (tagName == null) {
  throw "'tagName' parameter should not be null";
}

var client = endpoint.client();
try {
  var tagging = new com_vmware_cis_tagging_tag(client);
  var result = tagging.list();

  for (i = 0; i < result.length; i++) {
var tag = tagging.get(result[i])
if (tag.name == tagName) {
  System.log(tag.id);
}
  }

  var vmsvc = new com_vmware_vcenter_VM(client); 
  var filter = new com_vmware_vcenter_VM_filter__spec(); 
  filter.vms = [ tag.id ]; 
  var result = vmsvc.list(filter); 
  System.log(result);

} finally {
  client.close();
}

0 Kudos