Search in sources :

Example 96 with Task

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

the class VmService method deleteVM.

/**
 * Method used to connect to specified data center and delete the 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> deleteVM(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().destroyTask(vmMor);
            return new ResponseHelper(connectionResources, task).getResultsMap("Success: The [" + vmInputs.getVirtualMachineName() + "] VM was deleted. The taskId is: " + task.getValue(), "Failure: The [" + vmInputs.getVirtualMachineName() + "] VM could not be deleted.");
        } 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 97 with Task

use of com.vmware.vim25.mo.Task 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 98 with Task

use of com.vmware.vim25.mo.Task 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 99 with Task

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

the class VmService method cloneVM.

/**
 * Method used to connect to data center and clone 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 virtual machine that will be
 *                   cloned
 * @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> cloneVM(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) {
            VmUtils utils = new VmUtils();
            ManagedObjectReference folder = utils.getMorFolder(vmInputs.getFolderName(), connectionResources);
            ManagedObjectReference resourcePool = utils.getMorResourcePool(vmInputs.getCloneResourcePool(), connectionResources);
            ManagedObjectReference host = utils.getMorHost(vmInputs.getCloneHost(), connectionResources, vmMor);
            ManagedObjectReference dataStore = utils.getMorDataStore(vmInputs.getCloneDataStore(), connectionResources, vmMor, vmInputs);
            VirtualMachineRelocateSpec vmRelocateSpec = utils.getVirtualMachineRelocateSpec(resourcePool, host, dataStore, vmInputs);
            VirtualMachineCloneSpec cloneSpec = new VmConfigSpecs().getCloneSpec(vmInputs, vmRelocateSpec);
            ManagedObjectReference taskMor = connectionResources.getVimPortType().cloneVMTask(vmMor, folder, vmInputs.getCloneName(), cloneSpec);
            return new ResponseHelper(connectionResources, taskMor).getResultsMap("Success: The [" + vmInputs.getVirtualMachineName() + "] VM was successfully cloned. The taskId is: " + taskMor.getValue(), "Failure: The [" + vmInputs.getVirtualMachineName() + "] VM could not be cloned.");
        } 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 : VirtualMachineRelocateSpec(com.vmware.vim25.VirtualMachineRelocateSpec) VirtualMachineCloneSpec(com.vmware.vim25.VirtualMachineCloneSpec) 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) VmConfigSpecs(io.cloudslang.content.vmware.services.utils.VmConfigSpecs) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 100 with Task

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

the class ResponseHelper method getTaskResultAfterDone.

protected boolean getTaskResultAfterDone(ConnectionResources connectionResources, ManagedObjectReference task) throws InvalidPropertyFaultMsg, RuntimeFaultFaultMsg, InvalidCollectorVersionFaultMsg {
    WaitForValues waitForValues = new WaitForValues(connectionResources.getConnection());
    Object[] result = waitForValues.wait(task, new String[] { ManagedObjectType.INFO_STATE.getValue(), ManagedObjectType.INFO_ERROR.getValue() }, new String[] { ManagedObjectType.STATE.getValue() }, new Object[][] { new Object[] { TaskInfoState.SUCCESS, TaskInfoState.ERROR } });
    if (result[1] instanceof LocalizedMethodFault) {
        throw new RuntimeException(((LocalizedMethodFault) result[1]).getLocalizedMessage());
    }
    return result[0].equals(TaskInfoState.SUCCESS);
}
Also used : LocalizedMethodFault(com.vmware.vim25.LocalizedMethodFault) WaitForValues(io.cloudslang.content.vmware.connection.helpers.WaitForValues)

Aggregations

ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)59 Task (com.vmware.vim25.mo.Task)28 TaskInfo (com.vmware.vim25.TaskInfo)23 ArrayList (java.util.ArrayList)21 RemoteException (java.rmi.RemoteException)19 QueryTask (com.vmware.xenon.services.common.QueryTask)14 Operation (com.vmware.xenon.common.Operation)13 List (java.util.List)13 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)11 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)10 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)10 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)10 ComputeDescriptionService (com.vmware.photon.controller.model.resources.ComputeDescriptionService)9 DiskService (com.vmware.photon.controller.model.resources.DiskService)9 PhotonModelUriUtils (com.vmware.photon.controller.model.util.PhotonModelUriUtils)9 TaskInfoState (com.vmware.vim25.TaskInfoState)9 ComputeService (com.vmware.photon.controller.model.resources.ComputeService)8 RuntimeFaultFaultMsg (com.vmware.vim25.RuntimeFaultFaultMsg)8 InvalidPropertyFaultMsg (com.vmware.vim25.InvalidPropertyFaultMsg)7 VirtualMachine (com.vmware.vim25.mo.VirtualMachine)7