use of com.amazonaws.services.ec2.model.DescribeSubnetsResult in project herd by FINRAOS.
the class Ec2DaoImpl method getSubnets.
/**
* This implementation uses the DescribeSubnets API.
*/
@Override
public List<Subnet> getSubnets(Collection<String> subnetIds, AwsParamsDto awsParamsDto) {
AmazonEC2Client ec2Client = getEc2Client(awsParamsDto);
DescribeSubnetsRequest describeSubnetsRequest = new DescribeSubnetsRequest();
describeSubnetsRequest.setSubnetIds(subnetIds);
try {
DescribeSubnetsResult describeSubnetsResult = ec2Operations.describeSubnets(ec2Client, describeSubnetsRequest);
return describeSubnetsResult.getSubnets();
} catch (AmazonServiceException amazonServiceException) {
/*
* AWS throws a 400 error when any one of the specified subnet ID is not found.
* We want to catch it and throw as an handled herd error as a 404 not found.
*/
if (ERROR_CODE_SUBNET_ID_NOT_FOUND.equals(amazonServiceException.getErrorCode())) {
throw new ObjectNotFoundException(amazonServiceException.getErrorMessage(), amazonServiceException);
} else // Any other type of error we throw as is because they are unexpected.
{
throw amazonServiceException;
}
}
}
use of com.amazonaws.services.ec2.model.DescribeSubnetsResult in project photon-model by vmware.
the class AWSSubnetTaskServiceTest method deleteAwsSubnet.
public void deleteAwsSubnet() {
if (this.isMock) {
return;
}
DescribeSubnetsRequest subnetRequest = new DescribeSubnetsRequest().withFilters(new Filter(AWS_VPC_ID_FILTER, singletonList((String) this.awsTestContext.get(TestAWSSetupUtils.VPC_KEY)))).withFilters(new Filter(AWS_SUBNET_CIDR_FILTER, singletonList(AWS_NON_EXISTING_SUBNET_CIDR)));
DescribeSubnetsResult subnetResult = this.client.describeSubnets(subnetRequest);
subnetResult.getSubnets().forEach(subnet -> {
DeleteSubnetRequest deleteRequest = new DeleteSubnetRequest(subnet.getSubnetId());
this.client.deleteSubnet(deleteRequest);
});
}
use of com.amazonaws.services.ec2.model.DescribeSubnetsResult in project photon-model by vmware.
the class AWSInstanceContext method getSubnets.
/**
* For every NIC lookup associated AWS Subnet as specified by
* {@code AWSNicContext.subnetState.id}. If any of the subnets is not found then
* {@code AWSNicContext.subnet} is not populated. That's an indicator the subnet should be
* created.
*/
private DeferredResult<AWSInstanceContext> getSubnets(AWSInstanceContext context) {
if (context.nics.isEmpty()) {
return DeferredResult.completed(context);
}
List<DeferredResult<DescribeSubnetsResult>> getSubnetDRs = new ArrayList<>();
for (AWSNicContext nicCtx : context.nics) {
DescribeSubnetsRequest subnetRequest = new DescribeSubnetsRequest().withFilters(new Filter(AWS_VPC_ID_FILTER, singletonList(nicCtx.networkState.id))).withFilters(new Filter(AWS_SUBNET_ID_FILTER, singletonList(nicCtx.subnetState.id)));
String msg = "Getting AWS Subnet [" + nicCtx.networkState.id + "/" + nicCtx.subnetState.id + "] for [" + nicCtx.nicStateWithDesc.name + "] NIC for [" + context.child.name + "] VM";
AWSDeferredResultAsyncHandler<DescribeSubnetsRequest, DescribeSubnetsResult> subnetHandler = new AWSDeferredResultAsyncHandler<DescribeSubnetsRequest, DescribeSubnetsResult>(this.service, msg) {
@Override
protected DeferredResult<DescribeSubnetsResult> consumeSuccess(DescribeSubnetsRequest request, DescribeSubnetsResult result) {
// The subnet specified might not exist. It's OK cause it will be created.
if (!result.getSubnets().isEmpty()) {
nicCtx.subnet = result.getSubnets().get(0);
}
return DeferredResult.completed(result);
}
};
context.amazonEC2Client.describeSubnetsAsync(subnetRequest, subnetHandler);
getSubnetDRs.add(subnetHandler.toDeferredResult());
}
return DeferredResult.allOf(getSubnetDRs).handle((all, exc) -> {
if (exc != null) {
String msg = String.format("Error getting Subnets from AWS for [%s] VM.", context.child.name);
throw new IllegalStateException(msg, exc);
}
return context;
});
}
use of com.amazonaws.services.ec2.model.DescribeSubnetsResult in project photon-model by vmware.
the class TestAWSSetupUtils method createOrGetSubnet.
/**
* Creates a Subnet if not exist and return the Subnet id.
*/
public static Subnet createOrGetSubnet(AmazonEC2AsyncClient client, String subnetCidr, String vpcId, String zoneId) {
List<Filter> filters = new ArrayList<>();
Filter vpcFilter = new Filter();
vpcFilter.withName(VPC_KEY);
vpcFilter.withValues(vpcId);
filters.add(vpcFilter);
if (zoneId != null) {
Filter azFilter = new Filter();
azFilter.withName(AVAILABILITY_ZONE_FILTER);
azFilter.withValues(zoneId);
filters.add(azFilter);
}
DescribeSubnetsResult result = client.describeSubnets(new DescribeSubnetsRequest().withFilters(filters));
List<Subnet> defaultSubnets = new ArrayList<>();
result.getSubnets().stream().forEach(subnet -> {
subnet.getTags().stream().filter(tag -> tag.getKey().equalsIgnoreCase(TAG_KEY_FOR_TEST_RESOURCES) && tag.getValue().equalsIgnoreCase(AWS_DEFAULT_SUBNET_NAME)).forEach(tag -> defaultSubnets.add(subnet));
});
if (defaultSubnets != null && !defaultSubnets.isEmpty()) {
return defaultSubnets.get(0);
} else {
CreateSubnetRequest req = new CreateSubnetRequest().withCidrBlock(subnetCidr).withVpcId(vpcId);
if (zoneId != null) {
req.withAvailabilityZone(zoneId);
}
CreateSubnetResult res = client.createSubnet(req);
String subnetId = res.getSubnet().getSubnetId();
tagResources(client, Arrays.asList(subnetId), TAG_KEY_FOR_TEST_RESOURCES, AWS_DEFAULT_SUBNET_NAME);
return res.getSubnet();
}
}
use of com.amazonaws.services.ec2.model.DescribeSubnetsResult in project cloudbreak by hortonworks.
the class AwsResourceConnector method isMapPublicOnLaunch.
private boolean isMapPublicOnLaunch(AwsNetworkView awsNetworkView, AmazonEC2 amazonEC2Client) {
boolean mapPublicIpOnLaunch = true;
if (awsNetworkView.isExistingVPC() && awsNetworkView.isExistingSubnet()) {
DescribeSubnetsRequest describeSubnetsRequest = new DescribeSubnetsRequest();
describeSubnetsRequest.setSubnetIds(awsNetworkView.getSubnetList());
DescribeSubnetsResult describeSubnetsResult = amazonEC2Client.describeSubnets(describeSubnetsRequest);
if (!describeSubnetsResult.getSubnets().isEmpty()) {
mapPublicIpOnLaunch = describeSubnetsResult.getSubnets().get(0).isMapPublicIpOnLaunch();
}
}
return mapPublicIpOnLaunch;
}
Aggregations