Segment: Allow for setting multicast in advanced_config

This change enables specifying multicast in Segment's advanced_config
attribute. Upon update, the attribute is replaced. It is up to the
caller to make sure other components such as address_pool_paths are
not overwritten.

Change-Id: I738daa6243772006b69e6149b42de9451befa7e5
This commit is contained in:
Salvatore Orlando 2021-08-26 07:35:59 -07:00
parent d26d79d76a
commit 0272e1e3cf
3 changed files with 53 additions and 8 deletions

View File

@ -3930,7 +3930,8 @@ class TestPolicySegment(NsxPolicyLibTestCase):
self.resourceApi = self.policy_lib.segment
def _test_create(self, tier1_id=None, tier0_id=None, mdproxy=None,
dhcp_server=None, admin_state=None, overlay_id=None):
dhcp_server=None, admin_state=None, multicast=None,
overlay_id=None):
name = 'test'
description = 'desc'
subnets = [core_defs.Subnet(gateway_address="2.2.2.0/24")]
@ -3938,6 +3939,8 @@ class TestPolicySegment(NsxPolicyLibTestCase):
'subnets': subnets,
'ip_pool_id': 'external-ip-pool',
'tenant': TEST_TENANT}
if multicast:
kwargs['multicast'] = multicast
if tier1_id:
kwargs['tier1_id'] = tier1_id
@ -3968,6 +3971,13 @@ class TestPolicySegment(NsxPolicyLibTestCase):
self.assert_called_with_def(api_call, expected_def)
self.assertIsNotNone(result)
# In tests we are always putting an ip pool, verify it exists
expected_advanced_config = {
'address_pool_paths': ['/infra/ip-pools/external-ip-pool']}
if multicast is not None:
expected_advanced_config.update({'multicast': multicast})
self.assertEqual(expected_def.get_obj_dict()['advanced_config'],
expected_advanced_config)
def test_create_with_t1(self):
self._test_create(tier1_id='111')
@ -3995,6 +4005,9 @@ class TestPolicySegment(NsxPolicyLibTestCase):
def test_create_with_overlay_id(self):
self._test_create(overlay_id=100)
def test_create_with_multicast(self):
self._test_create(tier1_id='111', multicast=True)
def test_delete(self):
segment_id = '111'
with mock.patch.object(self.policy_api, "delete") as api_call:
@ -4039,6 +4052,25 @@ class TestPolicySegment(NsxPolicyLibTestCase):
tenant=TEST_TENANT)
self.assert_called_with_def(update_call, expected_def)
def test_update_with_multicast(self):
# Test and update call which sets advanced_config
segment_id = '111'
admin_state = False
with self.mock_get(segment_id, 'xxx'), \
self.mock_create_update() as update_call:
self.resourceApi.update(segment_id,
admin_state=admin_state,
multicast=False,
tenant=TEST_TENANT)
expected_def = core_defs.SegmentDef(
nsx_version="3.2.0",
segment_id=segment_id,
multicast=False,
admin_state=admin_state,
tenant=TEST_TENANT)
self.assert_called_with_def(update_call, expected_def)
def test_remove_connectivity_and_subnets(self):
segment_id = '111'
with mock.patch.object(self.policy_api, "get",

View File

@ -900,20 +900,26 @@ class BaseSegmentDef(ResourceDef):
for subnet in self.get_attr('subnets')]
self._set_attr_if_specified(body, 'subnets', value=subnets)
adv_cfg = {}
if self.has_attr('ip_pool_id'):
ip_pool_id = self.get_attr('ip_pool_id')
adv_cfg = self._get_adv_config(ip_pool_id)
self._set_attr_if_specified(body, 'ip_pool_id',
body_attr='advanced_config',
value=adv_cfg)
self._set_attrs_if_specified(body, ['domain_name', 'vlan_ids'])
adv_cfg.update(self._get_adv_config_ip_pool(ip_pool_id))
if self.has_attr('multicast'):
adv_cfg['multicast'] = self.get_attr('multicast')
if adv_cfg:
body['advanced_config'] = adv_cfg
self._set_attrs_if_specified(
body, ['domain_name', 'vlan_ids'])
return body
@staticmethod
def resource_type():
return 'Segment'
def _get_adv_config(self, ip_pool_id):
def _get_adv_config_ip_pool(self, ip_pool_id):
if ip_pool_id is None:
return {'address_pool_paths': []}
ip_pool_def = IpPoolDef(ip_pool_id=ip_pool_id)
ip_pool_path = ip_pool_def.get_resource_full_path()
return {'address_pool_paths': [ip_pool_path]}

View File

@ -1986,6 +1986,7 @@ class NsxPolicySegmentApi(NsxPolicyResourceBase):
vlan_ids=IGNORE,
transport_zone_id=IGNORE,
ip_pool_id=IGNORE,
multicast=IGNORE,
metadata_proxy_id=IGNORE,
dhcp_server_config_id=IGNORE,
admin_state=IGNORE,
@ -2010,6 +2011,7 @@ class NsxPolicySegmentApi(NsxPolicyResourceBase):
vlan_ids=vlan_ids,
transport_zone_id=transport_zone_id,
ip_pool_id=ip_pool_id,
multicast=multicast,
metadata_proxy_id=metadata_proxy_id,
dhcp_server_config_id=dhcp_server_config_id,
admin_state=admin_state,
@ -2044,10 +2046,14 @@ class NsxPolicySegmentApi(NsxPolicyResourceBase):
def update(self, segment_id, name=IGNORE, description=IGNORE,
tier1_id=IGNORE, tier0_id=IGNORE, subnets=IGNORE,
dns_domain_name=IGNORE,
vlan_ids=IGNORE, metadata_proxy_id=IGNORE,
vlan_ids=IGNORE, multicast=IGNORE, metadata_proxy_id=IGNORE,
dhcp_server_config_id=IGNORE, admin_state=IGNORE,
tags=IGNORE, tenant=constants.POLICY_INFRA_TENANT):
# NOTE: Setting multicast upon update will reset any other advanced
# config attribute that might have been set previously
# TODO(sorlando): Regardless of patch strategy always fetch advanced
# config and merge before updating
self._update(segment_id=segment_id,
name=name,
description=description,
@ -2056,6 +2062,7 @@ class NsxPolicySegmentApi(NsxPolicyResourceBase):
subnets=subnets,
dns_domain_name=dns_domain_name,
vlan_ids=vlan_ids,
multicast=multicast,
metadata_proxy_id=metadata_proxy_id,
dhcp_server_config_id=dhcp_server_config_id,
admin_state=admin_state,