Merge "Add vpc support for group/lb vs"

This commit is contained in:
Zuul 2024-06-28 00:17:43 +00:00 committed by Gerrit Code Review
commit a797301b8e
4 changed files with 170 additions and 5 deletions

View File

@ -24,6 +24,7 @@ from vmware_nsxlib.v3 import nsx_constants
from vmware_nsxlib.v3.policy import constants
from vmware_nsxlib.v3.policy import lb_defs
TEST_TENANT = 'test'
TEST_VPC_TENANT = 'orgs/default/projects/proj-2/vpcs/vpc-1234'
class TestPolicyLBClientSSLProfileApi(test_resources.NsxPolicyLibTestCase):
@ -1176,6 +1177,102 @@ class TestPolicyLBVirtualServer(test_resources.NsxPolicyLibTestCase):
self.assert_called_with_def(api_call, expected_def)
self.assertIsNotNone(result)
def test_create_with_project_in_path(self):
name = 'd1'
description = 'desc'
obj_id = '111'
waf_profile_id = 'waf'
waf_profile_path = self.policy_lib.waf_profile.get_path(
profile_id=waf_profile_id, tenant=TEST_TENANT)
waf_profile_binding = lb_defs.WAFProfileBindingDef(
waf_profile_path=waf_profile_path)
app_profile_id = "application_profile_id"
lb_persistence_profile_id = 'lb_persistence_profile_id'
lbs_id = 'lbs_id'
lb_acl = self.resourceApi.build_access_list_control(
constants.ACTION_ALLOW, 'fake_group_path', True)
with mock.patch.object(self.policy_api,
"create_or_update") as api_call:
self.resourceApi.update(
name=name,
virtual_server_id=obj_id,
waf_profile_binding=waf_profile_binding,
description=description,
access_list_control=lb_acl,
application_profile_id=app_profile_id,
lb_persistence_profile_id=lb_persistence_profile_id,
lb_service_id='lbs_id',
tenant=TEST_VPC_TENANT)
expected_def = lb_defs.LBVirtualServerDef(
nsx_version=self.policy_lib.get_version(),
virtual_server_id=obj_id, name=name, description=description,
waf_profile_binding=waf_profile_binding,
access_list_control=lb_acl.get_obj_dict(),
application_profile_id=app_profile_id,
lb_persistence_profile_id=lb_persistence_profile_id,
lb_service_id='lbs_id',
tenant=TEST_VPC_TENANT)
self.assert_called_with_def(api_call, expected_def)
body = expected_def.get_obj_dict()
lb_pers_prof_path = '/infra/lb-persistence-profiles/%s' \
% (lb_persistence_profile_id)
self.assertEqual(body['lb_persistence_profile_path'],
lb_pers_prof_path)
app_profile_path = '/infra/lb-app-profiles/%s' % app_profile_id
self.assertEqual(body['application_profile_path'],
app_profile_path)
lb_service_path = '/%s/lb-services/%s' % (TEST_VPC_TENANT, lbs_id)
self.assertEqual(body['lb_service_path'],
lb_service_path)
name = 'd1'
description = 'desc'
obj_id = '111'
waf_profile_id = 'waf'
waf_profile_path = self.policy_lib.waf_profile.get_path(
profile_id=waf_profile_id, tenant=TEST_TENANT)
waf_profile_binding = lb_defs.WAFProfileBindingDef(
waf_profile_path=waf_profile_path)
app_profile_id = "application_profile_id"
lb_acl = self.resourceApi.build_access_list_control(
constants.ACTION_ALLOW, 'fake_group_path', True)
with mock.patch.object(self.policy_api,
"create_or_update") as api_call:
result = self.resourceApi.create_or_overwrite(
name,
virtual_server_id=obj_id,
waf_profile_binding=waf_profile_binding,
description=description,
access_list_control=lb_acl,
application_profile_id=app_profile_id,
lb_persistence_profile_id=lb_persistence_profile_id,
lb_service_id='lbs_id',
tenant=TEST_TENANT)
expected_def = lb_defs.LBVirtualServerDef(
nsx_version=self.policy_lib.get_version(),
virtual_server_id=obj_id, name=name, description=description,
waf_profile_binding=waf_profile_binding,
application_profile_id=app_profile_id,
lb_persistence_profile_id=lb_persistence_profile_id,
lb_service_id='lbs_id',
access_list_control=lb_acl.get_obj_dict(),
tenant=TEST_TENANT)
self.assert_called_with_def(api_call, expected_def)
self.assertEqual(obj_id, result)
self.assert_called_with_def(api_call, expected_def)
body = expected_def.get_obj_dict()
lb_pers_prof_path = '/test/lb-persistence-profiles/%s' \
% (lb_persistence_profile_id)
self.assertEqual(body['lb_persistence_profile_path'],
lb_pers_prof_path)
app_profile_path = '/test/lb-app-profiles/%s' % app_profile_id
self.assertEqual(body['application_profile_path'],
app_profile_path)
lb_service_path = '/%s/lb-services/%s' % (TEST_TENANT, lbs_id)
self.assertEqual(body['lb_service_path'],
lb_service_path)
def test_delete(self):
obj_id = '111'
with mock.patch.object(self.policy_api, "delete") as api_call:

View File

@ -27,6 +27,7 @@ from vmware_nsxlib.v3.policy import core_defs
from vmware_nsxlib.v3.policy import core_resources
TEST_TENANT = 'test'
TEST_VPC_TENANT = 'orgs/default/projects/proj-2/vpcs/vpc-1234'
class NsxPolicyLibTestCase(policy_testcase.TestPolicyApi):
@ -255,6 +256,50 @@ class TestPolicyGroup(NsxPolicyLibTestCase):
self.assert_called_with_def(api_call, expected_def)
self.assertEqual(group_id, result)
def test_path_id(self):
domain_id = '111'
name = 'g1'
description = 'desc'
group_id = '222'
expected_def = core_defs.GroupDef(domain_id=domain_id,
group_id=group_id,
name=name,
description=description,
conditions=[],
tenant=TEST_VPC_TENANT)
path_ids = expected_def.path_ids
self.assertEqual(path_ids, ('tenant', 'group_id'))
expected_def = core_defs.GroupDef(domain_id=domain_id,
group_id=group_id,
name=name,
description=description,
conditions=[],
tenant=constants.POLICY_INFRA_TENANT)
path_ids = expected_def.path_ids
self.assertEqual(path_ids, ('tenant', 'domain_id', 'group_id'))
def test_path_pattern(self):
domain_id = '111'
name = 'g1'
description = 'desc'
group_id = '222'
expected_def = core_defs.GroupDef(domain_id=domain_id,
group_id=group_id,
name=name,
description=description,
conditions=[],
tenant=TEST_VPC_TENANT)
path_pattern = expected_def.path_pattern
self.assertEqual(path_pattern, '%s/groups/')
expected_def = core_defs.GroupDef(domain_id=domain_id,
group_id=group_id,
name=name,
description=description,
conditions=[],
tenant=constants.POLICY_INFRA_TENANT)
path_pattern = expected_def.path_pattern
self.assertEqual(path_pattern, '%s/domains/%s/groups/')
def test_create_without_id(self):
domain_id = '111'
name = 'g1'

View File

@ -170,7 +170,6 @@ class ResourceDef(object, metaclass=abc.ABCMeta):
def get_tenant(self):
if self.attrs.get('tenant'):
return self.attrs.get('tenant')
return constants.POLICY_INFRA_TENANT
def get_section_path(self):
@ -312,6 +311,13 @@ class ResourceDef(object, metaclass=abc.ABCMeta):
def set_default_mandatory_vals(self):
pass
def is_vpc_tenant(self):
tenant = self.get_tenant()
if tenant:
return '/projects/' in tenant
else:
return False
class TenantDef(ResourceDef):
@property
@ -1808,10 +1814,16 @@ class GroupDef(ResourceDef):
@property
def path_pattern(self):
if self.is_vpc_tenant():
return TENANTS_PATH_PATTERN + "groups/"
else:
return DOMAINS_PATH_PATTERN + "%s/groups/"
@property
def path_ids(self):
if self.is_vpc_tenant():
return ('tenant', 'group_id')
else:
return ('tenant', 'domain_id', 'group_id')
@staticmethod

View File

@ -23,8 +23,11 @@ LOG = logging.getLogger(__name__)
TENANTS_PATH_PATTERN = "%s/"
LB_VIRTUAL_SERVERS_PATH_PATTERN = TENANTS_PATH_PATTERN + "lb-virtual-servers/"
VPC_LB_VIRTUAL_SERVERS_PATH_PATTERN = (TENANTS_PATH_PATTERN +
"vpc-lb-virtual-servers/")
LB_SERVICES_PATH_PATTERN = TENANTS_PATH_PATTERN + "lb-services/"
LB_POOL_PATH_PATTERN = TENANTS_PATH_PATTERN + "lb-pools/"
VPC_LB_POOL_PATH_PATTERN = TENANTS_PATH_PATTERN + "vpc-lb-pools/"
LB_APP_PROFILE_PATTERN = TENANTS_PATH_PATTERN + "lb-app-profiles/"
LB_MONITOR_PROFILE_PATTERN = TENANTS_PATH_PATTERN + "lb-monitor-profiles/"
LB_CLIENT_SSL_PROFILE_PATTERN = (TENANTS_PATH_PATTERN +
@ -228,6 +231,8 @@ class LBPoolDef(ResourceDef):
@property
def path_pattern(self):
if self.is_vpc_tenant():
return VPC_LB_POOL_PATH_PATTERN
return LB_POOL_PATH_PATTERN
@property
@ -262,6 +267,8 @@ class LBVirtualServerDef(ResourceDef):
@property
def path_pattern(self):
if self.is_vpc_tenant():
return VPC_LB_VIRTUAL_SERVERS_PATH_PATTERN
return LB_VIRTUAL_SERVERS_PATH_PATTERN
@property
@ -305,9 +312,13 @@ class LBVirtualServerDef(ResourceDef):
rule = rule.get_obj_dict()
body['rules'].append(rule)
app_profile_id = self.get_attr('application_profile_id')
profile_tenant = self.get_tenant()
# vpc profile has different tenant with vpc itself
if self.is_vpc_tenant():
profile_tenant = constants.POLICY_INFRA_TENANT
if app_profile_id:
app_profile_def = LBAppProfileBaseDef(
lb_app_profile_id=app_profile_id, tenant=self.get_tenant())
lb_app_profile_id=app_profile_id, tenant=profile_tenant)
body['application_profile_path'] = (
app_profile_def.get_resource_full_path())
@ -318,7 +329,7 @@ class LBVirtualServerDef(ResourceDef):
if lb_persistence_profile_id:
lb_persistence_profile_def = LBPersistenceProfileBase(
persistence_profile_id=lb_persistence_profile_id,
tenant=self.get_tenant())
tenant=profile_tenant)
path = lb_persistence_profile_def.get_resource_full_path()
body['lb_persistence_profile_path'] = path
if self.has_attr('lb_service_id'):