Search in sources :

Example 11 with StatelessService

use of com.vmware.xenon.common.StatelessService in project photon-model by vmware.

the class VSphereVMContextTest method beforeTest.

@Before
public void beforeTest() {
    final URI mockUri = UriUtils.buildUri(this.host, UUID.randomUUID().toString());
    ResourceOperationRequest resourceOperationRequest = new ResourceOperationRequest();
    resourceOperationRequest.resourceReference = mockUri;
    // reboot operation
    resourceOperationRequest.operation = "Reboot";
    resourceOperationRequest.taskReference = mockUri;
    StatelessService mockService = new StatelessService();
    mockService.setHost(this.host);
    // create context calling the constructor, it should have errorHandler populated by default (i.e. errorHandler != null)
    this.context = new VSphereVMContext(mockService, resourceOperationRequest);
}
Also used : StatelessService(com.vmware.xenon.common.StatelessService) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) URI(java.net.URI) Before(org.junit.Before)

Example 12 with StatelessService

use of com.vmware.xenon.common.StatelessService in project photon-model by vmware.

the class AzurePowerServiceTest method createTaskResultListener.

private void createTaskResultListener(VerificationHost host, String taskLink, Function<Operation, Boolean> h) {
    StatelessService service = new StatelessService() {

        @Override
        public void handleRequest(Operation update) {
            if (!h.apply(update)) {
                super.handleRequest(update);
            }
        }
    };
    TestContext ctx = this.host.testCreate(1);
    Operation startOp = Operation.createPost(host, taskLink).setCompletion((o, e) -> {
        if (e != null) {
            ctx.failIteration(e);
            return;
        }
        ctx.completeIteration();
    }).setReferer(this.host.getReferer());
    this.host.startService(startOp, service);
    ctx.await();
}
Also used : PowerState(com.vmware.photon.controller.model.resources.ComputeService.PowerState) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) ProvisionComputeTaskService(com.vmware.photon.controller.model.tasks.ProvisionComputeTaskService) AzureTestUtil.deleteVMs(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.deleteVMs) PhotonModelServices(com.vmware.photon.controller.model.PhotonModelServices) VerificationHost(com.vmware.xenon.common.test.VerificationHost) AzureTestUtil.createDefaultResourcePool(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.createDefaultResourcePool) AzureTestUtil.createDefaultComputeHost(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.createDefaultComputeHost) After(org.junit.After) AzureEnvironment(com.microsoft.azure.AzureEnvironment) Assert.fail(org.junit.Assert.fail) URI(java.net.URI) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) AzureConstants(com.vmware.photon.controller.model.adapters.azure.constants.AzureConstants) DEFAULT_NIC_SPEC(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.DEFAULT_NIC_SPEC) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) StatelessService(com.vmware.xenon.common.StatelessService) ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) TestUtils(com.vmware.photon.controller.model.tasks.TestUtils) UUID(java.util.UUID) AzureAdaptersTestUtils(com.vmware.photon.controller.model.adapters.azure.base.AzureAdaptersTestUtils) InstanceViewStatus(com.microsoft.azure.management.compute.InstanceViewStatus) UriUtils(com.vmware.xenon.common.UriUtils) Action(com.vmware.xenon.common.Service.Action) InstanceViewTypes(com.microsoft.azure.management.compute.InstanceViewTypes) TaskState(com.vmware.xenon.common.TaskState) Assume.assumeFalse(org.junit.Assume.assumeFalse) PhotonModelMetricServices(com.vmware.photon.controller.model.PhotonModelMetricServices) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) Function(java.util.function.Function) ProvisionComputeTaskState(com.vmware.photon.controller.model.tasks.ProvisionComputeTaskService.ProvisionComputeTaskState) AzureTestUtil.createDefaultVMResource(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.createDefaultVMResource) Level(java.util.logging.Level) AzureTestUtil.createDefaultAuthCredentials(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.createDefaultAuthCredentials) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) TestName(org.junit.rules.TestName) VirtualMachineInner(com.microsoft.azure.management.compute.implementation.VirtualMachineInner) BasicReusableHostTestCase(com.vmware.xenon.common.BasicReusableHostTestCase) AzureTestUtil(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil) Before(org.junit.Before) ComputePowerRequest(com.vmware.photon.controller.model.adapterapi.ComputePowerRequest) PhotonModelTaskServices(com.vmware.photon.controller.model.tasks.PhotonModelTaskServices) ComputeManagementClientImpl(com.microsoft.azure.management.compute.implementation.ComputeManagementClientImpl) Assert.assertNotNull(org.junit.Assert.assertNotNull) Operation(com.vmware.xenon.common.Operation) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Rule(org.junit.Rule) TestContext(com.vmware.xenon.common.test.TestContext) PhotonModelAdaptersRegistryAdapters(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryAdapters) AzureTestUtil.generateName(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.generateName) AzureTestUtil.createDefaultEndpointState(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.createDefaultEndpointState) ApplicationTokenCredentials(com.microsoft.azure.credentials.ApplicationTokenCredentials) Assert.assertEquals(org.junit.Assert.assertEquals) StatelessService(com.vmware.xenon.common.StatelessService) TestContext(com.vmware.xenon.common.test.TestContext) Operation(com.vmware.xenon.common.Operation)

Example 13 with StatelessService

use of com.vmware.xenon.common.StatelessService in project photon-model by vmware.

the class TestGCPStatsCollection method issueStatsRequest.

/**
 * Creates a stateless service which will call GCPStatsService to collect stats of the
 * resource specified by the argument.
 * Receives response back and patches it to the caller service.
 * @param selfLink The self link to the document of enumerated resource.
 * @throws Throwable
 */
public void issueStatsRequest(String selfLink) throws Throwable {
    // Spin up a stateless service that acts as the parent link to patch back to
    StatelessService parentService = new StatelessService() {

        @Override
        public void handleRequest(Operation op) {
            if (op.getAction() == Action.PATCH) {
                if (!TestGCPStatsCollection.this.isMock) {
                    ComputeStatsResponse resp = op.getBody(ComputeStatsResponse.class);
                    if (resp == null) {
                        TestGCPStatsCollection.this.host.failIteration(new IllegalStateException("response was null."));
                        return;
                    }
                    if (resp.statsList.size() != 1) {
                        TestGCPStatsCollection.this.host.failIteration(new IllegalStateException("response size was incorrect."));
                        return;
                    }
                    if (resp.statsList.get(0).statValues.size() != 9) {
                        TestGCPStatsCollection.this.host.failIteration(new IllegalStateException("incorrect number of metrics received."));
                        return;
                    }
                    if (!resp.statsList.get(0).computeLink.equals(selfLink)) {
                        TestGCPStatsCollection.this.host.failIteration(new IllegalStateException("Incorrect resourceReference returned."));
                        return;
                    }
                }
                TestGCPStatsCollection.this.host.completeIteration();
            }
        }
    };
    String servicePath = UUID.randomUUID().toString();
    Operation startOp = Operation.createPost(UriUtils.buildUri(this.host, servicePath));
    this.host.startService(startOp, parentService);
    ComputeStatsRequest statsRequest = new ComputeStatsRequest();
    statsRequest.resourceReference = UriUtils.buildUri(this.host, selfLink);
    statsRequest.isMockRequest = this.isMock;
    statsRequest.taskReference = UriUtils.buildUri(this.host, servicePath);
    this.host.sendAndWait(Operation.createPatch(UriUtils.buildUri(this.host, GCPUriPaths.GCP_STATS_ADAPTER)).setBody(statsRequest).setReferer(this.host.getUri()));
}
Also used : ComputeStatsResponse(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse) ComputeStatsRequest(com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation)

Example 14 with StatelessService

use of com.vmware.xenon.common.StatelessService in project photon-model by vmware.

the class TestAWSReservedInstancePlansService method test.

@Test
public void test() throws Throwable {
    StatelessService parentService = new StatelessService() {

        @Override
        public void handleRequest(Operation op) {
            if (op.getAction() == Action.PATCH) {
                ComputeService.ComputeState state = op.getBody(ComputeService.ComputeState.class);
                Assert.assertNotNull(state.customProperties);
                Assert.assertNotNull(state.customProperties.get(AWSConstants.RESERVED_INSTANCE_PLAN_DETAILS));
                TestAWSReservedInstancePlansService.this.host.completeIteration();
            }
        }
    };
    String servicePath = COMPUTE_SELF_LINK;
    Operation startOp = Operation.createPost(UriUtils.buildUri(this.host, servicePath));
    this.host.startService(startOp, parentService);
    this.host.sendAndWait(Operation.createPost(UriUtils.buildUri(this.host, MockAWSReservedInstancePlansService.SELF_LINK)).setBody(COMPUTE_SELF_LINK).setReferer(this.host.getUri()));
}
Also used : StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) Test(org.junit.Test)

Example 15 with StatelessService

use of com.vmware.xenon.common.StatelessService in project photon-model by vmware.

the class TestAWSProvisionTask method issueStatsRequest.

private void issueStatsRequest(ComputeState vm, Long lastCollectionTime) throws Throwable {
    // spin up a stateless service that acts as the parent link to patch back to
    StatelessService parentService = new StatelessService() {

        @Override
        public void handleRequest(Operation op) {
            if (op.getAction() == Action.PATCH) {
                if (!TestAWSProvisionTask.this.isMock) {
                    ComputeStatsResponse resp = op.getBody(ComputeStatsResponse.class);
                    if (resp.statsList.size() != 1) {
                        TestAWSProvisionTask.this.host.failIteration(new IllegalStateException("response size was incorrect."));
                        return;
                    }
                    // Size == 1, because APICallCount
                    if (resp.statsList.get(0).statValues.size() == 1) {
                        TestAWSProvisionTask.this.host.failIteration(new IllegalStateException("incorrect number of metrics received."));
                        return;
                    }
                    if (lastCollectionTime != null) {
                        if (resp.statsList.get(0).statValues.get(PhotonModelConstants.CPU_UTILIZATION_PERCENT).size() < 2) {
                            TestAWSProvisionTask.this.host.failIteration(new IllegalStateException("incorrect number of data points received when collection window is specified."));
                            return;
                        }
                    }
                    if (!resp.statsList.get(0).computeLink.equals(vm.documentSelfLink)) {
                        TestAWSProvisionTask.this.host.failIteration(new IllegalStateException("Incorrect resourceReference returned."));
                        return;
                    }
                    verifyCollectedStats(resp, lastCollectionTime);
                }
                TestAWSProvisionTask.this.host.completeIteration();
            }
        }
    };
    String servicePath = UUID.randomUUID().toString();
    Operation startOp = Operation.createPost(UriUtils.buildUri(this.host, servicePath));
    this.host.startService(startOp, parentService);
    ComputeStatsRequest statsRequest = new ComputeStatsRequest();
    statsRequest.resourceReference = UriUtils.buildUri(this.host, vm.documentSelfLink);
    statsRequest.isMockRequest = this.isMock;
    statsRequest.nextStage = SingleResourceTaskCollectionStage.UPDATE_STATS.name();
    statsRequest.taskReference = UriUtils.buildUri(this.host, servicePath);
    if (lastCollectionTime != null) {
        statsRequest.lastCollectionTimeMicrosUtc = lastCollectionTime;
    }
    this.host.sendAndWait(Operation.createPatch(UriUtils.buildUri(this.host, AWSUriPaths.AWS_STATS_ADAPTER)).setBody(statsRequest).setReferer(this.host.getUri()));
}
Also used : ComputeStatsResponse(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse) ComputeStatsRequest(com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation)

Aggregations

StatelessService (com.vmware.xenon.common.StatelessService)17 Operation (com.vmware.xenon.common.Operation)14 AuthCredentialsServiceState (com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)6 ComputeStatsRequest (com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest)5 ComputeStatsResponse (com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse)5 URI (java.net.URI)4 AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)3 ResourceOperation (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation)3 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)3 EndpointState (com.vmware.photon.controller.model.resources.EndpointService.EndpointState)3 AsyncHandler (com.amazonaws.handlers.AsyncHandler)2 AmazonCloudWatchAsyncClient (com.amazonaws.services.cloudwatch.AmazonCloudWatchAsyncClient)2 DescribeAlarmsRequest (com.amazonaws.services.cloudwatch.model.DescribeAlarmsRequest)2 DescribeAlarmsResult (com.amazonaws.services.cloudwatch.model.DescribeAlarmsResult)2 AmazonElasticLoadBalancingAsyncClient (com.amazonaws.services.elasticloadbalancing.AmazonElasticLoadBalancingAsyncClient)2 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)2 TransferManager (com.amazonaws.services.s3.transfer.TransferManager)2 PhotonModelMetricServices (com.vmware.photon.controller.model.PhotonModelMetricServices)2 PhotonModelServices (com.vmware.photon.controller.model.PhotonModelServices)2 UriPaths (com.vmware.photon.controller.model.UriPaths)2