Merge "rpc/dhcp: avoid get_network_info to return segments if not needed"

This commit is contained in:
Zuul
2024-11-26 22:54:38 +00:00
committed by Gerrit Code Review
3 changed files with 27 additions and 24 deletions
+14 -11
View File
@@ -206,6 +206,7 @@ class DhcpRpcCallback:
seg_subnets = [subnet for subnet in subnets if
subnet.get('segment_id')]
nonlocal_subnets = []
network_segments = []
# If there are no subnets with segments, then this is not a routed
# network and no filtering should take place.
if seg_plug and seg_subnets:
@@ -223,6 +224,17 @@ class DhcpRpcCallback:
if subnet.get('segment_id') not in segment_ids]
subnets = [subnet for subnet in seg_subnets
if subnet.get('segment_id') in segment_ids]
network_segments = [{
'id': segment.id,
'network_id': segment.network_id,
'name': segment.name,
'network_type': segment.network_type,
'physical_network': segment.physical_network,
'segmentation_id': segment.segmentation_id,
'is_dynamic': segment.is_dynamic,
'segment_index': segment.segment_index,
'hosts': segment.hosts} for segment in network.segments]
# NOTE(kevinbenton): we sort these because the agent builds tags
# based on position in the list and has to restart the process if
# the order changes.
@@ -237,17 +249,8 @@ class DhcpRpcCallback:
key=operator.itemgetter('id')),
'ports': ports,
'mtu': network.mtu}
if seg_plug:
ret['segments'] = [{
'id': segment.id,
'network_id': segment.network_id,
'name': segment.name,
'network_type': segment.network_type,
'physical_network': segment.physical_network,
'segmentation_id': segment.segmentation_id,
'is_dynamic': segment.is_dynamic,
'segment_index': segment.segment_index,
'hosts': segment.hosts} for segment in network.segments]
if network_segments:
ret['segments'] = network_segments
return ret
@db_api.retry_db_errors
@@ -364,7 +364,6 @@ class TestDhcpAgent(base.BaseTestCase):
def test_call_driver(self):
network = mock.MagicMock()
network.id = '1'
network.segments = None
dhcp = dhcp_agent.DhcpAgent(cfg.CONF)
self.assertTrue(dhcp.call_driver('foo', network))
self.driver.assert_called_once_with(cfg.CONF,
@@ -408,7 +407,6 @@ class TestDhcpAgent(base.BaseTestCase):
trace_level='exception', expected_sync=True):
network = mock.MagicMock()
network.id = '1'
network.segments = None
self.driver.return_value.foo.side_effect = exc or Exception
dhcp = dhcp_agent.DhcpAgent(HOSTNAME)
with mock.patch.object(dhcp,
@@ -448,7 +446,6 @@ class TestDhcpAgent(base.BaseTestCase):
def test_call_driver_get_metadata_bind_interface_returns(self):
network = mock.MagicMock()
network.segments = None
self.driver().get_metadata_bind_interface.return_value = 'iface0'
agent = dhcp_agent.DhcpAgent(cfg.CONF)
self.assertEqual(
@@ -217,16 +217,19 @@ class TestDhcpRpcCallback(base.BaseTestCase):
'mtu': network.mtu}
# Plugin segment is activated globally, the tests is asserting the
# return.
ret['segments'] = [{'id': segment.id,
'network_id': segment.network_id,
'name': segment.name,
'network_type': segment.network_type,
'physical_network': segment.physical_network,
'segmentation_id': segment.segmentation_id,
'is_dynamic': segment.is_dynamic,
'segment_index': segment.segment_index,
'hosts': segment.hosts
} for segment in network.segments]
if non_local_subnets:
ret['segments'] = [{
'id': segment.id,
'network_id': segment.network_id,
'name': segment.name,
'network_type': segment.network_type,
'physical_network': segment.physical_network,
'segmentation_id': segment.segmentation_id,
'is_dynamic': segment.is_dynamic,
'segment_index': segment.segment_index,
'hosts': segment.hosts
} for segment in network.segments]
return ret
def _make_subnet_dict(subnet):