use of com.vmware.vim25.mo.Task in project photon-model by vmware.
the class InstanceClient method reconfigureBootDisk.
/**
* Reconfigure image disk with the customizations
*/
private void reconfigureBootDisk(ManagedObjectReference vmMoref, List<VirtualDeviceConfigSpec> deviceConfigSpecs) throws Exception {
if (deviceConfigSpecs != null && !deviceConfigSpecs.isEmpty()) {
VirtualMachineConfigSpec bootDiskSpec = new VirtualMachineConfigSpec();
bootDiskSpec.getDeviceChange().addAll(deviceConfigSpecs);
ManagedObjectReference task = getVimPort().reconfigVMTask(vmMoref, bootDiskSpec);
TaskInfo info = waitTaskEnd(task);
if (info.getState() == TaskInfoState.ERROR) {
VimUtils.rethrow(info.getError());
}
}
}
use of com.vmware.vim25.mo.Task in project photon-model by vmware.
the class InstanceClient method deleteInstance.
public void deleteInstance(ServiceHost serviceHost) throws Exception {
ManagedObjectReference vm = CustomProperties.of(this.ctx.child).getMoRef(CustomProperties.MOREF);
if (vm == null) {
logger.info("No moref associated with the given instance, skipping delete.");
return;
}
ArrayOfVirtualDevice devices = this.get.entityProp(vm, VimPath.vm_config_hardware_device);
// Handle disks of the VM during VM deletion based on its persistent flag.
handleVirtualDiskCleanup(serviceHost, vm, devices, this.ctx.disks);
TaskInfo info;
// power off
ManagedObjectReference task = getVimPort().powerOffVMTask(vm);
info = waitTaskEnd(task);
ignoreError("Ignore error powering off VM", info);
// delete vm
task = getVimPort().destroyTask(vm);
info = waitTaskEnd(task);
ignoreError("Ignore error deleting VM", info);
// Handle CD ROM folder clean up.
handleVirtualCDRomCleanup(this.ctx.disks);
}
use of com.vmware.vim25.mo.Task in project photon-model by vmware.
the class InstanceClient method createFullCloneAndAttach.
private VirtualDeviceConfigSpec createFullCloneAndAttach(String sourcePath, DiskStateExpanded ds, String dir, VirtualDevice scsiController, int unitNumber, List<VirtualMachineDefinedProfileSpec> pbmSpec) throws Exception {
ManagedObjectReference diskManager = this.connection.getServiceContent().getVirtualDiskManager();
String dsDirForDisk = getDatastorePathForDisk(ds, dir);
// put full clone in the vm folder
String destName = makePathToVmdkFile(ds.name, dsDirForDisk);
// all ops are within a datacenter
ManagedObjectReference sourceDc = this.ctx.datacenterMoRef;
ManagedObjectReference destDc = sourceDc;
Boolean force = true;
// spec is not supported, should use null for now
VirtualDiskSpec spec = null;
ManagedObjectReference task = getVimPort().copyVirtualDiskTask(diskManager, sourcePath, sourceDc, destName, destDc, spec, force);
// wait for the disk to be copied
TaskInfo taskInfo = waitTaskEnd(task);
if (taskInfo.getState() == TaskInfoState.ERROR) {
return VimUtils.rethrow(taskInfo.getError());
}
VirtualDiskFlatVer2BackingInfo backing = new VirtualDiskFlatVer2BackingInfo();
backing.setDiskMode(getDiskMode(ds));
VirtualDiskType provisionType = getDiskProvisioningType(ds);
if (provisionType != null) {
backing.setThinProvisioned(provisionType == VirtualDiskType.THIN);
backing.setEagerlyScrub(provisionType == VirtualDiskType.EAGER_ZEROED_THICK);
}
backing.setFileName(destName);
backing.setDatastore(getDataStoreForDisk(ds, pbmSpec));
VirtualDisk disk = new VirtualDisk();
disk.setBacking(backing);
disk.setStorageIOAllocation(getStorageIOAllocationInfo(ds));
disk.setControllerKey(scsiController.getKey());
disk.setUnitNumber(unitNumber);
fillInControllerUnitNumber(ds, unitNumber);
disk.setKey(-1);
VirtualDeviceConfigSpec change = new VirtualDeviceConfigSpec();
change.setDevice(disk);
// Add storage policy spec
if (pbmSpec != null) {
pbmSpec.stream().forEach(sp -> {
change.getProfile().add(sp);
});
}
change.setOperation(VirtualDeviceConfigSpecOperation.ADD);
return change;
}
use of com.vmware.vim25.mo.Task in project photon-model by vmware.
the class VSphereAdapterResizeComputeService method handleResourceOperationRequest.
private void handleResourceOperationRequest(VSphereVMContext ctx, int cpuCount, long memoryInMBytes, boolean rebootVM) {
ctx.pool.submit(ctx.getAdapterManagementReference(), ctx.parentAuth, (connection, ce) -> {
if (ctx.fail(ce)) {
return;
}
VirtualMachineConfigSpec spec = new VirtualMachineConfigSpec();
spec.setNumCPUs(cpuCount);
spec.setMemoryMB(memoryInMBytes);
ManagedObjectReference vmMoRef;
ManagedObjectReference task;
TaskInfo info;
try {
vmMoRef = CustomProperties.of(ctx.child).getMoRef(CustomProperties.MOREF);
PowerStateClient powerStateClient = new PowerStateClient(connection);
boolean isComputePowerStateOn = ComputeService.PowerState.ON.equals(ctx.child.powerState) ? true : false;
if (rebootVM && isComputePowerStateOn) {
// soft power-off the VM
// 180000000 micro seconds (3 minutes)
powerStateClient.shutdownGuest(vmMoRef, Utils.getNowMicrosUtc() + SOFT_POWER_OFF_TIMEOUT_MICROS);
}
// reconfigure VM with new CPU/memory values
task = connection.getVimPort().reconfigVMTask(vmMoRef, spec);
info = VimUtils.waitTaskEnd(connection, task);
if (rebootVM || isComputePowerStateOn) {
// power-on the VM back, since it was shutdown earlier
powerStateClient.changePowerState(vmMoRef, ComputeService.PowerState.ON, null, 0);
}
if (info.getState() != TaskInfoState.SUCCESS) {
VimUtils.rethrow(info.getError());
}
} catch (InsufficientResourcesFaultFaultMsg ex) {
ctx.failWithMessage("Cannot perform resize operation due to insufficient resources in host " + "for compute resource " + ctx.child.name, ex);
return;
} catch (Exception ex) {
ctx.failWithMessage("Cannot perform resize operation for compute resource " + ctx.child.name, ex);
return;
}
patchComputeAndCompleteRequest(ctx, cpuCount, memoryInMBytes);
});
}
use of com.vmware.vim25.mo.Task in project photon-model by vmware.
the class VSphereAdapterSnapshotService method revertSnapshot.
private void revertSnapshot(SnapshotContext context, Connection connection, DeferredResult<SnapshotContext> deferredResult) {
final SnapshotState snapshot = context.snapshotState;
SnapshotState existingSnapshotState = context.existingSnapshotState;
// Physical snapshot processing
ManagedObjectReference snapshotMoref = CustomProperties.of(snapshot).getMoRef(CustomProperties.MOREF);
if (snapshotMoref == null) {
deferredResult.fail(new IllegalStateException(String.format("Cannot find the snapshot %s to revert to", snapshotMoref)));
return;
}
ManagedObjectReference task;
TaskInfo info;
try {
logInfo("Reverting to snapshot with name %s", context.snapshotState.name);
task = connection.getVimPort().revertToSnapshotTask(snapshotMoref, null, false);
info = VimUtils.waitTaskEnd(connection, task);
if (info.getState() != TaskInfoState.SUCCESS) {
VimUtils.rethrow(info.getError());
}
} catch (Exception e) {
logSevere("Reverting to the snapshot %s failed", context.snapshotState.name);
deferredResult.fail(e);
return;
}
// cause concurrency issue
if (snapshot.isCurrent) {
deferredResult.complete(context);
} else {
snapshot.isCurrent = true;
context.snapshotOperations.add(Operation.createPatch(UriUtils.buildUri(getHost(), snapshot.documentSelfLink)).setBody(snapshot).setReferer(getUri()));
if (existingSnapshotState != null) {
existingSnapshotState.isCurrent = false;
context.snapshotOperations.add(Operation.createPatch(UriUtils.buildUri(getHost(), existingSnapshotState.documentSelfLink)).setBody(existingSnapshotState).setReferer(getUri()));
}
OperationJoin.JoinedCompletionHandler joinCompletion = (ox, exc) -> {
if (exc != null) {
this.logSevere(() -> String.format("Error updating the snapshot states: %s", Utils.toString(exc)));
deferredResult.fail(new IllegalStateException("Error updating the snapshot states"));
return;
}
deferredResult.complete(context);
};
OperationJoin joinOp = OperationJoin.create(context.snapshotOperations);
joinOp.setCompletion(joinCompletion);
joinOp.sendWith(this.getHost());
}
}
Aggregations