use of com.amazonaws.services.ec2.model.Address in project aws-doc-sdk-examples by awsdocs.
the class DescribeAddresses method main.
public static void main(String[] args) {
final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
DescribeAddressesResult response = ec2.describeAddresses();
for (Address address : response.getAddresses()) {
System.out.printf("Found address with public IP %s, " + "domain %s, " + "allocation id %s " + "and NIC id %s", address.getPublicIp(), address.getDomain(), address.getAllocationId(), address.getNetworkInterfaceId());
}
}
use of com.amazonaws.services.ec2.model.Address in project druid by druid-io.
the class EC2AutoScaler method ipToIdLookup.
@Override
public List<String> ipToIdLookup(List<String> ips) {
final List<String> retVal = FluentIterable.from(Lists.partition(ips, MAX_AWS_FILTER_VALUES)).transformAndConcat(new Function<List<String>, Iterable<Reservation>>() {
@Override
public Iterable<Reservation> apply(List<String> input) {
return amazonEC2Client.describeInstances(new DescribeInstancesRequest().withFilters(new Filter("private-ip-address", input))).getReservations();
}
}).transformAndConcat(new Function<Reservation, Iterable<Instance>>() {
@Override
public Iterable<Instance> apply(Reservation reservation) {
return reservation.getInstances();
}
}).transform(new Function<Instance, String>() {
@Override
public String apply(Instance instance) {
return instance.getInstanceId();
}
}).toList();
log.debug("Performing lookup: %s --> %s", ips, retVal);
return retVal;
}
use of com.amazonaws.services.ec2.model.Address in project druid by druid-io.
the class EC2AutoScalerTest method testIptoIdLookup.
@Test
public void testIptoIdLookup() throws Exception {
EC2AutoScaler autoScaler = new EC2AutoScaler(0, 1, ENV_CONFIG, amazonEC2Client, managementConfig);
final int n = 150;
Assert.assertTrue(n <= 2 * EC2AutoScaler.MAX_AWS_FILTER_VALUES);
List<String> ips = Lists.transform(ContiguousSet.create(Range.closedOpen(0, n), DiscreteDomain.integers()).asList(), Functions.toStringFunction());
EasyMock.expect(amazonEC2Client.describeInstances(new DescribeInstancesRequest().withFilters(new Filter("private-ip-address", ips.subList(0, EC2AutoScaler.MAX_AWS_FILTER_VALUES))))).andReturn(describeInstancesResult);
EasyMock.expect(amazonEC2Client.describeInstances(new DescribeInstancesRequest().withFilters(new Filter("private-ip-address", ips.subList(EC2AutoScaler.MAX_AWS_FILTER_VALUES, n))))).andReturn(describeInstancesResult);
EasyMock.replay(amazonEC2Client);
final Reservation[] chunk1 = new Reservation[EC2AutoScaler.MAX_AWS_FILTER_VALUES];
Arrays.fill(chunk1, reservation);
final Reservation[] chunk2 = new Reservation[n - EC2AutoScaler.MAX_AWS_FILTER_VALUES];
Arrays.fill(chunk2, reservation);
EasyMock.expect(describeInstancesResult.getReservations()).andReturn(Lists.newArrayList(chunk1));
EasyMock.expect(describeInstancesResult.getReservations()).andReturn(Lists.newArrayList(chunk2));
EasyMock.replay(describeInstancesResult);
EasyMock.expect(reservation.getInstances()).andReturn(Arrays.asList(instance)).times(n);
EasyMock.replay(reservation);
List<String> ids = autoScaler.ipToIdLookup(ips);
Assert.assertEquals(n, ids.size());
}
use of com.amazonaws.services.ec2.model.Address in project eureka by Netflix.
the class EIPManager method unbindEIP.
/**
* Unbind the EIP that this instance is associated with.
*/
public void unbindEIP() throws Exception {
InstanceInfo myInfo = applicationInfoManager.getInfo();
String myPublicIP = null;
if (myInfo != null && myInfo.getDataCenterInfo().getName() == Name.Amazon) {
myPublicIP = ((AmazonInfo) myInfo.getDataCenterInfo()).get(MetaDataKey.publicIpv4);
if (myPublicIP == null) {
logger.info("Instance is not associated with an EIP. Will not try to unbind");
return;
}
try {
AmazonEC2 ec2Service = getEC2Service();
DescribeAddressesRequest describeAddressRequest = new DescribeAddressesRequest().withPublicIps(myPublicIP);
DescribeAddressesResult result = ec2Service.describeAddresses(describeAddressRequest);
if ((result.getAddresses() != null) && (!result.getAddresses().isEmpty())) {
Address eipAddress = result.getAddresses().get(0);
DisassociateAddressRequest dissociateRequest = new DisassociateAddressRequest();
String domain = eipAddress.getDomain();
if ("vpc".equals(domain)) {
dissociateRequest.setAssociationId(eipAddress.getAssociationId());
} else {
dissociateRequest.setPublicIp(eipAddress.getPublicIp());
}
ec2Service.disassociateAddress(dissociateRequest);
logger.info("Dissociated the EIP {} from this instance", myPublicIP);
}
} catch (Throwable e) {
throw new RuntimeException("Cannot dissociate address from this instance", e);
}
}
}
use of com.amazonaws.services.ec2.model.Address in project eureka by Netflix.
the class EIPManager method bindEIP.
/**
* Checks if an EIP is bound and optionally binds the EIP.
*
* The list of EIPs are arranged with the EIPs allocated in the zone first
* followed by other EIPs.
*
* If an EIP is already bound to this instance this method simply returns. Otherwise, this method tries to find
* an unused EIP based on information from AWS. If it cannot find any unused EIP this method, it will be retried
* for a specified interval.
*
* One of the following scenarios can happen here :
*
* 1) If the instance is already bound to an EIP as deemed by AWS, no action is taken.
* 2) If an EIP is already bound to another instance as deemed by AWS, that EIP is skipped.
* 3) If an EIP is not already bound to an instance and if this instance is not bound to an EIP, then
* the EIP is bound to this instance.
*/
public void bindEIP() {
InstanceInfo myInfo = applicationInfoManager.getInfo();
String myInstanceId = ((AmazonInfo) myInfo.getDataCenterInfo()).get(MetaDataKey.instanceId);
String myZone = ((AmazonInfo) myInfo.getDataCenterInfo()).get(MetaDataKey.availabilityZone);
Collection<String> candidateEIPs = getCandidateEIPs(myInstanceId, myZone);
AmazonEC2 ec2Service = getEC2Service();
boolean isMyinstanceAssociatedWithEIP = false;
Address selectedEIP = null;
for (String eipEntry : candidateEIPs) {
try {
String associatedInstanceId;
// Check with AWS, if this EIP is already been used by another instance
DescribeAddressesRequest describeAddressRequest = new DescribeAddressesRequest().withPublicIps(eipEntry);
DescribeAddressesResult result = ec2Service.describeAddresses(describeAddressRequest);
if ((result.getAddresses() != null) && (!result.getAddresses().isEmpty())) {
Address eipAddress = result.getAddresses().get(0);
associatedInstanceId = eipAddress.getInstanceId();
// already marked.
if (((associatedInstanceId == null) || (associatedInstanceId.isEmpty()))) {
if (selectedEIP == null) {
selectedEIP = eipAddress;
}
} else if (isMyinstanceAssociatedWithEIP = (associatedInstanceId.equals(myInstanceId))) {
// This EIP is associated with an instance, check if this is the same as the current instance.
// If it is the same, stop searching for an EIP as this instance is already associated with an
// EIP
selectedEIP = eipAddress;
break;
} else {
// The EIP is used by some other instance, hence skip it
logger.warn("The selected EIP {} is associated with another instance {} according to AWS," + " hence skipping this", eipEntry, associatedInstanceId);
}
}
} catch (Throwable t) {
logger.error("Failed to bind elastic IP: {} to {}", eipEntry, myInstanceId, t);
}
}
if (null != selectedEIP) {
String publicIp = selectedEIP.getPublicIp();
// Only bind if the EIP is not already associated
if (!isMyinstanceAssociatedWithEIP) {
AssociateAddressRequest associateAddressRequest = new AssociateAddressRequest().withInstanceId(myInstanceId);
String domain = selectedEIP.getDomain();
if ("vpc".equals(domain)) {
associateAddressRequest.setAllocationId(selectedEIP.getAllocationId());
} else {
associateAddressRequest.setPublicIp(publicIp);
}
ec2Service.associateAddress(associateAddressRequest);
logger.info("\n\n\nAssociated {} running in zone: {} to elastic IP: {}", myInstanceId, myZone, publicIp);
}
logger.info("My instance {} seems to be already associated with the EIP {}", myInstanceId, publicIp);
} else {
logger.info("No EIP is free to be associated with this instance. Candidate EIPs are: {}", candidateEIPs);
}
}
Aggregations