Search in sources :

Example 21 with ResourceOperationRequest

use of com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest in project photon-model by vmware.

the class BaseVSphereAdapterTest method suspendVSphereVM.

protected void suspendVSphereVM(ComputeState computeState) {
    String taskLink = UUID.randomUUID().toString();
    ResourceOperationRequest suspendVMRequest = getResourceOperationRequest("Suspend", computeState.documentSelfLink, taskLink);
    TestContext ctx = this.host.testCreate(1);
    createTaskResultListener(this.host, taskLink, (u) -> {
        if (u.getAction() != Service.Action.PATCH) {
            return false;
        }
        ResourceOperationResponse response = u.getBody(ResourceOperationResponse.class);
        if (TaskState.isFailed(response.taskInfo)) {
            ctx.failIteration(new IllegalStateException(response.taskInfo.failure.message));
        } else {
            ctx.completeIteration();
        }
        return true;
    });
    TestContext ctx2 = this.host.testCreate(1);
    Operation suspendOp = Operation.createPatch(UriUtils.buildUri(this.host, VSphereAdapterD2PowerOpsService.SELF_LINK)).setBody(suspendVMRequest).setReferer(this.host.getReferer()).setCompletion((o, e) -> {
        if (e != null) {
            ctx2.failIteration(e);
            return;
        }
        ctx2.completeIteration();
    });
    this.host.send(suspendOp);
    ctx2.await();
    ComputeState[] cstate = new ComputeState[1];
    this.host.waitFor("Suspend request failed", () -> {
        cstate[0] = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, computeState.documentSelfLink));
        if (cstate[0].powerState.equals(ComputeService.PowerState.SUSPEND)) {
            assertTrue(cstate[0].address.isEmpty());
            return true;
        } else {
            return false;
        }
    });
    assertEquals(ComputeService.PowerState.SUSPEND, cstate[0].powerState);
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) TestContext(com.vmware.xenon.common.test.TestContext) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) Operation(com.vmware.xenon.common.Operation)

Example 22 with ResourceOperationRequest

use of com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest in project photon-model by vmware.

the class BaseVSphereAdapterTest method getCreateSnapshotRequest.

private ResourceOperationRequest getCreateSnapshotRequest(String operation, String documentSelfLink, String taskLink) {
    Map<String, String> payload = new HashMap<>();
    payload.put(VSphereConstants.VSPHERE_SNAPSHOT_REQUEST_TYPE, "CREATE");
    payload.put(VSphereConstants.VSPHERE_SNAPSHOT_MEMORY, "false");
    ResourceOperationRequest resourceOperationRequest = new ResourceOperationRequest();
    resourceOperationRequest.operation = operation;
    resourceOperationRequest.isMockRequest = isMock();
    resourceOperationRequest.resourceReference = UriUtils.buildUri(this.host, documentSelfLink);
    resourceOperationRequest.taskReference = UriUtils.buildUri(this.host, taskLink);
    resourceOperationRequest.payload = payload;
    return resourceOperationRequest;
}
Also used : HashMap(java.util.HashMap) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest)

Example 23 with ResourceOperationRequest

use of com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest in project photon-model by vmware.

the class BaseVSphereAdapterTest method revertToSnapshotAndWait.

protected void revertToSnapshotAndWait(ComputeState computeState) throws Throwable {
    // Get the snapshot to revert to
    SnapshotState snapshotStateToRevertTo = querySnapshotState(computeState.documentSelfLink, false);
    String taskLink = UUID.randomUUID().toString();
    ResourceOperationRequest snapshotRequest = getDeleteOrRevertSnapshotRequest(ResourceOperation.REVERT_SNAPSHOT.operation, "REVERT", computeState.documentSelfLink, snapshotStateToRevertTo.documentSelfLink, taskLink);
    Operation revertSnapshotOp = Operation.createPatch(UriUtils.buildUri(this.host, VSphereAdapterSnapshotService.SELF_LINK)).setBody(snapshotRequest).setReferer(this.host.getReferer()).setCompletion((o, e) -> Assert.assertNull(e));
    TestRequestSender sender = new TestRequestSender(this.host);
    sender.sendRequest(revertSnapshotOp);
    this.host.log("Waiting for the snapshot to be reverted");
    this.host.waitFor("Revert snapshot request failed", () -> {
        SnapshotState finalSnapshotState = querySnapshotState(computeState.documentSelfLink, true);
        ComputeState finalComputeState = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, computeState.documentSelfLink));
        String hasSnapshot = finalComputeState.customProperties.get(ComputeProperties.CUSTOM_PROP_COMPUTE_HAS_SNAPSHOTS);
        // Check for the snapshot state and _hasSnapshots flag in compute
        if (hasSnapshot != null && Boolean.parseBoolean(hasSnapshot) && finalSnapshotState.documentSelfLink.equals(snapshotStateToRevertTo.documentSelfLink)) {
            return true;
        } else {
            return false;
        }
    });
    this.host.log("Revert to snapshot operation completed successfully");
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState) TestRequestSender(com.vmware.xenon.common.test.TestRequestSender) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) Operation(com.vmware.xenon.common.Operation)

Example 24 with ResourceOperationRequest

use of com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest in project photon-model by vmware.

the class BaseVSphereAdapterTest method resizeVM.

protected void resizeVM(ComputeState vm) {
    String taskLink = UUID.randomUUID().toString();
    ResourceOperationRequest resizeRequest = getResizeComputeRequest(ResourceOperation.RESIZE.operation, vm.documentSelfLink, taskLink);
    Operation resizeComputeOperation = Operation.createPatch(UriUtils.buildUri(this.host, VSphereAdapterResizeComputeService.SELF_LINK)).setBody(resizeRequest).setReferer(this.host.getReferer()).setCompletion((o, e) -> Assert.assertNull(e));
    TestRequestSender sender = new TestRequestSender(this.host);
    sender.sendRequest(resizeComputeOperation);
    this.host.log("Waiting for the resource resize to complete");
    ComputeState cState = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, vm.documentSelfLink));
    this.host.waitFor("Resize compute request failed", () -> {
        Operation op = this.host.waitForResponse(Operation.createGet(this.host, cState.documentSelfLink + "?expand"));
        ComputeService.ComputeStateWithDescription cDesc = op.getBody(ComputeService.ComputeStateWithDescription.class);
        if (cDesc != null && cDesc.description.cpuCount == 4L) {
            return true;
        } else {
            return false;
        }
    });
    this.host.log("Resize compute operation completed successfully");
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) TestRequestSender(com.vmware.xenon.common.test.TestRequestSender) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) Operation(com.vmware.xenon.common.Operation) ComputeService(com.vmware.photon.controller.model.resources.ComputeService)

Example 25 with ResourceOperationRequest

use of com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest 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);
            }
        }
    }
}
Also used : ComputeDescriptionService(com.vmware.photon.controller.model.resources.ComputeDescriptionService) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ProvisionComputeTaskService(com.vmware.photon.controller.model.tasks.ProvisionComputeTaskService) ProvisionDiskTaskService(com.vmware.photon.controller.model.tasks.ProvisionDiskTaskService) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) DiskService(com.vmware.photon.controller.model.resources.DiskService) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) Test(org.junit.Test)

Aggregations

ResourceOperationRequest (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest)36 Operation (com.vmware.xenon.common.Operation)20 ResourceOperation (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation)19 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)16 ResourceOperationResponse (com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse)8 TestContext (com.vmware.xenon.common.test.TestContext)8 TestRequestSender (com.vmware.xenon.common.test.TestRequestSender)7 DefaultAdapterContext (com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.DefaultAdapterContext)5 StatelessService (com.vmware.xenon.common.StatelessService)5 ResourceOperationUtils.handleAdapterResourceOperationRegistration (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.handleAdapterResourceOperationRegistration)4 ComputeService (com.vmware.photon.controller.model.resources.ComputeService)4 DiskService (com.vmware.photon.controller.model.resources.DiskService)4 SnapshotState (com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState)4 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)4 FactoryService (com.vmware.xenon.common.FactoryService)4 Service (com.vmware.xenon.common.Service)4 ServiceDocument (com.vmware.xenon.common.ServiceDocument)4 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)3