Search in sources :

Example 16 with MorObjectHandler

use of io.cloudslang.content.vmware.services.helpers.MorObjectHandler in project cs-actions by CloudSlang.

the class VmService method getOsDescriptors.

/**
 * Method used to connect to data center to retrieve a list with all the guest operating system descriptors
 * supported by the host system.
 *
 * @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 host system
 * @param delimiter  String that represents the delimiter needed in the result list
 * @return Map with String as key and value that contains returnCode of the operation, a list that contains all the
 *         guest operating system descriptors supported by the host system or failure message and the exception if there is one
 * @throws Exception
 */
public Map<String, String> getOsDescriptors(HttpInputs httpInputs, VmInputs vmInputs, String delimiter) throws Exception {
    ConnectionResources connectionResources = new ConnectionResources(httpInputs, vmInputs);
    try {
        ManagedObjectReference environmentBrowserMor = new MorObjectHandler().getEnvironmentBrowser(connectionResources, ManagedObjectType.ENVIRONMENT_BROWSER.getValue());
        VirtualMachineConfigOption configOptions = connectionResources.getVimPortType().queryConfigOption(environmentBrowserMor, null, connectionResources.getHostMor());
        List<GuestOsDescriptor> guestOSDescriptors = configOptions.getGuestOSDescriptor();
        return ResponseUtils.getResultsMap(ResponseUtils.getResponseStringFromCollection(guestOSDescriptors, delimiter), Outputs.RETURN_CODE_SUCCESS);
    } catch (Exception ex) {
        return ResponseUtils.getResultsMap(ex.toString(), Outputs.RETURN_CODE_FAILURE);
    } finally {
        if (httpInputs.isCloseSession()) {
            connectionResources.getConnection().disconnect();
            clearConnectionFromContext(httpInputs.getGlobalSessionObject());
        }
    }
}
Also used : VirtualMachineConfigOption(com.vmware.vim25.VirtualMachineConfigOption) GuestOsDescriptor(com.vmware.vim25.GuestOsDescriptor) ConnectionResources(io.cloudslang.content.vmware.connection.ConnectionResources) MorObjectHandler(io.cloudslang.content.vmware.services.helpers.MorObjectHandler) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 17 with MorObjectHandler

use of io.cloudslang.content.vmware.services.helpers.MorObjectHandler 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)

Example 18 with MorObjectHandler

use of io.cloudslang.content.vmware.services.helpers.MorObjectHandler in project cs-actions by CloudSlang.

the class VmService method updateVM.

/**
 * Method used to connect to data center to update existing devices 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 device
 * @return Map with String as key and value that contains returnCode of the operation, success message with task id
 *         of the execution or failure message and the exception if there is one
 * @throws Exception
 */
public Map<String, String> updateVM(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());
        if (vmMor != null) {
            VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
            String device = Device.getValue(vmInputs.getDevice()).toLowerCase();
            if (Constants.CPU.equalsIgnoreCase(device) || Constants.MEMORY.equalsIgnoreCase(device)) {
                vmConfigSpec = new VmUtils().getUpdateConfigSpec(vmInputs, vmConfigSpec, device);
            } else {
                vmConfigSpec = new VmUtils().getAddOrRemoveSpecs(connectionResources, vmMor, vmInputs, vmConfigSpec, device);
            }
            ManagedObjectReference task = connectionResources.getVimPortType().reconfigVMTask(vmMor, vmConfigSpec);
            return new ResponseHelper(connectionResources, task).getResultsMap("Success: The [" + vmInputs.getVirtualMachineName() + "] VM was successfully reconfigured. The taskId is: " + task.getValue(), "Failure: The [" + vmInputs.getVirtualMachineName() + "] VM could not be reconfigured.");
        } else {
            return ResponseUtils.getVmNotFoundResultsMap(vmInputs);
        }
    } catch (Exception ex) {
        return ResponseUtils.getResultsMap(ex.toString(), Outputs.RETURN_CODE_FAILURE);
    } finally {
        if (httpInputs.isCloseSession()) {
            connectionResources.getConnection().disconnect();
            clearConnectionFromContext(httpInputs.getGlobalSessionObject());
        }
    }
}
Also used : VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VmUtils(io.cloudslang.content.vmware.services.utils.VmUtils) ConnectionResources(io.cloudslang.content.vmware.connection.ConnectionResources) ResponseHelper(io.cloudslang.content.vmware.services.helpers.ResponseHelper) MorObjectHandler(io.cloudslang.content.vmware.services.helpers.MorObjectHandler) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 19 with MorObjectHandler

use of io.cloudslang.content.vmware.services.helpers.MorObjectHandler in project cs-actions by CloudSlang.

the class VmUtils method getMorFolder.

public ManagedObjectReference getMorFolder(String folderName, ConnectionResources connectionResources) throws Exception {
    if (StringUtils.contains(folderName, "/")) {
        return getSpecificMorFolder(connectionResources, folderName);
    } else {
        // perform a search based on name
        ManagedObjectReference folder;
        if (isNotBlank(folderName)) {
            ManagedObjectReference morRootFolder = connectionResources.getMorRootFolder();
            folder = new MorObjectHandler().getSpecificMor(connectionResources, morRootFolder, FOLDER.getValue(), escapeSpecialCharacters(folderName));
            if (folder == null) {
                throw new RuntimeException(ErrorMessages.FOLDER_NOT_FOUND);
            }
        } else {
            folder = connectionResources.getVmFolderMor();
        }
        return folder;
    }
}
Also used : MorObjectHandler(io.cloudslang.content.vmware.services.helpers.MorObjectHandler) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 20 with MorObjectHandler

use of io.cloudslang.content.vmware.services.helpers.MorObjectHandler in project cs-actions by CloudSlang.

the class ClusterComputeResourceService method createAffinityRule.

public Map<String, String> createAffinityRule(HttpInputs httpInputs, VmInputs vmInputs, String affineHostGroupName, String antiAffineHostGroupName) throws Exception {
    ConnectionResources connectionResources = new ConnectionResources(httpInputs);
    try {
        ManagedObjectReference clusterMor = new MorObjectHandler().getSpecificMor(connectionResources, connectionResources.getMorRootFolder(), ClusterParameter.CLUSTER_COMPUTE_RESOURCE.getValue(), vmInputs.getClusterName());
        if (ruleExists(getClusterConfiguration(connectionResources, clusterMor, vmInputs.getClusterName()), vmInputs.getRuleName())) {
            throw new Exception(String.format(RULE_ALREADY_EXISTS, vmInputs.getRuleName()));
        } else {
            ClusterVmHostRuleInfo clusterVmHostRuleInfo = getClusterVmHostRuleInfo(getClusterConfiguration(connectionResources, clusterMor, vmInputs.getClusterName()), vmInputs, affineHostGroupName, antiAffineHostGroupName);
            ClusterRuleSpec clusterRuleSpec = new ClusterRuleSpec();
            clusterRuleSpec.setInfo(clusterVmHostRuleInfo);
            clusterRuleSpec.setOperation(ArrayUpdateOperation.ADD);
            return reconfigureClusterRule(vmInputs, connectionResources, clusterMor, clusterRuleSpec);
        }
    } finally {
        if (httpInputs.isCloseSession()) {
            connectionResources.getConnection().disconnect();
            clearConnectionFromContext(httpInputs.getGlobalSessionObject());
        }
    }
}
Also used : ConnectionResources(io.cloudslang.content.vmware.connection.ConnectionResources) MorObjectHandler(io.cloudslang.content.vmware.services.helpers.MorObjectHandler)

Aggregations

MorObjectHandler (io.cloudslang.content.vmware.services.helpers.MorObjectHandler)27 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)17 ConnectionResources (io.cloudslang.content.vmware.connection.ConnectionResources)17 ResponseHelper (io.cloudslang.content.vmware.services.helpers.ResponseHelper)7 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)6 VmUtils (io.cloudslang.content.vmware.services.utils.VmUtils)3 OvfNetworkMapping (com.vmware.vim25.OvfNetworkMapping)2 ArrayList (java.util.ArrayList)2 ConfigTarget (com.vmware.vim25.ConfigTarget)1 CustomizationSpec (com.vmware.vim25.CustomizationSpec)1 DatastoreSummary (com.vmware.vim25.DatastoreSummary)1 DynamicProperty (com.vmware.vim25.DynamicProperty)1 GuestOsDescriptor (com.vmware.vim25.GuestOsDescriptor)1 KeyValue (com.vmware.vim25.KeyValue)1 ObjectContent (com.vmware.vim25.ObjectContent)1 OvfCreateImportSpecResult (com.vmware.vim25.OvfCreateImportSpecResult)1 VirtualMachineCloneSpec (com.vmware.vim25.VirtualMachineCloneSpec)1 VirtualMachineConfigOption (com.vmware.vim25.VirtualMachineConfigOption)1 VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)1 VirtualMachineConfigSummary (com.vmware.vim25.VirtualMachineConfigSummary)1