Search in sources :

Example 26 with AmazonEC2AsyncClient

use of com.amazonaws.services.ec2.AmazonEC2AsyncClient in project photon-model by vmware.

the class AWSUtils method getAsyncClient.

/**
 * Method to get an EC2 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 #getEc2AsyncClient(AuthCredentialsServiceState, String,
 * ExecutorService)}.
 *
 * @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 AmazonEC2AsyncClient getAsyncClient(AuthCredentialsServiceState credentials, String region, ExecutorService executorService) {
    ClientConfiguration configuration = createClientConfiguration().withMaxConnections(100);
    AmazonEC2AsyncClientBuilder ec2AsyncClientBuilder = AmazonEC2AsyncClientBuilder.standard().withClientConfiguration(configuration).withCredentials(getAwsStaticCredentialsProvider(credentials)).withExecutorFactory(() -> executorService);
    if (region == null) {
        region = Regions.DEFAULT_REGION.getName();
    }
    if (isAwsClientMock()) {
        configuration.addHeader(AWS_REGION_HEADER, region);
        ec2AsyncClientBuilder.setClientConfiguration(configuration);
        AwsClientBuilder.EndpointConfiguration endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(getAWSMockHost() + AWS_MOCK_EC2_ENDPOINT, region);
        ec2AsyncClientBuilder.setEndpointConfiguration(endpointConfiguration);
    } else {
        ec2AsyncClientBuilder.setRegion(region);
    }
    return (AmazonEC2AsyncClient) ec2AsyncClientBuilder.build();
}
Also used : AmazonEC2AsyncClientBuilder(com.amazonaws.services.ec2.AmazonEC2AsyncClientBuilder) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) AwsClientBuilder(com.amazonaws.client.builder.AwsClientBuilder) ClientConfiguration(com.amazonaws.ClientConfiguration)

Example 27 with AmazonEC2AsyncClient

use of com.amazonaws.services.ec2.AmazonEC2AsyncClient in project photon-model by vmware.

the class AWSUtils method getResourceTags.

/*
     * Return the tags for a giving resource
     */
public static List<TagDescription> getResourceTags(String resourceID, AmazonEC2AsyncClient client) {
    Filter resource = new Filter().withName(AWS_FILTER_RESOURCE_ID).withValues(resourceID);
    DescribeTagsRequest req = new DescribeTagsRequest().withFilters(resource);
    DescribeTagsResult result = client.describeTags(req);
    return result.getTags();
}
Also used : DescribeTagsRequest(com.amazonaws.services.ec2.model.DescribeTagsRequest) DescribeTagsResult(com.amazonaws.services.ec2.model.DescribeTagsResult) Filter(com.amazonaws.services.ec2.model.Filter)

Example 28 with AmazonEC2AsyncClient

use of com.amazonaws.services.ec2.AmazonEC2AsyncClient 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 29 with AmazonEC2AsyncClient

use of com.amazonaws.services.ec2.AmazonEC2AsyncClient in project photon-model by vmware.

the class TestAWSImageEnumerationTask method lookupAwsImage.

// Kind of overhead cause it loads almost all images just to get the first one.
// Still we need that to make the tests STABLE.
private String lookupAwsImage(AmazonEC2AsyncClient client, String virtualizationType) {
    DescribeImagesRequest request = new DescribeImagesRequest().withFilters(new Filter(AWSConstants.AWS_IMAGE_STATE_FILTER).withValues(AWSConstants.AWS_IMAGE_STATE_AVAILABLE)).withFilters(new Filter(AWSConstants.AWS_IMAGE_IS_PUBLIC_FILTER).withValues(Boolean.TRUE.toString())).withFilters(new Filter("root-device-type").withValues("ebs")).withFilters(new Filter(AWSConstants.AWS_IMAGE_VIRTUALIZATION_TYPE_FILTER).withValues(virtualizationType));
    DescribeImagesResult describeImages = client.describeImages(request);
    Image image = describeImages.getImages().stream().filter(img -> {
        for (BlockDeviceMapping blockDeviceMapping : img.getBlockDeviceMappings()) {
            // blockDeviceMapping can be with noDevice
            EbsBlockDevice ebs = blockDeviceMapping.getEbs();
            if (ebs != null) {
                return true;
            }
        }
        return false;
    }).findFirst().get();
    getHost().log(Level.INFO, "AWS '%s' image loaded (out of %s): %s [%s]", virtualizationType, describeImages.getImages().size(), image.getName(), image);
    return image.getName();
}
Also used : Filter(com.amazonaws.services.ec2.model.Filter) DescribeImagesResult(com.amazonaws.services.ec2.model.DescribeImagesResult) DescribeImagesRequest(com.amazonaws.services.ec2.model.DescribeImagesRequest) EbsBlockDevice(com.amazonaws.services.ec2.model.EbsBlockDevice) BlockDeviceMapping(com.amazonaws.services.ec2.model.BlockDeviceMapping) Image(com.amazonaws.services.ec2.model.Image)

Example 30 with AmazonEC2AsyncClient

use of com.amazonaws.services.ec2.AmazonEC2AsyncClient in project photon-model by vmware.

the class TestProvisionAWSNetwork method validateAWSArtifacts.

private void validateAWSArtifacts(String networkDescriptionLink, AuthCredentialsServiceState creds) throws Throwable {
    NetworkState net = getNetworkState(networkDescriptionLink);
    AmazonEC2AsyncClient client = AWSUtils.getAsyncClient(creds, regionId, getExecutor());
    AWSNetworkClient netSVC = new AWSNetworkClient(client);
    // if any artifact is not present then an error will be thrown
    assertNotNull(netSVC.getVPC(net.customProperties.get(AWS_VPC_ID)));
    assertNotNull(netSVC.getInternetGateway(net.customProperties.get(AWS_GATEWAY_ID)));
    List<SubnetState> subnetStates = TestUtils.getSubnetStates(this.host, net);
    assertEquals(1, subnetStates.size());
    assertNotNull(netSVC.getSubnet(subnetStates.get(0).id));
}
Also used : AWSNetworkClient(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSNetworkClient) AmazonEC2AsyncClient(com.amazonaws.services.ec2.AmazonEC2AsyncClient) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState)

Aggregations

AmazonEC2AsyncClient (com.amazonaws.services.ec2.AmazonEC2AsyncClient)18 Operation (com.vmware.xenon.common.Operation)13 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)11 Filter (com.amazonaws.services.ec2.model.Filter)11 StatelessService (com.vmware.xenon.common.StatelessService)11 AWSClientManager (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManager)10 SecurityGroup (com.amazonaws.services.ec2.model.SecurityGroup)9 StopInstancesRequest (com.amazonaws.services.ec2.model.StopInstancesRequest)9 StopInstancesResult (com.amazonaws.services.ec2.model.StopInstancesResult)9 ArrayList (java.util.ArrayList)9 AsyncHandler (com.amazonaws.handlers.AsyncHandler)8 AWSClientManagerFactory (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSClientManagerFactory)8 ComputeService (com.vmware.photon.controller.model.resources.ComputeService)8 AuthCredentialsServiceState (com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)8 HashMap (java.util.HashMap)8 List (java.util.List)8 TimeUnit (java.util.concurrent.TimeUnit)8 AmazonEC2Exception (com.amazonaws.services.ec2.model.AmazonEC2Exception)7 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)7 AWSSecurityGroupClient (com.vmware.photon.controller.model.adapters.awsadapter.util.AWSSecurityGroupClient)7