use of com.vmware.vim25.VirtualDiskMode.PERSISTENT in project photon-model by vmware.
the class TestVSphereComputeDiskDay2Service method testComputePersistentDisk.
@Test
public /**
* 1. Create a VM
* 2. Create a HDD persistent disk
* 3. Attach HDD disk created in Step 2 to VM.
* 4. Delete VM.
* 5. Verify HDD disk still remains
* 6. Delete HDD disk
*/
void testComputePersistentDisk() throws Throwable {
DiskService.DiskState diskState = null;
try {
// Step 1: Create VM
prepareEnvironment();
if (isMock()) {
createNetwork(networkId);
}
snapshotFactoryState("clone-refresh", NetworkService.class);
ComputeDescriptionService.ComputeDescription vmDescription = createVmDescription();
this.vm = createVmState(vmDescription, true, null, false);
// kick off a provision task to do the actual VM creation
ProvisionComputeTaskService.ProvisionComputeTaskState provisionTask = createProvisionTask(this.vm);
awaitTaskEnd(provisionTask);
this.vm = getComputeState(this.vm);
// put fake moref in the vm
if (isMock()) {
ManagedObjectReference moref = new ManagedObjectReference();
moref.setValue("vm-0");
moref.setType(VimNames.TYPE_VM);
CustomProperties.of(this.vm).put(MOREF, moref);
this.vm = doPost(this.host, this.vm, ComputeState.class, UriUtils.buildUri(this.host, ComputeService.FACTORY_LINK));
return;
}
// Step 2: Create Disk
diskState = createDiskWithDatastore("AdditionalDisk1", DiskService.DiskType.HDD, ADDITIONAL_DISK_SIZE, buildCustomProperties(), true);
// start provision task to do the actual disk creation
String documentSelfLink = performDiskRequest(diskState, ProvisionDiskTaskService.ProvisionDiskTaskState.SubStage.CREATING_DISK);
this.host.waitForFinishedTask(ProvisionDiskTaskService.ProvisionDiskTaskState.class, documentSelfLink);
// Step 3: Attach Disk created in step 2
ResourceOperationRequest request = createResourceOperationRequest(diskState, createComputeDiskTaskService(), ResourceOperation.ATTACH_DISK);
sendRequest(request, DiskService.DiskType.HDD, computeAttachWaitHandler());
this.vm = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, this.vm.documentSelfLink));
// Step 4: Delete VM
cleanUpVm(this.vm, null);
// Get the latest state of the detached disk
diskState = this.host.getServiceState(null, DiskService.DiskState.class, UriUtils.buildUri(this.host, diskState.documentSelfLink));
assertEquals(DiskService.DiskStatus.AVAILABLE, diskState.status);
} finally {
if (!isMock()) {
// Step 8: Delete disk
if (diskState != null) {
String documentSelfLink = performDiskRequest(diskState, ProvisionDiskTaskService.ProvisionDiskTaskState.SubStage.DELETING_DISK);
this.host.waitForFinishedTask(ProvisionDiskTaskService.ProvisionDiskTaskState.class, documentSelfLink);
}
}
}
}
use of com.vmware.vim25.VirtualDiskMode.PERSISTENT 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.VirtualDiskMode.PERSISTENT in project photon-model by vmware.
the class InstanceClient method handleVirtualDiskCleanup.
/**
* For every disk states in the compute based on the persistent flag if it is set as TRUE,
* then disk will be detached before we delete the vm.
*/
private void handleVirtualDiskCleanup(ServiceHost serviceHost, ManagedObjectReference vm, ArrayOfVirtualDevice devices, List<DiskStateExpanded> disks) throws Exception {
if (CollectionUtils.isEmpty(disks)) {
return;
}
List<DiskStateExpanded> persistDisks = disks.stream().filter(disk -> disk.type == DiskType.HDD).filter(disk -> disk.persistent != null && disk.persistent).collect(Collectors.toList());
if (CollectionUtils.isEmpty(persistDisks)) {
return;
}
List<Operation> diskUpdateOps = new ArrayList<>(persistDisks.size());
for (DiskStateExpanded ds : persistDisks) {
VirtualDisk vd = (VirtualDisk) findMatchingVirtualDevice(getListOfVirtualDisk(devices), ds);
if (vd != null) {
detachDisk(this.connection, vd, vm, getVimPort());
}
// Now update the status of the persistent disks to be AVAILABLE
ds.status = DiskService.DiskStatus.AVAILABLE;
CustomProperties.of(ds).put(DISK_CONTROLLER_NUMBER, (String) null).put(PROVIDER_DISK_UNIQUE_ID, (String) null);
ds.id = UriUtils.getLastPathSegment(ds.documentSelfLink);
diskUpdateOps.add(Operation.createPut(PhotonModelUriUtils.createInventoryUri(serviceHost, ds.documentSelfLink)).setReferer(serviceHost.getUri()).setBody(ds));
disks.remove(ds);
}
// call patch operations on the disk states
OperationJoin.create(diskUpdateOps).setCompletion((os, errors) -> {
if (errors != null && !errors.isEmpty()) {
logger.warn(String.format("Exception in updating persistent disk during deletion of VM." + " Error : %s", Utils.toString(errors)));
}
}).sendWith(serviceHost);
}
Aggregations