Merge "Refactor for floating IP updating checks"

This commit is contained in:
Jenkins 2016-09-15 02:33:18 +00:00 committed by Gerrit Code Review
commit 38d4dd3caf

View File

@ -1066,42 +1066,27 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
return [ip for ip in port['fixed_ips'] return [ip for ip in port['fixed_ips']
if netaddr.IPAddress(ip['ip_address']).version == 4] if netaddr.IPAddress(ip['ip_address']).version == 4]
def _internal_fip_assoc_data(self, context, fip): def _internal_fip_assoc_data(self, context, fip, tenant_id):
"""Retrieve internal port data for floating IP. """Retrieve internal port data for floating IP.
Retrieve information concerning the internal port where Retrieve information concerning the internal port where
the floating IP should be associated to. the floating IP should be associated to.
""" """
internal_port = self._core_plugin.get_port(context, fip['port_id']) internal_port = self._core_plugin.get_port(context, fip['port_id'])
if not internal_port['tenant_id'] == fip['tenant_id']: if not internal_port['tenant_id'] == tenant_id:
port_id = fip['port_id'] port_id = fip['port_id']
if 'id' in fip: msg = (_('Cannot process floating IP association with '
floatingip_id = fip['id'] 'Port %s, since that port is owned by a '
data = {'port_id': port_id, 'different tenant') % port_id)
'floatingip_id': floatingip_id}
msg = (_('Port %(port_id)s is associated with a different '
'tenant than Floating IP %(floatingip_id)s and '
'therefore cannot be bound.') % data)
else:
msg = (_('Cannot create floating IP and bind it to '
'Port %s, since that port is owned by a '
'different tenant.') % port_id)
raise n_exc.BadRequest(resource='floatingip', msg=msg) raise n_exc.BadRequest(resource='floatingip', msg=msg)
internal_subnet_id = None internal_subnet_id = None
if fip.get('fixed_ip_address'): if fip.get('fixed_ip_address'):
internal_ip_address = fip['fixed_ip_address'] internal_ip_address = fip['fixed_ip_address']
if netaddr.IPAddress(internal_ip_address).version != 4: if netaddr.IPAddress(internal_ip_address).version != 4:
if 'id' in fip: msg = (_('Cannot process floating IP association with %s, '
data = {'floatingip_id': fip['id'], 'since that is not an IPv4 address') %
'internal_ip': internal_ip_address} internal_ip_address)
msg = (_('Floating IP %(floatingip_id)s is associated '
'with non-IPv4 address %(internal_ip)s and '
'therefore cannot be bound.') % data)
else:
msg = (_('Cannot create floating IP and bind it to %s, '
'since that is not an IPv4 address.') %
internal_ip_address)
raise n_exc.BadRequest(resource='floatingip', msg=msg) raise n_exc.BadRequest(resource='floatingip', msg=msg)
for ip in internal_port['fixed_ips']: for ip in internal_port['fixed_ips']:
if ip['ip_address'] == internal_ip_address: if ip['ip_address'] == internal_ip_address:
@ -1126,7 +1111,7 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
internal_subnet_id = ipv4_fixed_ips[0]['subnet_id'] internal_subnet_id = ipv4_fixed_ips[0]['subnet_id']
return internal_port, internal_subnet_id, internal_ip_address return internal_port, internal_subnet_id, internal_ip_address
def _get_assoc_data(self, context, fip, floating_network_id): def _get_assoc_data(self, context, fip, floatingip_db):
"""Determine/extract data associated with the internal port. """Determine/extract data associated with the internal port.
When a floating IP is associated with an internal port, When a floating IP is associated with an internal port,
@ -1136,11 +1121,11 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
owns the floating IP will be confirmed by _get_router_for_floatingip. owns the floating IP will be confirmed by _get_router_for_floatingip.
""" """
(internal_port, internal_subnet_id, (internal_port, internal_subnet_id,
internal_ip_address) = self._internal_fip_assoc_data(context, fip) internal_ip_address) = self._internal_fip_assoc_data(
router_id = self._get_router_for_floatingip(context, context, fip, floatingip_db['tenant_id'])
internal_port, router_id = self._get_router_for_floatingip(
internal_subnet_id, context, internal_port,
floating_network_id) internal_subnet_id, floatingip_db['floating_network_id'])
return (fip['port_id'], internal_ip_address, router_id) return (fip['port_id'], internal_ip_address, router_id)
@ -1153,7 +1138,7 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
port_id, internal_ip_address, router_id = self._get_assoc_data( port_id, internal_ip_address, router_id = self._get_assoc_data(
context, context,
fip, fip,
floatingip_db['floating_network_id']) floatingip_db)
if port_id == floatingip_db.fixed_port_id: if port_id == floatingip_db.fixed_port_id:
# Floating IP association is not changed. # Floating IP association is not changed.
@ -1300,8 +1285,6 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
with context.session.begin(subtransactions=True): with context.session.begin(subtransactions=True):
floatingip_db = self._get_floatingip(context, id) floatingip_db = self._get_floatingip(context, id)
old_floatingip = self._make_floatingip_dict(floatingip_db) old_floatingip = self._make_floatingip_dict(floatingip_db)
fip['tenant_id'] = floatingip_db['tenant_id']
fip['id'] = id
fip_port_id = floatingip_db['floating_port_id'] fip_port_id = floatingip_db['floating_port_id']
self._update_fip_assoc(context, fip, floatingip_db, self._update_fip_assoc(context, fip, floatingip_db,
self._core_plugin.get_port( self._core_plugin.get_port(