VMware {code} Community
PoolMaster
Contributor
Contributor

Read Tags from a VirtualMachine

How to get tags from a virtual machine in the example below:

// Get reference to the PropertyCollector

propertyCollector = serviceContent.getPropertyCollector();

// Create a new ManagedObjectReference to get

rootFolder = serviceContent.getRootFolder();


viewMgrRef = serviceContent.getViewManager();

propColl = serviceContent.getPropertyCollector();

vmList = new ArrayList<String>();

vmList.add("ManagedEntity");

cViewRef = vimPort.createContainerView(viewMgrRef, serviceContent.getRootFolder(), vmList, true );    

// Create an ObjectSpec to define the beginning of the traversal

// We are traversing the root folder, so set the obj property to it

objectSpec = new ObjectSpec();

objectSpec.setObj(cViewRef);

objectSpec.setSkip(true);

tSpec = new TraversalSpec();

tSpec.setName("traverseEntities");

tSpec.setPath("view");

tSpec.setSkip(false);

tSpec.setType("ContainerView");

objectSpec.getSelectSet().add(tSpec);

// Create a PropertySpec to specify the properties we want.

propertySpec = new PropertySpec();

propertySpec.setType("VirtualMachine");

propertySpec.setAll(true);

// Create a PropertyFilterSpec and add the ObjectSpec and

// PropertySpec to it.  As above, the getter methods will automatically

// initialize the lists

propertyFilterSpec = new PropertyFilterSpec();

propertyFilterSpec.getObjectSet().add(objectSpec);

propertyFilterSpec.getPropSet().add(propertySpec);

// The RetrievePropertiesEx method takes a list of PropertyFilterSpec, so we need

// to create a list and add our propertyFilterSpec to it

propertyFilterSpecList = new ArrayList<PropertyFilterSpec>();

propertyFilterSpecList.add(propertyFilterSpec);

// Although the RetrieveOptions parameter is optional, in Java we must pass

// something in.  A null will give us an exception, so we must pass in an empty

// RetrieveOptions object

retrieveOptions = new RetrieveOptions();

// Finally, make the call and get the results

result = vimPort.retrievePropertiesEx(propertyCollector, propertyFilterSpecList, retrieveOptions);

if (result != null) {

     for (ObjectContent objectContent : result.getObjects()) {

          properties = objectContent.getPropSet();

          for (DynamicProperty property : properties) {

               out.println(property.getName() + ": " + property.getVal());

               if ("tag".equals(property.getName())) {

                    tags = (ArrayOfTag) property.getVal();

                    out.println("TAGS .... " +tags.getTag().size());

               }

          }

     }

}

Output:

tag: com.vmware.vim25.ArrayOfTag@15b079f

TAGS .... 0

Problem: size is always 0 even we have tagged the VMs.

Reply
0 Kudos
3 Replies
doskiran
Enthusiast
Enthusiast

There are multiple to do the Tag operations for VC inventory objects,

1) Use Inventory service Data providers , - VMware Knowledge Base

Using Mob - https://vCenter_Server_FQDN/invsvc/mob1

2)  VMware-vSphere-Automation-SDK-Java-6.5.0

- Use package "com.vmware.cis.tagging" , which provide the Interfaces and classes to create, read, update, delete Category and Tag.

3)  Use vSphere REST APIs,

- From VC -> Browse to vSphere REST APIs ->Select API-"cis"

Reply
0 Kudos
PoolMaster
Contributor
Contributor

We implement and application based on "VMware vSphere Web Services SDK" and don't want to use the VMware-vSphere-Automation-SDK-Java-6.5.0.

There we do not understand why the ArrayOfTag field as result of our PropertyCollector is always empty. Means the object is set but no tags are included.

Do we do something wrong?

Reply
0 Kudos
doskiran
Enthusiast
Enthusiast

Tags are actually stored and managed by the Inventory Service, use the Inventory service tagging API to retrieve the tags.
For more information - https://communities.vmware.com/message/2409321#2409321
Reply
0 Kudos