Adding scope_operator support for NSX 3.2 API.

Change-Id: I045745857317dc6effbb6a5ac627239354a3b230
This commit is contained in:
tathgurt 2021-10-21 15:04:05 -07:00
parent 3e1146a275
commit 283eff2881
4 changed files with 160 additions and 9 deletions

View File

@ -356,6 +356,45 @@ class TestPolicyGroup(NsxPolicyLibTestCase):
self.assert_called_with_def(api_call, expected_def)
self.assertIsNotNone(result)
def test_create_with_simple_condition_nsx_3_2_0(self):
# New field 'scope_operator' has been added for nsx_version 3.2.0
domain_id = '111'
name = 'g1'
description = 'desc'
cond_val = '123'
cond_op = constants.CONDITION_OP_EQUALS
cond_member_type = constants.CONDITION_MEMBER_VM
cond_key = constants.CONDITION_KEY_TAG
cond_scope_op = constants.CONDITION_OP_EQUALS
version = '3.2.0'
with mock.patch.object(self.resourceApi, 'version', version):
cond = self.resourceApi.build_condition(
cond_val=cond_val,
cond_op=cond_op,
cond_scope_op=cond_scope_op,
cond_member_type=cond_member_type,
cond_key=cond_key)
with mock.patch.object(self.policy_api,
"create_or_update") as api_call:
result = self.resourceApi.create_or_overwrite_with_conditions(
name, domain_id, description=description,
conditions=[cond],
tenant=TEST_TENANT)
exp_cond = core_defs.Condition(value=cond_val,
key=cond_key,
operator=cond_op,
scope_operator=cond_scope_op,
member_type=cond_member_type,
nsx_version=version)
expected_def = core_defs.GroupDef(domain_id=domain_id,
group_id=mock.ANY,
name=name,
description=description,
conditions=[exp_cond],
tenant=TEST_TENANT)
self.assert_called_with_def(api_call, expected_def)
self.assertIsNotNone(result)
def _test_create_with_condition(self, condition, exp_condition):
domain_id = '111'
name = 'g1'
@ -423,7 +462,6 @@ class TestPolicyGroup(NsxPolicyLibTestCase):
cond_op = constants.CONDITION_OP_EQUALS
cond_member_type = constants.CONDITION_MEMBER_VM
cond_key = constants.CONDITION_KEY_TAG
cond1 = self.resourceApi.build_condition(
cond_val=cond_val1,
cond_op=cond_op,
@ -436,7 +474,6 @@ class TestPolicyGroup(NsxPolicyLibTestCase):
cond_key=cond_key)
nested = self.resourceApi.build_nested_condition(
conditions=[cond1, cond2])
exp_cond1 = core_defs.Condition(value=cond_val1,
key=cond_key,
operator=cond_op,
@ -451,6 +488,101 @@ class TestPolicyGroup(NsxPolicyLibTestCase):
exp_cond = core_defs.NestedExpression(expressions=expressions)
self._test_create_with_condition(nested, exp_cond)
def test_create_with_union_condition_nsx_3_2_0(self):
# New field 'scope_operator' has been added for nsx_version 3.2.0
cond_val1 = '123'
cond_val2 = '456'
cond_op = constants.CONDITION_OP_EQUALS
cond_member_type = constants.CONDITION_MEMBER_VM
cond_key = constants.CONDITION_KEY_TAG
cond_scope_op = constants.CONDITION_OP_EQUALS
version = '3.2.0'
with mock.patch.object(self.resourceApi, 'version', version):
cond1 = self.resourceApi.build_condition(
cond_val=cond_val1,
cond_op=cond_op,
cond_scope_op=cond_scope_op,
cond_member_type=cond_member_type,
cond_key=cond_key)
cond2 = self.resourceApi.build_condition(
cond_val=cond_val2,
cond_op=cond_op,
cond_scope_op=cond_scope_op,
cond_member_type=cond_member_type,
cond_key=cond_key)
cond1_dup = self.resourceApi.build_condition(
cond_val=cond_val1,
cond_op=cond_op,
cond_scope_op=cond_scope_op,
cond_member_type=cond_member_type,
cond_key=cond_key)
union_cond_no_dup = self.resourceApi.build_union_condition(
conditions=[cond1, cond2])
union_cond_dup = self.resourceApi.build_union_condition(
conditions=[cond1, cond1_dup])
exp_cond1 = core_defs.Condition(value=cond_val1,
key=cond_key,
operator=cond_op,
scope_operator=cond_scope_op,
member_type=cond_member_type,
nsx_version=version)
exp_cond2 = core_defs.Condition(value=cond_val2,
key=cond_key,
operator=cond_op,
scope_operator=cond_scope_op,
member_type=cond_member_type,
nsx_version=version)
or_cond = core_defs.ConjunctionOperator(
operator=constants.CONDITION_OP_OR)
exp_cond = list({exp_cond1, exp_cond2})
exp_cond.insert(1, or_cond)
self._test_create_with_condition(union_cond_no_dup, exp_cond)
self._test_create_with_condition(union_cond_dup, [exp_cond1])
def test_create_with_nested_condition_nsx_3_2_0(self):
# New field 'scope_operator' has been added for nsx_version 3.2.0
cond_val1 = '123'
cond_val2 = '456'
cond_op = constants.CONDITION_OP_EQUALS
cond_member_type = constants.CONDITION_MEMBER_VM
cond_key = constants.CONDITION_KEY_TAG
cond_scope_op = constants.CONDITION_OP_EQUALS
version = '3.2.0'
with mock.patch.object(self.resourceApi, 'version', version):
cond1 = self.resourceApi.build_condition(
cond_val=cond_val1,
cond_op=cond_op,
cond_scope_op=cond_scope_op,
cond_member_type=cond_member_type,
cond_key=cond_key)
cond2 = self.resourceApi.build_condition(
cond_val=cond_val2,
cond_op=cond_op,
cond_scope_op=cond_scope_op,
cond_member_type=cond_member_type,
cond_key=cond_key)
nested = self.resourceApi.build_nested_condition(
conditions=[cond1, cond2])
exp_cond1 = core_defs.Condition(value=cond_val1,
key=cond_key,
operator=cond_op,
scope_operator=cond_scope_op,
member_type=cond_member_type,
nsx_version=version)
exp_cond2 = core_defs.Condition(value=cond_val2,
key=cond_key,
operator=cond_op,
scope_operator=cond_scope_op,
member_type=cond_member_type,
nsx_version=version)
and_cond = core_defs.ConjunctionOperator()
expressions = list({exp_cond1, exp_cond2})
expressions.insert(1, and_cond)
exp_cond = core_defs.NestedExpression(expressions=expressions)
self._test_create_with_condition(nested, exp_cond)
def test_create_with_dup_nested_condition(self):
cond_val1 = '123'
cond_val2 = '456'

View File

@ -32,6 +32,7 @@ CONDITION_MEMBER_VM = 'VirtualMachine'
CONDITION_MEMBER_PORT = 'LogicalPort'
CONDITION_MEMBER_SWITCH = 'LogicalSwitch'
CONDITION_OP_EQUALS = 'EQUALS'
CONDITION_OP_NOTEQUALS = 'NOTEQUALS'
CONDITION_OP_CONTAINS = 'CONTAINS'
CONDITION_OP_STARTS_WITH = 'STARTSWITH'
CONDITION_OP_AND = 'AND'

View File

@ -1611,18 +1611,33 @@ class IpPoolStaticSubnetDef(IpPoolSubnetDef):
class Condition(object):
def __init__(self, value, key=constants.CONDITION_KEY_TAG,
member_type=constants.CONDITION_MEMBER_PORT,
operator=constants.CONDITION_OP_EQUALS):
operator=constants.CONDITION_OP_EQUALS,
scope_operator=None, nsx_version=None):
# Adding NSX version for 3.2.0, new field 'scope_operator' added
self.value = value
self.key = key
self.member_type = member_type
self.operator = operator
self.scope_operator = scope_operator
self.nsx_version = nsx_version
def get_obj_dict(self):
return {'resource_type': 'Condition',
body = {'resource_type': 'Condition',
'member_type': self.member_type,
'key': self.key,
'value': self.value,
'operator': self.operator}
if self.scope_operator is not None:
if (version.LooseVersion(self.nsx_version) >=
version.LooseVersion(nsx_constants.NSX_VERSION_3_2_0)):
body['scope_operator'] = self.scope_operator
else:
LOG.warning(
"Ignoring scope_operator '%s' for NSX Policy Group : this "
"feature is not supported.Current NSX version: %s. Minimum"
" supported version: %s", self.scope_operator,
self.nsx_version, '3.2.0')
return body
def __eq__(self, other):
if isinstance(other, Condition):

View File

@ -576,14 +576,17 @@ class NsxPolicyGroupApi(NsxPolicyResourceBase):
return group_id
def build_condition(
self, cond_val=None,
cond_key=constants.CONDITION_KEY_TAG,
cond_op=constants.CONDITION_OP_EQUALS,
cond_member_type=constants.CONDITION_MEMBER_PORT):
self, cond_val=None,
cond_key=constants.CONDITION_KEY_TAG,
cond_op=constants.CONDITION_OP_EQUALS,
cond_scope_op=None,
cond_member_type=constants.CONDITION_MEMBER_PORT):
return core_defs.Condition(value=cond_val,
key=cond_key,
operator=cond_op,
member_type=cond_member_type)
scope_operator=cond_scope_op,
member_type=cond_member_type,
nsx_version=self.version)
def build_ip_address_expression(self, ip_addresses):
return core_defs.IPAddressExpression(ip_addresses)