use of com.amazonaws.services.ec2.model.DescribeSubnetsRequest 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.DescribeSubnetsRequest in project photon-model by vmware.
the class AWSSubnetTaskServiceTest method testCreateSubnetWithOutboundAccess.
@Test
public void testCreateSubnetWithOutboundAccess() throws Throwable {
// provision a "public" subnet first
SubnetState publicSubnetState = provisionSubnet(AWS_NON_EXISTING_PUBLIC_SUBNET_NAME, AWS_NON_EXISTING_PUBLIC_SUBNET_CIDR, null);
assertNotNull(publicSubnetState.id);
assertEquals(LifecycleState.READY, publicSubnetState.lifecycleState);
SubnetState subnetState = provisionSubnet(AWS_NON_EXISTING_SUBNET_NAME, AWS_NON_EXISTING_SUBNET_CIDR, publicSubnetState.documentSelfLink);
assertNotNull(subnetState.id);
assertEquals(LifecycleState.READY, subnetState.lifecycleState);
if (!this.isMock) {
// Verify that the subnet was created.
DescribeSubnetsRequest describeRequest = new DescribeSubnetsRequest().withSubnetIds(Collections.singletonList(subnetState.id));
List<Subnet> subnets = this.client.describeSubnets(describeRequest).getSubnets();
assertNotNull(subnets);
assertEquals(1, subnets.size());
// Verify that a NAT gateway was created
assertNotNull(subnetState.customProperties);
String natGatewayId = subnetState.customProperties.get(AWS_NAT_GATEWAY_ID);
String routeTableId = subnetState.customProperties.get(AWS_ROUTE_TABLE_ID);
String allocationId = subnetState.customProperties.get(AWS_ELASTIC_IP_ALLOCATION_ID);
assertNotNull(natGatewayId);
assertNotNull(routeTableId);
assertNotNull(allocationId);
DescribeNatGatewaysRequest describeNatGatewaysRequest = new DescribeNatGatewaysRequest().withNatGatewayIds(Collections.singletonList(natGatewayId));
List<NatGateway> natGateways = this.client.describeNatGateways(describeNatGatewaysRequest).getNatGateways();
assertNotNull(natGateways);
assertEquals(1, natGateways.size());
NatGateway natGateway = natGateways.get(0);
assertEquals(publicSubnetState.id, natGateway.getSubnetId());
assertNotNull(natGateway.getNatGatewayAddresses());
assertEquals(1, natGateway.getNatGatewayAddresses().size());
assertEquals(allocationId, natGateway.getNatGatewayAddresses().get(0).getAllocationId());
assertEquals("available", natGateways.get(0).getState());
// verify that a route table was created
DescribeRouteTablesRequest describeRouteTablesRequest = new DescribeRouteTablesRequest().withRouteTableIds(Collections.singletonList(routeTableId));
List<RouteTable> routeTables = this.client.describeRouteTables(describeRouteTablesRequest).getRouteTables();
assertNotNull(routeTables);
assertEquals(1, routeTables.size());
RouteTable routeTable = routeTables.get(0);
assertNotNull(routeTable.getAssociations());
assertEquals(1, routeTable.getAssociations().size());
assertEquals(subnetState.id, routeTable.getAssociations().get(0).getSubnetId());
assertNotNull(routeTable.getRoutes());
assertEquals(2, routeTable.getRoutes().size());
boolean hasRouteToNatGateway = false;
for (Route route : routeTable.getRoutes()) {
if (route.getDestinationCidrBlock().equals("0.0.0.0/0") && route.getNatGatewayId() != null && route.getNatGatewayId().equals(natGatewayId)) {
hasRouteToNatGateway = true;
break;
}
}
assertTrue(hasRouteToNatGateway);
// Verify that an IP address allocation was created
DescribeAddressesRequest describeAddressesRequest = new DescribeAddressesRequest().withAllocationIds(Collections.singletonList(allocationId));
List<Address> addresses = this.client.describeAddresses(describeAddressesRequest).getAddresses();
assertNotNull(addresses);
assertEquals(1, addresses.size());
}
// delete the subnet
kickOffSubnetProvision(InstanceRequestType.DELETE, subnetState, TaskStage.FINISHED);
if (!this.isMock) {
// Verify that the subnet was deleted.
DescribeSubnetsRequest describeRequest = new DescribeSubnetsRequest().withSubnetIds(Collections.singletonList(subnetState.id));
try {
this.client.describeSubnets(describeRequest).getSubnets();
fail("Subnet should not exist in AWS.");
} catch (AmazonEC2Exception ex) {
assertEquals(HttpResponseStatus.BAD_REQUEST.code(), ex.getStatusCode());
}
// Verify that the NAT gateway was deleted
String natGatewayId = subnetState.customProperties.get(AWS_NAT_GATEWAY_ID);
String routeTableId = subnetState.customProperties.get(AWS_ROUTE_TABLE_ID);
String allocationId = subnetState.customProperties.get(AWS_ELASTIC_IP_ALLOCATION_ID);
DescribeNatGatewaysRequest describeNatGatewaysRequest = new DescribeNatGatewaysRequest().withNatGatewayIds(Collections.singletonList(natGatewayId));
List<NatGateway> natGateways = this.client.describeNatGateways(describeNatGatewaysRequest).getNatGateways();
assertNotNull(natGateways);
assertEquals(1, natGateways.size());
assertEquals("deleted", natGateways.get(0).getState());
// Verify that the route table was deleted
DescribeRouteTablesRequest describeRouteTablesRequest = new DescribeRouteTablesRequest().withRouteTableIds(Collections.singletonList(routeTableId));
try {
this.client.describeRouteTables(describeRouteTablesRequest).getRouteTables();
fail("Route table should not exist in AWS.");
} catch (AmazonEC2Exception ex) {
assertEquals(HttpResponseStatus.BAD_REQUEST.code(), ex.getStatusCode());
}
DescribeAddressesRequest describeAddressesRequest = new DescribeAddressesRequest().withAllocationIds(Collections.singletonList(allocationId));
try {
this.client.describeAddresses(describeAddressesRequest).getAddresses();
fail("IP address allocation should not exist in AWS.");
} catch (AmazonEC2Exception ex) {
assertEquals(HttpResponseStatus.BAD_REQUEST.code(), ex.getStatusCode());
}
}
}
use of com.amazonaws.services.ec2.model.DescribeSubnetsRequest 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.DescribeSubnetsRequest 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.DescribeSubnetsRequest in project photon-model by vmware.
the class AWSNetworkStateEnumerationAdapterService method getSubnetInformation.
/**
* Gets the Subnets that are attached to the VPCs that were discovered during the enumeration
* process.
*/
private void getSubnetInformation(AWSNetworkStateCreationContext context, AWSNetworkStateCreationStage next) {
DescribeSubnetsRequest subnetRequest = new DescribeSubnetsRequest();
List<String> vpcList = new ArrayList<>(context.vpcs.keySet());
Filter filter = new Filter(AWS_VPC_ID_FILTER, vpcList);
subnetRequest.getFilters().add(filter);
AWSSubnetAsyncHandler asyncHandler = new AWSSubnetAsyncHandler(next, context);
context.amazonEC2Client.describeSubnetsAsync(subnetRequest, asyncHandler);
}
Aggregations