Search in sources :

Example 6 with ResponseHelper

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

the class VmService method powerOffVM.

/**
 * Method used to connect to specified data center and power-off 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, success message with task id
 *         of the execution or failure message and the exception if there is one
 * @throws Exception
 */
public Map<String, String> powerOffVM(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) {
            ManagedObjectReference task = connectionResources.getVimPortType().powerOffVMTask(vmMor);
            return new ResponseHelper(connectionResources, task).getResultsMap("Success: The [" + vmInputs.getVirtualMachineName() + "] VM was successfully powered off. The taskId is: " + task.getValue(), "Failure: The [" + vmInputs.getVirtualMachineName() + "] VM could not be powered off.");
        } 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 : 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 7 with ResponseHelper

use of io.cloudslang.content.vmware.services.helpers.ResponseHelper 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 8 with ResponseHelper

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

the class GuestService method customizeVM.

/**
 * Method used to connect to specified data center and customize the windows OS based 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
 * @param guestInputs Object that has all specific inputs necessary to customize specified virtual machine
 * @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> customizeVM(HttpInputs httpInputs, VmInputs vmInputs, GuestInputs guestInputs, boolean isWin) throws Exception {
    ConnectionResources connectionResources = new ConnectionResources(httpInputs, vmInputs);
    try {
        ManagedObjectReference vmMor = new MorObjectHandler().getMor(connectionResources, ManagedObjectType.VIRTUAL_MACHINE.getValue(), vmInputs.getVirtualMachineName());
        if (vmMor != null) {
            CustomizationSpec customizationSpec = isWin ? new GuestConfigSpecs().getWinCustomizationSpec(guestInputs) : new GuestConfigSpecs().getLinuxCustomizationSpec(guestInputs);
            connectionResources.getVimPortType().checkCustomizationSpec(vmMor, customizationSpec);
            ManagedObjectReference task = connectionResources.getVimPortType().customizeVMTask(vmMor, customizationSpec);
            return new ResponseHelper(connectionResources, task).getResultsMap("Success: The [" + vmInputs.getVirtualMachineName() + "] VM was successfully customized. The taskId is: " + task.getValue(), "Failure: The [" + vmInputs.getVirtualMachineName() + "] VM could not be customized.");
        } 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 : CustomizationSpec(com.vmware.vim25.CustomizationSpec) 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) GuestConfigSpecs(io.cloudslang.content.vmware.services.utils.GuestConfigSpecs)

Aggregations

ConnectionResources (io.cloudslang.content.vmware.connection.ConnectionResources)8 ResponseHelper (io.cloudslang.content.vmware.services.helpers.ResponseHelper)8 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)7 MorObjectHandler (io.cloudslang.content.vmware.services.helpers.MorObjectHandler)7 VmUtils (io.cloudslang.content.vmware.services.utils.VmUtils)3 VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)2 VmConfigSpecs (io.cloudslang.content.vmware.services.utils.VmConfigSpecs)2 CustomizationSpec (com.vmware.vim25.CustomizationSpec)1 VirtualMachineCloneSpec (com.vmware.vim25.VirtualMachineCloneSpec)1 VirtualMachineRelocateSpec (com.vmware.vim25.VirtualMachineRelocateSpec)1 GuestConfigSpecs (io.cloudslang.content.vmware.services.utils.GuestConfigSpecs)1