use of com.amazonaws.services.ec2.model.DescribeNatGatewaysRequest 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.DescribeNatGatewaysRequest in project photon-model by vmware.
the class AWSTaskStatusChecker method buildRequest.
private AmazonWebServiceRequest buildRequest(T type) {
if (type instanceof Instance) {
DescribeInstancesRequest descRequest = new DescribeInstancesRequest();
List<String> instanceIdList = new ArrayList<>();
instanceIdList.add(this.instanceId);
descRequest.setInstanceIds(instanceIdList);
return descRequest;
} else if (type instanceof NatGateway) {
DescribeNatGatewaysRequest descRequest = new DescribeNatGatewaysRequest();
List<String> instanceIdList = new ArrayList<>();
instanceIdList.add(this.instanceId);
descRequest.setNatGatewayIds(instanceIdList);
return descRequest;
} else if (type instanceof Volume) {
DescribeVolumesRequest descRequest = new DescribeVolumesRequest();
List<String> volumeIdList = new ArrayList<>();
volumeIdList.add(this.instanceId);
descRequest.setVolumeIds(volumeIdList);
return descRequest;
} else {
AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(new IllegalArgumentException("Invalid type " + type));
return null;
}
}
use of com.amazonaws.services.ec2.model.DescribeNatGatewaysRequest in project photon-model by vmware.
the class AWSTaskStatusChecker method runSearch.
private void runSearch(T type) {
AmazonWebServiceRequest descRequest = buildRequest(type);
AsyncHandler describeHandler = buildHandler(type);
if (type instanceof Instance) {
this.amazonEC2Client.describeInstancesAsync((DescribeInstancesRequest) descRequest, describeHandler);
} else if (type instanceof NatGateway) {
this.amazonEC2Client.describeNatGatewaysAsync((DescribeNatGatewaysRequest) descRequest, describeHandler);
} else if (type instanceof Volume) {
this.amazonEC2Client.describeVolumesAsync((DescribeVolumesRequest) descRequest, describeHandler);
} else {
AWSTaskStatusChecker.this.taskManager.patchTaskToFailure(new IllegalArgumentException("Invalid type " + type));
}
}
use of com.amazonaws.services.ec2.model.DescribeNatGatewaysRequest in project photon-model by vmware.
the class AWSRemoteCleanup method deleteNATGateway.
private void deleteNATGateway(String vpcId, AmazonEC2 usEastEc2Client) {
DescribeNatGatewaysRequest natGatewaysRequest = new DescribeNatGatewaysRequest().withFilter(new Filter(VPC_KEY, Collections.singletonList(vpcId)));
DescribeNatGatewaysResult natGatewaysResult = usEastEc2Client.describeNatGateways(natGatewaysRequest);
natGatewaysResult.getNatGateways().forEach(natGateway -> {
DeleteNatGatewayRequest deleteNatGatewayRequest = new DeleteNatGatewayRequest().withNatGatewayId(natGateway.getNatGatewayId());
this.host.log("Terminating stale NAT gateway: %s", natGateway.getNatGatewayId());
usEastEc2Client.deleteNatGateway(deleteNatGatewayRequest);
});
}
Aggregations