Fix group def in Policy API

When updating group with empty conditions list, "expression" list
should be explicitly setting to empty list instead of ignored,
so that group gets updated properly with empty expressions.

Change-Id: I779dca3587721f7d9b0da83385a243e3a1132f7c
This commit is contained in:
Enhao Cui 2020-02-18 14:52:12 -08:00
parent 447bb9dec2
commit 886854fc4a
3 changed files with 29 additions and 9 deletions

View File

@ -297,6 +297,24 @@ class TestPolicyGroup(NsxPolicyLibTestCase):
self.assert_called_with_def(api_call, expected_def)
self.assertIsNotNone(result)
def test_create_with_empty_condition(self):
domain_id = '111'
name = 'g1'
description = 'desc'
with mock.patch.object(self.policy_api,
"create_or_update") as api_call:
result = self.resourceApi.create_or_overwrite(
name, domain_id, description=description,
tenant=TEST_TENANT)
expected_def = core_defs.GroupDef(domain_id=domain_id,
group_id=mock.ANY,
name=name,
description=description,
conditions=[],
tenant=TEST_TENANT)
self.assert_called_with_def(api_call, expected_def)
self.assertIsNotNone(result)
def test_create_with_simple_condition(self):
domain_id = '111'
name = 'g1'

View File

@ -77,15 +77,15 @@ class TestPolicyTransaction(policy_testcase.TestPolicyApi):
g1 = {'resource_type': 'Group', 'id': 'group1',
'display_name': 'g1',
'description': 'first group',
'tags': None}
'tags': None, 'expression': []}
g2 = {'resource_type': 'Group', 'id': 'group2',
'description': 'second group',
'display_name': 'g2',
'tags': tags}
'tags': tags, 'expression': []}
g3 = {'resource_type': 'Group', 'id': 'group3',
'display_name': 'g3',
'description': 'third group',
'tags': None}
'tags': None, 'expression': []}
d1 = {'resource_type': 'Domain', 'id': 'domain1',
'display_name': 'd1', 'description': 'first domain',
'tags': tags}
@ -168,10 +168,10 @@ class TestPolicyTransaction(policy_testcase.TestPolicyApi):
g1 = {'resource_type': 'Group', 'id': 'group1',
'display_name': 'g1',
'description': 'first group'}
'description': 'first group', 'expression': []}
g2 = {'resource_type': 'Group', 'id': 'group2',
'description': 'second group',
'display_name': 'g2'}
'display_name': 'g2', 'expression': []}
d1 = {'resource_type': 'Domain', 'id': 'domain1'}
d2 = {'resource_type': 'Domain', 'id': 'domain2'}

View File

@ -1516,11 +1516,13 @@ class GroupDef(ResourceDef):
def get_obj_dict(self):
body = super(GroupDef, self).get_obj_dict()
conds = self.get_attr('conditions')
if conds:
# If conditions were IGNORE, conds would be None here.
# Otherwise, conds could be an empty list which denotes
# updating group expression to empty list.
if conds is not None:
conds = conds if isinstance(conds, list) else [conds]
if conds:
body['expression'] = [condition.get_obj_dict()
for condition in conds]
body['expression'] = [condition.get_obj_dict()
for condition in conds]
return body