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);
}
}
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;
}
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);
}
}
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);
}
}
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);
}
}
Aggregations