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.
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"
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?