vmware / vsphere-automation-sdk-java

Java samples, language bindings, and API reference documentation for vSphere, VMC, and NSX-T using the VMware REST API
MIT License
210 stars 134 forks source link

Problems with the findObject Method #205

Closed GVLLIFESTYLE closed 1 year ago

GVLLIFESTYLE commented 1 year ago

Hello! I have the problem, that on large setups of VMWare only the first 100 Objects from the type VirtualMachien are returned by the API. I use this method to retreive the VirtualMachien:

` public ObjectContent findObject(String objectType, String name, String... properties) throws Exception {

    // Get references to the ViewManager and PropertyCollector
    ManagedObjectReference viewMgrRef = serviceContent.getViewManager();
    ManagedObjectReference propColl = serviceContent.getPropertyCollector();

    // use a container view for virtual machines to define the traversal
    // - invoke the VimPortType method createContainerView (corresponds
    // to the ViewManager method) - pass the ViewManager MOR and
    // the other parameters required for the method invocation
    // (use a List<String> for the type parameter's string[])
    List<String> typeList = new ArrayList<>();
    typeList.add(objectType);

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

    // create an object spec to define the beginning of the traversal;
    // container view is the root object for this traversal
    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(cViewRef);
    oSpec.setSkip(true);

    // create a traversal spec to select all objects in the view
    TraversalSpec tSpec = new TraversalSpec();
    tSpec.setName("traverseEntities");
    tSpec.setPath("view");
    tSpec.setSkip(false);
    tSpec.setType("ContainerView");

    // add the traversal spec to the object spec;
    // the accessor method (getSelectSet) returns a reference
    // to the mapped XML representation of the list; using this
    // reference to add the spec will update the selectSet list
    oSpec.getSelectSet().add(tSpec);

    // specify the properties for retrieval
    // (virtual machine name, network summary accessible, rp runtime props);
    // the accessor method (getPathSet) returns a reference to the mapped
    // XML representation of the list; using this reference to add the
    // property names will update the pathSet list
    boolean gotName = false;
    PropertySpec pSpec = new PropertySpec();
    pSpec.setType(objectType);
    if (properties != null) {
        for (String property : properties) {
            pSpec.getPathSet().add(property);
            if (property.equalsIgnoreCase("name")) {
                gotName = true;
            }
        }
    }
    if (!gotName) {
        pSpec.getPathSet().add("name");
    }

    // create a PropertyFilterSpec and add the object and
    // property specs to it; use the getter methods to reference
    // the mapped XML representation of the lists and add the specs
    // directly to the objectSet and propSet lists
    PropertyFilterSpec fSpec = new PropertyFilterSpec();
    fSpec.getObjectSet().add(oSpec);
    fSpec.getPropSet().add(pSpec);

    // Create a list for the filters and add the spec to it
    List<PropertyFilterSpec> fSpecList = new ArrayList<>();
    fSpecList.add(fSpec);

    // get the data from the server
    RetrieveOptions retrieveOptions = new RetrieveOptions();
    RetrieveResult props = null;
    try {
        props = vimPort.retrievePropertiesEx(propColl, fSpecList, retrieveOptions);
    } catch (com.vmware.vim25.InvalidPropertyFaultMsg ipfm) {
        String oneStringOfProperties = "";
        if (properties != null) {
            for (String property : properties) {
                oneStringOfProperties += property + " ";
            }
        }
        throw new Exception(
                "One of the properties passed to findObject was not present on the object found.  Here is a list of properties which might be wrong: "
                        + oneStringOfProperties, ipfm);
    }

    // go through the returned list and print out the data
    if (props != null) {
        for (ObjectContent oc : props.getObjects()) {
            String value = null;
            String path = null;
            List<com.vmware.vim25.DynamicProperty> dps = oc.getPropSet();
            if (dps != null) {
                for (DynamicProperty dp : dps) {
                    path = dp.getName();
                    if (path.equals("name")) {
                        value = (String) dp.getVal();
                        System.out.println(value+" \n");
                        if (value.equals(name)) {
                            return oc;
                        }
                    }
                }
            }
        }
    }
    return null;
}`

I use the latest automation api: 8.0U1 and our vcenter is on latest ESX 7. Can you advice?

GVLLIFESTYLE commented 1 year ago

you can close the ticket. I fixed it ;)