Search in sources :

Example 1 with ResourceOperationRequest

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

Example 2 with ResourceOperationRequest

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;
        }
    }));
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Utils(com.vmware.xenon.common.Utils) UriPaths(com.vmware.photon.controller.model.UriPaths) Operation(com.vmware.xenon.common.Operation) UriUtils(com.vmware.xenon.common.UriUtils) TaskState(com.vmware.xenon.common.TaskState) URI(java.net.URI) TaskService(com.vmware.xenon.services.common.TaskService) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) SnapshotService(com.vmware.photon.controller.model.resources.SnapshotService) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest)

Example 3 with ResourceOperationRequest

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);
            });
        });
    }
}
Also used : DefaultAdapterContext(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.DefaultAdapterContext) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest)

Example 4 with ResourceOperationRequest

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);
}
Also used : Service(com.vmware.xenon.common.Service) ResourceOperationSpec(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationSpecService.ResourceOperationSpec) RebootInstancesRequest(com.amazonaws.services.ec2.model.RebootInstancesRequest) RebootInstancesResult(com.amazonaws.services.ec2.model.RebootInstancesResult) ResourceOperationUtils.handleAdapterResourceOperationRegistration(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.handleAdapterResourceOperationRegistration) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation) ServiceDocument(com.vmware.xenon.common.ServiceDocument) BaseAdapterStage(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.BaseAdapterStage) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) TargetCriteria(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.TargetCriteria) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) DefaultAdapterContext(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.DefaultAdapterContext) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) AsyncHandler(com.amazonaws.handlers.AsyncHandler) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) EndpointType(com.vmware.photon.controller.model.constants.PhotonModelConstants.EndpointType) FactoryService(com.vmware.xenon.common.FactoryService) AWSClientManagerFactory(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory) ResourceType(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationSpecService.ResourceType) AWSClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState)

Example 5 with ResourceOperationRequest

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);
            });
        });
    }
}
Also used : DefaultAdapterContext(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.DefaultAdapterContext) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest)

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