use of software.amazon.awssdk.services.ec2.model.Filter in project druid by druid-io.
the class EC2AutoScaler method terminate.
@Override
public AutoScalingData terminate(List<String> ips) {
if (ips.isEmpty()) {
return new AutoScalingData(Lists.<String>newArrayList());
}
DescribeInstancesResult result = amazonEC2Client.describeInstances(new DescribeInstancesRequest().withFilters(new Filter("private-ip-address", ips)));
List<Instance> instances = Lists.newArrayList();
for (Reservation reservation : result.getReservations()) {
instances.addAll(reservation.getInstances());
}
try {
return terminateWithIds(Lists.transform(instances, new Function<Instance, String>() {
@Override
public String apply(Instance input) {
return input.getInstanceId();
}
}));
} catch (Exception e) {
log.error(e, "Unable to terminate any instances.");
}
return null;
}
use of software.amazon.awssdk.services.ec2.model.Filter in project photon-model by vmware.
the class AWSRemoteCleanup method deleteNetworkInterfaces.
private void deleteNetworkInterfaces(String vpcId, AmazonEC2 usEastEc2Client) {
DescribeNetworkInterfacesRequest networkInterfacesRequest = new DescribeNetworkInterfacesRequest().withFilters(new Filter(VPC_KEY, Collections.singletonList(vpcId)));
DescribeNetworkInterfacesResult networkInterfacesResult = usEastEc2Client.describeNetworkInterfaces(networkInterfacesRequest);
networkInterfacesResult.getNetworkInterfaces().forEach(networkInterface -> {
DescribeAddressesRequest addressesRequest = new DescribeAddressesRequest().withFilters(new Filter(NETWORK_INTERFACE_KEY, Collections.singletonList(networkInterface.getNetworkInterfaceId())));
DescribeAddressesResult addressResult = usEastEc2Client.describeAddresses(addressesRequest);
addressResult.getAddresses().forEach(address -> {
// There is no hardcore dependency on EIP, but we may run out of addresses and
// would be good to disassociate followed by releasing them.
DisassociateAddressRequest disassociateAddressRequest = new DisassociateAddressRequest().withAssociationId(address.getAssociationId());
usEastEc2Client.disassociateAddress(disassociateAddressRequest);
ReleaseAddressRequest releaseAddressRequest = new ReleaseAddressRequest().withAllocationId(address.getAllocationId());
usEastEc2Client.releaseAddress(releaseAddressRequest);
});
// Deleting Network Interfaces
DeleteNetworkInterfaceRequest deleteNetworkInterfaceRequest = new DeleteNetworkInterfaceRequest().withNetworkInterfaceId(networkInterface.getNetworkInterfaceId());
this.host.log("Terminating stale NIC: %s", networkInterface.getNetworkInterfaceId());
usEastEc2Client.deleteNetworkInterface(deleteNetworkInterfaceRequest);
});
}
use of software.amazon.awssdk.services.ec2.model.Filter 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 software.amazon.awssdk.services.ec2.model.Filter 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 software.amazon.awssdk.services.ec2.model.Filter 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());
}
}
Aggregations