Use network.external DB model parameter when creating a floating IP

When a floating IP is being created, the network provided should be
external. Instead of quering the DB to find out if the
"externalnetworks" DB register exists, the "network" register is
retrieved and the "external" parameter is used (loaded using a
back reference relationship). This will avoid one DB access.

Change-Id: Iead245da166ee2ae691227bb18ae377fe0af4c04
Related-Bug: #1880969
This commit is contained in:
Rodolfo Alonso Hernandez 2020-06-19 16:32:25 +00:00
parent 9867baa86f
commit f43f5dc64f
2 changed files with 12 additions and 6 deletions

View File

@ -1250,9 +1250,8 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
'context': context,
'association_event': association_event}
def _is_ipv4_network(self, context, net_id):
net = self._core_plugin._get_network(context, net_id)
return any(s.ip_version == 4 for s in net.subnets)
def _is_ipv4_network(self, context, net_db):
return any(s.ip_version == 4 for s in net_db.subnets)
def _create_floatingip(self, context, floatingip,
initial_status=constants.FLOATINGIP_STATUS_ACTIVE):
@ -1268,11 +1267,12 @@ class L3_NAT_dbonly_mixin(l3.RouterPluginBase,
fip_id = uuidutils.generate_uuid()
f_net_id = fip['floating_network_id']
if not self._core_plugin._network_is_external(context, f_net_id):
f_net_db = self._core_plugin._get_network(context, f_net_id)
if not f_net_db.external:
msg = _("Network %s is not a valid external network") % f_net_id
raise n_exc.BadRequest(resource='floatingip', msg=msg)
if not self._is_ipv4_network(context, f_net_id):
if not self._is_ipv4_network(context, f_net_db):
msg = _("Network %s does not contain any IPv4 subnet") % f_net_id
raise n_exc.BadRequest(resource='floatingip', msg=msg)

View File

@ -2961,9 +2961,15 @@ class L3NatTestCaseBase(L3NatTestCaseMixin):
def test_create_floatingip_invalid_fixed_ipv6_address_returns_400(self):
# API-level test - no need to create all objects for l3 plugin
res = self._create_floatingip(self.fmt, uuidutils.generate_uuid(),
uuidutils.generate_uuid(), '2001:db8::a')
uuidutils.generate_uuid(), '2001:db8::m')
self.assertEqual(400, res.status_int)
def test_create_floatingip_not_existing_network_returns_404(self):
# API-level test - no need to create all objects for l3 plugin
res = self._create_floatingip(self.fmt, uuidutils.generate_uuid(),
uuidutils.generate_uuid(), '2001:db8::a')
self.assertEqual(404, res.status_int)
def test_floatingip_list_with_sort(self):
with self.subnet(cidr="10.0.0.0/24") as s1,\
self.subnet(cidr="11.0.0.0/24") as s2,\