Search in sources :

Example 1 with Ip

use of org.openstack.model.compute.Addresses.Network.Ip 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)

Example 2 with Ip

use of org.openstack.model.compute.Addresses.Network.Ip in project platformlayer by platformlayer.

the class OpenstackComputeMachine method getNetworkPoint.

@Override
public NetworkPoint getNetworkPoint() throws OpsException {
    if (networkPoint == null) {
        // TODO: Check private networks
        OpenstackCloudHelpers helpers = new OpenstackCloudHelpers();
        List<InetAddress> addresses = Lists.newArrayList();
        // We assume that private networks can still reach the public internet, so these work for everyone
        List<Ip> publicIps = helpers.findPublicIps(cloud, server);
        for (Ip ip : publicIps) {
            // if (Objects.equal("6", ip.getVersion())) {
            // continue;
            // }
            String addr = ip.getAddr();
            if (!Strings.isNullOrEmpty(addr)) {
                addresses.add(InetAddresses.forString(addr));
            }
        }
        if (addresses.size() != 1) {
            throw new OpsException("Found multiple addresses: " + Joiner.on(",").join(addresses));
        }
        networkPoint = NetworkPoint.forAddress(addresses.get(0));
    }
    return networkPoint;
}
Also used : OpsException(org.platformlayer.ops.OpsException) Ip(org.openstack.model.compute.Addresses.Network.Ip) InetAddress(java.net.InetAddress)

Example 3 with Ip

use of org.openstack.model.compute.Addresses.Network.Ip in project platformlayer by platformlayer.

the class OpenstackCloudHelpers method findPublicIps.

public List<Ip> findPublicIps(OpenstackCloud cloud, Server server) {
    List<Ip> ips = Lists.newArrayList();
    // {
    // String ip = server.getAccessIpV4();
    // if (!Strings.isNullOrEmpty(ip)) {
    // tags.add(new Tag(Tag.NETWORK_ADDRESS, ip));
    // }
    // }
    Addresses addresses = server.getAddresses();
    for (Network network : addresses.getNetworks()) {
        if ("public".equals(network.getId())) {
            for (Ip ip : network.getIps()) {
                if (Strings.isNullOrEmpty(ip.getAddr())) {
                    continue;
                }
                ips.add(ip);
            }
        }
    }
    CloudBehaviours behaviours = new CloudBehaviours(cloud);
    if (behaviours.publicIpsReportedAsPrivate()) {
        for (Network network : addresses.getNetworks()) {
            if ("private".equals(network.getId())) {
                for (Ip ip : network.getIps()) {
                    if (Strings.isNullOrEmpty(ip.getAddr())) {
                        continue;
                    }
                    if (behaviours.isPublic(ip)) {
                        ips.add(ip);
                    }
                }
            }
        }
    }
    return ips;
}
Also used : Addresses(org.openstack.model.compute.Addresses) Ip(org.openstack.model.compute.Addresses.Network.Ip) Network(org.openstack.model.compute.Addresses.Network)

Example 4 with Ip

use of org.openstack.model.compute.Addresses.Network.Ip in project platformlayer by platformlayer.

the class OpenstackComputeMachine method buildAddressTags.

public List<Tag> buildAddressTags() {
    List<Tag> tags = Lists.newArrayList();
    OpenstackCloudHelpers helpers = new OpenstackCloudHelpers();
    List<Ip> publicIps = helpers.findPublicIps(cloud, server);
    for (Ip ip : publicIps) {
        InetAddress addr = InetAddresses.forString(ip.getAddr());
        tags.add(Tag.NETWORK_ADDRESS.build(addr));
    }
    return tags;
}
Also used : Ip(org.openstack.model.compute.Addresses.Network.Ip) Tag(org.platformlayer.core.model.Tag) InetAddress(java.net.InetAddress)

Aggregations

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