use of org.platformlayer.TimeoutPoll.PollFunction in project platformlayer by platformlayer.
the class OpenstackCloudContext method ensureHasPublicIp.
public Server ensureHasPublicIp(final OpenstackCloud cloud, Server server) throws OpsException {
final OpenstackCloudHelpers helpers = new OpenstackCloudHelpers();
List<Ip> publicIps = helpers.findPublicIps(cloud, server);
if (!publicIps.isEmpty()) {
return server;
}
final OpenstackComputeClient compute = getComputeClient(cloud);
log.info("Creating floating IP");
FloatingIp floatingIp = compute.root().floatingIps().create();
// TODO: Don't abandon the IP e.g. if the attach fails
log.info("Attching floating IP " + floatingIp.getIp() + " to " + server.getId());
compute.root().servers().server(server.getId()).addFloatingIp(floatingIp.getIp());
final String serverId = server.getId();
try {
server = TimeoutPoll.poll(TimeSpan.FIVE_MINUTES, TimeSpan.TEN_SECONDS, new PollFunction<Server>() {
@Override
public Server call() throws Exception {
log.info("Waiting for floating IP attach; polling server: " + serverId);
Server server = compute.root().servers().server(serverId).show();
List<Ip> publicIps = helpers.findPublicIps(cloud, server);
if (publicIps.isEmpty()) {
return null;
}
return server;
}
});
} catch (TimeoutException e) {
throw new OpsException("Timeout while waiting for attached public IP to show up", e);
} catch (ExecutionException e) {
throw new OpsException("Error while waiting for attached public IP to show up", e);
}
return server;
}
Aggregations