Search in sources :

Example 81 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project GNS by MobilityFirst.

the class AWSEC2 method describeKeyPairs.

/**
   * Describe Key Pairs
   *
   * @param ec2
   */
public static void describeKeyPairs(AmazonEC2 ec2) {
    StringBuilder output = new StringBuilder();
    String prefix = currentTab + "Key Pairs: ";
    DescribeKeyPairsResult dkr = ec2.describeKeyPairs();
    for (KeyPairInfo keyPairInfo : dkr.getKeyPairs()) {
        output.append(prefix);
        prefix = ", ";
        output.append(keyPairInfo.getKeyName());
    }
    System.out.println(output);
}
Also used : KeyPairInfo(com.amazonaws.services.ec2.model.KeyPairInfo) DescribeKeyPairsResult(com.amazonaws.services.ec2.model.DescribeKeyPairsResult)

Example 82 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project GNS by MobilityFirst.

the class AWSEC2 method findOrCreateSecurityGroup.

/**
   * Returns an existing security group of the given name or creates one if it does not exist.
   *
   * @param ec2
   * @param name
   * @return the name of the group
   */
public static SecurityGroup findOrCreateSecurityGroup(AmazonEC2 ec2, String name) {
    SecurityGroup result = findSecurityGroup(ec2, name);
    if (result == null) {
        createSecurityGroup(ec2, name);
        System.out.println("Created security group " + name);
    }
    return findSecurityGroup(ec2, name);
}
Also used : SecurityGroup(com.amazonaws.services.ec2.model.SecurityGroup)

Example 83 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 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);
    });
}
Also used : Filter(com.amazonaws.services.ec2.model.Filter) DescribeNatGatewaysRequest(com.amazonaws.services.ec2.model.DescribeNatGatewaysRequest) DescribeNatGatewaysResult(com.amazonaws.services.ec2.model.DescribeNatGatewaysResult) DeleteNatGatewayRequest(com.amazonaws.services.ec2.model.DeleteNatGatewayRequest)

Example 84 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project photon-model by vmware.

the class AWSRemoteCleanup method deleteStaleAwsEc2instances.

/**
 * Delete stale AWS EC2 Instances.
 */
@Test
public void deleteStaleAwsEc2instances() {
    AmazonEC2 usEastEc2Client = this.ec2Clients.get(US_EAST_1_TAG);
    List<String> instanceIdsToBeDeleted = new ArrayList<>();
    DescribeInstancesResult instancesResult = usEastEc2Client.describeInstances();
    List<Reservation> reservations = instancesResult.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) && shouldDelete(instance)) {
                this.host.log(Level.INFO, "Marking %s instance for deletion", instance.getInstanceId());
                instanceIdsToBeDeleted.add(instance.getInstanceId());
            }
        }
    }
    triggerEC2Deletion(instanceIdsToBeDeleted, usEastEc2Client);
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) Instance(com.amazonaws.services.ec2.model.Instance) ArrayList(java.util.ArrayList) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) Test(org.junit.Test)

Example 85 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project photon-model by vmware.

the class AWSRemoteCleanup method deleteVirtualPrivateGateways.

private void deleteVirtualPrivateGateways(String vpcId, AmazonEC2 usEastEc2Client) {
    DescribeVpnGatewaysRequest vpnGatewaysRequest = new DescribeVpnGatewaysRequest().withFilters(new Filter(ATTACHMENT_VPC_KEY, Collections.singletonList(vpcId)));
    DescribeVpnGatewaysResult vpnGatewaysResult = usEastEc2Client.describeVpnGateways(vpnGatewaysRequest);
    vpnGatewaysResult.getVpnGateways().forEach(vpnGateway -> {
        DetachVpnGatewayRequest detachVpnGatewayRequest = new DetachVpnGatewayRequest().withVpnGatewayId(vpnGateway.getVpnGatewayId());
        detachVpnGatewayRequest.setVpcId(vpcId);
        usEastEc2Client.detachVpnGateway(detachVpnGatewayRequest);
        DeleteVpnGatewayRequest deleteVpnGatewayRequest = new DeleteVpnGatewayRequest().withVpnGatewayId(detachVpnGatewayRequest.getVpnGatewayId());
        this.host.log("Terminating stale virtual private gateway: %s", detachVpnGatewayRequest.getVpnGatewayId());
        usEastEc2Client.deleteVpnGateway(deleteVpnGatewayRequest);
    });
}
Also used : DeleteVpnGatewayRequest(com.amazonaws.services.ec2.model.DeleteVpnGatewayRequest) DescribeVpnGatewaysRequest(com.amazonaws.services.ec2.model.DescribeVpnGatewaysRequest) DescribeVpnGatewaysResult(com.amazonaws.services.ec2.model.DescribeVpnGatewaysResult) Filter(com.amazonaws.services.ec2.model.Filter) DetachVpnGatewayRequest(com.amazonaws.services.ec2.model.DetachVpnGatewayRequest)

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