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);
}
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);
}
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);
});
}
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);
}
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);
});
}
Aggregations