Java Code Examples for com.vmware.vim25.DynamicProperty#getVal()

The following examples show how to use com.vmware.vim25.DynamicProperty#getVal() . 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: VMwareDatacenterInventory.java    From development with Apache License 2.0 6 votes vote down vote up
/**
 * Adds a host instance to the inventory based on given properties.
 *
 * @return the created host instance
 */
public VMwareHost addHostSystem(List<DynamicProperty> properties) {

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

    VMwareHost result = new VMwareHost(this);
    for (DynamicProperty dp : properties) {
        String key = dp.getName();
        if ("name".equals(key) && dp.getVal() != null) {
            result.setName(dp.getVal().toString());
        } else if ("summary.hardware.memorySize".equals(key)
                && dp.getVal() != null) {
            result.setMemorySizeMB(VMwareValue
                    .fromBytes(Long.parseLong(dp.getVal().toString()))
                    .getValue(Unit.MB));
        } else if ("summary.hardware.numCpuCores".equals(key)
                && dp.getVal() != null) {
            result.setCpuCores(Integer.parseInt(dp.getVal().toString()));
        }
    }
    hostsSystems.put(result.getName(), result);
    return result;
}
 
Example 2
Source File: VmUtils.java    From cs-actions with Apache License 2.0 6 votes vote down vote up
public ManagedObjectReference getMorHost(String hostname, ConnectionResources connectionResources,
                                         ManagedObjectReference vmMor) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
    ManagedObjectReference host = null;
    if (isNotBlank(hostname)) {
        host = getManagedObjectReference(hostname, connectionResources,
                ManagedObjectType.HOST_SYSTEM.getValue(), ErrorMessages.HOST_NOT_FOUND);
    } else if (StringUtils.isBlank(hostname) && vmMor != null) {
        ObjectContent[] objectContents = GetObjectProperties.getObjectProperties(connectionResources, vmMor,
                new String[]{ManagedObjectType.SUMMARY.getValue()});

        for (ObjectContent objectItem : objectContents) {
            List<DynamicProperty> vmProperties = objectItem.getPropSet();
            for (DynamicProperty propertyItem : vmProperties) {
                VirtualMachineSummary virtualMachineSummary = (VirtualMachineSummary) propertyItem.getVal();
                host = virtualMachineSummary.getRuntime().getHost();
                break;
            }
            break;
        }
    } else {
        host = connectionResources.getHostMor();
    }
    return host;
}
 
Example 3
Source File: HypervisorHostHelper.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
public static VirtualMachineMO findVmFromObjectContent(VmwareContext context, ObjectContent[] ocs, String name, String instanceNameCustomField) {

        if (ocs != null && ocs.length > 0) {
            for (ObjectContent oc : ocs) {
                String vmNameInvCenter = null;
                String vmInternalCSName = null;
                List<DynamicProperty> objProps = oc.getPropSet();
                if (objProps != null) {
                    for (DynamicProperty objProp : objProps) {
                        if (objProp.getName().equals("name")) {
                            vmNameInvCenter = (String)objProp.getVal();
                        } else if (objProp.getName().contains(instanceNameCustomField)) {
                            if (objProp.getVal() != null)
                                vmInternalCSName = ((CustomFieldStringValue)objProp.getVal()).getValue();
                        }

                        if ((vmNameInvCenter != null && name.equalsIgnoreCase(vmNameInvCenter)) || (vmInternalCSName != null && name.equalsIgnoreCase(vmInternalCSName))) {
                            VirtualMachineMO vmMo = new VirtualMachineMO(context, oc.getObj());
                            return vmMo;
                        }
                    }
                }
            }
        }
        return null;
    }
 
Example 4
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 5
Source File: VMwareDatacenterInventory.java    From development with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a storage instance to the inventory based on given properties.
 *
 * @return the created storage instance
 */
public VMwareStorage addStorage(String host,
        List<DynamicProperty> properties) {

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

    VMwareStorage result = new VMwareStorage();
    for (DynamicProperty dp : properties) {
        String key = dp.getName();
        if ("summary.name".equals(key) && dp.getVal() != null) {
            result.setName(dp.getVal().toString());
        } else if ("summary.capacity".equals(key) && dp.getVal() != null) {
            result.setCapacity(VMwareValue
                    .fromBytes(Long.parseLong(dp.getVal().toString())));
        } else if ("summary.freeSpace".equals(key) && dp.getVal() != null) {
            result.setFreeStorage(VMwareValue
                    .fromBytes(Long.parseLong(dp.getVal().toString())));
        }
    }
    storages.put(result.getName(), result);

    if (storageByHost.containsKey(host)) {
        storageByHost.get(host).add(result);
    } else {
        List<VMwareStorage> storage = new ArrayList<VMwareStorage>();
        storage.add(result);
        storageByHost.put(host, storage);
    }
    return result;
}
 
Example 6
Source File: ClusterMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
@Override
public ManagedObjectReference findDatastore(String poolUuid) throws Exception {

    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - findDatastore(). target MOR: " + _mor.getValue() + ", poolUuid: " + poolUuid);

    CustomFieldsManagerMO cfmMo = new CustomFieldsManagerMO(_context, _context.getServiceContent().getCustomFieldsManager());
    int key = cfmMo.getCustomFieldKey("Datastore", CustomFieldConstants.CLOUD_UUID);
    assert (key != 0);

    ObjectContent[] ocs = getDatastorePropertiesOnHyperHost(new String[] {"name", String.format("value[%d]", key)});
    if (ocs != null) {
        for (ObjectContent oc : ocs) {
            if (oc.getPropSet().get(0).getVal().equals(poolUuid))
                return oc.getObj();

            if (oc.getPropSet().size() > 1) {
                DynamicProperty prop = oc.getPropSet().get(1);
                if (prop != null && prop.getVal() != null) {
                    if (prop.getVal() instanceof CustomFieldStringValue) {
                        String val = ((CustomFieldStringValue)prop.getVal()).getValue();
                        if (val.equalsIgnoreCase(poolUuid)) {

                            if (s_logger.isTraceEnabled())
                                s_logger.trace("vCenter API trace - findDatastore() done(successfully)");
                            return oc.getObj();
                        }
                    }
                }
            }
        }
    }

    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - findDatastore() done(failed)");
    return null;
}
 
Example 7
Source File: VirtualMachineMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public VirtualMachineFileLayoutEx getFileLayout() throws Exception {
    VirtualMachineFileLayoutEx fileLayout = null;
    PropertySpec pSpec = new PropertySpec();
    pSpec.setType("VirtualMachine");
    pSpec.getPathSet().add("layoutEx");

    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);

    if (ocs != null) {
        for (ObjectContent oc : ocs) {
            List<DynamicProperty> props = oc.getPropSet();
            if (props != null) {
                for (DynamicProperty prop : props) {
                    if (prop.getName().equals("layoutEx")) {
                        fileLayout = (VirtualMachineFileLayoutEx)prop.getVal();
                        break;
                    }
                }
            }
        }
    }

    return fileLayout;
}
 
Example 8
Source File: DatacenterMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public VirtualMachineMO checkIfVmAlreadyExistsInVcenter(String vmNameOnVcenter, String vmNameInCS) throws Exception {
    int key = getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_VM_INTERNAL_NAME);
    if (key == 0) {
        s_logger.warn("Custom field " + CustomFieldConstants.CLOUD_VM_INTERNAL_NAME + " is not registered ?!");
    }

    List<ObjectContent> ocs = getVmPropertiesOnDatacenterVmFolder(new String[] {"name", String.format("value[%d]", key)});
    if (ocs != null && ocs.size() > 0) {
        for (ObjectContent oc : ocs) {
            List<DynamicProperty> props = oc.getPropSet();
            if (props != null) {
                String vmVcenterName = null;
                String vmInternalCSName = null;
                for (DynamicProperty prop : props) {
                    if (prop.getName().equals("name")) {
                        vmVcenterName = prop.getVal().toString();
                    }
                    if (prop.getName().startsWith("value[") && prop.getVal() != null) {
                        vmInternalCSName = ((CustomFieldStringValue)prop.getVal()).getValue();
                    }
                }
                if (vmNameOnVcenter.equals(vmVcenterName)) {
                    if (vmInternalCSName != null && !vmInternalCSName.isEmpty() && !vmNameInCS.equals(vmInternalCSName)) {
                        return (new VirtualMachineMO(_context, oc.getObj()));
                    }
                }
            }
        }
    }
    return null;
}
 
Example 9
Source File: DatacenterMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public List<VirtualMachineMO> findVmByNameAndLabel(String vmLabel) throws Exception {
    CustomFieldsManagerMO cfmMo = new CustomFieldsManagerMO(_context, _context.getServiceContent().getCustomFieldsManager());
    int key = cfmMo.getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_UUID);
    assert (key != 0);

    List<VirtualMachineMO> list = new ArrayList<VirtualMachineMO>();

    List<ObjectContent> ocs = getVmPropertiesOnDatacenterVmFolder(new String[] {"name", String.format("value[%d]", key)});
    if (ocs != null && ocs.size() > 0) {
        for (ObjectContent oc : ocs) {
            List<DynamicProperty> props = oc.getPropSet();
            if (props != null) {
                for (DynamicProperty prop : props) {
                    if (prop.getVal() != null) {
                        if (prop.getName().equalsIgnoreCase("name")) {
                            if (prop.getVal().toString().equals(vmLabel)) {
                                list.add(new VirtualMachineMO(_context, oc.getObj()));
                                break;        // break out inner loop
                            }
                        } else if (prop.getVal() instanceof CustomFieldStringValue) {
                            String val = ((CustomFieldStringValue)prop.getVal()).getValue();
                            if (val.equals(vmLabel)) {
                                list.add(new VirtualMachineMO(_context, oc.getObj()));
                                break;        // break out inner loop
                            }
                        }
                    }
                }
            }
        }
    }
    return list;
}
 
Example 10
Source File: VmwareHelper.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public static Object getPropValue(ObjectContent oc, String name) {
    List<DynamicProperty> props = oc.getPropSet();

    for (DynamicProperty prop : props) {
        if (prop.getName().equalsIgnoreCase(name))
            return prop.getVal();
    }

    return null;
}
 
Example 11
Source File: VmUtils.java    From cs-actions with Apache License 2.0 5 votes vote down vote up
public ManagedObjectReference getMorDataStore(String dataStoreName, ConnectionResources connectionResources,
                                              ManagedObjectReference vmMor, VmInputs vmInputs)
        throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg {
    ManagedObjectReference dataStore = null;
    if (isNotBlank(dataStoreName)) {
        ManagedObjectReference cloneHostMor = getMorHost(vmInputs.getCloneHost(), connectionResources, vmMor);
        ConfigTarget configTarget = getHostConfigTarget(connectionResources, cloneHostMor);
        List<VirtualMachineDatastoreInfo> dataStoreInfoList = configTarget.getDatastore();
        for (VirtualMachineDatastoreInfo dataStoreInfo : dataStoreInfoList) {
            if (vmInputs.getCloneDataStore().equals(dataStoreInfo.getDatastore().getName())) {
                dataStore = getDataStoreRef(vmInputs.getCloneDataStore(), dataStoreInfoList);
                break;
            }
        }

        if (dataStore == null) {
            throw new RuntimeException(ErrorMessages.DATA_STORE_NOT_FOUND);
        }
    } else {
        ObjectContent[] objectContents = GetObjectProperties.getObjectProperties(connectionResources, vmMor,
                new String[]{ManagedObjectType.SUMMARY.getValue()});

        for (ObjectContent objectItem : objectContents) {
            List<DynamicProperty> vmProperties = objectItem.getPropSet();
            for (DynamicProperty propertyItem : vmProperties) {
                VirtualMachineSummary virtualMachineSummary = (VirtualMachineSummary) propertyItem.getVal();
                String vmPathName = virtualMachineSummary.getConfig().getVmPathName();
                dataStoreName = vmPathName.substring(1, vmPathName.indexOf(Constants.RIGHT_SQUARE_BRACKET));
                dataStore = getDataStore(dataStoreName, connectionResources, vmMor);
                break;
            }
            break;
        }
    }
    return dataStore;
}
 
Example 12
Source File: ClusterMO.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
public HashMap<String, Integer> getVmVncPortsOnCluster() throws Exception {
    ObjectContent[] ocs = getVmPropertiesOnHyperHost(new String[] {"name", "config.extraConfig[\"RemoteDisplay.vnc.port\"]"});

    HashMap<String, Integer> portInfo = new HashMap<String, Integer>();
    if (ocs != null && ocs.length > 0) {
        for (ObjectContent oc : ocs) {
            List<DynamicProperty> objProps = oc.getPropSet();
            if (objProps != null) {
                String name = null;
                String value = null;
                for (DynamicProperty objProp : objProps) {
                    if (objProp.getName().equals("name")) {
                        name = (String)objProp.getVal();
                    } else {
                        OptionValue optValue = (OptionValue)objProp.getVal();
                        value = (String)optValue.getValue();
                    }
                }

                if (name != null && value != null) {
                    portInfo.put(name, Integer.parseInt(value));
                }
            }
        }
    }

    return portInfo;
}
 
Example 13
Source File: VmwareClient.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
/**
 * Get the property value of a managed object.
 *
 * @param mor
 *            managed object reference
 * @param propertyName
 *            property name.
 * @return property value.
 * @throws Exception
 *             in case of error.
 */
@SuppressWarnings("unchecked")
public <T> T getDynamicProperty(ManagedObjectReference mor, String propertyName) throws Exception {
    List<String> props = new ArrayList<String>();
    props.add(propertyName);
    List<ObjectContent> objContent = retrieveMoRefProperties(mor, props);

    Object propertyValue = null;
    if (objContent != null && objContent.size() > 0) {
        List<DynamicProperty> dynamicProperty = objContent.get(0).getPropSet();
        if (dynamicProperty != null && dynamicProperty.size() > 0) {
            DynamicProperty dp = dynamicProperty.get(0);
            propertyValue = dp.getVal();
            /*
             * If object is ArrayOfXXX object, then get the XXX[] by
             * invoking getXXX() on the object.
             * For Ex:
             * ArrayOfManagedObjectReference.getManagedObjectReference()
             * returns ManagedObjectReference[] array.
             */
            Class dpCls = propertyValue.getClass();
            String dynamicPropertyName = dpCls.getName();
            if (dynamicPropertyName.indexOf("ArrayOf") != -1) {
                String methodName = "get" + dynamicPropertyName.substring(dynamicPropertyName.indexOf("ArrayOf") + "ArrayOf".length(), dynamicPropertyName.length());

                Method getMorMethod = dpCls.getDeclaredMethod(methodName, null);
                propertyValue = getMorMethod.invoke(propertyValue, (Object[])null);
            }
        }
    }
    return (T)propertyValue;
}
 
Example 14
Source File: HostMO.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
private void loadVmCache() throws Exception {
    if (s_logger.isDebugEnabled())
        s_logger.debug("load VM cache on host");

    _vmCache.clear();

    int key = getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_VM_INTERNAL_NAME);
    if (key == 0) {
        s_logger.warn("Custom field " + CustomFieldConstants.CLOUD_VM_INTERNAL_NAME + " is not registered ?!");
    }

    // name is the name of the VM as it appears in vCenter. The CLOUD_VM_INTERNAL_NAME custom
    // field value contains the name of the VM as it is maintained internally by cloudstack (i-x-y).
    ObjectContent[] ocs = getVmPropertiesOnHyperHost(new String[] {"name", "value[" + key + "]"});
    if (ocs != null && ocs.length > 0) {
        for (ObjectContent oc : ocs) {
            List<DynamicProperty> props = oc.getPropSet();
            if (props != null) {
                String vmVcenterName = null;
                String vmInternalCSName = null;
                for (DynamicProperty prop : props) {
                    if (prop.getName().equals("name")) {
                        vmVcenterName = prop.getVal().toString();
                    } else if (prop.getName().startsWith("value[")) {
                        if (prop.getVal() != null)
                            vmInternalCSName = ((CustomFieldStringValue)prop.getVal()).getValue();
                    }
                }
                String vmName = null;
                if (vmInternalCSName != null && isUserVMInternalCSName(vmInternalCSName)) {
                    vmName = vmInternalCSName;
                } else {
                    vmName = vmVcenterName;
                }

                if (s_logger.isTraceEnabled())
                    s_logger.trace("put " + vmName + " into host cache");

                _vmCache.put(vmName, new VirtualMachineMO(_context, oc.getObj()));
            }
        }
    }
}
 
Example 15
Source File: HostMO.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
public HashMap<String, Integer> getVmVncPortsOnHost() throws Exception {

        int key = getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_VM_INTERNAL_NAME);
        if (key == 0) {
            s_logger.warn("Custom field " + CustomFieldConstants.CLOUD_VM_INTERNAL_NAME + " is not registered ?!");
        }

        ObjectContent[] ocs = getVmPropertiesOnHyperHost(new String[] {"name", "config.extraConfig[\"RemoteDisplay.vnc.port\"]", "value[" + key + "]"});

        HashMap<String, Integer> portInfo = new HashMap<String, Integer>();
        if (ocs != null && ocs.length > 0) {
            for (ObjectContent oc : ocs) {
                List<DynamicProperty> objProps = oc.getPropSet();
                if (objProps != null) {
                    String vmName = null;
                    String value = null;
                    String vmInternalCSName = null;
                    for (DynamicProperty objProp : objProps) {
                        if (objProp.getName().equals("name")) {
                            vmName = (String)objProp.getVal();
                        } else if (objProp.getName().startsWith("value[")) {
                            if (objProp.getVal() != null)
                                vmInternalCSName = ((CustomFieldStringValue)objProp.getVal()).getValue();
                        } else {
                            OptionValue optValue = (OptionValue)objProp.getVal();
                            value = (String)optValue.getValue();
                        }
                    }

                    if (vmInternalCSName != null && isUserVMInternalCSName(vmInternalCSName))
                        vmName = vmInternalCSName;

                    if (vmName != null && value != null) {
                        portInfo.put(vmName, Integer.parseInt(value));
                    }
                }
            }
        }

        return portInfo;
    }
 
Example 16
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 17
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 18
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 19
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 20
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;
}