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();
}
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();
}
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;
}
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();
}
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));
}
Aggregations