Search in sources :

Example 11 with Server

use of org.openstack4j.model.compute.Server in project camel by apache.

the class ServerProducerTest method createServerWithHeaders.

@Test
public void createServerWithHeaders() throws Exception {
    final String expectedFlavorID = UUID.randomUUID().toString();
    when(testOSServer.getId()).thenReturn(expectedFlavorID);
    msg.setHeader(OpenstackConstants.OPERATION, OpenstackConstants.CREATE);
    msg.setHeader(OpenstackConstants.NAME, dummyServer.getName());
    msg.setHeader(NovaConstants.FLAVOR_ID, dummyServer.getFlavorRef());
    msg.setHeader(NovaConstants.IMAGE_ID, dummyServer.getImageRef());
    producer.process(exchange);
    final Server created = msg.getBody(Server.class);
    checkCreatedServer(dummyServer, created);
}
Also used : Server(org.openstack4j.model.compute.Server) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 12 with Server

use of org.openstack4j.model.compute.Server in project camel by apache.

the class ServerProducerTest method createServer.

@Test
public void createServer() throws Exception {
    when(endpoint.getOperation()).thenReturn(OpenstackConstants.CREATE);
    final String expectedFlavorID = UUID.randomUUID().toString();
    when(testOSServer.getId()).thenReturn(expectedFlavorID);
    msg.setBody(dummyServer);
    producer.process(exchange);
    final Server created = msg.getBody(Server.class);
    checkCreatedServer(dummyServer, created);
}
Also used : Server(org.openstack4j.model.compute.Server) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 13 with Server

use of org.openstack4j.model.compute.Server in project camel by apache.

the class ServerProducer method doCreate.

private void doCreate(Exchange exchange) {
    final ServerCreate in = messageToServer(exchange.getIn());
    final Server out = os.compute().servers().boot(in);
    exchange.getIn().setBody(out);
}
Also used : Server(org.openstack4j.model.compute.Server) ServerCreate(org.openstack4j.model.compute.ServerCreate)

Example 14 with Server

use of org.openstack4j.model.compute.Server 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 15 with Server

use of org.openstack4j.model.compute.Server 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

Server (org.openstack4j.model.compute.Server)20 CloudVmInstanceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmInstanceStatus)6 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)5 FileNotFoundException (java.io.FileNotFoundException)5 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)4 ArrayList (java.util.ArrayList)4 InstanceTemplate (com.sequenceiq.cloudbreak.cloud.model.InstanceTemplate)3 Test (org.junit.Test)3 FloatingIP (org.openstack4j.model.compute.FloatingIP)3 ServerCreate (org.openstack4j.model.compute.ServerCreate)3 CloudInstanceMetaData (com.sequenceiq.cloudbreak.cloud.model.CloudInstanceMetaData)2 CloudVmMetaDataStatus (com.sequenceiq.cloudbreak.cloud.model.CloudVmMetaDataStatus)2 OpenStackResourceException (com.sequenceiq.cloudbreak.cloud.openstack.nativ.OpenStackResourceException)2 Matchers.anyString (org.mockito.Matchers.anyString)2 AbstractTest (org.openstack4j.api.AbstractTest)2 OS4JException (org.openstack4j.api.exceptions.OS4JException)2 Test (org.testng.annotations.Test)2 InstanceStatus (com.sequenceiq.cloudbreak.cloud.model.InstanceStatus)1 NovaInstanceStatus (com.sequenceiq.cloudbreak.cloud.openstack.status.NovaInstanceStatus)1 KeystoneCredentialView (com.sequenceiq.cloudbreak.cloud.openstack.view.KeystoneCredentialView)1