Java Code Examples for com.vmware.vim25.PropertySpec#setAll()

The following examples show how to use com.vmware.vim25.PropertySpec#setAll() . 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: VmwareClient.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
private List<ObjectContent> retrieveMoRefProperties(ManagedObjectReference mObj, List<String> props) throws Exception {
    PropertySpec pSpec = new PropertySpec();
    pSpec.setAll(false);
    pSpec.setType(mObj.getType());
    pSpec.getPathSet().addAll(props);

    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(mObj);
    oSpec.setSkip(false);
    PropertyFilterSpec spec = new PropertyFilterSpec();
    spec.getPropSet().add(pSpec);
    spec.getObjectSet().add(oSpec);
    List<PropertyFilterSpec> specArr = new ArrayList<PropertyFilterSpec>();
    specArr.add(spec);

    return vimPort.retrieveProperties(getPropCol(), specArr);
}
 
Example 2
Source File: VmwareClient.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public boolean validate() {
    //
    // There is no official API to validate an open vCenter API session. This is hacking way to tell if
    // an open vCenter API session is still valid for making calls.
    //
    // It will give false result if there really does not exist data-center in the inventory, however, I consider
    // this really is not possible in production deployment
    //

    // Create PropertySpecs
    PropertySpec pSpec = new PropertySpec();
    pSpec.setType("Datacenter");
    pSpec.setAll(false);
    pSpec.getPathSet().add("name");

    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(getRootFolder());
    oSpec.setSkip(false);
    oSpec.getSelectSet().addAll(constructCompleteTraversalSpec());

    PropertyFilterSpec spec = new PropertyFilterSpec();
    spec.getPropSet().add(pSpec);
    spec.getObjectSet().add(oSpec);
    List<PropertyFilterSpec> specArr = new ArrayList<PropertyFilterSpec>();
    specArr.add(spec);

    try {
        List<ObjectContent> ocary = vimPort.retrieveProperties(getPropCol(), specArr);
        if (ocary != null && ocary.size() > 0)
            return true;
    } catch (Exception e) {
        return false;
    }

    return false;
}
 
Example 3
Source File: VimUtil.java    From vsphere-automation-sdk-java with MIT License 4 votes vote down vote up
/**
 * Retrieves the list of hosts of the given cluster.
 *
 * @param vimPort
 *            vimPort
 * @param serviceContent
 *            serviceContent
 * @param cluster
 *            cluster
 * @return the list of hosts of the clusters
 * @throws InvalidPropertyFaultMsg
 * @throws RuntimeFaultFaultMsg
 */
public static List<ManagedObjectReference> getHosts(VimPortType vimPort,
        ServiceContent serviceContent, ManagedObjectReference cluster)
        throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
    PropertySpec hostPropSpec = new PropertySpec();
    hostPropSpec.setType("HostSystem");
    hostPropSpec.setAll(false);
    hostPropSpec.getPathSet().addAll(Collections.<String>emptyList());

    TraversalSpec hostTSpec = new TraversalSpec();
    hostTSpec.setType("ComputeResource");
    hostTSpec.setPath("host");
    hostTSpec.setName("hosts");

    final SelectionSpec selectionSpec = new SelectionSpec();
    selectionSpec.setName(hostTSpec.getName());

    hostTSpec.getSelectSet().add(selectionSpec);

    List<ObjectSpec> ospecList = new ArrayList<>();
    ObjectSpec ospec = new ObjectSpec();
    ospec.setObj(cluster);
    ospec.setSkip(true);
    ospec.getSelectSet().addAll(Arrays.asList(hostTSpec));
    ospecList.add(ospec);

    PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
    propertyFilterSpec.getPropSet().addAll(Arrays.asList(hostPropSpec));
    propertyFilterSpec.getObjectSet().addAll(ospecList);

    List<PropertyFilterSpec> listpfs = new ArrayList<>(1);
    listpfs.add(propertyFilterSpec);
    List<ObjectContent> listObjContent = VimUtil
            .retrievePropertiesAllObjects(vimPort,
                    serviceContent.getPropertyCollector(), listpfs);

    List<ManagedObjectReference> hosts = new ArrayList<>();

    if (listObjContent != null) {
        for (ObjectContent oc : listObjContent) {
            hosts.add(oc.getObj());
        }
    }
    return hosts;
}
 
Example 4
Source File: VimUtil.java    From vsphere-automation-sdk-java with MIT License 4 votes vote down vote up
/**
 * Retrieves the vm managed object reference for the specified vm name using
 * the vim port type.
 *
 * @param vimPortType
 * @param serviceContent
 * @param vmname
 * @return
 * @throws NotFoundFaultMsg
 * @throws RuntimeFaultFaultMsg
 * @throws InvalidPropertyFaultMsg
 */
public static ManagedObjectReference getVM(VimPortType vimPortType,
        ServiceContent serviceContent, String vmname)
        throws NotFoundFaultMsg, InvalidPropertyFaultMsg,
        RuntimeFaultFaultMsg {
    ManagedObjectReference propCollectorRef = serviceContent
            .getPropertyCollector();
    ManagedObjectReference rootFolderRef = serviceContent.getRootFolder();

    ManagedObjectReference retVmRef = null;
    TraversalSpec tSpec = getVMTraversalSpec();

    PropertySpec propertySpec = new PropertySpec();
    propertySpec.setAll(Boolean.FALSE);
    propertySpec.getPathSet().add("name");
    propertySpec.setType("VirtualMachine");

    ObjectSpec objectSpec = new ObjectSpec();
    objectSpec.setObj(rootFolderRef);
    objectSpec.setSkip(Boolean.TRUE);
    objectSpec.getSelectSet().add(tSpec);

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

    List<PropertyFilterSpec> listpfs = new ArrayList<PropertyFilterSpec>(1);
    listpfs.add(propertyFilterSpec);
    List<ObjectContent> listobjcont = retrievePropertiesAllObjects(
            vimPortType, propCollectorRef, listpfs);

    if (listobjcont != null) {
        for (ObjectContent oc : listobjcont) {
            ManagedObjectReference mr = oc.getObj();
            String vmnm = null;
            List<DynamicProperty> dps = oc.getPropSet();
            if (dps != null) {
                for (DynamicProperty dp : dps) {
                    vmnm = (String) dp.getVal();
                }
            }
            if (vmnm != null && vmnm.equals(vmname)) {
                retVmRef = mr;
                break;
            }
        }
    }
    if (retVmRef == null) {
        throw new NotFoundFaultMsg("VM Not Found - " + vmname,
                new NotFound());
    }
    return retVmRef;
}
 
Example 5
Source File: VimUtil.java    From vsphere-automation-sdk-java with MIT License 4 votes vote down vote up
/**
 * Getting the MOREF of the entity.
 */
public static ManagedObjectReference getEntityByName(
        VimPortType vimPortType, ServiceContent serviceContent,
        String entityName, String entityType) {
    ManagedObjectReference propCollectorRef = serviceContent
            .getPropertyCollector();
    ManagedObjectReference rootFolderRef = serviceContent.getRootFolder();
    ManagedObjectReference retVal = null;

    try {
        // Create Property Spec
        PropertySpec propertySpec = new PropertySpec();
        propertySpec.setAll(Boolean.FALSE);
        propertySpec.setType(entityType);
        propertySpec.getPathSet().add("name");

        // Now create Object Spec
        ObjectSpec objectSpec = new ObjectSpec();
        objectSpec.setObj(rootFolderRef);
        objectSpec.setSkip(Boolean.TRUE);
        objectSpec.getSelectSet().addAll(Arrays.asList(buildTraversal()));

        // Create PropertyFilterSpec using the PropertySpec and ObjectPec
        // created above.
        PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
        propertyFilterSpec.getPropSet().add(propertySpec);
        propertyFilterSpec.getObjectSet().add(objectSpec);

        List<PropertyFilterSpec> listpfs =
                new ArrayList<PropertyFilterSpec>(1);
        listpfs.add(propertyFilterSpec);
        List<ObjectContent> listobjcont = retrievePropertiesAllObjects(
                vimPortType, propCollectorRef, listpfs);
        if (listobjcont != null) {
            for (ObjectContent oc : listobjcont) {
                if (oc.getPropSet().get(0).getVal().equals(entityName)) {
                    retVal = oc.getObj();
                    break;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return retVal;
}
 
Example 6
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 7
Source File: VmwareClient.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
/**
 * Get the ManagedObjectReference for an item under the
 * specified root folder that has the type and name specified.
 *
 * @param root a root folder if available, or null for default
 * @param type type of the managed object
 * @param name name to match
 *
 * @return First ManagedObjectReference of the type / name pair found
 */
public ManagedObjectReference getDecendentMoRef(ManagedObjectReference root, String type, String name) throws Exception {
    if (name == null || name.length() == 0) {
        return null;
    }

    try {
        // Create PropertySpecs
        PropertySpec pSpec = new PropertySpec();
        pSpec.setType(type);
        pSpec.setAll(false);
        pSpec.getPathSet().add("name");

        ObjectSpec oSpec = new ObjectSpec();
        oSpec.setObj(root);
        oSpec.setSkip(false);
        oSpec.getSelectSet().addAll(constructCompleteTraversalSpec());

        PropertyFilterSpec spec = new PropertyFilterSpec();
        spec.getPropSet().add(pSpec);
        spec.getObjectSet().add(oSpec);
        List<PropertyFilterSpec> specArr = new ArrayList<PropertyFilterSpec>();
        specArr.add(spec);

        ManagedObjectReference propCollector = getPropCol();
        List<ObjectContent> ocary = vimPort.retrieveProperties(propCollector, specArr);

        if (ocary == null || ocary.size() == 0) {
            return null;
        }

        // filter through retrieved objects to get the first match.
        for (ObjectContent oc : ocary) {
            ManagedObjectReference mor = oc.getObj();
            List<DynamicProperty> propary = oc.getPropSet();
            if (type == null || type.equals(mor.getType())) {
                if (propary.size() > 0) {
                    String propval = (String)propary.get(0).getVal();
                    if (propval != null && name.equalsIgnoreCase(propval))
                        return mor;
                }
            }
        }
    } catch (InvalidPropertyFaultMsg invalidPropertyException) {
        s_logger.debug("Failed to get Vmware ManagedObjectReference for name: " + name + " and type: " + type + " due to " + invalidPropertyException.getMessage());
        throw invalidPropertyException;
    } catch (RuntimeFaultFaultMsg runtimeFaultException) {
        s_logger.debug("Failed to get Vmware ManagedObjectReference for name: " + name + " and type: " + type + " due to " + runtimeFaultException.getMessage());
        throw runtimeFaultException;
    }

    return null;
}
 
Example 8
Source File: VMwareUtil.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
public static Map<String, ManagedObjectReference> getVms(VMwareConnection connection) throws Exception {
    Map<String, ManagedObjectReference> nameToVm = new HashMap<>();

    ManagedObjectReference rootFolder = connection.getServiceContent().getRootFolder();

    TraversalSpec tSpec = getVMTraversalSpec();

    PropertySpec propertySpec = new PropertySpec();

    propertySpec.setAll(Boolean.FALSE);
    propertySpec.getPathSet().add("name");
    propertySpec.setType("VirtualMachine");

    ObjectSpec objectSpec = new ObjectSpec();

    objectSpec.setObj(rootFolder);
    objectSpec.setSkip(Boolean.TRUE);
    objectSpec.getSelectSet().add(tSpec);

    PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();

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

    List<PropertyFilterSpec> lstPfs = new ArrayList<>(1);

    lstPfs.add(propertyFilterSpec);

    VimPortType vimPortType = connection.getVimPortType();
    ManagedObjectReference propertyCollector = connection.getServiceContent().getPropertyCollector();

    List<ObjectContent> lstObjectContent = retrievePropertiesAllObjects(lstPfs, vimPortType, propertyCollector);

    if (lstObjectContent != null) {
        for (ObjectContent oc : lstObjectContent) {
            ManagedObjectReference mor = oc.getObj();
            List<DynamicProperty> dps = oc.getPropSet();
            String vmName = null;

            if (dps != null) {
                for (DynamicProperty dp : dps) {
                    vmName = (String)dp.getVal();
                }
            }

            if (vmName != null) {
                nameToVm.put(vmName, mor);
            }
        }
    }

    return nameToVm;
}
 
Example 9
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;
}