use of com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest in project photon-model by vmware.
the class TestVSphereComputeDiskDay2Service method testDiskOperationOnCompute.
@Test
public /**
* 1. Create a VM
* 2. Create a HDD disk
* 3. Attach HDD disk created in Step 2 to VM.
* 4. Attach a CD-ROM disk
* 5. Attach a Floppy disk
* 6. Detach HDD disk created in step 2
* 7. Delete VM.
* 8. Delete HDD disk
*/
void testDiskOperationOnCompute() 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(), false);
// 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: Attach a CD-ROM disk
// prepare a disk with CD-ROM type
DiskService.DiskState cdDiskState = createCDromWithIso("cdrom-1", DiskService.DiskType.CDROM, 0, null, 1024, null, false, true);
request = createResourceOperationRequest(cdDiskState, createComputeDiskTaskService(), ResourceOperation.ATTACH_DISK);
sendRequest(request, DiskService.DiskType.CDROM, computeAttachWaitHandler());
this.vm = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, this.vm.documentSelfLink));
this.CDDiskState = this.host.getServiceState(null, DiskService.DiskState.class, UriUtils.buildUri(this.host, cdDiskState.documentSelfLink));
// Step 5: Attach a floppy disk
DiskService.DiskState floppyDiskState = createDisk("floppy-1", DiskService.DiskType.FLOPPY, 0, null, 1024, null);
request = createResourceOperationRequest(floppyDiskState, createComputeDiskTaskService(), ResourceOperation.ATTACH_DISK);
sendRequest(request, DiskService.DiskType.FLOPPY, computeAttachWaitHandler());
this.vm = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, this.vm.documentSelfLink));
assertNotNull(this.vm.diskLinks);
assertEquals(6, this.vm.diskLinks.size());
// Get the latest state of the attached disk
diskState = this.host.getServiceState(null, DiskService.DiskState.class, UriUtils.buildUri(this.host, diskState.documentSelfLink));
assertEquals(DiskService.DiskStatus.ATTACHED, diskState.status);
// Step 6: Detach HDD disk create in Step 2 from VM
request = createResourceOperationRequest(diskState, createComputeDiskTaskService(), ResourceOperation.DETACH_DISK);
sendRequest(request, DiskService.DiskType.HDD, computeDetachWaitHandler());
this.vm = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, this.vm.documentSelfLink));
assertNotNull(this.vm.diskLinks);
assertEquals(5, this.vm.diskLinks.size());
// 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 7: Delete VM
cleanUpVm(this.vm, null);
// 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.photon.controller.model.adapters.registry.operations.ResourceOperationRequest in project photon-model by vmware.
the class SnapshotTaskService method createSnapshot.
private void createSnapshot(SnapshotTaskState updatedState, String subTaskLink) {
if (subTaskLink == null) {
createSubTaskForSnapshotCallback(updatedState);
return;
}
logFine(() -> String.format("Starting to create snapshot using sub task %s", subTaskLink));
ResourceOperationRequest sr = new ResourceOperationRequest();
sr.resourceReference = createInventoryUri(this.getHost(), updatedState.snapshotLink);
sr.taskReference = UriUtils.buildUri(getHost(), subTaskLink);
sr.isMockRequest = updatedState.isMockRequest;
sendRequest(Operation.createPatch(updatedState.snapshotAdapterReference).setBody(sr).setCompletion((o, e) -> {
if (e != null) {
failTask(e);
return;
}
}));
}
use of com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest in project photon-model by vmware.
the class AWSRebootService method handlePatch.
@Override
public void handlePatch(Operation op) {
if (!op.hasBody()) {
op.fail(new IllegalArgumentException("body is required"));
return;
}
ResourceOperationRequest request = op.getBody(ResourceOperationRequest.class);
op.complete();
logInfo("Handle operation %s for compute %s.", request.operation, request.resourceLink());
if (request.isMockRequest) {
updateComputeState(request, new DefaultAdapterContext(this, request));
} else {
new DefaultAdapterContext(this, request).populateBaseContext(BaseAdapterStage.VMDESC).whenComplete((c, e) -> {
this.clientManager.getOrCreateEC2ClientAsync(c.endpointAuth, c.child.description.regionId, this).whenComplete((client, t) -> {
if (t != null) {
c.taskManager.patchTaskToFailure(t);
return;
}
reboot(client, request, c);
});
});
}
}
use of com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest in project photon-model by vmware.
the class AWSRebootService method updateComputeState.
private void updateComputeState(ResourceOperationRequest ror, DefaultAdapterContext c) {
ComputeState state = new ComputeState();
state.powerState = ComputeService.PowerState.ON;
Operation.createPatch(ror.resourceReference).setBody(state).setCompletion((o, e) -> {
if (e != null) {
c.taskManager.patchTaskToFailure(e);
return;
}
c.taskManager.finishTask();
}).sendWith(this);
}
use of com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest in project photon-model by vmware.
the class AWSResetService method handlePatch.
@Override
public void handlePatch(Operation op) {
if (!op.hasBody()) {
op.fail(new IllegalArgumentException("body is required"));
return;
}
ResourceOperationRequest request = op.getBody(ResourceOperationRequest.class);
op.complete();
logInfo(() -> String.format("Handle operation %s for compute %s.", request.operation, request.resourceLink()));
if (request.isMockRequest) {
updateComputeState(new DefaultAdapterContext(this, request));
} else {
new DefaultAdapterContext(this, request).populateBaseContext(BaseAdapterStage.VMDESC).whenComplete((c, e) -> {
this.clientManager.getOrCreateEC2ClientAsync(c.endpointAuth, c.child.description.regionId, this).whenComplete((client, t) -> {
if (t != null) {
c.taskManager.patchTaskToFailure(t);
return;
}
reset(client, request, c);
});
});
}
}
Aggregations