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;
}
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;
}
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;
}
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;
}
Aggregations