use of com.amazonaws.services.ec2.AmazonEC2 in project photon-model by vmware.
the class AWSRemoteCleanup method deleteInternetGateways.
private void deleteInternetGateways(String vpcId, AmazonEC2 usEastEc2Client) {
DescribeInternetGatewaysRequest internetGatewaysRequest = new DescribeInternetGatewaysRequest().withFilters(new Filter(ATTACHMENT_VPC_KEY, Collections.singletonList(vpcId)));
DescribeInternetGatewaysResult internetGatewaysResult = usEastEc2Client.describeInternetGateways(internetGatewaysRequest);
internetGatewaysResult.getInternetGateways().forEach(internetGateway -> {
DetachInternetGatewayRequest detachInternetGatewayRequest = new DetachInternetGatewayRequest().withInternetGatewayId(internetGateway.getInternetGatewayId());
detachInternetGatewayRequest.setVpcId(vpcId);
usEastEc2Client.detachInternetGateway(detachInternetGatewayRequest);
DeleteInternetGatewayRequest deleteInternetGatewayRequest = new DeleteInternetGatewayRequest().withInternetGatewayId(internetGateway.getInternetGatewayId());
this.host.log("Terminating stale internet gateway: %s", internetGateway.getInternetGatewayId());
usEastEc2Client.deleteInternetGateway(deleteInternetGatewayRequest);
});
}
use of com.amazonaws.services.ec2.AmazonEC2 in project photon-model by vmware.
the class AWSRemoteCleanup method deleteSecurityGroups.
private void deleteSecurityGroups(String vpcId, AmazonEC2 usEastEc2Client) {
DescribeSecurityGroupsRequest securityGroupsRequest = new DescribeSecurityGroupsRequest().withFilters(new Filter(VPC_KEY, Collections.singletonList(vpcId)));
DescribeSecurityGroupsResult securityGroupsResult = usEastEc2Client.describeSecurityGroups(securityGroupsRequest);
securityGroupsResult.getSecurityGroups().forEach(securityGroup -> {
if (!(securityGroup.getGroupName().equalsIgnoreCase(DEFAULT_TAG))) {
DeleteSecurityGroupRequest deleteSecurityGroupRequest = new DeleteSecurityGroupRequest().withGroupId(securityGroup.getGroupId());
this.host.log("Terminating stale security group: %s", securityGroup.getGroupId());
usEastEc2Client.deleteSecurityGroup(deleteSecurityGroupRequest);
}
});
}
use of com.amazonaws.services.ec2.AmazonEC2 in project photon-model by vmware.
the class AWSRemoteCleanup method deleteAwsEc2instances.
private void deleteAwsEc2instances(List<String> vpcIdsToBeDeleted, DescribeInstancesResult describeInstancesResult, AmazonEC2 ec2Client) {
List<String> instanceIdsToBeDeleted = new ArrayList<>();
List<Reservation> reservations = describeInstancesResult.getReservations();
for (Reservation reservation : reservations) {
List<Instance> instances = reservation.getInstances();
for (Instance instance : instances) {
long instanceLaunchTimeMicros = TimeUnit.MILLISECONDS.toMicros(instance.getLaunchTime().getTime());
long timeDifference = Utils.getNowMicrosUtc() - instanceLaunchTimeMicros;
if (timeDifference > TimeUnit.HOURS.toMicros(1) && vpcIdsToBeDeleted.contains(instance.getVpcId()) && shouldDelete(instance)) {
this.host.log(Level.INFO, "Marking %s instance for deletion", instance.getInstanceId());
instanceIdsToBeDeleted.add(instance.getInstanceId());
}
}
}
triggerEC2Deletion(instanceIdsToBeDeleted, ec2Client);
}
use of com.amazonaws.services.ec2.AmazonEC2 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.AmazonEC2 in project photon-model by vmware.
the class AWSRemoteCleanup method triggerEC2Deletion.
private void triggerEC2Deletion(List<String> instanceIdsToBeDeleted, AmazonEC2 ec2Client) {
if (instanceIdsToBeDeleted.isEmpty()) {
return;
}
TerminateInstancesRequest terminateInstancesRequest = new TerminateInstancesRequest(instanceIdsToBeDeleted);
TerminateInstancesResult terminateInstancesResult = ec2Client.terminateInstances(terminateInstancesRequest);
terminateInstancesResult.getTerminatingInstances().stream().forEach(instanceStateChange -> {
this.host.log("Terminating stale instance: %s", instanceStateChange.getInstanceId());
});
}
Aggregations