Search in sources :

Example 16 with ResourceOperationRequest

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

the class AWSComputeDiskDay2ServiceTest method performDiskOperationAndVerify.

private void performDiskOperationAndVerify(String computeStateLink, List<String> diskLinks, String requestType, DiskTaskService.DiskTaskState.SubStage expectedTerminalState) throws Throwable {
    List<String> taskServiceLinks = new ArrayList<>();
    diskLinks.forEach(diskLink -> {
        DiskTaskService.DiskTaskState diskOpTask = new DiskTaskService.DiskTaskState();
        diskOpTask.taskSubStage = DiskTaskService.DiskTaskState.SubStage.STARTED;
        try {
            diskOpTask = TestUtils.doPost(this.host, diskOpTask, DiskTaskService.DiskTaskState.class, UriUtils.buildUri(this.host, DiskTaskService.FACTORY_LINK));
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        ResourceOperationRequest request = new ResourceOperationRequest();
        request.isMockRequest = this.isMock;
        request.operation = requestType;
        request.payload = new HashMap<>();
        request.payload.put(PhotonModelConstants.DISK_LINK, diskLink);
        request.resourceReference = UriUtils.buildUri(this.host, computeStateLink);
        request.taskReference = UriUtils.buildUri(this.host, diskOpTask.documentSelfLink);
        Operation diskOp = Operation.createPatch(UriUtils.buildUri(this.host, AWSComputeDiskDay2Service.SELF_LINK)).setBody(request).setReferer(this.host.getReferer());
        TestRequestSender sender = new TestRequestSender(this.host);
        sender.sendRequest(diskOp);
        this.host.log("Waiting for disk operation to complete");
        taskServiceLinks.add(diskOpTask.documentSelfLink);
    });
    taskServiceLinks.forEach(taskServiceLink -> {
        this.host.waitFor("disk Operation failed.", () -> {
            DiskTaskService.DiskTaskState diskTaskState = this.host.getServiceState(null, DiskTaskService.DiskTaskState.class, UriUtils.buildUri(this.host, taskServiceLink));
            // Check if the disk operation is successful or not.
            if (diskTaskState.taskSubStage == DiskTaskService.DiskTaskState.SubStage.FINISHED || diskTaskState.taskSubStage == DiskTaskService.DiskTaskState.SubStage.FAILED) {
                assertEquals("diskOperation Task did not terminate as expected.", expectedTerminalState, diskTaskState.taskSubStage);
                return true;
            } else {
                return false;
            }
        });
    });
}
Also used : ProvisionDiskTaskService(com.vmware.photon.controller.model.tasks.ProvisionDiskTaskService) ArrayList(java.util.ArrayList) 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 17 with ResourceOperationRequest

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

the class AWSRebootServiceTest method testReboot.

// Test the reboot operation.
@Test
public void testReboot() throws Throwable {
    provisionSingleAWS();
    // check that the VM has been created
    ProvisioningUtils.queryComputeInstances(this.host, 2);
    ComputeState compute = getCompute(this.host, this.vmState.documentSelfLink);
    if (!this.isMock) {
        List<Instance> instances = getAwsInstancesByIds(this.client, this.host, Collections.singletonList(compute.id));
        Instance instance = instances.get(0);
        ComputeState vm = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, this.vmState.documentSelfLink));
        assertAndSetVMSecurityGroupsToBeDeleted(instance, vm);
    }
    String taskLink = UUID.randomUUID().toString();
    ResourceOperationRequest request = new ResourceOperationRequest();
    request.isMockRequest = this.isMock;
    request.operation = ResourceOperation.REBOOT.operation;
    request.payload = new HashMap<>();
    request.resourceReference = UriUtils.buildUri(this.host, compute.documentSelfLink);
    request.taskReference = UriUtils.buildUri(this.host, taskLink);
    TestContext ctx = this.host.testCreate(2);
    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 if (TaskState.isFinished(response.taskInfo)) {
            ctx.completeIteration();
        }
        return true;
    });
    Operation rebootOp = Operation.createPatch(UriUtils.buildUri(this.host, AWSRebootService.SELF_LINK)).setBody(request).setReferer(this.host.getReferer()).setCompletion((o, e) -> {
        if (e != null) {
            ctx.failIteration(e);
            return;
        }
        ctx.completeIteration();
    });
    this.host.send(rebootOp);
    ctx.await();
    ComputeState vm = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, this.vmState.documentSelfLink));
    assertEquals(ComputeService.PowerState.ON, vm.powerState);
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) Instance(com.amazonaws.services.ec2.model.Instance) 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) Test(org.junit.Test)

Example 18 with ResourceOperationRequest

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

the class AWSResetService method reset.

private void reset(AmazonEC2AsyncClient client, ResourceOperationRequest pr, DefaultAdapterContext c) {
    if (!c.child.powerState.equals(ComputeService.PowerState.ON)) {
        logWarning(() -> String.format("Cannot perform a reset on this EC2 instance. " + "The machine should be in powered on state"));
        c.taskManager.patchTaskToFailure(new IllegalStateException("Incorrect power state. Expected the machine " + "to be powered on "));
        return;
    }
    // The stop action for reset is a force stop. So we use the withForce method to set the force parameter to TRUE
    // This is similar to unplugging the machine from the power circuit.
    // The OS and the applications are forcefully stopped.
    StopInstancesRequest stopRequest = new StopInstancesRequest();
    stopRequest.withInstanceIds(c.child.id).withForce(Boolean.TRUE);
    client.stopInstancesAsync(stopRequest, new AWSAsyncHandler<StopInstancesRequest, StopInstancesResult>() {

        @Override
        protected void handleError(Exception e) {
            c.taskManager.patchTaskToFailure(e);
        }

        @Override
        protected void handleSuccess(StopInstancesRequest request, StopInstancesResult result) {
            AWSUtils.waitForTransitionCompletion(getHost(), result.getStoppingInstances(), "stopped", client, (is, e) -> {
                if (e != null) {
                    onError(e);
                    return;
                }
                // Instances will be started only if they're successfully stopped
                startInstance(client, c);
            });
        }
    });
}
Also used : Service(com.vmware.xenon.common.Service) ResourceOperationSpec(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationSpecService.ResourceOperationSpec) ResourceOperationUtils.handleAdapterResourceOperationRegistration(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.handleAdapterResourceOperationRegistration) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) AWSAsyncHandler(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSAsyncHandler) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation) StartInstancesResult(com.amazonaws.services.ec2.model.StartInstancesResult) ServiceDocument(com.vmware.xenon.common.ServiceDocument) BaseAdapterStage(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.BaseAdapterStage) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) StartInstancesRequest(com.amazonaws.services.ec2.model.StartInstancesRequest) DefaultAdapterContext(com.vmware.photon.controller.model.adapters.util.BaseAdapterContext.DefaultAdapterContext) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) 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) ResourceOperationUtils(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils) AWSClientManager(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) StopInstancesResult(com.amazonaws.services.ec2.model.StopInstancesResult) StopInstancesRequest(com.amazonaws.services.ec2.model.StopInstancesRequest)

Example 19 with ResourceOperationRequest

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

the class AWSResetServiceTest method testReset.

// Test the reset operation.
@Test
public void testReset() throws Throwable {
    provisionSingleAWS();
    // check that the VM has been created
    ProvisioningUtils.queryComputeInstances(this.host, 2);
    ComputeState compute = getCompute(this.host, this.vmState.documentSelfLink);
    if (!this.isMock) {
        List<Instance> instances = getAwsInstancesByIds(this.client, this.host, Collections.singletonList(compute.id));
        Instance instance = instances.get(0);
        ComputeState vm = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, this.vmState.documentSelfLink));
        setVMSecurityGroupsToBeDeleted(instance, vm);
    }
    String taskLink = UUID.randomUUID().toString();
    ResourceOperationRequest request = new ResourceOperationRequest();
    request.isMockRequest = this.isMock;
    request.operation = ResourceOperation.RESET.operation;
    request.payload = new HashMap<>();
    request.resourceReference = UriUtils.buildUri(this.host, compute.documentSelfLink);
    request.taskReference = UriUtils.buildUri(this.host, 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 if (TaskState.isFinished(response.taskInfo)) {
            ctx.completeIteration();
        }
        return true;
    });
    TestContext ctx2 = this.host.testCreate(1);
    Operation resetOp = Operation.createPatch(UriUtils.buildUri(this.host, AWSResetService.SELF_LINK)).setBody(request).setReferer(this.host.getReferer()).setCompletion((o, e) -> {
        if (e != null) {
            ctx2.failIteration(e);
            return;
        }
        ctx2.completeIteration();
    });
    this.host.send(resetOp);
    ctx2.await();
    // in EC2 documentation
    if (!this.isMock) {
        // Waiting for power off
        this.host.waitFor("Timed out waiting for EC2 to power off", () -> {
            String state = getVMState(this.client, compute.id);
            if (("stopping".equals(state) || "stopped".equals(state))) {
                this.host.log(Level.INFO, "EC2 is being powered off");
                return true;
            } else {
                return false;
            }
        });
        // Waiting for power on
        this.host.waitFor("Timed out waiting for EC2 to power on", () -> {
            String state = getVMState(this.client, compute.id);
            if ("running".equals(state)) {
                this.host.log(Level.INFO, "EC2 is being powered on");
                return true;
            } else {
                return false;
            }
        });
    }
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) Instance(com.amazonaws.services.ec2.model.Instance) 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) Test(org.junit.Test)

Example 20 with ResourceOperationRequest

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

the class BaseVSphereAdapterTest method getDeleteOrRevertSnapshotRequest.

private ResourceOperationRequest getDeleteOrRevertSnapshotRequest(String operationId, String operationType, String computeSelfLink, String snapshotSelfLink, String taskLink) {
    Map<String, String> payload = new HashMap<>();
    payload.put(VSphereConstants.VSPHERE_SNAPSHOT_REQUEST_TYPE, operationType);
    payload.put(VSphereConstants.VSPHERE_SNAPSHOT_DOCUMENT_LINK, snapshotSelfLink);
    ResourceOperationRequest resourceOperationRequest = new ResourceOperationRequest();
    resourceOperationRequest.operation = operationId;
    resourceOperationRequest.isMockRequest = isMock();
    resourceOperationRequest.resourceReference = UriUtils.buildUri(this.host, computeSelfLink);
    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)

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