Search in sources :

Example 11 with VirtualMachine

use of com.vmware.vim25.mo.VirtualMachine in project CloudStack-archive by CloudStack-extras.

the class HostMO method getVmPropertiesOnHyperHost.

public ObjectContent[] getVmPropertiesOnHyperHost(String[] propertyPaths) throws Exception {
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - retrieveProperties() for VM properties. target MOR: " + _mor.get_value() + ", properties: " + new Gson().toJson(propertyPaths));
    PropertySpec pSpec = new PropertySpec();
    pSpec.setType("VirtualMachine");
    pSpec.setPathSet(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.setSelectSet(new SelectionSpec[] { host2VmTraversal });
    PropertyFilterSpec pfSpec = new PropertyFilterSpec();
    pfSpec.setPropSet(new PropertySpec[] { pSpec });
    pfSpec.setObjectSet(new ObjectSpec[] { oSpec });
    ObjectContent[] properties = _context.getService().retrieveProperties(_context.getServiceContent().getPropertyCollector(), new PropertyFilterSpec[] { pfSpec });
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - retrieveProperties() done");
    return properties;
}
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) Gson(com.google.gson.Gson)

Example 12 with VirtualMachine

use of com.vmware.vim25.mo.VirtualMachine in project cloudstack by apache.

the class VmwareResource method getVmStates.

private HashMap<String, PowerState> getVmStates() throws Exception {
    VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
    int key = ((HostMO) hyperHost).getCustomFieldKey("VirtualMachine", CustomFieldConstants.CLOUD_VM_INTERNAL_NAME);
    if (key == 0) {
        s_logger.warn("Custom field " + CustomFieldConstants.CLOUD_VM_INTERNAL_NAME + " is not registered ?!");
    }
    String instanceNameCustomField = "value[" + key + "]";
    // CLOUD_VM_INTERNAL_NAME stores the internal CS generated vm name. This was earlier stored in name. Now, name can be either the hostname or
    // the internal CS name, but the custom field CLOUD_VM_INTERNAL_NAME always stores the internal CS name.
    ObjectContent[] ocs = hyperHost.getVmPropertiesOnHyperHost(new String[] { "name", "runtime.powerState", "config.template", instanceNameCustomField });
    HashMap<String, PowerState> newStates = new HashMap<String, PowerState>();
    if (ocs != null && ocs.length > 0) {
        for (ObjectContent oc : ocs) {
            List<DynamicProperty> objProps = oc.getPropSet();
            if (objProps != null) {
                boolean isTemplate = false;
                String name = null;
                String VMInternalCSName = null;
                VirtualMachinePowerState powerState = VirtualMachinePowerState.POWERED_OFF;
                for (DynamicProperty objProp : objProps) {
                    if (objProp.getName().equals("config.template")) {
                        if (objProp.getVal().toString().equalsIgnoreCase("true")) {
                            isTemplate = true;
                        }
                    } else if (objProp.getName().equals("runtime.powerState")) {
                        powerState = (VirtualMachinePowerState) objProp.getVal();
                    } else if (objProp.getName().equals("name")) {
                        name = (String) objProp.getVal();
                    } else if (objProp.getName().contains(instanceNameCustomField)) {
                        if (objProp.getVal() != null)
                            VMInternalCSName = ((CustomFieldStringValue) objProp.getVal()).getValue();
                    } else {
                        assert (false);
                    }
                }
                if (VMInternalCSName != null)
                    name = VMInternalCSName;
                if (!isTemplate) {
                    newStates.put(name, convertPowerState(powerState));
                }
            }
        }
    }
    return newStates;
}
Also used : DynamicProperty(com.vmware.vim25.DynamicProperty) HostMO(com.cloud.hypervisor.vmware.mo.HostMO) HashMap(java.util.HashMap) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) ObjectContent(com.vmware.vim25.ObjectContent) VirtualMachinePowerState(com.vmware.vim25.VirtualMachinePowerState) PowerState(com.cloud.vm.VirtualMachine.PowerState) VirtualMachinePowerState(com.vmware.vim25.VirtualMachinePowerState)

Example 13 with VirtualMachine

use of com.vmware.vim25.mo.VirtualMachine in project cloudstack by apache.

the class DatacenterMO method findVmByNameAndLabel.

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 out inner loop
                                break;
                            }
                        } else if (prop.getVal() instanceof CustomFieldStringValue) {
                            String val = ((CustomFieldStringValue) prop.getVal()).getValue();
                            if (val.equals(vmLabel)) {
                                list.add(new VirtualMachineMO(_context, oc.getObj()));
                                // break out inner loop
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
    return list;
}
Also used : ObjectContent(com.vmware.vim25.ObjectContent) DynamicProperty(com.vmware.vim25.DynamicProperty) ArrayList(java.util.ArrayList) CustomFieldStringValue(com.vmware.vim25.CustomFieldStringValue)

Example 14 with VirtualMachine

use of com.vmware.vim25.mo.VirtualMachine in project cloudstack by apache.

the class DatacenterMO method checkIfVmAlreadyExistsInVcenter.

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

Example 15 with VirtualMachine

use of com.vmware.vim25.mo.VirtualMachine in project cloudstack by apache.

the class ClusterMO method getRestartPriorityForVM.

private String getRestartPriorityForVM(VirtualMachineMO vmMo) throws Exception {
    if (vmMo == null) {
        s_logger.debug("Failed to get restart priority for VM, invalid VM object reference");
        return null;
    }
    ManagedObjectReference vmMor = vmMo.getMor();
    if (vmMor == null || !vmMor.getType().equals("VirtualMachine")) {
        s_logger.debug("Failed to get restart priority for VM: " + vmMo.getName() + ", invalid VM object reference");
        return null;
    }
    ClusterConfigInfoEx configInfo = getClusterConfigInfo();
    if (configInfo == null) {
        s_logger.debug("Failed to get restart priority for VM: " + vmMo.getName() + ", no cluster config information");
        return null;
    }
    List<ClusterDasVmConfigInfo> dasVmConfig = configInfo.getDasVmConfig();
    for (int dasVmConfigIndex = 0; dasVmConfigIndex < dasVmConfig.size(); dasVmConfigIndex++) {
        ClusterDasVmConfigInfo dasVmConfigInfo = dasVmConfig.get(dasVmConfigIndex);
        if (dasVmConfigInfo != null && dasVmConfigInfo.getKey().getValue().equals(vmMor.getValue())) {
            DasVmPriority dasVmPriority = dasVmConfigInfo.getRestartPriority();
            if (dasVmPriority != null) {
                return dasVmPriority.value();
            } else {
                //VM uses cluster restart priority when DasVmPriority for the VM is null.
                return ClusterDasVmSettingsRestartPriority.CLUSTER_RESTART_PRIORITY.value();
            }
        }
    }
    s_logger.debug("VM: " + vmMo.getName() + " uses default restart priority in the cluster: " + getName());
    return null;
}
Also used : ClusterConfigInfoEx(com.vmware.vim25.ClusterConfigInfoEx) ClusterDasVmConfigInfo(com.vmware.vim25.ClusterDasVmConfigInfo) DasVmPriority(com.vmware.vim25.DasVmPriority) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Aggregations

ObjectContent (com.vmware.vim25.ObjectContent)22 DynamicProperty (com.vmware.vim25.DynamicProperty)16 ObjectSpec (com.vmware.vim25.ObjectSpec)15 PropertyFilterSpec (com.vmware.vim25.PropertyFilterSpec)15 PropertySpec (com.vmware.vim25.PropertySpec)15 ArrayList (java.util.ArrayList)14 VirtualMachine (com.vmware.vim25.mo.VirtualMachine)13 TraversalSpec (com.vmware.vim25.TraversalSpec)12 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)7 RemoteException (java.rmi.RemoteException)7 ManagedEntity (com.vmware.vim25.mo.ManagedEntity)6 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)5 CustomFieldStringValue (com.vmware.vim25.CustomFieldStringValue)5 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)4 Gson (com.google.gson.Gson)4 InventoryNavigator (com.vmware.vim25.mo.InventoryNavigator)4 HashMap (java.util.HashMap)4 PerfCounterInfo (com.vmware.vim25.PerfCounterInfo)3 VirtualMachinePowerState (com.vmware.vim25.VirtualMachinePowerState)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3