Search in sources :

Example 6 with ResourceOperationResponse

use of com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse in project photon-model by vmware.

the class BaseVSphereAdapterTest method resetVSphereVM.

protected void resetVSphereVM(ComputeState computeState) {
    String taskLink = UUID.randomUUID().toString();
    ResourceOperationRequest resetVMRequest = getResourceOperationRequest("Reset", 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 resetOp = Operation.createPatch(UriUtils.buildUri(this.host, VSphereAdapterD2PowerOpsService.SELF_LINK)).setBody(resetVMRequest).setReferer(this.host.getReferer()).setCompletion((o, e) -> {
        if (e != null) {
            ctx2.failIteration(e);
            return;
        }
        ctx2.completeIteration();
    });
    this.host.send(resetOp);
    ctx2.await();
    ComputeState[] cState = new ComputeState[1];
    this.host.waitFor("Reset VM request failed", () -> {
        cState[0] = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, computeState.documentSelfLink));
        if (ComputeService.PowerState.ON.equals(cState[0].powerState)) {
            return true;
        } else {
            return false;
        }
    });
    assertEquals(ComputeService.PowerState.ON, 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 7 with ResourceOperationResponse

use of com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse in project photon-model by vmware.

the class BaseVSphereAdapterTest method shutdownGuestOS.

protected void shutdownGuestOS(ComputeState computeState) {
    String taskLink = UUID.randomUUID().toString();
    ResourceOperationRequest shutdownGuestRequest = getResourceOperationRequest("Shutdown", 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 shutdownOp = Operation.createPatch(UriUtils.buildUri(this.host, VSphereAdapterD2PowerOpsService.SELF_LINK)).setBody(shutdownGuestRequest).setReferer(this.host.getReferer()).setCompletion((o, e) -> {
        if (e != null) {
            ctx2.failIteration(e);
            return;
        }
        ctx2.completeIteration();
    });
    this.host.send(shutdownOp);
    ctx2.await();
    ComputeState[] cState = new ComputeState[1];
    this.host.waitFor("Guest shutdown request failed", () -> {
        cState[0] = this.host.getServiceState(null, ComputeState.class, UriUtils.buildUri(this.host, computeState.documentSelfLink));
        if (cState[0].powerState.equals(ComputeService.PowerState.OFF)) {
            assertTrue(cState[0].address.isEmpty());
            return true;
        } else {
            return false;
        }
    });
    assertEquals(ComputeService.PowerState.OFF, 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 8 with ResourceOperationResponse

use of com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse 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 9 with ResourceOperationResponse

use of com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse 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 10 with ResourceOperationResponse

use of com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse 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)

Aggregations

ResourceOperationResponse (com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse)17 Operation (com.vmware.xenon.common.Operation)14 TestContext (com.vmware.xenon.common.test.TestContext)10 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)9 ResourceOperation (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation)8 ResourceOperationRequest (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest)8 UriPaths (com.vmware.photon.controller.model.UriPaths)4 PhotonModelUriUtils.createInventoryUri (com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri)4 PropertyUsageOption (com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption)4 TaskState (com.vmware.xenon.common.TaskState)4 UriUtils (com.vmware.xenon.common.UriUtils)4 Utils (com.vmware.xenon.common.Utils)4 TaskService (com.vmware.xenon.services.common.TaskService)4 List (java.util.List)4 Consumer (java.util.function.Consumer)4 ComputeInstanceRequest (com.vmware.photon.controller.model.adapterapi.ComputeInstanceRequest)3 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)3 QueryByPages (com.vmware.photon.controller.model.query.QueryUtils.QueryByPages)3 ComputeStateWithDescription (com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription)3 SnapshotService (com.vmware.photon.controller.model.resources.SnapshotService)3