Example #1
0
def _get_fixed_ip(context, given_fixed_ip, port):
    if not given_fixed_ip:
        fixed_ip = _get_next_available_fixed_ip(port)
        if not fixed_ip:
            raise q_exc.NoAvailableFixedIpsForPort(port_id=port.id)
    else:
        fixed_ip = next((ip for ip in port.ip_addresses
                         if (ip['address_readable'] == given_fixed_ip
                             and ip.get('address_type') == ip_types.FIXED)),
                        None)

        if not fixed_ip:
            raise q_exc.FixedIpDoesNotExistsForPort(fixed_ip=given_fixed_ip,
                                                    port_id=port.id)

        if any(ip for ip in port.ip_addresses
               if (ip.get('address_type') in (ip_types.FLOATING,
                                              ip_types.SCALING)
                   and ip.fixed_ip['address_readable'] == given_fixed_ip)):
            raise q_exc.PortAlreadyContainsFloatingIp(port_id=port.id)
    return fixed_ip
Example #2
0
def create_floatingip(context, content):
    """Allocate or reallocate a floating IP.

    :param context: neutron api request context.
    :param content: dictionary describing the floating ip, with keys
        as listed in the RESOURCE_ATTRIBUTE_MAP object in
        neutron/api/v2/attributes.py.  All keys will be populated.

    :returns: Dictionary containing details for the new floating IP.  If values
        are declared in the fields parameter, then only those keys will be
        present.
    """
    LOG.info('create_floatingip %s for tenant %s and body %s' %
             (id, context.tenant_id, content))
    tenant_id = content.get('tenant_id')
    network_id = content.get('floating_network_id')
    fixed_ip_address = content.get('fixed_ip_address')
    ip_address = content.get('floating_ip_address')
    port_id = content.get('port_id')

    if not tenant_id:
        tenant_id = context.tenant_id

    if not network_id:
        raise exceptions.BadRequest(resource='floating_ip',
                                    msg='floating_network_id is required.')

    network = db_api.network_find(context, id=network_id, scope=db_api.ONE)

    if not network:
        raise exceptions.NetworkNotFound(net_id=network_id)

    fixed_ip = None
    port = None
    if port_id:
        port = db_api.port_find(context, id=port_id, scope=db_api.ONE)

        if not port:
            raise exceptions.PortNotFound(port_id=port_id)

        if not port.ip_addresses or len(port.ip_addresses) == 0:
            raise qex.NoAvailableFixedIpsForPort(port_id=port_id)

        if not fixed_ip_address:
            fixed_ip = _get_next_available_fixed_ip(port)
            if not fixed_ip:
                raise qex.NoAvailableFixedIpsForPort(
                    port_id=port_id)
        else:
            fixed_ip = next((ip for ip in port.ip_addresses
                            if (ip['address_readable'] == fixed_ip_address and
                                ip.get('address_type') == ip_types.FIXED)),
                            None)

            if not fixed_ip:
                raise qex.FixedIpDoesNotExistsForPort(
                    fixed_ip=fixed_ip_address, port_id=port_id)

            if any(ip for ip in port.ip_addresses
                   if (ip.get('address_type') == ip_types.FLOATING and
                       ip.fixed_ip['address_readable'] == fixed_ip_address)):
                raise qex.PortAlreadyContainsFloatingIp(
                    port_id=port_id)

    new_addresses = []
    ip_addresses = []
    if ip_address:
        ip_addresses.append(ip_address)

    seg_name = CONF.QUARK.floating_ip_segment_name
    strategy_name = CONF.QUARK.floating_ip_ipam_strategy

    if strategy_name.upper() == 'NETWORK':
        strategy_name = network.get("ipam_strategy")

    ipam_driver = ipam.IPAM_REGISTRY.get_strategy(strategy_name)
    ipam_driver.allocate_ip_address(context, new_addresses, network_id,
                                    port_id, CONF.QUARK.ipam_reuse_after,
                                    seg_name, version=4,
                                    ip_addresses=ip_addresses,
                                    address_type=ip_types.FLOATING)

    flip = new_addresses[0]

    if fixed_ip and port:
        context.session.begin()
        try:
            flip = db_api.port_associate_ip(context, [port], flip, [port_id])
            flip = db_api.floating_ip_associate_fixed_ip(context, flip,
                                                         fixed_ip)

            flip_driver = registry.DRIVER_REGISTRY.get_driver()

            flip_driver.register_floating_ip(flip, port, fixed_ip)
            context.session.commit()
        except Exception:
            context.session.rollback()
            raise

    return v._make_floating_ip_dict(flip, port_id)
Example #3
0
def create_floatingip(context, content):
    LOG.info("create_floatingip %s for tenant %s and body %s" %
             (id, context.tenant_id, content))
    tenant_id = content.get("tenant_id")
    network_id = content.get("floating_network_id")
    fixed_ip_address = content.get("fixed_ip_address")
    ip_address = content.get("floating_ip_address")
    port_id = content.get("port_id")

    if not tenant_id:
        tenant_id = context.tenant_id

    if not network_id:
        raise exceptions.BadRequest(resource="floating_ip",
                                    msg="floating_network_id is required.")

    network = db_api.network_find(context, id=network_id, scope=db_api.ONE)

    if not network:
        raise exceptions.NetworkNotFound(net_id=network_id)

    fixed_ip = None
    port = None
    if port_id:
        port = db_api.port_find(context, id=port_id, scope=db_api.ONE)

        if not port:
            raise exceptions.PortNotFound(port_id=port_id)

        if not port.ip_addresses or len(port.ip_addresses) == 0:
            raise quark_exceptions.NoAvailableFixedIPsForPort(port_id=port_id)

        if not fixed_ip_address:
            fixed_ip = _get_next_available_fixed_ip(port)
            if not fixed_ip:
                raise quark_exceptions.NoAvailableFixedIPsForPort(
                    port_id=port_id)
        else:
            fixed_ip = next(
                (ip for ip in port.ip_addresses
                 if (ip["address_readable"] == fixed_ip_address
                     and ip.get("address_type") == ip_types.FIXED)), None)

            if not fixed_ip:
                raise quark_exceptions.FixedIpDoesNotExistsForPort(
                    fixed_ip=fixed_ip_address, port_id=port_id)

            if any(ip for ip in port.ip_addresses
                   if (ip.get("address_type") == ip_types.FLOATING and
                       ip.fixed_ip["address_readable"] == fixed_ip_address)):
                raise quark_exceptions.PortAlreadyContainsFloatingIp(
                    port_id=port_id)

    new_addresses = []
    ip_addresses = []
    if ip_address:
        ip_addresses.append(ip_address)

    seg_name = CONF.QUARK.floating_ip_segment_name
    strategy_name = network.get("ipam_strategy")
    ipam_driver = ipam.IPAM_REGISTRY.get_strategy(strategy_name)
    ipam_driver.allocate_ip_address(context,
                                    new_addresses,
                                    network_id,
                                    port_id,
                                    CONF.QUARK.ipam_reuse_after,
                                    seg_name,
                                    version=4,
                                    ip_addresses=ip_addresses,
                                    address_type=ip_types.FLOATING)

    floating_ip = new_addresses[0]

    if fixed_ip and port:
        with context.session.begin():
            floating_ip = db_api.floating_ip_associate_fixed_ip(
                context, floating_ip, fixed_ip)

        flip_driver_type = CONF.QUARK.default_floating_ip_driver
        flip_driver = registry.DRIVER_REGISTRY.get_driver(flip_driver_type)

        flip_driver.register_floating_ip(floating_ip, port, fixed_ip)

    return v._make_floating_ip_dict(floating_ip)