Java Code Examples for com.vmware.vim25.ObjectSpec#setSkip()

The following examples show how to use com.vmware.vim25.ObjectSpec#setSkip() . 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: HostDatastoreSystemMO.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public List<ObjectContent> getDatastorePropertiesOnHostDatastoreSystem(String[] propertyPaths) throws Exception {

        PropertySpec pSpec = new PropertySpec();
        pSpec.setType("Datastore");
        pSpec.getPathSet().addAll(Arrays.asList(propertyPaths));

        TraversalSpec hostDsSys2DatastoreTraversal = new TraversalSpec();
        hostDsSys2DatastoreTraversal.setType("HostDatastoreSystem");
        hostDsSys2DatastoreTraversal.setPath("datastore");
        hostDsSys2DatastoreTraversal.setName("hostDsSys2DatastoreTraversal");

        ObjectSpec oSpec = new ObjectSpec();
        oSpec.setObj(_mor);
        oSpec.setSkip(Boolean.TRUE);
        oSpec.getSelectSet().add(hostDsSys2DatastoreTraversal);

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

        return _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);
    }
 
Example 2
Source File: HostMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
@Override
public ObjectContent[] getVmPropertiesOnHyperHost(String[] propertyPaths) throws Exception {
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - retrieveProperties() for VM properties. target MOR: " + _mor.getValue() + ", properties: " +
                new Gson().toJson(propertyPaths));

    PropertySpec pSpec = new PropertySpec();
    pSpec.setType("VirtualMachine");
    pSpec.getPathSet().addAll(Arrays.asList(propertyPaths));

    TraversalSpec host2VmTraversal = new TraversalSpec();
    host2VmTraversal.setType("HostSystem");
    host2VmTraversal.setPath("vm");
    host2VmTraversal.setName("host2VmTraversal");

    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(_mor);
    oSpec.setSkip(Boolean.TRUE);
    oSpec.getSelectSet().add(host2VmTraversal);

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

    List<ObjectContent> properties = _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);

    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - retrieveProperties() done");
    return properties.toArray(new ObjectContent[properties.size()]);
}
 
Example 3
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 4
Source File: DatacenterMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public ManagedObjectReference getDvPortGroupMor(String dvPortGroupName) throws Exception {
    PropertySpec pSpec = new PropertySpec();
    pSpec.setType("DistributedVirtualPortgroup");
    pSpec.getPathSet().add("name");

    TraversalSpec datacenter2DvPortGroupTraversal = new TraversalSpec();
    datacenter2DvPortGroupTraversal.setType("Datacenter");
    datacenter2DvPortGroupTraversal.setPath("network");
    datacenter2DvPortGroupTraversal.setName("datacenter2DvPortgroupTraversal");

    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(_mor);
    oSpec.setSkip(Boolean.TRUE);
    oSpec.getSelectSet().add(datacenter2DvPortGroupTraversal);

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

    List<ObjectContent> ocs = _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);

    if (ocs != null) {
        for (ObjectContent oc : ocs) {
            List<DynamicProperty> props = oc.getPropSet();
            if (props != null) {
                for (DynamicProperty prop : props) {
                    if (prop.getVal().equals(dvPortGroupName))
                        return oc.getObj();
                }
            }
        }
    }
    return null;
}
 
Example 5
Source File: ClusterMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
@Override
public ObjectContent[] getDatastorePropertiesOnHyperHost(String[] propertyPaths) throws Exception {
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - retrieveProperties() on Datastore properties. target MOR: " + _mor.getValue() + ", properties: " + new Gson().toJson(propertyPaths));

    PropertySpec pSpec = new PropertySpec();
    pSpec.setType("Datastore");
    pSpec.getPathSet().addAll(Arrays.asList(propertyPaths));

    TraversalSpec cluster2DatastoreTraversal = new TraversalSpec();
    cluster2DatastoreTraversal.setType("ClusterComputeResource");
    cluster2DatastoreTraversal.setPath("datastore");
    cluster2DatastoreTraversal.setName("cluster2DatastoreTraversal");

    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(_mor);
    oSpec.setSkip(Boolean.TRUE);
    oSpec.getSelectSet().add(cluster2DatastoreTraversal);

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

    List<ObjectContent> properties = _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);

    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - retrieveProperties() done");
    return properties.toArray(new ObjectContent[properties.size()]);
}
 
Example 6
Source File: DatacenterMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public List<ObjectContent> getVmPropertiesOnDatacenterVmFolder(String[] propertyPaths) throws Exception {
    PropertySpec pSpec = new PropertySpec();
    pSpec.setType("VirtualMachine");
    pSpec.getPathSet().addAll(Arrays.asList(propertyPaths));

    TraversalSpec dc2VmFolderTraversal = new TraversalSpec();
    dc2VmFolderTraversal.setType("Datacenter");
    dc2VmFolderTraversal.setPath("vmFolder");
    dc2VmFolderTraversal.setName("dc2VmFolderTraversal");

    SelectionSpec recurseFolders = new SelectionSpec();
    recurseFolders.setName("folder2childEntity");

    TraversalSpec folder2childEntity = new TraversalSpec();
    folder2childEntity.setType("Folder");
    folder2childEntity.setPath("childEntity");
    folder2childEntity.setName(recurseFolders.getName());
    folder2childEntity.getSelectSet().add(recurseFolders);
    dc2VmFolderTraversal.getSelectSet().add(folder2childEntity);

    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(_mor);
    oSpec.setSkip(Boolean.TRUE);
    oSpec.getSelectSet().add(dc2VmFolderTraversal);

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

    return _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);
}
 
Example 7
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public List<DatastoreMO> getAllDatastores() throws Exception {
    PropertySpec pSpec = new PropertySpec();
    pSpec.setType("Datastore");
    pSpec.getPathSet().add("name");

    TraversalSpec vmDatastoreTraversal = new TraversalSpec();
    vmDatastoreTraversal.setType("VirtualMachine");
    vmDatastoreTraversal.setPath("datastore");
    vmDatastoreTraversal.setName("vmDatastoreTraversal");

    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(_mor);
    oSpec.setSkip(Boolean.TRUE);
    oSpec.getSelectSet().add(vmDatastoreTraversal);

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

    List<ObjectContent> ocs = _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);

    List<DatastoreMO> datastores = new ArrayList<DatastoreMO>();
    if (CollectionUtils.isNotEmpty(ocs)) {
        for (ObjectContent oc : ocs) {
            datastores.add(new DatastoreMO(_context, oc.getObj()));
        }
    }
    return datastores;
}
 
Example 8
Source File: ClusterMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public ObjectContent[] getHostPropertiesOnCluster(String[] propertyPaths) throws Exception {
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - retrieveProperties() on Host properties. target MOR: " + _mor.getValue() + ", properties: " + new Gson().toJson(propertyPaths));

    PropertySpec pSpec = new PropertySpec();
    pSpec.setType("HostSystem");
    pSpec.getPathSet().addAll(Arrays.asList(propertyPaths));

    TraversalSpec cluster2HostTraversal = new TraversalSpec();
    cluster2HostTraversal.setType("ClusterComputeResource");
    cluster2HostTraversal.setPath("host");
    cluster2HostTraversal.setName("cluster2HostTraversal");

    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(_mor);
    oSpec.setSkip(Boolean.TRUE);
    oSpec.getSelectSet().add(cluster2HostTraversal);

    PropertyFilterSpec pfSpec = new PropertyFilterSpec();
    pfSpec.getPropSet().add(pSpec);
    pfSpec.getObjectSet().add(oSpec);

    List<PropertyFilterSpec> pfSpecArr = new ArrayList<PropertyFilterSpec>();
    pfSpecArr.add(pfSpec);

    List<ObjectContent> properties = _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);

    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - retrieveProperties() done");
    return properties.toArray(new ObjectContent[properties.size()]);
}
 
Example 9
Source File: HostMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public ManagedObjectReference getNetworkMor(String portGroupName) throws Exception {
    PropertySpec pSpec = new PropertySpec();
    pSpec.setType("Network");
    pSpec.getPathSet().add("summary.name");

    TraversalSpec host2NetworkTraversal = new TraversalSpec();
    host2NetworkTraversal.setType("HostSystem");
    host2NetworkTraversal.setPath("network");
    host2NetworkTraversal.setName("host2NetworkTraversal");

    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(_mor);
    oSpec.setSkip(Boolean.TRUE);
    oSpec.getSelectSet().add(host2NetworkTraversal);

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

    List<ObjectContent> ocs = _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);

    if (ocs != null) {
        for (ObjectContent oc : ocs) {
            List<DynamicProperty> props = oc.getPropSet();
            if (props != null) {
                for (DynamicProperty prop : props) {
                    if (prop.getVal().equals(portGroupName))
                        return oc.getObj();
                }
            }
        }
    }
    return null;
}
 
Example 10
Source File: WaitForValues.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
private PropertyFilterSpec propertyFilterSpec(ManagedObjectReference objmor, String[] filterProps) {
    ObjectSpec objSpec = new ObjectSpec();
    objSpec.setObj(objmor);
    objSpec.setSkip(false);

    PropertySpec propSpec = new PropertySpec();
    propSpec.getPathSet().addAll(Arrays.asList(filterProps));
    propSpec.setType(objmor.getType());

    PropertyFilterSpec spec = new PropertyFilterSpec();
    spec.getObjectSet().add(objSpec);
    spec.getPropSet().add(propSpec);

    return spec;
}
 
Example 11
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 12
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
public List<NetworkDetails> getNetworksWithDetails() throws Exception {
    List<NetworkDetails> networks = new ArrayList<NetworkDetails>();

    int gcTagKey = getCustomFieldKey("Network", CustomFieldConstants.CLOUD_GC);

    if (gcTagKey == 0) {
        gcTagKey = getCustomFieldKey("DistributedVirtualPortgroup", CustomFieldConstants.CLOUD_GC_DVP);
        s_logger.debug("The custom key for dvPortGroup is : " + gcTagKey);
    }

    PropertySpec pSpec = new PropertySpec();
    pSpec.setType("Network");
    pSpec.getPathSet().add("name");
    pSpec.getPathSet().add("vm");
    pSpec.getPathSet().add(String.format("value[%d]", gcTagKey));

    TraversalSpec vm2NetworkTraversal = new TraversalSpec();
    vm2NetworkTraversal.setType("VirtualMachine");
    vm2NetworkTraversal.setPath("network");
    vm2NetworkTraversal.setName("vm2NetworkTraversal");

    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(_mor);
    oSpec.setSkip(Boolean.TRUE);
    oSpec.getSelectSet().add(vm2NetworkTraversal);

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

    List<ObjectContent> ocs = _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);

    if (ocs != null && ocs.size() > 0) {
        for (ObjectContent oc : ocs) {
            ArrayOfManagedObjectReference morVms = null;
            String gcTagValue = null;
            String name = null;

            for (DynamicProperty prop : oc.getPropSet()) {
                if (prop.getName().equals("name"))
                    name = prop.getVal().toString();
                else if (prop.getName().equals("vm"))
                    morVms = (ArrayOfManagedObjectReference)prop.getVal();
                else if (prop.getName().startsWith("value[")) {
                    CustomFieldStringValue val = (CustomFieldStringValue)prop.getVal();
                    if (val != null)
                        gcTagValue = val.getValue();
                }
            }

            NetworkDetails details =
                    new NetworkDetails(name, oc.getObj(), (morVms != null ? morVms.getManagedObjectReference().toArray(
                            new ManagedObjectReference[morVms.getManagedObjectReference().size()]) : null), gcTagValue);

            networks.add(details);
        }
        s_logger.debug("Retrieved " + networks.size() + " networks with key : " + gcTagKey);
    }

    return networks;
}
 
Example 13
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
public String getSnapshotDescriptorDatastorePath() throws Exception {
    PropertySpec pSpec = new PropertySpec();
    pSpec.setType("VirtualMachine");
    pSpec.getPathSet().add("name");
    pSpec.getPathSet().add("config.files");

    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(_mor);
    oSpec.setSkip(Boolean.FALSE);

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

    List<ObjectContent> ocs = _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);
    assert (ocs != null);

    String vmName = null;
    VirtualMachineFileInfo fileInfo = null;

    assert (ocs.size() == 1);
    for (ObjectContent oc : ocs) {
        List<DynamicProperty> props = oc.getPropSet();
        if (props != null) {
            assert (props.size() == 2);

            for (DynamicProperty prop : props) {
                if (prop.getName().equals("name")) {
                    vmName = prop.getVal().toString();
                } else {
                    fileInfo = (VirtualMachineFileInfo)prop.getVal();
                }
            }
        }
    }
    assert (vmName != null);
    assert (fileInfo != null);

    // .vmsd file exists at the same directory of .vmx file
    DatastoreFile vmxFile = new DatastoreFile(fileInfo.getVmPathName());
    return vmxFile.getCompanionPath(vmName + ".vmsd");
}
 
Example 14
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 15
Source File: VMwareUtil.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
private static Object[] waitForValues(VMwareConnection connection, ManagedObjectReference morObj, String[] filterProps,
        String[] endWaitProps, Object[][] expectedVals) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg,
        InvalidCollectorVersionFaultMsg {
    String version = "";
    Object[] endVals = new Object[endWaitProps.length];
    Object[] filterVals = new Object[filterProps.length];

    PropertyFilterSpec spec = new PropertyFilterSpec();

    ObjectSpec oSpec = new ObjectSpec();

    oSpec.setObj(morObj);
    oSpec.setSkip(Boolean.FALSE);

    spec.getObjectSet().add(oSpec);

    PropertySpec pSpec = new PropertySpec();

    pSpec.getPathSet().addAll(Arrays.asList(filterProps));
    pSpec.setType(morObj.getType());

    spec.getPropSet().add(pSpec);

    ManagedObjectReference propertyCollector = connection.getServiceContent().getPropertyCollector();
    ManagedObjectReference filterSpecRef = connection.getVimPortType().createFilter(propertyCollector, spec, true);

    boolean reached = false;

    UpdateSet updateSet;
    List<PropertyFilterUpdate> lstPropertyFilterUpdates;
    List<ObjectUpdate> lstObjectUpdates;
    List<PropertyChange> lstPropertyChanges;

    while (!reached) {
        updateSet = connection.getVimPortType().waitForUpdates(propertyCollector, version);

        if (updateSet == null || updateSet.getFilterSet() == null) {
            continue;
        }

        version = updateSet.getVersion();

        lstPropertyFilterUpdates = updateSet.getFilterSet();

        for (PropertyFilterUpdate propertyFilterUpdate : lstPropertyFilterUpdates) {
            lstObjectUpdates = propertyFilterUpdate.getObjectSet();

            for (ObjectUpdate objUpdate : lstObjectUpdates) {
                if (objUpdate.getKind() == ObjectUpdateKind.MODIFY || objUpdate.getKind() == ObjectUpdateKind.ENTER ||
                        objUpdate.getKind() == ObjectUpdateKind.LEAVE) {
                    lstPropertyChanges = objUpdate.getChangeSet();

                    for (PropertyChange propchg : lstPropertyChanges) {
                        updateValues(endWaitProps, endVals, propchg);
                        updateValues(filterProps, filterVals, propchg);
                    }
                }
            }
        }

        Object expectedValue;

        // Check if the expected values have been reached and exit the loop if done.
        // Also, exit the WaitForUpdates loop if this is the case.
        for (int chgi = 0; chgi < endVals.length && !reached; chgi++) {
            for (int vali = 0; vali < expectedVals[chgi].length && !reached; vali++) {
                expectedValue = expectedVals[chgi][vali];

                reached = expectedValue.equals(endVals[chgi]) || reached;
            }
        }
    }

    // Destroy the filter when we are done.
    connection.getVimPortType().destroyPropertyFilter(filterSpecRef);

    return filterVals;
}
 
Example 16
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 17
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 18
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 19
Source File: DatacenterMO.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
public DVPortgroupConfigInfo getDvPortGroupSpec(String dvPortGroupName) throws Exception {
    DVPortgroupConfigInfo configSpec = null;
    String nameProperty = null;
    PropertySpec pSpec = new PropertySpec();
    pSpec.setType("DistributedVirtualPortgroup");
    pSpec.getPathSet().add("name");
    pSpec.getPathSet().add("config");

    TraversalSpec datacenter2DvPortGroupTraversal = new TraversalSpec();
    datacenter2DvPortGroupTraversal.setType("Datacenter");
    datacenter2DvPortGroupTraversal.setPath("network");
    datacenter2DvPortGroupTraversal.setName("datacenter2DvPortgroupTraversal");

    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(_mor);
    oSpec.setSkip(Boolean.TRUE);
    oSpec.getSelectSet().add(datacenter2DvPortGroupTraversal);

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

    List<ObjectContent> ocs = _context.getService().retrieveProperties(_context.getPropertyCollector(), pfSpecArr);

    if (ocs != null) {
        for (ObjectContent oc : ocs) {
            List<DynamicProperty> props = oc.getPropSet();
            if (props != null) {
                assert (props.size() == 2);
                for (DynamicProperty prop : props) {
                    if (prop.getName().equals("config")) {
                        configSpec = (DVPortgroupConfigInfo)prop.getVal();
                    } else {
                        nameProperty = prop.getVal().toString();
                    }
                }
                if (nameProperty.equalsIgnoreCase(dvPortGroupName)) {
                    return configSpec;
                }
            }
        }
    }
    return null;
}
 
Example 20
Source File: VimUtil.java    From vsphere-automation-sdk-java with MIT License 4 votes vote down vote up
/**
 * Retrieves the cluster managed object reference for the specified cluster
 * name using the vim port type.
 *
 * @param vimPortType
 *
 * @param serviceContent
 *            {@link ServiceContent}
 * @param clusterName
 *            name of the cluster to be searched for.
 * @return {@link ManagedObjectReference}
 * @throws InvalidPropertyFaultMsg
 * @throws RuntimeFaultFaultMsg
 * @throws NotFoundFaultMsg
 */
public static ManagedObjectReference getCluster(VimPortType vimPortType,
        ServiceContent serviceContent, String clusterName)
        throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg,
        NotFoundFaultMsg {

    // Spec to allow recursion on the Folder to Folder traversal
    SelectionSpec folderToFolderSelection = new SelectionSpec();
    folderToFolderSelection.setName("folderToFolder");

    SelectionSpec dcToHostFolderSelection = new SelectionSpec();
    dcToHostFolderSelection.setName("dcToHostFolder");

    // spec to traverse from Datacenter to hostFolder
    TraversalSpec dcToHostFolderTraversal = new TraversalSpec();
    dcToHostFolderTraversal.setName("dcToHostFolder");
    dcToHostFolderTraversal.setPath("hostFolder");
    dcToHostFolderTraversal.setType("Datacenter");
    dcToHostFolderTraversal.getSelectSet().addAll(
            Arrays.asList(new SelectionSpec[] { folderToFolderSelection }));
    dcToHostFolderTraversal.setSkip(false);

    // spec to traverse from Folder to a child folder
    TraversalSpec folderToFolderTraversal = new TraversalSpec();
    folderToFolderTraversal.setName("folderToFolder");
    folderToFolderTraversal.setPath("childEntity");
    folderToFolderTraversal.setType("Folder");
    folderToFolderTraversal.getSelectSet()
            .addAll(Arrays.asList(new SelectionSpec[] {
                folderToFolderSelection, dcToHostFolderSelection }));
    folderToFolderTraversal.setSkip(false);

    PropertySpec propertySpec = new PropertySpec();
    propertySpec.getPathSet()
            .addAll(Arrays.asList(new String[] { "name" }));
    propertySpec.setType("ClusterComputeResource");

    ObjectSpec objectSpec = new ObjectSpec();
    objectSpec.setObj(serviceContent.getRootFolder());
    objectSpec.getSelectSet().addAll(Arrays.asList(new SelectionSpec[] {
        folderToFolderTraversal, dcToHostFolderTraversal }));
    objectSpec.setSkip(false);

    // Create PropertyFilterSpec using the PropertySpec and ObjectPec
    PropertyFilterSpec propertyFilterSpec = new PropertyFilterSpec();
    propertyFilterSpec.getPropSet()
            .addAll(Arrays.asList(new PropertySpec[] { propertySpec }));
    propertyFilterSpec.getObjectSet()
            .addAll(Arrays.asList(new ObjectSpec[] { objectSpec }));

    ManagedObjectReference morPropertyCollector = serviceContent
            .getPropertyCollector();
    List<ObjectContent> objectContents = vimPortType.retrieveProperties(
            morPropertyCollector,
            Arrays.asList(new PropertyFilterSpec[] { propertyFilterSpec }));

    for (ObjectContent objectContent : objectContents) {
        ManagedObjectReference clusterManagedObjectReference = objectContent
                .getObj();
        List<DynamicProperty> dynamicProperties = objectContent
                .getPropSet();
        for (DynamicProperty dynamicProperty : dynamicProperties) {
            if (dynamicProperty.getName().equalsIgnoreCase("name")) {
                if (dynamicProperty.getVal().toString()
                        .equalsIgnoreCase(clusterName)) {
                    return clusterManagedObjectReference;
                }
            }
        }
    }
    throw new NotFoundFaultMsg("Cluster Not Found - " + clusterName,
            new NotFound());
}