Search in sources :

Example 21 with AmazonEC2

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);
    });
}
Also used : DescribeInternetGatewaysResult(com.amazonaws.services.ec2.model.DescribeInternetGatewaysResult) DeleteInternetGatewayRequest(com.amazonaws.services.ec2.model.DeleteInternetGatewayRequest) Filter(com.amazonaws.services.ec2.model.Filter) DetachInternetGatewayRequest(com.amazonaws.services.ec2.model.DetachInternetGatewayRequest) DescribeInternetGatewaysRequest(com.amazonaws.services.ec2.model.DescribeInternetGatewaysRequest)

Example 22 with AmazonEC2

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);
        }
    });
}
Also used : DescribeSecurityGroupsRequest(com.amazonaws.services.ec2.model.DescribeSecurityGroupsRequest) DeleteSecurityGroupRequest(com.amazonaws.services.ec2.model.DeleteSecurityGroupRequest) Filter(com.amazonaws.services.ec2.model.Filter) DescribeSecurityGroupsResult(com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult)

Example 23 with AmazonEC2

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);
}
Also used : Reservation(com.amazonaws.services.ec2.model.Reservation) Instance(com.amazonaws.services.ec2.model.Instance) ArrayList(java.util.ArrayList)

Example 24 with AmazonEC2

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());
    }
}
Also used : DescribeVpcsResult(com.amazonaws.services.ec2.model.DescribeVpcsResult) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) DescribeNetworkAclsResult(com.amazonaws.services.ec2.model.DescribeNetworkAclsResult) DescribeSubnetsRequest(com.amazonaws.services.ec2.model.DescribeSubnetsRequest) DeleteVpnGatewayRequest(com.amazonaws.services.ec2.model.DeleteVpnGatewayRequest) TerminateInstancesResult(com.amazonaws.services.ec2.model.TerminateInstancesResult) ReleaseAddressRequest(com.amazonaws.services.ec2.model.ReleaseAddressRequest) DescribeSubnetsResult(com.amazonaws.services.ec2.model.DescribeSubnetsResult) CommandLineArgumentParser(com.vmware.xenon.common.CommandLineArgumentParser) Utils(com.vmware.xenon.common.Utils) DescribeNetworkAclsRequest(com.amazonaws.services.ec2.model.DescribeNetworkAclsRequest) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) Map(java.util.Map) DescribeNetworkInterfacesResult(com.amazonaws.services.ec2.model.DescribeNetworkInterfacesResult) DeleteSubnetRequest(com.amazonaws.services.ec2.model.DeleteSubnetRequest) Reservation(com.amazonaws.services.ec2.model.Reservation) DeleteVpcRequest(com.amazonaws.services.ec2.model.DeleteVpcRequest) DescribeInternetGatewaysResult(com.amazonaws.services.ec2.model.DescribeInternetGatewaysResult) DescribeVolumesResult(com.amazonaws.services.ec2.model.DescribeVolumesResult) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) DeleteNetworkInterfaceRequest(com.amazonaws.services.ec2.model.DeleteNetworkInterfaceRequest) List(java.util.List) Tag(com.amazonaws.services.ec2.model.Tag) DeleteInternetGatewayRequest(com.amazonaws.services.ec2.model.DeleteInternetGatewayRequest) BasicTestCase(com.vmware.xenon.common.BasicTestCase) HashMap(java.util.HashMap) DeleteNetworkAclRequest(com.amazonaws.services.ec2.model.DeleteNetworkAclRequest) DescribeNetworkInterfacesRequest(com.amazonaws.services.ec2.model.DescribeNetworkInterfacesRequest) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Regions(com.amazonaws.regions.Regions) DescribeVpcsResult(com.amazonaws.services.ec2.model.DescribeVpcsResult) DisassociateAddressRequest(com.amazonaws.services.ec2.model.DisassociateAddressRequest) DescribeSecurityGroupsResult(com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeSecurityGroupsRequest(com.amazonaws.services.ec2.model.DescribeSecurityGroupsRequest) Filter(com.amazonaws.services.ec2.model.Filter) Volume(com.amazonaws.services.ec2.model.Volume) DescribeVpnGatewaysResult(com.amazonaws.services.ec2.model.DescribeVpnGatewaysResult) Instance(com.amazonaws.services.ec2.model.Instance) Before(org.junit.Before) Vpc(com.amazonaws.services.ec2.model.Vpc) DescribeNatGatewaysResult(com.amazonaws.services.ec2.model.DescribeNatGatewaysResult) Test(org.junit.Test) DescribeAddressesRequest(com.amazonaws.services.ec2.model.DescribeAddressesRequest) DetachInternetGatewayRequest(com.amazonaws.services.ec2.model.DetachInternetGatewayRequest) DetachVpnGatewayRequest(com.amazonaws.services.ec2.model.DetachVpnGatewayRequest) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest) DeleteNatGatewayRequest(com.amazonaws.services.ec2.model.DeleteNatGatewayRequest) Bucket(com.amazonaws.services.s3.model.Bucket) DeleteSecurityGroupRequest(com.amazonaws.services.ec2.model.DeleteSecurityGroupRequest) DescribeNatGatewaysRequest(com.amazonaws.services.ec2.model.DescribeNatGatewaysRequest) TimeUnit(java.util.concurrent.TimeUnit) DescribeVpnGatewaysRequest(com.amazonaws.services.ec2.model.DescribeVpnGatewaysRequest) DeleteVolumeRequest(com.amazonaws.services.ec2.model.DeleteVolumeRequest) DescribeAddressesResult(com.amazonaws.services.ec2.model.DescribeAddressesResult) Collections(java.util.Collections) DescribeInternetGatewaysRequest(com.amazonaws.services.ec2.model.DescribeInternetGatewaysRequest) Vpc(com.amazonaws.services.ec2.model.Vpc) ArrayList(java.util.ArrayList) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Filter(com.amazonaws.services.ec2.model.Filter) DeleteVpcRequest(com.amazonaws.services.ec2.model.DeleteVpcRequest) Test(org.junit.Test)

Example 25 with AmazonEC2

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());
    });
}
Also used : TerminateInstancesResult(com.amazonaws.services.ec2.model.TerminateInstancesResult) TerminateInstancesRequest(com.amazonaws.services.ec2.model.TerminateInstancesRequest)

Aggregations

AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)65 Filter (com.amazonaws.services.ec2.model.Filter)25 Instance (com.amazonaws.services.ec2.model.Instance)20 DescribeImagesRequest (com.amazonaws.services.ec2.model.DescribeImagesRequest)17 DescribeImagesResult (com.amazonaws.services.ec2.model.DescribeImagesResult)15 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)12 Reservation (com.amazonaws.services.ec2.model.Reservation)12 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)10 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)9 DescribeAddressesResult (com.amazonaws.services.ec2.model.DescribeAddressesResult)8 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)8 Tag (com.amazonaws.services.ec2.model.Tag)8 AWSCredentials (com.amazonaws.auth.AWSCredentials)6 DescribeAddressesRequest (com.amazonaws.services.ec2.model.DescribeAddressesRequest)6 PropertiesCredentials (com.amazonaws.auth.PropertiesCredentials)5 DescribeSecurityGroupsResult (com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult)5 DisassociateAddressRequest (com.amazonaws.services.ec2.model.DisassociateAddressRequest)5 ReleaseAddressRequest (com.amazonaws.services.ec2.model.ReleaseAddressRequest)5 AmazonServiceException (com.amazonaws.AmazonServiceException)4