Search in sources :

Example 1 with RetrieveOptions

use of com.vmware.vim25.RetrieveOptions in project photon-model by vmware.

the class GetMoRef method inFolderByType.

/**
 * Returns all the MOREFs of the specified type that are present under the
 * folder
 *
 * @param folder    {@link ManagedObjectReference} of the folder 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> inFolderByType(final ManagedObjectReference folder, final String morefType, final RetrieveOptions retrieveOptions) throws RuntimeFaultFaultMsg, InvalidPropertyFaultMsg {
    final PropertyFilterSpec[] propertyFilterSpecs = propertyFilterSpecs(folder, morefType, "name");
    // reuse this property collector again later to scroll through results
    final ManagedObjectReference propertyCollector = this.serviceContent.getPropertyCollector();
    RetrieveResult results = this.vimPort.retrievePropertiesEx(propertyCollector, Arrays.asList(propertyFilterSpecs), retrieveOptions);
    final Map<String, ManagedObjectReference> tgtMoref = new HashMap<>();
    while (results != null && !results.getObjects().isEmpty()) {
        resultsToTgtMorefMap(results, tgtMoref);
        final String token = results.getToken();
        // if we have a token, we can scroll through additional results, else there's nothing to do.
        results = (token != null) ? this.vimPort.continueRetrievePropertiesEx(propertyCollector, token) : null;
    }
    return tgtMoref;
}
Also used : PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) HashMap(java.util.HashMap) RetrieveResult(com.vmware.vim25.RetrieveResult) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 2 with RetrieveOptions

use of com.vmware.vim25.RetrieveOptions in project photon-model by vmware.

the class GetMoRef method entityProps.

/**
 * Method to retrieve properties of list of {@link ManagedObjectReference}
 *
 * @param entityMors List of {@link ManagedObjectReference} for which the properties
 *                   needs to be retrieved
 * @param props      Common properties that need to be retrieved for all the
 *                   {@link ManagedObjectReference} passed
 * @return Map of {@link ManagedObjectReference} and their corresponding name
 *         value pair of properties
 * @throws InvalidPropertyFaultMsg
 * @throws RuntimeFaultFaultMsg
 */
public Map<ManagedObjectReference, Map<String, Object>> entityProps(List<ManagedObjectReference> entityMors, String[] props) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
    init();
    Map<ManagedObjectReference, Map<String, Object>> retVal = new HashMap<ManagedObjectReference, Map<String, Object>>();
    // Create PropertyFilterSpec
    PropertyFilterSpecBuilder propertyFilterSpec = new PropertyFilterSpecBuilder();
    Map<String, String> typesCovered = new HashMap<String, String>();
    for (ManagedObjectReference mor : entityMors) {
        if (!typesCovered.containsKey(mor.getType())) {
            // Create & add new property Spec
            propertyFilterSpec.propSet(new PropertySpecBuilder().all(Boolean.FALSE).type(mor.getType()).pathSet(props));
            typesCovered.put(mor.getType(), "");
        }
        // Now create & add Object Spec
        propertyFilterSpec.objectSet(new ObjectSpecBuilder().obj(mor));
    }
    List<PropertyFilterSpec> propertyFilterSpecs = new ArrayList<PropertyFilterSpec>();
    propertyFilterSpecs.add(propertyFilterSpec);
    RetrieveResult rslts = this.vimPort.retrievePropertiesEx(this.serviceContent.getPropertyCollector(), propertyFilterSpecs, new RetrieveOptions());
    List<ObjectContent> listobjcontent = new ArrayList<ObjectContent>();
    String token = populate(rslts, listobjcontent);
    while (token != null && !token.isEmpty()) {
        rslts = this.vimPort.continueRetrievePropertiesEx(this.serviceContent.getPropertyCollector(), token);
        token = populate(rslts, listobjcontent);
    }
    for (ObjectContent oc : listobjcontent) {
        List<DynamicProperty> dps = oc.getPropSet();
        Map<String, Object> propMap = new HashMap<String, Object>();
        if (dps != null) {
            for (DynamicProperty dp : dps) {
                propMap.put(dp.getName(), dp.getVal());
            }
        }
        retVal.put(oc.getObj(), propMap);
    }
    return retVal;
}
Also used : PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) DynamicProperty(com.vmware.vim25.DynamicProperty) HashMap(java.util.HashMap) PropertyFilterSpecBuilder(com.vmware.photon.controller.model.adapters.vsphere.util.builders.PropertyFilterSpecBuilder) ArrayList(java.util.ArrayList) RetrieveOptions(com.vmware.vim25.RetrieveOptions) ObjectContent(com.vmware.vim25.ObjectContent) PropertySpecBuilder(com.vmware.photon.controller.model.adapters.vsphere.util.builders.PropertySpecBuilder) HashMap(java.util.HashMap) Map(java.util.Map) RetrieveResult(com.vmware.vim25.RetrieveResult) ObjectSpecBuilder(com.vmware.photon.controller.model.adapters.vsphere.util.builders.ObjectSpecBuilder) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 3 with RetrieveOptions

use of com.vmware.vim25.RetrieveOptions in project photon-model by vmware.

the class GetMoRef method inContainerByType.

/**
 * 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 {
    init();
    RetrieveResult rslts = containerViewByType(folder, morefType, retrieveOptions);
    return toMap(rslts);
}
Also used : RetrieveResult(com.vmware.vim25.RetrieveResult)

Example 4 with RetrieveOptions

use of com.vmware.vim25.RetrieveOptions in project photon-model by vmware.

the class GetMoRef method inContainerByType.

/**
 * Returns all the MOREFs of the specified type that are present under the
 * container
 *
 * @param container       {@link ManagedObjectReference} of the container to begin the
 *                        search from
 * @param morefType       Type of the managed entity that needs to be searched
 * @param morefProperties Array of properties to be fetched for the moref
 * @return Map of MOREF and Map of name value pair of properties requested of
 *         the managed objects present. If none exist then empty Map is
 *         returned
 * @throws InvalidPropertyFaultMsg
 * @throws RuntimeFaultFaultMsg
 */
public Map<ManagedObjectReference, Map<String, Object>> inContainerByType(ManagedObjectReference container, String morefType, String[] morefProperties, RetrieveOptions retrieveOptions) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
    List<ObjectContent> oCont = containerViewByType(container, morefType, retrieveOptions, morefProperties).getObjects();
    Map<ManagedObjectReference, Map<String, Object>> tgtMoref = new HashMap<ManagedObjectReference, Map<String, Object>>();
    if (oCont != null) {
        for (ObjectContent oc : oCont) {
            Map<String, Object> propMap = new HashMap<String, Object>();
            List<DynamicProperty> dps = oc.getPropSet();
            if (dps != null) {
                for (DynamicProperty dp : dps) {
                    propMap.put(dp.getName(), dp.getVal());
                }
            }
            tgtMoref.put(oc.getObj(), propMap);
        }
    }
    return tgtMoref;
}
Also used : ObjectContent(com.vmware.vim25.ObjectContent) DynamicProperty(com.vmware.vim25.DynamicProperty) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 5 with RetrieveOptions

use of com.vmware.vim25.RetrieveOptions in project photon-model by vmware.

the class GetMoRef method vmByVMname.

/**
 * Get the MOR of the Virtual Machine by its name.
 *
 * @param vmName           The name of the Virtual Machine
 * @param propCollectorRef
 * @return The Managed Object reference for this VM
 * @throws RuntimeFaultFaultMsg
 * @throws InvalidPropertyFaultMsg
 */
public ManagedObjectReference vmByVMname(final String vmName, final ManagedObjectReference propCollectorRef) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
    init();
    ManagedObjectReference rootFolder = this.serviceContent.getRootFolder();
    TraversalSpec tSpec = getVMTraversalSpec();
    // Create Property Spec
    PropertySpec propertySpec = new PropertySpecBuilder().all(Boolean.FALSE).pathSet("name").type("VirtualMachine");
    // Now create Object Spec
    ObjectSpec objectSpec = new ObjectSpecBuilder().obj(rootFolder).skip(Boolean.TRUE).selectSet(tSpec);
    // Create PropertyFilterSpec using the PropertySpec and ObjectPec
    // created above.
    PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpecBuilder().propSet(propertySpec).objectSet(objectSpec);
    List<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(1);
    listpfs.add(propertyFilterSpec);
    RetrieveOptions options = new RetrieveOptions();
    // Query returns a fixed number of results
    // if token is returned we can get more results
    RetrieveResult retrieveResult = this.vimPort.retrievePropertiesEx(propCollectorRef, listpfs, options);
    String token = null;
    do {
        token = (retrieveResult != null) ? retrieveResult.getToken() : null;
        List<ObjectContent> listobcont = (retrieveResult != null) ? retrieveResult.getObjects() : null;
        if (listobcont != null) {
            for (ObjectContent oc : listobcont) {
                ManagedObjectReference mr = oc.getObj();
                String vmnm = null;
                List<DynamicProperty> dps = oc.getPropSet();
                if (dps != null) {
                    for (DynamicProperty dp : dps) {
                        vmnm = (String) dp.getVal();
                        if (vmName.equals(vmnm)) {
                            return mr;
                        }
                    }
                }
            }
        }
        if (token != null) {
            retrieveResult = this.vimPort.continueRetrievePropertiesEx(propCollectorRef, token);
        }
    } while (token != null);
    return null;
}
Also used : PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) DynamicProperty(com.vmware.vim25.DynamicProperty) PropertyFilterSpecBuilder(com.vmware.photon.controller.model.adapters.vsphere.util.builders.PropertyFilterSpecBuilder) ArrayList(java.util.ArrayList) RetrieveOptions(com.vmware.vim25.RetrieveOptions) ObjectContent(com.vmware.vim25.ObjectContent) ObjectSpec(com.vmware.vim25.ObjectSpec) PropertySpec(com.vmware.vim25.PropertySpec) TraversalSpec(com.vmware.vim25.TraversalSpec) PropertySpecBuilder(com.vmware.photon.controller.model.adapters.vsphere.util.builders.PropertySpecBuilder) RetrieveResult(com.vmware.vim25.RetrieveResult) ObjectSpecBuilder(com.vmware.photon.controller.model.adapters.vsphere.util.builders.ObjectSpecBuilder) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Aggregations

DynamicProperty (com.vmware.vim25.DynamicProperty)4 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)4 ObjectContent (com.vmware.vim25.ObjectContent)4 PropertyFilterSpec (com.vmware.vim25.PropertyFilterSpec)4 RetrieveResult (com.vmware.vim25.RetrieveResult)4 HashMap (java.util.HashMap)4 ObjectSpecBuilder (com.vmware.photon.controller.model.adapters.vsphere.util.builders.ObjectSpecBuilder)3 PropertyFilterSpecBuilder (com.vmware.photon.controller.model.adapters.vsphere.util.builders.PropertyFilterSpecBuilder)3 PropertySpecBuilder (com.vmware.photon.controller.model.adapters.vsphere.util.builders.PropertySpecBuilder)3 RetrieveOptions (com.vmware.vim25.RetrieveOptions)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ObjectSpec (com.vmware.vim25.ObjectSpec)1 PropertySpec (com.vmware.vim25.PropertySpec)1 TraversalSpec (com.vmware.vim25.TraversalSpec)1