3 Replies Latest reply on Sep 9, 2018 11:35 PM by doskiran

    Read Tags from a VirtualMachine

    PoolMaster Lurker

      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.