diff --git a/vmware_nsx/services/dynamic_routing/bgp_plugin.py b/vmware_nsx/services/dynamic_routing/bgp_plugin.py index 9ff223e191..9f14f3fd28 100644 --- a/vmware_nsx/services/dynamic_routing/bgp_plugin.py +++ b/vmware_nsx/services/dynamic_routing/bgp_plugin.py @@ -93,17 +93,17 @@ class NSXvBgpPlugin(service_base.ServicePluginBase, bgp_db.BgpDbMixin): super(NSXvBgpPlugin, self).delete_bgp_speaker(context, bgp_speaker_id) - def _get_esg_peer_info(self, context, bgp_peer_id): + def _add_esg_peer_info(self, context, peer): binding = nsxv_db.get_nsxv_bgp_peer_edge_binding(context.session, - bgp_peer_id) + peer['id']) if binding: - return binding['edge_id'] + peer['esg_id'] = binding['edge_id'] def get_bgp_peer(self, context, bgp_peer_id, fields=None): peer = super(NSXvBgpPlugin, self).get_bgp_peer(context, bgp_peer_id, fields) - if fields is None or 'esg_id' in fields: - peer['esg_id'] = self._get_esg_peer_info(context, bgp_peer_id) + if not fields or 'esg_id' in fields: + self._add_esg_peer_info(context, peer) return peer def get_bgp_peers_by_bgp_speaker(self, context, @@ -112,7 +112,7 @@ class NSXvBgpPlugin(service_base.ServicePluginBase, bgp_db.BgpDbMixin): context, bgp_speaker_id, fields=fields) if fields is None or 'esg_id' in fields: for peer in ret: - peer['esg_id'] = self._get_esg_peer_info(context, peer['id']) + self._add_esg_peer_info(context, peer) return ret def create_bgp_peer(self, context, bgp_peer): diff --git a/vmware_nsx/tests/unit/services/dynamic_routing/test_nsxv_bgp_driver.py b/vmware_nsx/tests/unit/services/dynamic_routing/test_nsxv_bgp_driver.py index 5adf0c94e4..8925dd57f1 100644 --- a/vmware_nsx/tests/unit/services/dynamic_routing/test_nsxv_bgp_driver.py +++ b/vmware_nsx/tests/unit/services/dynamic_routing/test_nsxv_bgp_driver.py @@ -12,6 +12,9 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. +import contextlib +import mock + from neutron.api import extensions from neutron_dynamic_routing.db import bgp_db # noqa from neutron_dynamic_routing import extensions as dr_extensions @@ -22,6 +25,7 @@ from neutron_lib import exceptions as n_exc from neutron_lib.plugins import directory from vmware_nsx.services.dynamic_routing import bgp_plugin +from vmware_nsx.services.dynamic_routing.nsx_v import driver as bgp_driver from vmware_nsx.tests.unit.nsx_v import test_plugin BGP_PLUGIN = 'vmware_nsx.services.dynamic_routing.bgp_plugin.NSXvBgpPlugin' @@ -39,6 +43,20 @@ class TestNSXvBgpPlugin(test_plugin.NsxVPluginV2TestCase, self.plugin.init_is_complete = True self.context = context.get_admin_context() + @contextlib.contextmanager + def esg_bgp_peer(self, esg_id): + data = {'name': '', + 'peer_ip': '192.168.1.10', + 'remote_as': '65000', + 'esg_id': esg_id, + 'auth_type': 'none', + 'password': '', + 'tenant_id': ''} + bgp_peer = self.bgp_plugin.create_bgp_peer(self.context, + {'bgp_peer': data}) + yield bgp_peer + self.bgp_plugin.delete_bgp_peer(self.context, bgp_peer['id']) + def test_create_v6_bgp_speaker(self): fake_bgp_speaker = { "bgp_speaker": { @@ -64,6 +82,18 @@ class TestNSXvBgpPlugin(test_plugin.NsxVPluginV2TestCase, self.bgp_plugin.create_bgp_peer, self.context, fake_bgp_peer) + def test_bgp_peer_esg_id(self): + edge_id = 'edge-123' + with mock.patch.object(bgp_driver.NSXvBgpDriver, + '_validate_bgp_configuration_on_peer_esg', + side_effect=None): + with self.esg_bgp_peer(esg_id='edge-123') as esg_peer: + self.assertEqual(edge_id, esg_peer['esg_id']) + + peer_id = esg_peer['id'] + bgp_peer = self.bgp_plugin.get_bgp_peer(self.context, peer_id) + self.assertEqual(edge_id, bgp_peer['esg_id']) + def test_create_bgp_peer_md5_auth_no_password(self): # TODO(roeyc): Test requires a minor fix in base class. pass