throw new IllegalStateException("Expected EC2 datacenter manager, found: " + datacenterManager);
}
Ec2DatacenterManager manager = (Ec2DatacenterManager) datacenterManager;
AmazonEC2Client ec2 = manager.getEc2Client(host);
String ec2InstanceId = manager.findHost(host);
if (ec2InstanceId == null) {
throw new IllegalStateException("Unable to find EC2 instance for host: " + host);
}
Instance ec2Instance = describeInstance(ec2, ec2InstanceId);
String subnetId = ec2Instance.getSubnetId();
// Use the default network interface
int networkInterfaceIndex = 0;
InstanceNetworkInterface networkInterface = findNetworkInterface(ec2Instance, networkInterfaceIndex);
if (networkInterface == null) {
throw new UnsupportedOperationException();
/*
* // TODO: Reuse unattached network interfaces (with the
* fathomcloud // tag)???
*
* NetworkInterface created; { CreateNetworkInterfaceRequest request
* = new CreateNetworkInterfaceRequest();
* request.setSubnetId(subnetId);
*
* CreateNetworkInterfaceResult response =
* ec2.createNetworkInterface(request); created =
* response.getNetworkInterface();
* log.info("Created network interface {}",
* created.getNetworkInterfaceId()); }
*
* addTag(ec2, created, "fathomcloud", "1");
*
* { AttachNetworkInterfaceRequest request = new
* AttachNetworkInterfaceRequest(); request.setDeviceIndex(1);
* request.setInstanceId(ec2InstanceId);
* request.setNetworkInterfaceId(created.getNetworkInterfaceId());
*
* AttachNetworkInterfaceResult response =
* ec2.attachNetworkInterface(request);
* log.info("Attached network interface as {}",
* response.getAttachmentId()); }
*
* ec2Instance = describeInstance(ec2, ec2InstanceId);
*
* networkInterface = findNetworkInterface(ec2Instance, 1);
*
* if (networkInterface == null) { throw new IllegalStateException(
* "Did not find network interface after attaching: " +
* created.getNetworkInterfaceId()); }
*/
}
InstancePrivateIpAddress privateIp = findUnusedIp(networkInterface);
if (privateIp == null) {
// TODO: Prune private ip addresses from NICs?
// TODO: Need to tag??
{
AssignPrivateIpAddressesRequest request = new AssignPrivateIpAddressesRequest();
request.setNetworkInterfaceId(networkInterface.getNetworkInterfaceId());
request.setSecondaryPrivateIpAddressCount(1);
ec2.assignPrivateIpAddresses(request);
}
ec2Instance = describeInstance(ec2, ec2InstanceId);
networkInterface = findNetworkInterface(ec2Instance, networkInterfaceIndex);
privateIp = findUnusedIp(networkInterface);
if (privateIp == null) {
throw new IllegalStateException("Unable to find private IP address");
}
}
String privateIpAddress = privateIp.getPrivateIpAddress();
{
AssociateAddressRequest request = new AssociateAddressRequest();
request.setPublicIp(vip.getData().getIp());
request.setPrivateIpAddress(privateIpAddress);
request.setNetworkInterfaceId(networkInterface.getNetworkInterfaceId());
request.setInstanceId(ec2InstanceId);
AssociateAddressResult response = ec2.associateAddress(request);
log.info("Associated public IP with assocation id: {}", response.getAssociationId());
}
return privateIpAddress;
}