From a03afc1bc2d855e66633228fdfd5797c7414b39c Mon Sep 17 00:00:00 2001 From: Rodolfo Alonso Hernandez Date: Mon, 21 Oct 2024 22:21:55 +0000 Subject: [PATCH] Exit fast checking the external network default If the network is not external, there is no need to check if there is an associated ``externalnetwork`` register. Closes-Bug: #2085447 Change-Id: I54de12dd8df99c605bf3da6dea4c6c5a074e3b86 --- neutron/services/auto_allocate/db.py | 4 ++- .../unit/services/auto_allocate/test_db.py | 34 +++++++++++++------ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/neutron/services/auto_allocate/db.py b/neutron/services/auto_allocate/db.py index 1d6d77519e8..65aa63c62df 100644 --- a/neutron/services/auto_allocate/db.py +++ b/neutron/services/auto_allocate/db.py @@ -15,6 +15,7 @@ # under the License. from neutron_lib.api.definitions import constants as api_const +from neutron_lib.api.definitions import external_net as external_net_apidef from neutron_lib.api.definitions import l3 as l3_apidef from neutron_lib.api.definitions import network as net_def from neutron_lib.callbacks import events @@ -53,7 +54,8 @@ def _ensure_external_network_default_value_callback( def _do_ensure_external_network_default_value_callback( context, request, orig, network): is_default = request.get(api_const.IS_DEFAULT) - if is_default is None: + is_external = request.get(external_net_apidef.EXTERNAL) + if is_default is None or not is_external: return if is_default: # ensure only one default external network at any given time diff --git a/neutron/tests/unit/services/auto_allocate/test_db.py b/neutron/tests/unit/services/auto_allocate/test_db.py index 2ff3cf7955a..6fbed1b9d3a 100644 --- a/neutron/tests/unit/services/auto_allocate/test_db.py +++ b/neutron/tests/unit/services/auto_allocate/test_db.py @@ -14,6 +14,7 @@ from unittest import mock from neutron_lib.api.definitions import constants as api_const +from neutron_lib.api.definitions import external_net as external_net_apidef from neutron_lib.callbacks import events from neutron_lib import constants from neutron_lib import context @@ -46,15 +47,18 @@ class AutoAllocateTestCase(testlib_api.SqlTestCase): "context": self.ctx, "request": { "id": network_id, - api_const.IS_DEFAULT: True + api_const.IS_DEFAULT: True, + external_net_apidef.EXTERNAL: True, }, "network": { "id": network_id, - api_const.IS_DEFAULT: False + api_const.IS_DEFAULT: False, + external_net_apidef.EXTERNAL: False, }, "original_network": { "id": network_id, - api_const.IS_DEFAULT: False + api_const.IS_DEFAULT: False, + external_net_apidef.EXTERNAL: False, } } network_mock = mock.MagicMock(network_id=network_id, is_default=False) @@ -82,11 +86,13 @@ class AutoAllocateTestCase(testlib_api.SqlTestCase): "context": self.ctx, "request": { "id": network_id, - api_const.IS_DEFAULT: None + api_const.IS_DEFAULT: None, + external_net_apidef.EXTERNAL: False, }, "network": { "id": network_id, - api_const.IS_DEFAULT: False + api_const.IS_DEFAULT: False, + external_net_apidef.EXTERNAL: False, }, } network_mock = mock.MagicMock(network_id=network_id, is_default=False) @@ -112,15 +118,18 @@ class AutoAllocateTestCase(testlib_api.SqlTestCase): "context": self.ctx, "request": { "id": network_id, - api_const.IS_DEFAULT: True + api_const.IS_DEFAULT: True, + external_net_apidef.EXTERNAL: True, }, "network": { "id": network_id, - api_const.IS_DEFAULT: True + api_const.IS_DEFAULT: True, + external_net_apidef.EXTERNAL: True, }, "original_network": { "id": network_id, - api_const.IS_DEFAULT: True + api_const.IS_DEFAULT: True, + external_net_apidef.EXTERNAL: True, } } network_mock = mock.MagicMock(network_id=network_id, is_default=True) @@ -147,15 +156,18 @@ class AutoAllocateTestCase(testlib_api.SqlTestCase): "context": self.ctx, "request": { "id": network_id, - api_const.IS_DEFAULT: True + api_const.IS_DEFAULT: True, + external_net_apidef.EXTERNAL: True, }, "network": { "id": network_id, - api_const.IS_DEFAULT: False + api_const.IS_DEFAULT: False, + external_net_apidef.EXTERNAL: False, }, "original_network": { "id": network_id, - api_const.IS_DEFAULT: False + api_const.IS_DEFAULT: False, + external_net_apidef.EXTERNAL: False, } } network_mock = mock.MagicMock(network_id='fake_id', is_default=False)