use of org.openstack4j.model.compute.FloatingIP in project cloudbreak by hortonworks.
the class OpenStackFloatingIPBuilder method build.
@Override
public List<CloudResource> build(OpenStackContext context, long privateId, AuthenticatedContext auth, Group group, Image image, List<CloudResource> buildableResource, Map<String, String> tags) {
CloudResource resource = buildableResource.get(0);
try {
String publicNetId = context.getStringParameter(OpenStackConstants.PUBLIC_NET_ID);
if (publicNetId != null) {
OSClient<?> osClient = createOSClient(auth);
List<CloudResource> computeResources = context.getComputeResources(privateId);
CloudResource instance = getInstance(computeResources);
FloatingIP unusedIp = osClient.compute().floatingIps().allocateIP(publicNetId);
ActionResponse response = osClient.compute().floatingIps().addFloatingIP(instance.getParameter(OpenStackConstants.SERVER, Server.class), unusedIp.getFloatingIpAddress());
if (!response.isSuccess()) {
throw new OpenStackResourceException("Add floating-ip to server failed", resourceType(), resource.getName(), auth.getCloudContext().getId(), response.getFault());
}
return Collections.singletonList(createPersistedResource(resource, group.getName(), unusedIp.getId()));
}
return Collections.emptyList();
} catch (OS4JException ex) {
throw new OpenStackResourceException("Add floating-ip to server failed", resourceType(), resource.getName(), ex);
}
}
use of org.openstack4j.model.compute.FloatingIP in project openstack4j by ContainX.
the class FloatingIPTests method allocateFloatingIP.
@Test(dataProvider = "floatingIPs")
public void allocateFloatingIP(String ip) throws IOException {
final String POOL = "floating";
String jsonResponse = String.format("{\"floating_ip\": {" + "\"instance_id\": null, " + "\"ip\": \"%s\", " + "\"fixed_ip\": null, " + "\"id\": \"%s\", " + "\"pool\": \"%s\"}}", ip, UUID.randomUUID().toString(), POOL);
respondWith(200, jsonResponse);
FloatingIP fip = osv3().compute().floatingIps().allocateIP(POOL);
assertNotNull(fip);
assertEquals(fip.getFloatingIpAddress(), ip);
assertEquals(fip.getPool(), POOL);
assertNotNull(fip.getId());
assertNull(fip.getFixedIpAddress());
assertNull(fip.getInstanceId());
}
use of org.openstack4j.model.compute.FloatingIP in project airavata by apache.
the class OpenstackIntfImpl method addFloatingIP.
@Override
public void addFloatingIP(String serverId) {
try {
Server server = this.getServer(serverId);
// Floating IP to allocate.
FloatingIP floatIp = null;
if (server != null) {
List<? extends FloatingIP> floatIPList = os.compute().floatingIps().list();
// Iterate through the floating ips from the pool present if any.
if (floatIPList.size() > 0) {
for (FloatingIP ip : floatIPList) {
logger.info("Checking if floating ip : " + ip.getFloatingIpAddress() + " is free to use.");
Boolean isFloatingIpUsed = OpenstackIntfUtil.isFloatingIPUsed(ip);
if (isFloatingIpUsed != null && !isFloatingIpUsed) {
floatIp = ip;
logger.info("Floating ip " + ip.getFloatingIpAddress() + " found to be free.");
break;
}
}
}
// If all floating IPs are used, or there are no free floating ips, create new one.
if (floatIp == null) {
floatIp = os.compute().floatingIps().allocateIP(properties.getProperty(Constants.OS_FLOATING_IP_POOL));
logger.info("Created new floating ip " + floatIp.getFloatingIpAddress());
}
if (floatIp != null) {
String ipAddr = floatIp.getFloatingIpAddress();
if (ipAddr != null) {
ActionResponse response = os.compute().floatingIps().addFloatingIP(server, ipAddr);
logger.info(response.isSuccess() + ":" + response.getCode() + ":" + response.getFault() + ":" + response.toString());
if (response.isSuccess()) {
logger.info("Floating IP " + ipAddr + " assigned successfully to server with ID: " + serverId);
} else {
logger.error("Failed to associate Floating IP.");
}
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
// TODO: Check with the team on how to handle exceptions.
logger.error("Failed to associate floating IP to server with ID: " + serverId);
}
}
use of org.openstack4j.model.compute.FloatingIP in project airavata by apache.
the class OpenstackIntfImpl method deleteServer.
@Override
public void deleteServer(String serverId) {
try {
Server server = this.getServer(serverId);
// Get Floating IP if there is one associated.
String floatingIpAddr = null;
for (Address novaAddress : server.getAddresses().getAddresses().get(properties.getProperty(Constants.OS_NETWORK_NAME))) {
novaAddress = (NovaAddress) novaAddress;
if (novaAddress.getType().equals(IPType.FLOATING.toString())) {
floatingIpAddr = novaAddress.getAddr();
break;
}
}
if (server != null) {
os.compute().servers().delete(serverId);
// Deallocating Floating IP.
if (floatingIpAddr != null) {
for (FloatingIP floatIp : os.compute().floatingIps().list()) {
if (floatIp.getFloatingIpAddress().equals(floatingIpAddr)) {
os.compute().floatingIps().deallocateIP(floatIp.getId());
}
}
}
logger.info("Server deleted successfully for ID: " + serverId);
}
} catch (Exception ex) {
ex.printStackTrace();
// TODO: Check with the team on how to handle exceptions.
logger.error("Failed to delete server with ID: " + serverId);
}
}
Aggregations