Search in sources :

Example 11 with DynamicProperty

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

the class VmwareClient method getDecendentMoRef.

/**
     * 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;
}
Also used : PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) DynamicProperty(com.vmware.vim25.DynamicProperty) ArrayList(java.util.ArrayList) RuntimeFaultFaultMsg(com.vmware.vim25.RuntimeFaultFaultMsg) ObjectContent(com.vmware.vim25.ObjectContent) ObjectSpec(com.vmware.vim25.ObjectSpec) PropertySpec(com.vmware.vim25.PropertySpec) InvalidPropertyFaultMsg(com.vmware.vim25.InvalidPropertyFaultMsg) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 12 with DynamicProperty

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

the class DatacenterMO method getDvSwitchMor.

public ManagedObjectReference getDvSwitchMor(ManagedObjectReference dvPortGroupMor) throws Exception {
    String dvPortGroupKey = null;
    ManagedObjectReference dvSwitchMor = null;
    PropertySpec pSpec = new PropertySpec();
    pSpec.setType("DistributedVirtualPortgroup");
    pSpec.getPathSet().add("key");
    pSpec.getPathSet().add("config.distributedVirtualSwitch");
    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("key")) {
                        dvPortGroupKey = (String) prop.getVal();
                    } else {
                        dvSwitchMor = (ManagedObjectReference) prop.getVal();
                    }
                }
                if ((dvPortGroupKey != null) && dvPortGroupKey.equals(dvPortGroupMor.getValue())) {
                    return dvSwitchMor;
                }
            }
        }
    }
    return null;
}
Also used : PropertyFilterSpec(com.vmware.vim25.PropertyFilterSpec) ObjectContent(com.vmware.vim25.ObjectContent) ObjectSpec(com.vmware.vim25.ObjectSpec) PropertySpec(com.vmware.vim25.PropertySpec) DynamicProperty(com.vmware.vim25.DynamicProperty) TraversalSpec(com.vmware.vim25.TraversalSpec) ArrayList(java.util.ArrayList) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 13 with DynamicProperty

use of com.vmware.vim25.DynamicProperty 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 DynamicProperty

use of com.vmware.vim25.DynamicProperty 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 DynamicProperty

use of com.vmware.vim25.DynamicProperty 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)

Aggregations

DynamicProperty (com.vmware.vim25.DynamicProperty)34 ObjectContent (com.vmware.vim25.ObjectContent)34 ObjectSpec (com.vmware.vim25.ObjectSpec)16 PropertyFilterSpec (com.vmware.vim25.PropertyFilterSpec)16 PropertySpec (com.vmware.vim25.PropertySpec)16 ArrayList (java.util.ArrayList)14 TraversalSpec (com.vmware.vim25.TraversalSpec)12 CustomFieldStringValue (com.vmware.vim25.CustomFieldStringValue)9 HashMap (java.util.HashMap)7 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)5 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)4 VmwareHypervisorHost (com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost)4 OptionValue (com.vmware.vim25.OptionValue)4 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)2 Pair (com.cloud.utils.Pair)2 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)2 DVPortgroupConfigInfo (com.vmware.vim25.DVPortgroupConfigInfo)2 VirtualMachineFileInfo (com.vmware.vim25.VirtualMachineFileInfo)2 VirtualMachinePowerState (com.vmware.vim25.VirtualMachinePowerState)2 HostVmStateReportEntry (com.cloud.agent.api.HostVmStateReportEntry)1