@Override
public Entry<InternetService, PublicIpAddress> getNewInternetServiceAndIp(VApp vApp, int port, Protocol protocol) {
logger.debug(">> creating InternetService in vDC %s:%s:%d", vApp.getVDC().getHref(), protocol, port);
InternetService is = null;
PublicIpAddress ip = null;
try {
ip = client.activatePublicIpInVDC(vApp.getVDC().getHref());
} catch (InsufficientResourcesException e) {
logger.warn(">> no more ip addresses available, looking for one to re-use");
Set<PublicIpAddress> publicIps = client.getPublicIpsAssociatedWithVDC(vApp.getVDC().getHref());
for (PublicIpAddress existingIp : publicIps) {
Set<InternetService> services = client.getInternetServicesOnPublicIp(existingIp.getId());
if (services.size() == 0) {
ip = existingIp;
break;
}
}
if (ip == null)
throw new InsufficientResourcesException(
"no more ip addresses available and existing ips all have services attached: " + publicIps, e
.getCause());
}
is = client.addInternetServiceToExistingIp(ip.getId(), vApp.getName() + "-" + port, protocol, port,
withDescription(String.format("port %d access to serverId: %s name: %s", port, vApp.getName(), vApp
.getName())));
Map<InternetService, PublicIpAddress> result = ImmutableMap.<InternetService, PublicIpAddress> of(is, ip);
Entry<InternetService, PublicIpAddress> entry = Iterables.getOnlyElement(result.entrySet());
return entry;