VMware Cloud Community
oendelm
Enthusiast
Enthusiast
Jump to solution

How to associate a tag using vAPI

Hi guys,

I'm trying to associate a vCenter tag to some VMs using the vAPI in orchestrator.

I've found the "com_vmware_cis_tagging_tag_association" object that has the attach method. The input parametes to the method are tag_id (string) and object_id (com_vmware_vapi_std_dynamic_id).

I've found a way to get the tag_id but I was not able to get the object_id. How can I get an object of the type "com_vmware_vapi_std_dynamic_id" from a VcVirtualMachine?

Thanks.

1 Solution

Accepted Solutions
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

Here is some sample code (dynamic ID creation is on lines 33-35). It also shows how you can search for a VM using vAPI if VC:VirtualMachine object is not available.

// Input parameters

//  endpoint - vAPI endpoint

//  vm - VC:VirtualMachine (optional)

if (endpoint == null) {

  throw "'endpoint' parameter should not be null";

}

var client = endpoint.client();

var vmid;

if (vm != null) {

  // VC:VirtualMachine input parameter is provided; get VM ID from it

  vmid = vm.id;

} else {

  // Find VM by name using vAPI; you can find it also by other properties

  var vmsvc = new com_vmware_vcenter_VM(client);

  var spec = new com_vmware_vcenter_VM_filter__spec();

  spec.names = ["your-vm-name"]; // replace with the name of VM you wan to find

  var found = vmsvc.list(spec);

  if (found == null || found.length == 0) {

    throw "No VM found";

  }

  if (found.length > 1) {

    System.log("Multiple VMs found; will use the first one");

  }

  vmid = found[0].vm;

}

// Attach the tag

var tagsvc = new com_vmware_cis_tagging_tag__association(client);

var tagid = "urn:vmomi:InventoryServiceTag:63c7dd25-af15-4020-9c9a-6490b4c5f40b:GLOBAL"; // replace with your tag ID

var dynid = new com_vmware_vapi_std_dynamic__ID();

dynid.id = vmid;

dynid.type = "VirtualMachine";

tagsvc.attach(tagid, dynid);

View solution in original post

3 Replies
iiliev
VMware Employee
VMware Employee
Jump to solution

Hi,

Here is some sample code (dynamic ID creation is on lines 33-35). It also shows how you can search for a VM using vAPI if VC:VirtualMachine object is not available.

// Input parameters

//  endpoint - vAPI endpoint

//  vm - VC:VirtualMachine (optional)

if (endpoint == null) {

  throw "'endpoint' parameter should not be null";

}

var client = endpoint.client();

var vmid;

if (vm != null) {

  // VC:VirtualMachine input parameter is provided; get VM ID from it

  vmid = vm.id;

} else {

  // Find VM by name using vAPI; you can find it also by other properties

  var vmsvc = new com_vmware_vcenter_VM(client);

  var spec = new com_vmware_vcenter_VM_filter__spec();

  spec.names = ["your-vm-name"]; // replace with the name of VM you wan to find

  var found = vmsvc.list(spec);

  if (found == null || found.length == 0) {

    throw "No VM found";

  }

  if (found.length > 1) {

    System.log("Multiple VMs found; will use the first one");

  }

  vmid = found[0].vm;

}

// Attach the tag

var tagsvc = new com_vmware_cis_tagging_tag__association(client);

var tagid = "urn:vmomi:InventoryServiceTag:63c7dd25-af15-4020-9c9a-6490b4c5f40b:GLOBAL"; // replace with your tag ID

var dynid = new com_vmware_vapi_std_dynamic__ID();

dynid.id = vmid;

dynid.type = "VirtualMachine";

tagsvc.attach(tagid, dynid);

oendelm
Enthusiast
Enthusiast
Jump to solution

Hi,

Thank you very much again.

0 Kudos
Mnemonic
Enthusiast
Enthusiast
Jump to solution

I wrote a short article about how to do this. https://vm.knutsson.it/2017/01/assigning-vcenter-tags-using-vrealize-orchestrator/

There are also links to a vRO package with example workflows.

0 Kudos