Search in sources :

Example 1 with FloatingIps

use of com.woorea.openstack.nova.model.FloatingIps in project AJSC by att.

the class OpenStackNetworkService method reserveFreeFloatingIPAddress.

/**
 * This method is used o determine which IP addresses in the floating ip address pool specified are free, and to
 * reserve the first one
 *
 * @param pool
 *            The name of the pool to be searched, or null if we are searching all pools available to the tenant
 * @return The reserved IP address (or null if there are none available in the pool)
 * @throws OpenStackResponseException
 * @throws OpenStackConnectException
 */
@SuppressWarnings("nls")
private String reserveFreeFloatingIPAddress(String pool) throws OpenStackConnectException, OpenStackResponseException {
    Nova client = novaConnector.getClient();
    HashSet<String> available = new HashSet<>();
    Context context = getContext();
    FloatingIps ips = client.floatingIps().list().execute();
    for (FloatingIp ip : ips.getList()) {
        if (pool == null || pool.equalsIgnoreCase(ip.getPool())) {
            available.add(ip.getIp());
        }
    }
    Servers servers = client.servers().list(true).execute();
    for (com.woorea.openstack.nova.model.Server server : servers.getList()) {
        Addresses allocatedAddresses = server.getAddresses();
        Map<String, List<Address>> addressMap = allocatedAddresses.getAddresses();
        for (Map.Entry<String, List<Address>> entry : addressMap.entrySet()) {
            List<Address> addressList = entry.getValue();
            for (Address address : addressList) {
                if (address.getType().equalsIgnoreCase("floating")) {
                    available.remove(address.getAddr());
                }
            }
        }
    }
    if (!available.isEmpty()) {
        Iterator<String> it = available.iterator();
        while (it.hasNext()) {
            String ip = it.next();
            if (((OpenStackContext) context).allocateFloatingIP(ip)) {
                return ip;
            }
        }
    }
    return null;
}
Also used : Context(com.att.cdp.zones.Context) OpenStackContext(com.att.cdp.openstack.OpenStackContext) Address(com.woorea.openstack.nova.model.Server.Addresses.Address) FloatingIps(com.woorea.openstack.nova.model.FloatingIps) Nova(com.woorea.openstack.nova.Nova) Servers(com.woorea.openstack.nova.model.Servers) FloatingIp(com.woorea.openstack.nova.model.FloatingIp) Addresses(com.woorea.openstack.nova.model.Server.Addresses) OpenStackContext(com.att.cdp.openstack.OpenStackContext) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashSet(java.util.HashSet)

Example 2 with FloatingIps

use of com.woorea.openstack.nova.model.FloatingIps in project AJSC by att.

the class OpenStackNetworkService method getAvailableAddresses.

/**
 * This method allows the caller to obtain a list of the available IP addresses from the Floating IP address (DNS)
 * pool.
 *
 * @return The list of available IP Addresses that can be used to assign a floating IP address to a server.
 * @throws ZoneException
 *             If the service is not available or the pool cannot be obtained
 * @see com.att.cdp.zones.NetworkService#getAvailableAddresses(java.lang.String)
 */
@SuppressWarnings("nls")
@Override
public List<String> getAvailableAddresses(String pool) throws ZoneException {
    checkArg(pool, "pool");
    connect();
    Context context = getContext();
    trackRequest();
    RequestState.put(RequestState.POOL, pool);
    RequestState.put(RequestState.SERVICE, "Compute");
    RequestState.put(RequestState.SERVICE_URL, novaConnector.getEndpoint());
    ArrayList<String> addresses = new ArrayList<>();
    FloatingIpsExtension extension = novaConnector.getClient().floatingIps();
    if (extension == null) {
        throw new NotSupportedException(EELFResourceManager.format(OSMsg.PAL_OS_UNSUPPORTED_OPERATION, "getAvailableAddresses", context.getProvider().getName()));
    }
    try {
        FloatingIps ips = extension.list().execute();
        for (FloatingIp ip : ips) {
            if (ip.getPool().equalsIgnoreCase(pool)) {
                String ipAddress = ip.getIp();
                addresses.add(ipAddress);
            }
        }
    } catch (OpenStackBaseException ex) {
        ExceptionMapper.mapException(ex);
    }
    return addresses;
}
Also used : Context(com.att.cdp.zones.Context) OpenStackContext(com.att.cdp.openstack.OpenStackContext) FloatingIps(com.woorea.openstack.nova.model.FloatingIps) OpenStackBaseException(com.woorea.openstack.base.client.OpenStackBaseException) FloatingIpsExtension(com.woorea.openstack.nova.api.extensions.FloatingIpsExtension) ArrayList(java.util.ArrayList) NotSupportedException(com.att.cdp.exceptions.NotSupportedException) FloatingIp(com.woorea.openstack.nova.model.FloatingIp)

Aggregations

OpenStackContext (com.att.cdp.openstack.OpenStackContext)2 Context (com.att.cdp.zones.Context)2 FloatingIp (com.woorea.openstack.nova.model.FloatingIp)2 FloatingIps (com.woorea.openstack.nova.model.FloatingIps)2 ArrayList (java.util.ArrayList)2 NotSupportedException (com.att.cdp.exceptions.NotSupportedException)1 OpenStackBaseException (com.woorea.openstack.base.client.OpenStackBaseException)1 Nova (com.woorea.openstack.nova.Nova)1 FloatingIpsExtension (com.woorea.openstack.nova.api.extensions.FloatingIpsExtension)1 Addresses (com.woorea.openstack.nova.model.Server.Addresses)1 Address (com.woorea.openstack.nova.model.Server.Addresses.Address)1 Servers (com.woorea.openstack.nova.model.Servers)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1