Search in sources :

Example 1 with FloatingIp

use of org.openstack.model.compute.FloatingIp 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;
}
Also used : OpsException(org.platformlayer.ops.OpsException) Server(org.openstack.model.compute.Server) OpenstackComputeClient(org.openstack.client.common.OpenstackComputeClient) PollFunction(org.platformlayer.TimeoutPoll.PollFunction) Ip(org.openstack.model.compute.Addresses.Network.Ip) FloatingIp(org.openstack.model.compute.FloatingIp) ExecutionException(java.util.concurrent.ExecutionException) FloatingIp(org.openstack.model.compute.FloatingIp) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 OpenstackComputeClient (org.openstack.client.common.OpenstackComputeClient)1 Ip (org.openstack.model.compute.Addresses.Network.Ip)1 FloatingIp (org.openstack.model.compute.FloatingIp)1 Server (org.openstack.model.compute.Server)1 PollFunction (org.platformlayer.TimeoutPoll.PollFunction)1 OpsException (org.platformlayer.ops.OpsException)1