Search in sources :

Example 1 with ActionResponse

use of org.openstack4j.model.compute.ActionResponse 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);
    }
}
Also used : Server(org.openstack4j.model.compute.Server) FloatingIP(org.openstack4j.model.compute.FloatingIP) ActionResponse(org.openstack4j.model.compute.ActionResponse) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)1 ActionResponse (org.openstack4j.model.compute.ActionResponse)1 FloatingIP (org.openstack4j.model.compute.FloatingIP)1 Server (org.openstack4j.model.compute.Server)1