Search in sources :

Example 1 with VirtualMachineSummary

use of com.vmware.vim25.VirtualMachineSummary in project cs-actions by CloudSlang.

the class VmUtils method getMorDataStore.

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

Example 2 with VirtualMachineSummary

use of com.vmware.vim25.VirtualMachineSummary in project cs-actions by CloudSlang.

the class VmService method getVMDetails.

/**
 * Method used to connect to data center to retrieve details of a virtual machine identified by the inputs provided.
 *
 * @param httpInputs Object that has all the inputs necessary to made a connection to data center
 * @param vmInputs   Object that has all the specific inputs necessary to identify the targeted virtual machine
 * @return Map with String as key and value that contains returnCode of the operation, a JSON formatted string that
 *         contains details of the virtual machine or failure message and the exception if there is one
 * @throws Exception
 */
public Map<String, String> getVMDetails(HttpInputs httpInputs, VmInputs vmInputs) throws Exception {
    ConnectionResources connectionResources = new ConnectionResources(httpInputs, vmInputs);
    try {
        ManagedObjectReference vmMor = new MorObjectHandler().getMor(connectionResources, ManagedObjectType.VIRTUAL_MACHINE.getValue(), vmInputs.getVirtualMachineName());
        ObjectContent[] objectContents = GetObjectProperties.getObjectProperties(connectionResources, vmMor, new String[] { ManagedObjectType.SUMMARY.getValue() });
        if (objectContents != null) {
            Map<String, String> vmDetails = new HashMap<>();
            for (ObjectContent objectItem : objectContents) {
                List<DynamicProperty> vmProperties = objectItem.getPropSet();
                for (DynamicProperty propertyItem : vmProperties) {
                    VirtualMachineSummary virtualMachineSummary = (VirtualMachineSummary) propertyItem.getVal();
                    VirtualMachineConfigSummary virtualMachineConfigSummary = virtualMachineSummary.getConfig();
                    ResponseUtils.addDataToVmDetailsMap(vmDetails, virtualMachineSummary, virtualMachineConfigSummary);
                }
            }
            String responseJson = ResponseUtils.getJsonString(vmDetails);
            return ResponseUtils.getResultsMap(responseJson, Outputs.RETURN_CODE_SUCCESS);
        } else {
            return ResponseUtils.getResultsMap("Could not retrieve the details for: [" + vmInputs.getVirtualMachineName() + "] VM.", Outputs.RETURN_CODE_FAILURE);
        }
    } catch (Exception ex) {
        return ResponseUtils.getResultsMap(ex.toString(), Outputs.RETURN_CODE_FAILURE);
    } finally {
        if (httpInputs.isCloseSession()) {
            connectionResources.getConnection().disconnect();
            clearConnectionFromContext(httpInputs.getGlobalSessionObject());
        }
    }
}
Also used : DynamicProperty(com.vmware.vim25.DynamicProperty) VirtualMachineSummary(com.vmware.vim25.VirtualMachineSummary) HashMap(java.util.HashMap) ConnectionResources(io.cloudslang.content.vmware.connection.ConnectionResources) ObjectContent(com.vmware.vim25.ObjectContent) VirtualMachineConfigSummary(com.vmware.vim25.VirtualMachineConfigSummary) MorObjectHandler(io.cloudslang.content.vmware.services.helpers.MorObjectHandler) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Aggregations

DynamicProperty (com.vmware.vim25.DynamicProperty)2 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)2 ObjectContent (com.vmware.vim25.ObjectContent)2 VirtualMachineSummary (com.vmware.vim25.VirtualMachineSummary)2 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)1 ConfigTarget (com.vmware.vim25.ConfigTarget)1 VirtualMachineConfigSummary (com.vmware.vim25.VirtualMachineConfigSummary)1 VirtualMachineDatastoreInfo (com.vmware.vim25.VirtualMachineDatastoreInfo)1 ConnectionResources (io.cloudslang.content.vmware.connection.ConnectionResources)1 MorObjectHandler (io.cloudslang.content.vmware.services.helpers.MorObjectHandler)1 HashMap (java.util.HashMap)1