List<Instance> ec2Instances = client.runInstances(amiID, 1, null, null, null, null, null, null);
assertNotNull(ec2Instances);
assertEquals(ec2Instances.size(), 1);
// Allocate an address
Address address = client.allocateAddress();
assertNotNull(address);
// Describe all addresses
client.describeAddresses(null);
// Describe addresses.
List<Address> addressList = client.describeAddresses(new String[]{address.getPublicIp()});
assertNotNull(addressList);
assertTrue(addressList.size() > 0);
for (Address addrs : addressList) {
System.out.println("Address: " + addrs.getPublicIp() +
", instance=" + addrs.getInstance());
assertEquals(addrs, address);
assertNull(addrs.getInstance());
}
// Associate the allocated address to the instance we started above
boolean result = client.associateAddress(ec2Instances.get(0).getInstanceId(), address.getPublicIp());
assertTrue(result);
// Check the association
addressList = client.describeAddresses(new String[]{address.getPublicIp()});
assertNotNull(addressList);
assertTrue(addressList.size() > 0);
for (Address addrs : addressList) {
System.out.println("Address: " + addrs.getPublicIp() +
", instance=" + addrs.getInstance());
assertEquals(addrs.getInstance(), ec2Instances.get(0));
assertEquals(addrs, address);
}
// Disassociate the address
result = client.disassociateAddress(address.getPublicIp());
assertTrue(result);
// release the address
result = client.releaseAddress(address.getPublicIp());
assertTrue(result);
try {
client.describeAddresses(new String[]{address.getPublicIp()});
} catch (EC2Exception e) {
assertTrue(e.getCause().getMessage().indexOf("Address '" + address.getPublicIp() + "' not found.") == 0);
}
List<Instance> instances =
client.terminateInstances(new String[]{ec2Instances.get(0).getInstanceId()});
assertEquals(instances.size(), 1);