Search in sources :

Example 36 with ObjectContent

use of com.vmware.vim25.ObjectContent in project cloudstack by apache.

the class HostMO method loadVmCache.

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()));
            }
        }
    }
}
Also used : ObjectContent(com.vmware.vim25.ObjectContent) DynamicProperty(com.vmware.vim25.DynamicProperty)

Example 37 with ObjectContent

use of com.vmware.vim25.ObjectContent in project cloudstack by apache.

the class HostMO method getDatastorePropertiesOnHyperHost.

@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 host2DatastoreTraversal = new TraversalSpec();
    host2DatastoreTraversal.setType("HostSystem");
    host2DatastoreTraversal.setPath("datastore");
    host2DatastoreTraversal.setName("host2DatastoreTraversal");
    ObjectSpec oSpec = new ObjectSpec();
    oSpec.setObj(_mor);
    oSpec.setSkip(Boolean.TRUE);
    oSpec.getSelectSet().add(host2DatastoreTraversal);
    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()]);
}
Also used : PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) ObjectContent(com.vmware.vim25.ObjectContent) ObjectSpec(com.vmware.vim25.ObjectSpec) PropertySpec(com.vmware.vim25.PropertySpec) TraversalSpec(com.vmware.vim25.TraversalSpec) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson)

Example 38 with ObjectContent

use of com.vmware.vim25.ObjectContent in project cloudstack by apache.

the class HostMO method getVmVncPortsOnHost.

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;
}
Also used : ObjectContent(com.vmware.vim25.ObjectContent) DynamicProperty(com.vmware.vim25.DynamicProperty) HashMap(java.util.HashMap) OptionValue(com.vmware.vim25.OptionValue)

Example 39 with ObjectContent

use of com.vmware.vim25.ObjectContent in project cloudstack by apache.

the class HostMO method getLocalDatastoreOnHost.

public List<Pair<ManagedObjectReference, String>> getLocalDatastoreOnHost() throws Exception {
    List<Pair<ManagedObjectReference, String>> dsList = new ArrayList<Pair<ManagedObjectReference, String>>();
    ObjectContent[] ocs = getDatastorePropertiesOnHyperHost(new String[] { "name", "summary" });
    if (ocs != null) {
        for (ObjectContent oc : ocs) {
            DatastoreSummary dsSummary = (DatastoreSummary) VmwareHelper.getPropValue(oc, "summary");
            if (dsSummary.isMultipleHostAccess() == false && dsSummary.isAccessible() && dsSummary.getType().equalsIgnoreCase("vmfs")) {
                ManagedObjectReference morDs = oc.getObj();
                String name = (String) VmwareHelper.getPropValue(oc, "name");
                if (!name.startsWith("-iqn.") && !name.startsWith("_iqn.")) {
                    dsList.add(new Pair<ManagedObjectReference, String>(morDs, name));
                }
            }
        }
    }
    return dsList;
}
Also used : ObjectContent(com.vmware.vim25.ObjectContent) DatastoreSummary(com.vmware.vim25.DatastoreSummary) ArrayList(java.util.ArrayList) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Pair(com.cloud.utils.Pair)

Example 40 with ObjectContent

use of com.vmware.vim25.ObjectContent in project cloudstack by apache.

the class HypervisorHostHelper method findVmFromObjectContent.

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;
}
Also used : ObjectContent(com.vmware.vim25.ObjectContent) DynamicProperty(com.vmware.vim25.DynamicProperty)

Aggregations

ObjectContent (com.vmware.vim25.ObjectContent)65 ObjectSpec (com.vmware.vim25.ObjectSpec)44 PropertyFilterSpec (com.vmware.vim25.PropertyFilterSpec)44 PropertySpec (com.vmware.vim25.PropertySpec)44 TraversalSpec (com.vmware.vim25.TraversalSpec)38 ArrayList (java.util.ArrayList)38 DynamicProperty (com.vmware.vim25.DynamicProperty)34 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)15 Pair (com.cloud.utils.Pair)12 Gson (com.google.gson.Gson)10 CustomFieldStringValue (com.vmware.vim25.CustomFieldStringValue)9 SelectionSpec (com.vmware.vim25.SelectionSpec)9 HashMap (java.util.HashMap)7 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)5 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)4 OptionValue (com.vmware.vim25.OptionValue)4 VmwareManager (com.cloud.hypervisor.vmware.manager.VmwareManager)2 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)2 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)2 DVPortgroupConfigInfo (com.vmware.vim25.DVPortgroupConfigInfo)2