Search in sources :

Example 86 with Task

use of com.vmware.vim25.mo.Task in project vsphere-cloud-plugin by jenkinsci.

the class VSphere method renameVm.

/**
 * Renames the VM vSphere
 * @param oldName the current name of the vm
 * @param newName the new name of the vm
 * @throws VSphereException If an error occurred.
 */
public void renameVm(String oldName, String newName) throws VSphereException {
    try {
        VirtualMachine vm = getVmByName(oldName);
        if (vm == null) {
            throw new VSphereNotFoundException("VM", oldName);
        }
        final Task task = vm.rename_Task(newName);
        final String status = task.waitForTask();
        if (status.equals(Task.SUCCESS)) {
            LOGGER.log(Level.FINER, "VM was renamed successfully.");
            return;
        }
        throw newVSphereException(task.getTaskInfo(), "Could not rename VM \"" + oldName + "\"!");
    } catch (RuntimeException | VSphereException e) {
        throw e;
    } catch (Exception e) {
        throw new VSphereException(e.getMessage(), e);
    }
}
Also used : Task(com.vmware.vim25.mo.Task) MalformedURLException(java.net.MalformedURLException) RemoteException(java.rmi.RemoteException) VirtualMachine(com.vmware.vim25.mo.VirtualMachine)

Example 87 with Task

use of com.vmware.vim25.mo.Task in project vsphere-cloud-plugin by jenkinsci.

the class ReconfigureDisk method addSCSIController.

private VirtualLsiLogicController addSCSIController(VirtualMachine vm) throws Exception {
    VirtualMachineConfigInfo vmConfig = vm.getConfig();
    VirtualPCIController pci = null;
    Set<Integer> scsiBuses = new HashSet<Integer>();
    for (VirtualDevice vmDevice : vmConfig.getHardware().getDevice()) {
        if (vmDevice instanceof VirtualPCIController) {
            pci = (VirtualPCIController) vmDevice;
        } else if (vmDevice instanceof VirtualSCSIController) {
            VirtualSCSIController ctrl = (VirtualSCSIController) vmDevice;
            scsiBuses.add(ctrl.getBusNumber());
        }
    }
    if (pci == null) {
        throw new VSphereException("No PCI controller found");
    }
    VirtualMachineConfigSpec vmSpec = new VirtualMachineConfigSpec();
    VirtualDeviceConfigSpec deviceSpec = new VirtualDeviceConfigSpec();
    deviceSpec.setOperation(VirtualDeviceConfigSpecOperation.add);
    VirtualLsiLogicController scsiCtrl = new VirtualLsiLogicController();
    scsiCtrl.setControllerKey(pci.getKey());
    scsiCtrl.setSharedBus(VirtualSCSISharing.noSharing);
    for (int i = 0; ; ++i) {
        if (!scsiBuses.contains(Integer.valueOf(i))) {
            scsiCtrl.setBusNumber(i);
            break;
        }
    }
    deviceSpec.setDevice(scsiCtrl);
    vmSpec.setDeviceChange(new VirtualDeviceConfigSpec[] { deviceSpec });
    Task task = vm.reconfigVM_Task(vmSpec);
    task.waitForTask();
    return scsiCtrl;
}
Also used : Task(com.vmware.vim25.mo.Task) VSphereException(org.jenkinsci.plugins.vsphere.tools.VSphereException) HashSet(java.util.HashSet)

Example 88 with Task

use of com.vmware.vim25.mo.Task in project vsphere-cloud-plugin by jenkinsci.

the class VSphere method destroyVm.

/**
 * Destroys the VM in vSphere
 * @param name - VM object to destroy
 * @param failOnNoExist If true and the VM does not exist then a {@link VSphereNotFoundException} will be thrown.
 * @throws VSphereException If an error occurred.
 */
public void destroyVm(String name, boolean failOnNoExist) throws VSphereException {
    try {
        VirtualMachine vm = getVmByName(name);
        if (vm == null) {
            if (failOnNoExist)
                throw new VSphereNotFoundException("VM", name);
            LOGGER.log(Level.FINER, "VM \"" + name + "\" does not exist, or already deleted!");
            return;
        }
        if (!vm.getConfig().template) {
            powerOffVm(vm, true, 0);
        }
        final Task task = vm.destroy_Task();
        String status = task.waitForTask();
        if (status.equals(Task.SUCCESS)) {
            LOGGER.log(Level.FINER, "VM \"" + name + "\" was deleted successfully.");
            return;
        }
        throw newVSphereException(task.getTaskInfo(), "Could not delete VM \"" + name + "\"!");
    } catch (RuntimeException | VSphereException e) {
        throw e;
    } catch (Exception e) {
        throw new VSphereException(e.getMessage(), e);
    }
}
Also used : Task(com.vmware.vim25.mo.Task) MalformedURLException(java.net.MalformedURLException) RemoteException(java.rmi.RemoteException) VirtualMachine(com.vmware.vim25.mo.VirtualMachine)

Example 89 with Task

use of com.vmware.vim25.mo.Task in project vsphere-cloud-plugin by jenkinsci.

the class VSphere method reconfigureVm.

public void reconfigureVm(String name, VirtualMachineConfigSpec spec) throws VSphereException {
    VirtualMachine vm = getVmByName(name);
    if (vm == null) {
        throw new VSphereNotFoundException("VM or template", name);
    }
    LOGGER.log(Level.FINER, "Reconfiguring VM. Please wait ...");
    try {
        Task task = vm.reconfigVM_Task(spec);
        String status = task.waitForTask();
        if (status.equals(TaskInfoState.success.toString())) {
            return;
        }
        throw newVSphereException(task.getTaskInfo(), "Couldn't reconfigure \"" + name + "\"!");
    } catch (RuntimeException | VSphereException e) {
        throw e;
    } catch (Exception e) {
        throw new VSphereException("VM cannot be reconfigured:" + e.getMessage(), e);
    }
}
Also used : Task(com.vmware.vim25.mo.Task) MalformedURLException(java.net.MalformedURLException) RemoteException(java.rmi.RemoteException) VirtualMachine(com.vmware.vim25.mo.VirtualMachine)

Example 90 with Task

use of com.vmware.vim25.mo.Task in project vsphere-cloud-plugin by jenkinsci.

the class VSphere method takeSnapshot.

public void takeSnapshot(String vmName, String snapshot, String description, boolean snapMemory) throws VSphereException {
    final String message = "Could not take snapshot";
    VirtualMachine vmToSnapshot = getVmByName(vmName);
    if (vmToSnapshot == null) {
        throw new VSphereNotFoundException("VM", vmName);
    }
    try {
        Task task = vmToSnapshot.createSnapshot_Task(snapshot, description, snapMemory, !snapMemory);
        if (task.waitForTask().equals(Task.SUCCESS)) {
            return;
        }
        throw newVSphereException(task.getTaskInfo(), message);
    } catch (RuntimeException | VSphereException e) {
        throw e;
    } catch (Exception e) {
        throw new VSphereException(message, e);
    }
}
Also used : Task(com.vmware.vim25.mo.Task) MalformedURLException(java.net.MalformedURLException) RemoteException(java.rmi.RemoteException) VirtualMachine(com.vmware.vim25.mo.VirtualMachine)

Aggregations

ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)58 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