Search in sources :

Example 1 with FloatingIP

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);
    }
}
Also used : Server(org.openstack4j.model.compute.Server) OpenStackResourceException(com.sequenceiq.cloudbreak.cloud.openstack.nativ.OpenStackResourceException) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) FloatingIP(org.openstack4j.model.compute.FloatingIP) OS4JException(org.openstack4j.api.exceptions.OS4JException) ActionResponse(org.openstack4j.model.common.ActionResponse)

Example 2 with FloatingIP

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());
}
Also used : FloatingIP(org.openstack4j.model.compute.FloatingIP) Test(org.testng.annotations.Test) AbstractTest(org.openstack4j.api.AbstractTest)

Example 3 with FloatingIP

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

Example 4 with FloatingIP

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);
    }
}
Also used : Server(org.openstack4j.model.compute.Server) Address(org.openstack4j.model.compute.Address) NovaAddress(org.openstack4j.openstack.compute.domain.NovaAddresses.NovaAddress) FloatingIP(org.openstack4j.model.compute.FloatingIP) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

FloatingIP (org.openstack4j.model.compute.FloatingIP)4 Server (org.openstack4j.model.compute.Server)3 FileNotFoundException (java.io.FileNotFoundException)2 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)1 OpenStackResourceException (com.sequenceiq.cloudbreak.cloud.openstack.nativ.OpenStackResourceException)1 AbstractTest (org.openstack4j.api.AbstractTest)1 OS4JException (org.openstack4j.api.exceptions.OS4JException)1 ActionResponse (org.openstack4j.model.common.ActionResponse)1 ActionResponse (org.openstack4j.model.compute.ActionResponse)1 Address (org.openstack4j.model.compute.Address)1 NovaAddress (org.openstack4j.openstack.compute.domain.NovaAddresses.NovaAddress)1 Test (org.testng.annotations.Test)1