use of com.amazonaws.services.ec2.model.DeleteVpcRequest in project photon-model by vmware.
the class AWSRemoteCleanup method cleanUpVpc.
/**
* Cleaning all VPC's that are not tagged with a name: enumtest-vpc or a default VPC in US_EAST_1 region
* Deleting a VPC would require its dependencies to be deleted in the following order:
* 1) EC2 Instances
* 2) NAT Gateway
* 3) Internet Gateway
* 4) VPN Gateway
* 5) Network ACL's
* 6) Security Group ( not deleting default SG)
* 7) Subnets
* NOTE: Not deleting RouteTables currently
*/
@Test
public void cleanUpVpc() {
if (this.isMock) {
return;
}
AmazonEC2 usEastEc2Client = this.ec2Clients.get(US_EAST_1_TAG);
DescribeVpcsResult vpcsResult = usEastEc2Client.describeVpcs();
List<Vpc> vpcs = vpcsResult.getVpcs();
List<String> vpcIdsToBeDeleted = new ArrayList<>();
List<String> enumTestVpcIds = new ArrayList<>();
try {
vpcs.stream().forEach(vpc -> {
vpc.getTags().stream().filter(tag -> tag.getKey().equalsIgnoreCase(NAME_TAG_KEY) && this.vpcTagsNotToBeDeleted.contains(tag.getValue().toLowerCase())).forEach(tag -> enumTestVpcIds.add(vpc.getVpcId()));
if (!vpc.getIsDefault()) {
vpcIdsToBeDeleted.add(vpc.getVpcId());
}
});
vpcIdsToBeDeleted.removeAll(enumTestVpcIds);
vpcIdsToBeDeleted.stream().forEach(vpcId -> {
DescribeInstancesRequest instancesRequest = new DescribeInstancesRequest().withFilters(new Filter(VPC_KEY, Collections.singletonList(vpcId)));
DescribeInstancesResult instancesResult = usEastEc2Client.describeInstances(instancesRequest);
deleteAwsEc2instances(vpcIdsToBeDeleted, instancesResult, usEastEc2Client);
deleteNATGateway(vpcId, usEastEc2Client);
deleteNetworkInterfaces(vpcId, usEastEc2Client);
deleteInternetGateways(vpcId, usEastEc2Client);
deleteVirtualPrivateGateways(vpcId, usEastEc2Client);
disassociateAndDeleteNetworkACLs(vpcId, usEastEc2Client);
deleteSecurityGroups(vpcId, usEastEc2Client);
deleteSubnets(vpcId, usEastEc2Client);
DeleteVpcRequest deleteVpcRequest = new DeleteVpcRequest().withVpcId(vpcId);
this.host.log("Terminating stale vpc: %s", vpcId);
usEastEc2Client.deleteVpc(deleteVpcRequest);
});
} catch (Exception e) {
this.host.log(Level.INFO, e.getMessage());
}
}
use of com.amazonaws.services.ec2.model.DeleteVpcRequest in project photon-model by vmware.
the class AWSNetworkClient method deleteVPC.
/**
* Delete the specified VPC
*/
public void deleteVPC(String vpcId) {
DeleteVpcRequest req = new DeleteVpcRequest().withVpcId(vpcId);
this.client.deleteVpc(req);
}
Aggregations