diff options
author | Zuul <zuul@review.opendev.org> | 2023-05-03 22:37:27 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2023-05-03 22:37:27 +0000 |
commit | 7ebd753bbfadb18be07ac88606083805caacbc6d (patch) | |
tree | 48f1a7166343873fdaa1fbfb56bac5cf8b9f4865 | |
parent | 7a7791d2c214fecf907ad6c7edf246a5bc926721 (diff) | |
parent | b1cc242faddd4c378dfee7b65ed72b4b190b2467 (diff) | |
download | neutron-7ebd753bbfadb18be07ac88606083805caacbc6d.tar.gz |
Merge "Add a method to retrieve router gateway ports"
-rw-r--r-- | neutron/objects/router.py | 9 | ||||
-rw-r--r-- | neutron/tests/unit/objects/test_router.py | 31 |
2 files changed, 40 insertions, 0 deletions
diff --git a/neutron/objects/router.py b/neutron/objects/router.py index 2f1e314d93..f462a1b57c 100644 --- a/neutron/objects/router.py +++ b/neutron/objects/router.py @@ -163,6 +163,15 @@ class RouterPort(base.NeutronDbObject): query = query.distinct() return [r[0] for r in query] + @classmethod + @db_api.CONTEXT_READER + def get_gw_port_ids_by_router_id(cls, context, router_id): + query = context.session.query(l3.RouterPort) + query = query.filter( + l3.RouterPort.router_id == router_id, + l3.RouterPort.port_type == n_const.DEVICE_OWNER_ROUTER_GW) + return [rp.port_id for rp in query] + @base.NeutronObjectRegistry.register class DVRMacAddress(base.NeutronDbObject): diff --git a/neutron/tests/unit/objects/test_router.py b/neutron/tests/unit/objects/test_router.py index 5c86aec2fc..f1886db0c8 100644 --- a/neutron/tests/unit/objects/test_router.py +++ b/neutron/tests/unit/objects/test_router.py @@ -17,6 +17,7 @@ from unittest import mock import netaddr +from neutron_lib import constants from neutron_lib.db import api as db_api from oslo_utils import uuidutils @@ -206,6 +207,36 @@ class RouterPortDbObjectTestCase(obj_test_base.BaseDbObjectTestCase, {'router_id': lambda: self._create_test_router_id(), 'port_id': lambda: self._create_test_port_id()}) + def test_get_gw_port_ids_by_router_id(self): + router_id = self._create_test_router_id() + router_gws = [ + self._make_object({ + 'router_id': router_id, + 'port_id': self._create_test_port_id( + device_owner=constants.DEVICE_OWNER_ROUTER_GW), + 'port_type': constants.DEVICE_OWNER_ROUTER_GW}), + self._make_object({ + 'router_id': router_id, + 'port_id': self._create_test_port_id( + device_owner=constants.DEVICE_OWNER_ROUTER_GW), + 'port_type': constants.DEVICE_OWNER_ROUTER_GW, + }) + ] + for gw in router_gws: + gw.create() + + other = self._make_object({ + 'router_id': router_id, + 'port_id': self._create_test_port_id( + device_owner=constants.DEVICE_OWNER_ROUTER_INTF), + 'port_type': constants.DEVICE_OWNER_ROUTER_INTF, + }) + other.create() + + res_gws = self._test_class.get_gw_port_ids_by_router_id(self.context, + router_id) + self.assertCountEqual(res_gws, [rp.port_id for rp in router_gws]) + class DVRMacAddressIfaceObjectTestCase(obj_test_base.BaseObjectIfaceTestCase): |