Search in sources :

Example 1 with PropertySpecBuilder

use of com.vmware.photon.controller.model.adapters.vsphere.util.builders.PropertySpecBuilder 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 2 with PropertySpecBuilder

use of com.vmware.photon.controller.model.adapters.vsphere.util.builders.PropertySpecBuilder in project photon-model by vmware.

the class GetMoRef method propertyFilterSpecs.

public PropertyFilterSpec[] propertyFilterSpecs(ManagedObjectReference container, String morefType, String... morefProperties) throws RuntimeFaultFaultMsg {
    init();
    ManagedObjectReference viewManager = this.serviceContent.getViewManager();
    ManagedObjectReference containerView = this.vimPort.createContainerView(viewManager, container, Arrays.asList(morefType), true);
    return new PropertyFilterSpec[] { new PropertyFilterSpecBuilder().propSet(new PropertySpecBuilder().all(Boolean.FALSE).type(morefType).pathSet(morefProperties)).objectSet(new ObjectSpecBuilder().obj(containerView).skip(Boolean.TRUE).selectSet(new TraversalSpecBuilder().name("view").path("view").skip(false).type("ContainerView"))) };
}
Also used : PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) PropertyFilterSpecBuilder(com.vmware.photon.controller.model.adapters.vsphere.util.builders.PropertyFilterSpecBuilder) PropertySpecBuilder(com.vmware.photon.controller.model.adapters.vsphere.util.builders.PropertySpecBuilder) TraversalSpecBuilder(com.vmware.photon.controller.model.adapters.vsphere.util.builders.TraversalSpecBuilder) ObjectSpecBuilder(com.vmware.photon.controller.model.adapters.vsphere.util.builders.ObjectSpecBuilder) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 3 with PropertySpecBuilder

use of com.vmware.photon.controller.model.adapters.vsphere.util.builders.PropertySpecBuilder 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)

Example 4 with PropertySpecBuilder

use of com.vmware.photon.controller.model.adapters.vsphere.util.builders.PropertySpecBuilder in project photon-model by vmware.

the class GetMoRef method entityProps.

/**
 * Method to retrieve properties of a {@link ManagedObjectReference}
 *
 * @param entityMor {@link ManagedObjectReference} of the entity
 * @param props     Array of properties to be looked up
 * @return Map of the property name and its corresponding value
 * @throws InvalidPropertyFaultMsg If a property does not exist
 * @throws RuntimeFaultFaultMsg
 */
public Map<String, Object> entityProps(ManagedObjectReference entityMor, String... props) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
    init();
    final HashMap<String, Object> retVal = new HashMap<>();
    // Create PropertyFilterSpec using the PropertySpec and ObjectPec
    PropertyFilterSpec[] propertyFilterSpecs = { new PropertyFilterSpecBuilder().propSet(// Create Property Spec
    new PropertySpecBuilder().all(Boolean.FALSE).type(entityMor.getType()).pathSet(props)).objectSet(// Now create Object Spec
    new ObjectSpecBuilder().obj(entityMor)) };
    List<ObjectContent> oCont = this.vimPort.retrievePropertiesEx(this.serviceContent.getPropertyCollector(), Arrays.asList(propertyFilterSpecs), new RetrieveOptions()).getObjects();
    if (oCont != null) {
        for (ObjectContent oc : oCont) {
            List<DynamicProperty> dps = oc.getPropSet();
            for (DynamicProperty dp : dps) {
                retVal.put(dp.getName(), dp.getVal());
            }
        }
    }
    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) RetrieveOptions(com.vmware.vim25.RetrieveOptions) ObjectContent(com.vmware.vim25.ObjectContent) PropertySpecBuilder(com.vmware.photon.controller.model.adapters.vsphere.util.builders.PropertySpecBuilder) ObjectSpecBuilder(com.vmware.photon.controller.model.adapters.vsphere.util.builders.ObjectSpecBuilder)

Aggregations

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