Search in sources :

Example 6 with StatelessService

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

the class AzureLifecycleOperationServiceTest 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 : AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) ProvisionComputeTaskService(com.vmware.photon.controller.model.tasks.ProvisionComputeTaskService) Assume.assumeFalse(org.junit.Assume.assumeFalse) 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) PhotonModelMetricServices(com.vmware.photon.controller.model.PhotonModelMetricServices) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) AzureTestUtil.createDefaultResourcePool(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.createDefaultResourcePool) 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) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) 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) AzureTestUtil.createDefaultComputeHost(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.createDefaultComputeHost) After(org.junit.After) AzureEnvironment(com.microsoft.azure.AzureEnvironment) BasicReusableHostTestCase(com.vmware.xenon.common.BasicReusableHostTestCase) Before(org.junit.Before) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) DEFAULT_NIC_SPEC(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.DEFAULT_NIC_SPEC) PhotonModelTaskServices(com.vmware.photon.controller.model.tasks.PhotonModelTaskServices) ComputeManagementClientImpl(com.microsoft.azure.management.compute.implementation.ComputeManagementClientImpl) StatelessService(com.vmware.xenon.common.StatelessService) Operation(com.vmware.xenon.common.Operation) ResourceOperationResponse(com.vmware.photon.controller.model.adapterapi.ResourceOperationResponse) TestUtils(com.vmware.photon.controller.model.tasks.TestUtils) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) UUID(java.util.UUID) Rule(org.junit.Rule) TestContext(com.vmware.xenon.common.test.TestContext) PhotonModelAdaptersRegistryAdapters(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryAdapters) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) AzureAdaptersTestUtils(com.vmware.photon.controller.model.adapters.azure.base.AzureAdaptersTestUtils) AzureTestUtil.generateName(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.generateName) UriUtils(com.vmware.xenon.common.UriUtils) Action(com.vmware.xenon.common.Service.Action) TaskState(com.vmware.xenon.common.TaskState) AzureTestUtil.createDefaultEndpointState(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.createDefaultEndpointState) ApplicationTokenCredentials(com.microsoft.azure.credentials.ApplicationTokenCredentials) StatelessService(com.vmware.xenon.common.StatelessService) TestContext(com.vmware.xenon.common.test.TestContext) Operation(com.vmware.xenon.common.Operation) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation)

Example 7 with StatelessService

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

the class BaseVSphereAdapterTest method createTaskResultListener.

protected 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);
            }
        }
    };
    Operation startOp = Operation.createPost(host, taskLink).setCompletion(this.host.getCompletion()).setReferer(this.host.getReferer());
    this.host.startService(startOp, service);
}
Also used : StatelessService(com.vmware.xenon.common.StatelessService) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) Operation(com.vmware.xenon.common.Operation)

Example 8 with StatelessService

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

the class AWSClientManager method getOrCreateCloudWatchClient.

/**
 * Get or create a CloudWatch Client instance that will be used to get stats from AWS.
 *
 * Note: ARN-based credentials will not be accepted unless they have already been exchanged to
 * AWS for session credentials. If unset, this method will throw a
 * {@link UnsupportedOperationException} exception in this circumstance. To enable ARN-based
 * credentials, migrate to {@link #getOrCreateCloudWatchClientAsync(AuthCredentialsServiceState,
 * String, StatelessService, boolean)}.
 *
 * @param credentials The auth credentials to be used for the client creation
 * @param regionId The region of the AWS client
 * @param service The stateless service for which the operation is being performed.
 * @param isMock Indicates if this a mock request
 * @return
 */
public AmazonCloudWatchAsyncClient getOrCreateCloudWatchClient(AuthCredentialsServiceState credentials, String regionId, StatelessService service, boolean isMock, Consumer<Throwable> failConsumer) {
    if (this.awsClientType != AwsClientType.CLOUD_WATCH) {
        throw new UnsupportedOperationException("This client manager supports only AWS " + this.awsClientType + " clients.");
    }
    if (isArnCredentials(credentials) && !isSetCredentials(credentials)) {
        throw new UnsupportedOperationException("For ARN-based credentials, exchange for session-based access key/secret key first before retrieving the client.");
    }
    String cacheKey = createCredentialRegionCacheKey(credentials, regionId);
    if (isCloudWatchClientInvalid(cacheKey)) {
        failConsumer.accept(new IllegalStateException("Invalid cloud watch client for key: " + cacheKey));
        return null;
    }
    AmazonCloudWatchAsyncClient amazonCloudWatchClient = null;
    try {
        amazonCloudWatchClient = this.cloudWatchClientCache.computeIfAbsent(cacheKey, key -> {
            AmazonCloudWatchAsyncClient client = AWSUtils.getStatsAsyncClient(credentials, regionId, getExecutor(), isMock);
            client.describeAlarmsAsync(new AsyncHandler<DescribeAlarmsRequest, DescribeAlarmsResult>() {

                @Override
                public void onError(Exception exception) {
                    markCloudWatchClientInvalid(service, cacheKey);
                }

                @Override
                public void onSuccess(DescribeAlarmsRequest request, DescribeAlarmsResult result) {
                // noop
                }
            });
            return client;
        });
    } catch (Throwable e) {
        service.logSevere(e);
        failConsumer.accept(e);
    }
    return amazonCloudWatchClient;
}
Also used : S3_TM_CLIENT_CACHE_INITIAL_SIZE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.S3_TM_CLIENT_CACHE_INITIAL_SIZE) TransferManager(com.amazonaws.services.s3.transfer.TransferManager) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) LRUCache(com.vmware.photon.controller.model.adapters.util.LRUCache) OperationContext(com.vmware.xenon.common.OperationContext) INVALID_CLIENT_CACHE_INITIAL_SIZE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.INVALID_CLIENT_CACHE_INITIAL_SIZE) AmazonCloudWatchAsyncClient(com.amazonaws.services.cloudwatch.AmazonCloudWatchAsyncClient) HashMap(java.util.HashMap) S3_CLIENT_CACHE_INITIAL_SIZE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.S3_CLIENT_CACHE_INITIAL_SIZE) AWSUtils.isArnCredentials(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.isArnCredentials) Utils(com.vmware.xenon.common.Utils) UriPaths(com.vmware.photon.controller.model.UriPaths) CW_CLIENT_CACHE_INITIAL_SIZE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.CW_CLIENT_CACHE_INITIAL_SIZE) Map(java.util.Map) INVALID_CLIENT_CACHE_MAX_SIZE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.INVALID_CLIENT_CACHE_MAX_SIZE) EXTERNAL_ID_KEY(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.EXTERNAL_ID_KEY) AwsClientType(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AwsClientType) EC2_CLIENT_CACHE_MAX_SIZE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.EC2_CLIENT_CACHE_MAX_SIZE) LB_CLIENT_CACHE_INITIAL_SIZE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.LB_CLIENT_CACHE_INITIAL_SIZE) ExecutorService(java.util.concurrent.ExecutorService) AWSUtils.awsSessionCredentialsToAuthCredentialsState(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.awsSessionCredentialsToAuthCredentialsState) EC2_CLIENT_CACHE_INITIAL_SIZE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.EC2_CLIENT_CACHE_INITIAL_SIZE) AWSUtils.getArnSessionCredentialsAsync(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.getArnSessionCredentialsAsync) DescribeAlarmsRequest(com.amazonaws.services.cloudwatch.model.DescribeAlarmsRequest) StatelessService(com.vmware.xenon.common.StatelessService) TILDA(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.TILDA) CompletionException(java.util.concurrent.CompletionException) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) S3_CLIENT_CACHE_MAX_SIZE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.S3_CLIENT_CACHE_MAX_SIZE) AWSUtils(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CW_CLIENT_CACHE_MAX_SIZE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.CW_CLIENT_CACHE_MAX_SIZE) AmazonElasticLoadBalancingAsyncClient(com.amazonaws.services.elasticloadbalancing.AmazonElasticLoadBalancingAsyncClient) ARN_KEY(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.ARN_KEY) DescribeAlarmsResult(com.amazonaws.services.cloudwatch.model.DescribeAlarmsResult) S3_TM_CLIENT_CACHE_MAX_SIZE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.S3_TM_CLIENT_CACHE_MAX_SIZE) AsyncHandler(com.amazonaws.handlers.AsyncHandler) DeferredResult(com.vmware.xenon.common.DeferredResult) LB_CLIENT_CACHE_MAX_SIZE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.LB_CLIENT_CACHE_MAX_SIZE) Collections(java.util.Collections) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) AsyncHandler(com.amazonaws.handlers.AsyncHandler) DescribeAlarmsResult(com.amazonaws.services.cloudwatch.model.DescribeAlarmsResult) AmazonCloudWatchAsyncClient(com.amazonaws.services.cloudwatch.AmazonCloudWatchAsyncClient) DescribeAlarmsRequest(com.amazonaws.services.cloudwatch.model.DescribeAlarmsRequest) CompletionException(java.util.concurrent.CompletionException)

Example 9 with StatelessService

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

the class AWSClientManager method getOrCreateEC2Client.

/**
 * Accesses the client cache to get the EC2 client for the given auth credentials and regionId.
 * If a client is not found to exist, creates a new one and adds an entry in the cache for it.
 *
 * Note: ARN-based credentials will not be accepted unless they have already been exchanged to
 * AWS for session credentials. If unset, this method will throw a
 * {@link UnsupportedOperationException} exception in this circumstance. To enable ARN-based
 * credentials, migrate to {@link #getOrCreateEC2ClientAsync(AuthCredentialsServiceState,
 * String, StatelessService)}.
 *
 * @param credentials The auth credentials to be used for the client creation
 * @param regionId The region of the AWS client
 * @param service The stateless service making the request and for which the executor pool needs to be allocated.
 * @return The AWSClient
 */
public AmazonEC2AsyncClient getOrCreateEC2Client(AuthCredentialsServiceState credentials, String regionId, StatelessService service, Consumer<Throwable> failConsumer) {
    if (this.awsClientType != AwsClientType.EC2) {
        throw new UnsupportedOperationException("This client manager supports only AWS " + this.awsClientType + " clients.");
    }
    if (isArnCredentials(credentials) && !isSetCredentials(credentials)) {
        throw new UnsupportedOperationException("For ARN-based credentials, exchange for session-based access key/secret key first before retrieving the client.");
    }
    AmazonEC2AsyncClient amazonEC2Client = null;
    String cacheKey = createCredentialRegionCacheKey(credentials, regionId);
    try {
        amazonEC2Client = this.ec2ClientCache.computeIfAbsent(cacheKey, key -> AWSUtils.getAsyncClient(credentials, regionId, getExecutor()));
    } catch (Throwable e) {
        service.logSevere(e);
        failConsumer.accept(e);
    }
    return amazonEC2Client;
}
Also used : S3_TM_CLIENT_CACHE_INITIAL_SIZE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.S3_TM_CLIENT_CACHE_INITIAL_SIZE) TransferManager(com.amazonaws.services.s3.transfer.TransferManager) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) LRUCache(com.vmware.photon.controller.model.adapters.util.LRUCache) OperationContext(com.vmware.xenon.common.OperationContext) INVALID_CLIENT_CACHE_INITIAL_SIZE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.INVALID_CLIENT_CACHE_INITIAL_SIZE) AmazonCloudWatchAsyncClient(com.amazonaws.services.cloudwatch.AmazonCloudWatchAsyncClient) HashMap(java.util.HashMap) S3_CLIENT_CACHE_INITIAL_SIZE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.S3_CLIENT_CACHE_INITIAL_SIZE) AWSUtils.isArnCredentials(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.isArnCredentials) Utils(com.vmware.xenon.common.Utils) UriPaths(com.vmware.photon.controller.model.UriPaths) CW_CLIENT_CACHE_INITIAL_SIZE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.CW_CLIENT_CACHE_INITIAL_SIZE) Map(java.util.Map) INVALID_CLIENT_CACHE_MAX_SIZE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.INVALID_CLIENT_CACHE_MAX_SIZE) EXTERNAL_ID_KEY(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.EXTERNAL_ID_KEY) AwsClientType(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AwsClientType) EC2_CLIENT_CACHE_MAX_SIZE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.EC2_CLIENT_CACHE_MAX_SIZE) LB_CLIENT_CACHE_INITIAL_SIZE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.LB_CLIENT_CACHE_INITIAL_SIZE) ExecutorService(java.util.concurrent.ExecutorService) AWSUtils.awsSessionCredentialsToAuthCredentialsState(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.awsSessionCredentialsToAuthCredentialsState) EC2_CLIENT_CACHE_INITIAL_SIZE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.EC2_CLIENT_CACHE_INITIAL_SIZE) AWSUtils.getArnSessionCredentialsAsync(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.getArnSessionCredentialsAsync) DescribeAlarmsRequest(com.amazonaws.services.cloudwatch.model.DescribeAlarmsRequest) StatelessService(com.vmware.xenon.common.StatelessService) TILDA(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils.TILDA) CompletionException(java.util.concurrent.CompletionException) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) S3_CLIENT_CACHE_MAX_SIZE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.S3_CLIENT_CACHE_MAX_SIZE) AWSUtils(com.vmware.photon.controller.model.adapters.awsadapter.AWSUtils) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CW_CLIENT_CACHE_MAX_SIZE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.CW_CLIENT_CACHE_MAX_SIZE) AmazonElasticLoadBalancingAsyncClient(com.amazonaws.services.elasticloadbalancing.AmazonElasticLoadBalancingAsyncClient) ARN_KEY(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.ARN_KEY) DescribeAlarmsResult(com.amazonaws.services.cloudwatch.model.DescribeAlarmsResult) S3_TM_CLIENT_CACHE_MAX_SIZE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.S3_TM_CLIENT_CACHE_MAX_SIZE) AsyncHandler(com.amazonaws.handlers.AsyncHandler) DeferredResult(com.vmware.xenon.common.DeferredResult) LB_CLIENT_CACHE_MAX_SIZE(com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.LB_CLIENT_CACHE_MAX_SIZE) Collections(java.util.Collections) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient)

Example 10 with StatelessService

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

the class AWSResetServiceTest 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);
            }
        }
    };
    Operation startOp = Operation.createPost(host, taskLink).setCompletion(this.host.getCompletion()).setReferer(this.host.getReferer());
    this.host.startService(startOp, service);
}
Also used : StatelessService(com.vmware.xenon.common.StatelessService) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) 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