.getContext();
NovaApi novaClient = context.unwrap(NovaApiMetadata.CONTEXT_TOKEN).getApi();
String region = ComputeServiceBuilderUtil.extractRegion(iaasInfo);
FloatingIPApi floatingIp = novaClient.getFloatingIPExtensionForZone(
region).get();
String ip = null;
// first try to find an unassigned IP.
ArrayList<FloatingIP> unassignedIps = Lists.newArrayList(Iterables
.filter(floatingIp.list(),
new Predicate<FloatingIP>() {
@Override
public boolean apply(FloatingIP arg0) {
// FIXME is this the correct filter?
return arg0.getFixedIp() == null;
}
}));
if (!unassignedIps.isEmpty()) {
// try to prevent multiple parallel launches from choosing the same
// ip.
Collections.shuffle(unassignedIps);
ip = Iterables.getLast(unassignedIps).getIp();
}
// if no unassigned IP is available, we'll try to allocate an IP.
if (ip == null || ip.isEmpty()) {
FloatingIP allocatedFloatingIP = floatingIp.create();
if (allocatedFloatingIP == null) {
String msg = "Failed to allocate an IP address.";
log.error(msg);
throw new CloudControllerException(msg);
}