com.vmware.vim25.RetrieveResult Java Examples

The following examples show how to use com.vmware.vim25.RetrieveResult. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MoRefHandler.java    From cs-actions with Apache License 2.0 6 votes vote down vote up
private static String populate(final RetrieveResult results, final Map<String, ManagedObjectReference> tgtMoref) {
    String token = null;
    if (results != null) {
        token = results.getToken();
        for (ObjectContent oc : results.getObjects()) {
            ManagedObjectReference mr = oc.getObj();
            String entityNm = null;
            List<DynamicProperty> dps = oc.getPropSet();
            if (dps != null) {
                for (DynamicProperty dp : dps) {
                    entityNm = (String) dp.getVal();
                }
            }
            tgtMoref.put(entityNm, mr);
        }
    }

    return token;
}
 
Example #2
Source File: VimUtil.java    From vsphere-automation-sdk-java with MIT License 5 votes vote down vote up
/**
 * Uses the new RetrievePropertiesEx method to emulate the now deprecated
 * RetrieveProperties method.
 *
 * @param listpfs
 * @return list of object content
 * @throws Exception
 */
public static List<ObjectContent> retrievePropertiesAllObjects(
        VimPortType vimPort, ManagedObjectReference propCollectorRef,
        List<PropertyFilterSpec> listpfs)
        throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
    RetrieveOptions propObjectRetrieveOpts = new RetrieveOptions();
    List<ObjectContent> listobjcontent = new ArrayList<ObjectContent>();

    RetrieveResult rslts = vimPort.retrievePropertiesEx(propCollectorRef,
            listpfs, propObjectRetrieveOpts);
    if (rslts != null && rslts.getObjects() != null
            && !rslts.getObjects().isEmpty()) {
        listobjcontent.addAll(rslts.getObjects());
    }
    String token = null;
    if (rslts != null && rslts.getToken() != null) {
        token = rslts.getToken();
    }
    while (token != null && !token.isEmpty()) {
        rslts = vimPort.continueRetrievePropertiesEx(propCollectorRef,
                token);
        token = null;
        if (rslts != null) {
            token = rslts.getToken();
            if (rslts.getObjects() != null
                    && !rslts.getObjects().isEmpty()) {
                listobjcontent.addAll(rslts.getObjects());
            }
        }
    }

    return listobjcontent;
}
 
Example #3
Source File: ManagedObjectAccessor.java    From development with Apache License 2.0 5 votes vote down vote up
/**
 * Uses the new RetrievePropertiesEx method to emulate the now deprecated
 * RetrieveProperties method
 *
 * @param filterSpecs
 * @return list of object content
 * @throws RuntimeFaultFaultMsg
 * @throws InvalidPropertyFaultMsg
 * @throws Exception
 */
private List<ObjectContent> retrievePropertiesAllObjects(
        List<PropertyFilterSpec> filterSpecs)
        throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {

    RetrieveOptions retrieveOptions = new RetrieveOptions();
    ManagedObjectReference collector = serviceContent
            .getPropertyCollector();

    List<ObjectContent> contents = new ArrayList<ObjectContent>();

    RetrieveResult results = vimPort.retrievePropertiesEx(collector,
            filterSpecs, retrieveOptions);
    if (results != null && results.getObjects() != null
            && !results.getObjects().isEmpty()) {
        contents.addAll(results.getObjects());
    }
    String token = null;
    if (results != null && results.getToken() != null) {
        token = results.getToken();
    }
    while (token != null && token.length() > 0) {
        results = vimPort.continueRetrievePropertiesEx(collector, token);
        token = null;
        if (results != null) {
            token = results.getToken();
            if (results.getObjects() != null
                    && !results.getObjects().isEmpty()) {
                contents.addAll(results.getObjects());
            }
        }
    }

    return contents;
}
 
Example #4
Source File: Utils.java    From GraphiteReceiver with Apache License 2.0 5 votes vote down vote up
/**
 * initClusterHostMap is a self recursive method for generating VM/ESX to Cluster Hash Map.
 * In the first iteration it gathers all clusters and in consecutive calls for each cluster it updates Hash Map.
 * The logic here is use ComputeResource Entity as a base for gathering all virtual machines and ESX Hosts.
 * As part of configurations, GraphiteReceiver invokes this method at regular intervals (configured) and during runtime
 * if VM/ESX does not exist in the hash map.
 */
public static boolean initClusterHostMap(String clusterName, ManagedObjectReference rootFolder, ExecutionContext context, Map<String,String> clusterMap){
    try {
        if(clusterName == null){
            clusterMap.clear();
        }
        VimConnection connection = context.getConnection();
        RetrieveResult retrieveResult = getRetrieveResult(clusterName, connection, rootFolder);

        while((retrieveResult != null) && (retrieveResult.getObjects() != null) && (retrieveResult.getObjects().size() > 0)){

            String token = retrieveResult.getToken();

            for(ObjectContent objectContent : retrieveResult.getObjects()){
                List<DynamicProperty> dynamicProperties = objectContent.getPropSet();
                if(clusterName != null){
                    String dpsGet = String.valueOf(dynamicProperties.get(0).getVal());
                    clusterMap.put(dpsGet.replace(" ", "_"), clusterName.replace(" ", "_"));
                } else {
                    initClusterHostMap((String) (dynamicProperties.get(0).getVal()), objectContent.getObj(), context, clusterMap);
                }
            }

            if (token == null) {
                return true;
            }
            retrieveResult = connection.getVimPort().continueRetrievePropertiesEx(connection.getPropertyCollector(), token);
        }
        return true;
    } catch(Exception e){
        logger.fatal("Critical Error Detected.");
        logger.fatal(e.getLocalizedMessage());
        return false;
    }
}
 
Example #5
Source File: Utils.java    From GraphiteReceiver with Apache License 2.0 5 votes vote down vote up
private static RetrieveResult getRetrieveResult(String clusterName, VimConnection connection, ManagedObjectReference rootFolder) throws RuntimeFaultFaultMsg, InvalidPropertyFaultMsg {
    List<String> clusterList = new ArrayList<String>();
    clusterList.add("ComputeResource");
    clusterList.add("HostSystem");
    clusterList.add("VirtualMachine");

    ManagedObjectReference rootFolderAux = (clusterName == null)? connection.getRootFolder():rootFolder;
    ManagedObjectReference viewManager = connection.getVimPort().createContainerView(connection.getViewManager(), rootFolderAux, clusterList, true);

    if(viewManager == null) {
        logger.debug("cViewRef is null: " + clusterName);
        return null;
    }
    logger.debug("cViewRef is not null: " + clusterName);

    ObjectSpec objectSpec = new ObjectSpec();
    objectSpec.setObj(viewManager);
    objectSpec.setSkip(true);
    objectSpec.getSelectSet().add(getTraversalSpec(clusterName));

    PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
    propertyFilterSpec.getObjectSet().add(objectSpec);

    if(clusterName == null){
        propertyFilterSpec.getPropSet().add(getPropertySpec("ComputeResource"));
    }else{
        propertyFilterSpec.getPropSet().add(getPropertySpec("HostSystem"));
        propertyFilterSpec.getPropSet().add(getPropertySpec("VirtualMachine"));
    }
    List<PropertyFilterSpec> propertyFilterSpecs = new LinkedList<PropertyFilterSpec>();
    propertyFilterSpecs.add(propertyFilterSpec);
    return connection.getVimPort().retrievePropertiesEx(connection.getPropertyCollector(), propertyFilterSpecs, new RetrieveOptions());
}
 
Example #6
Source File: GetObjectProperties.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
/**
 * Uses the new RetrievePropertiesEx method to emulate the now deprecated
 * RetrieveProperties method
 *
 * @param propertyFilterSpecList
 * @return list of object content
 * @throws Exception
 */
private static List<ObjectContent> retrievePropertiesAllObjects(ConnectionResources connectionResources,
                                                               List<PropertyFilterSpec> propertyFilterSpecList)
        throws RuntimeFaultFaultMsg, InvalidPropertyFaultMsg {

    VimPortType vimPort = connectionResources.getVimPortType();
    ManagedObjectReference serviceInstance = connectionResources.getServiceInstance();
    ServiceContent serviceContent = vimPort.retrieveServiceContent(serviceInstance);
    ManagedObjectReference propertyCollectorReference = serviceContent.getPropertyCollector();
    RetrieveOptions propertyObjectRetrieveOptions = new RetrieveOptions();
    List<ObjectContent> objectContentList = new ArrayList<>();

    RetrieveResult results = vimPort.retrievePropertiesEx(propertyCollectorReference,
            propertyFilterSpecList,
            propertyObjectRetrieveOptions);

    if (results != null && results.getObjects() != null && !results.getObjects().isEmpty()) {
        objectContentList.addAll(results.getObjects());
    }

    String token = null;
    if (results != null && results.getToken() != null) {
        token = results.getToken();
    }

    while (token != null && !token.isEmpty()) {
        results = vimPort.continueRetrievePropertiesEx(propertyCollectorReference, token);
        token = null;
        if (results != null) {
            token = results.getToken();
            if (results.getObjects() != null && !results.getObjects().isEmpty()) {
                objectContentList.addAll(results.getObjects());
            }
        }
    }

    return objectContentList;
}
 
Example #7
Source File: MoRefHandler.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize the helper object on the current connection at invocation time. Do not initialize on construction
 * since the connection may not be ready yet.
 */

private RetrieveResult containerViewByType(final ManagedObjectReference container,
                                           final String morefType,
                                           final RetrieveOptions retrieveOptions
) throws RuntimeFaultFaultMsg, InvalidPropertyFaultMsg {
    return this.containerViewByType(container, morefType, retrieveOptions, ManagedObjectType.NAME.getValue());
}
 
Example #8
Source File: MoRefHandler.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
private Map<String, ManagedObjectReference> toMap(RetrieveResult result)
        throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
    final Map<String, ManagedObjectReference> tgtMoref = new HashMap<>();

    String token = populate(result, tgtMoref);
    while (token != null && !token.isEmpty()) {
        // fetch results based on new token
        result = vimPort.continueRetrievePropertiesEx(serviceContent.getPropertyCollector(), token);
        token = populate(result, tgtMoref);
    }

    return tgtMoref;
}
 
Example #9
Source File: MoRefHandler.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
private ManagedObjectReference findComponentReference(final PropertyFilterSpec[] propertyFilterSpecs,
                                                      final RetrieveOptions retrieveOptions, final String id) throws Exception {
    String token = null;
    ManagedObjectReference searched;
    do {
        final RetrieveResult retrieveResult = retrievePropertiesEx(Arrays.asList(propertyFilterSpecs), retrieveOptions, token);
        token = retrieveResult.getToken();
        searched = getFromRetrieveResult(retrieveResult, id);
    } while (searched == null && StringUtilities.isNotEmpty(token));
    return searched;
}
 
Example #10
Source File: MoRefHandler.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
private RetrieveResult retrievePropertiesEx(final List<PropertyFilterSpec> propertyFilterSpecs, final RetrieveOptions retrieveOptions,
                                            final String token) throws Exception {
    if (isEmpty(token)) {
        return vimPort.retrievePropertiesEx(serviceContent.getPropertyCollector(), propertyFilterSpecs, retrieveOptions);
    }
    return vimPort.continueRetrievePropertiesEx(serviceContent.getPropertyCollector(), token);
}
 
Example #11
Source File: MoRefHandler.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
private ManagedObjectReference getFromRetrieveResult(final RetrieveResult retrieveResult, final String id) {
    for (final ObjectContent oc : retrieveResult.getObjects()) {
        if (StringUtilities.equals(id, oc.getObj().getValue())) {
            return oc.getObj();
        }
    }
    return null;
}
 
Example #12
Source File: ManagedObjectAccessor.java    From development with Apache License 2.0 4 votes vote down vote up
/**
 * Returns all the managed object references of the specified type that are
 * present under the container.
 *
 * @param folder
 *            {@link ManagedObjectReference} of the container to begin the
 *            search from
 * @param morefType
 *            type of the managed entity that needs to be searched
 *
 * @return map of name and MoRef of the managed objects present. May be
 *         empty but not <code>null</code>
 *
 * @throws InvalidPropertyFaultMsg
 * @throws RuntimeFaultFaultMsg
 */
public Map<String, ManagedObjectReference> getMoRefsInContainerByType(
        ManagedObjectReference folder, String morefType)
        throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {

    String PROP_ME_NAME = "name";
    ManagedObjectReference viewManager = serviceContent.getViewManager();
    ManagedObjectReference containerView = vimPort.createContainerView(
            viewManager, folder, Arrays.asList(morefType), true);

    Map<String, ManagedObjectReference> tgtMoref = new HashMap<String, ManagedObjectReference>();

    PropertySpec propertySpec = new PropertySpec();
    propertySpec.setAll(Boolean.FALSE);
    propertySpec.setType(morefType);
    propertySpec.getPathSet().add(PROP_ME_NAME);

    TraversalSpec ts = new TraversalSpec();
    ts.setName("view");
    ts.setPath("view");
    ts.setSkip(Boolean.FALSE);
    ts.setType("ContainerView");

    ObjectSpec objectSpec = new ObjectSpec();
    objectSpec.setObj(containerView);
    objectSpec.setSkip(Boolean.TRUE);
    objectSpec.getSelectSet().add(ts);

    PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
    propertyFilterSpec.getPropSet().add(propertySpec);
    propertyFilterSpec.getObjectSet().add(objectSpec);

    List<PropertyFilterSpec> propertyFilterSpecs = new ArrayList<PropertyFilterSpec>();
    propertyFilterSpecs.add(propertyFilterSpec);

    RetrieveResult rslts = vimPort.retrievePropertiesEx(
            serviceContent.getPropertyCollector(), propertyFilterSpecs,
            new RetrieveOptions());

    List<ObjectContent> listobjcontent = new ArrayList<ObjectContent>();
    if (rslts != null && rslts.getObjects() != null
            && !rslts.getObjects().isEmpty()) {
        listobjcontent.addAll(rslts.getObjects());
    }
    String token = null;
    if (rslts != null && rslts.getToken() != null) {
        token = rslts.getToken();
    }
    while (token != null && token.length() > 0) {
        rslts = vimPort.continueRetrievePropertiesEx(
                serviceContent.getPropertyCollector(), token);
        token = null;
        if (rslts != null) {
            token = rslts.getToken();
            if (rslts.getObjects() != null
                    && !rslts.getObjects().isEmpty()) {
                listobjcontent.addAll(rslts.getObjects());
            }
        }
    }
    for (ObjectContent oc : listobjcontent) {
        ManagedObjectReference mr = oc.getObj();
        String entityNm = null;
        List<DynamicProperty> dps = oc.getPropSet();
        if (dps != null) {
            for (DynamicProperty dp : dps) {
                entityNm = (String) dp.getVal();
            }
        }
        tgtMoref.put(entityNm, mr);
    }
    return tgtMoref;
}
 
Example #13
Source File: MoRefHandler.java    From cs-actions with Apache License 2.0 4 votes vote down vote up
private RetrieveResult containerViewByType(final RetrieveOptions retrieveOptions,
                                           final PropertyFilterSpec... propertyFilterSpecs)
        throws RuntimeFaultFaultMsg, InvalidPropertyFaultMsg {
    return vimPort.retrievePropertiesEx(serviceContent.getPropertyCollector(), Arrays.asList(propertyFilterSpecs),
            retrieveOptions);
}
 
Example #14
Source File: VMwareUtil.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
public static Map<String, Object> getEntityProps(VMwareConnection connection, ManagedObjectReference entityMor, String[] props)
        throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
    Map<String, Object> retVal = new HashMap<>();

    PropertySpec propertySpec = new PropertySpec();

    propertySpec.setAll(Boolean.FALSE);
    propertySpec.setType(entityMor.getType());
    propertySpec.getPathSet().addAll(Arrays.asList(props));

    ObjectSpec objectSpec = new ObjectSpec();

    objectSpec.setObj(entityMor);

    // Create PropertyFilterSpec using the PropertySpec and ObjectPec created above.
    PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();

    propertyFilterSpec.getPropSet().add(propertySpec);
    propertyFilterSpec.getObjectSet().add(objectSpec);

    List<PropertyFilterSpec> propertyFilterSpecs = new ArrayList<>();

    propertyFilterSpecs.add(propertyFilterSpec);

    RetrieveResult rslts = connection.getVimPortType().retrievePropertiesEx(connection.getServiceContent().getPropertyCollector(),
            propertyFilterSpecs, new RetrieveOptions());
    List<ObjectContent> listobjcontent = new ArrayList<>();

    if (rslts != null && rslts.getObjects() != null && !rslts.getObjects().isEmpty()) {
        listobjcontent.addAll(rslts.getObjects());
    }

    String token = null;

    if (rslts != null && rslts.getToken() != null) {
        token = rslts.getToken();
    }

    while (token != null && !token.isEmpty()) {
        rslts = connection.getVimPortType().continueRetrievePropertiesEx(connection.getServiceContent().getPropertyCollector(),
                token);

        token = null;

        if (rslts != null) {
            token = rslts.getToken();

            if (rslts.getObjects() != null && !rslts.getObjects().isEmpty()) {
                listobjcontent.addAll(rslts.getObjects());
            }
        }
    }

    for (ObjectContent oc : listobjcontent) {
        List<DynamicProperty> dps = oc.getPropSet();

        if (dps != null) {
            for (DynamicProperty dp : dps) {
                retVal.put(dp.getName(), dp.getVal());
            }
        }
    }

    return retVal;
}
 
Example #15
Source File: MoRefHandler.java    From cs-actions with Apache License 2.0 3 votes vote down vote up
/**
 * Returns all the MOREFs of the specified type that are present under the
 * container
 *
 * @param folder    {@link ManagedObjectReference} of the container to begin the
 *                  search from
 * @param morefType Type of the managed entity that needs to be searched
 * @return Map of name and MOREF of the managed objects present. If none
 *         exist then empty Map is returned
 * @throws InvalidPropertyFaultMsg
 * @throws RuntimeFaultFaultMsg
 */
public Map<String, ManagedObjectReference> inContainerByType(ManagedObjectReference folder,
                                                             String morefType,
                                                             RetrieveOptions retrieveOptions)
        throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
    RetrieveResult results = containerViewByType(folder, morefType, retrieveOptions);

    return toMap(results);
}
 
Example #16
Source File: MoRefHandler.java    From cs-actions with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the raw RetrieveResult object for the provided container filtered on properties list
 *
 * @param container       - container to look in
 * @param morefType       - type to filter for
 * @param morefProperties - properties to include
 * @return com.vmware.vim25.RetrieveResult for this query
 * @throws RuntimeFaultFaultMsg
 * @throws InvalidPropertyFaultMsg
 */
private RetrieveResult containerViewByType(final ManagedObjectReference container,
                                           final String morefType,
                                           final RetrieveOptions retrieveOptions,
                                           final String... morefProperties
) throws RuntimeFaultFaultMsg, InvalidPropertyFaultMsg {
    PropertyFilterSpec[] propertyFilterSpecs = propertyFilterSpecs(container, morefType, morefProperties);

    return containerViewByType(retrieveOptions, propertyFilterSpecs);
}