Search in sources :

Example 1 with AmazonCloudWatchAsyncClient

use of com.amazonaws.services.cloudwatch.AmazonCloudWatchAsyncClient in project photon-model by vmware.

the class AWSUtils method getStatsAsyncClient.

/**
 * Method to get a CloudWatch Async Client.
 *
 * Note: ARN-based credentials will not work unless they have already been exchanged to
 * AWS for session credentials. If unset, this method will fail. To enable ARN-based
 * credentials, migrate to {@link #getCloudWatchStatsAsyncClient(AuthCredentialsServiceState,
 * String, ExecutorService, boolean)}.
 *
 * @param credentials An {@link AuthCredentialsServiceState} object.
 * @param region The region to get the AWS client in.
 * @param executorService The executor service to run async services in.
 */
public static AmazonCloudWatchAsyncClient getStatsAsyncClient(AuthCredentialsServiceState credentials, String region, ExecutorService executorService, boolean isMockRequest) {
    ClientConfiguration configuration = createClientConfiguration();
    AmazonCloudWatchAsyncClientBuilder amazonCloudWatchAsyncClientBuilder = AmazonCloudWatchAsyncClientBuilder.standard().withClientConfiguration(configuration).withCredentials(getAwsStaticCredentialsProvider(credentials)).withExecutorFactory(() -> executorService);
    if (region == null) {
        region = Regions.DEFAULT_REGION.getName();
    }
    if (isAwsClientMock()) {
        configuration.addHeader(AWS_REGION_HEADER, region);
        amazonCloudWatchAsyncClientBuilder.setClientConfiguration(configuration);
        AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(getAWSMockHost() + AWS_MOCK_CLOUDWATCH_ENDPOINT, region);
        amazonCloudWatchAsyncClientBuilder.setEndpointConfiguration(endpointConfiguration);
    } else {
        amazonCloudWatchAsyncClientBuilder.setRegion(region);
    }
    return (AmazonCloudWatchAsyncClient) amazonCloudWatchAsyncClientBuilder.build();
}
Also used : AmazonCloudWatchAsyncClientBuilder(com.amazonaws.services.cloudwatch.AmazonCloudWatchAsyncClientBuilder) AmazonCloudWatchAsyncClient(com.amazonaws.services.cloudwatch.AmazonCloudWatchAsyncClient) AwsClientBuilder(com.amazonaws.client.builder.AwsClientBuilder) ClientConfiguration(com.amazonaws.ClientConfiguration)

Example 2 with AmazonCloudWatchAsyncClient

use of com.amazonaws.services.cloudwatch.AmazonCloudWatchAsyncClient 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)

Aggregations

AmazonCloudWatchAsyncClient (com.amazonaws.services.cloudwatch.AmazonCloudWatchAsyncClient)2 ClientConfiguration (com.amazonaws.ClientConfiguration)1 AwsClientBuilder (com.amazonaws.client.builder.AwsClientBuilder)1 AsyncHandler (com.amazonaws.handlers.AsyncHandler)1 AmazonCloudWatchAsyncClientBuilder (com.amazonaws.services.cloudwatch.AmazonCloudWatchAsyncClientBuilder)1 DescribeAlarmsRequest (com.amazonaws.services.cloudwatch.model.DescribeAlarmsRequest)1 DescribeAlarmsResult (com.amazonaws.services.cloudwatch.model.DescribeAlarmsResult)1 AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)1 AmazonElasticLoadBalancingAsyncClient (com.amazonaws.services.elasticloadbalancing.AmazonElasticLoadBalancingAsyncClient)1 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)1 TransferManager (com.amazonaws.services.s3.transfer.TransferManager)1 UriPaths (com.vmware.photon.controller.model.UriPaths)1 ARN_KEY (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.ARN_KEY)1 EXTERNAL_ID_KEY (com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest.EXTERNAL_ID_KEY)1 AwsClientType (com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.AwsClientType)1 CW_CLIENT_CACHE_INITIAL_SIZE (com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.CW_CLIENT_CACHE_INITIAL_SIZE)1 CW_CLIENT_CACHE_MAX_SIZE (com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.CW_CLIENT_CACHE_MAX_SIZE)1 EC2_CLIENT_CACHE_INITIAL_SIZE (com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.EC2_CLIENT_CACHE_INITIAL_SIZE)1 EC2_CLIENT_CACHE_MAX_SIZE (com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.EC2_CLIENT_CACHE_MAX_SIZE)1 INVALID_CLIENT_CACHE_INITIAL_SIZE (com.vmware.photon.controller.model.adapters.awsadapter.AWSConstants.INVALID_CLIENT_CACHE_INITIAL_SIZE)1