From 6e61443bd5289f1b82366de4bc28338efc1042e9 Mon Sep 17 00:00:00 2001 From: Adit Sarfaty Date: Mon, 24 Dec 2018 14:19:12 +0200 Subject: [PATCH] Add optional project id to build_v3_api_version_project_tag This will allow policy objects to have project id tag even without a neutron resource id Change-Id: I58ae385981e020ff3876da9c0ac20afd1ceca3be --- vmware_nsxlib/tests/unit/v3/test_utils.py | 15 +++++++++++++++ vmware_nsxlib/v3/__init__.py | 5 +++-- vmware_nsxlib/v3/utils.py | 8 ++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/vmware_nsxlib/tests/unit/v3/test_utils.py b/vmware_nsxlib/tests/unit/v3/test_utils.py index a9a9fc20..dd9ef79e 100644 --- a/vmware_nsxlib/tests/unit/v3/test_utils.py +++ b/vmware_nsxlib/tests/unit/v3/test_utils.py @@ -77,6 +77,21 @@ class TestNsxV3Utils(nsxlib_testcase.NsxClientTestCase): 'tag': proj}] self.assertEqual(expected, result) + def test_build_v3_api_version_project_id_tag(self): + proj = 'project_x' + proj_id = 'project_id' + result = self.nsxlib.build_v3_api_version_project_tag( + proj, project_id=proj_id) + expected = [{'scope': nsxlib_testcase.PLUGIN_SCOPE, + 'tag': nsxlib_testcase.PLUGIN_TAG}, + {'scope': 'os-api-version', + 'tag': nsxlib_testcase.PLUGIN_VER}, + {'scope': 'os-project-name', + 'tag': proj}, + {'scope': 'os-project-id', + 'tag': proj_id}] + self.assertEqual(expected, result) + def test_is_internal_resource(self): project_tag = self.nsxlib.build_v3_tags_payload( {'id': 'fake_id', diff --git a/vmware_nsxlib/v3/__init__.py b/vmware_nsxlib/v3/__init__.py index 09ae6770..5f0f708e 100644 --- a/vmware_nsxlib/v3/__init__.py +++ b/vmware_nsxlib/v3/__init__.py @@ -94,8 +94,9 @@ class NsxLibBase(object): def is_internal_resource(self, nsx_resource): return self.general_apis.is_internal_resource(nsx_resource) - def build_v3_api_version_project_tag(self, project_name): - return self.general_apis.build_v3_api_version_project_tag(project_name) + def build_v3_api_version_project_tag(self, project_name, project_id=None): + return self.general_apis.build_v3_api_version_project_tag( + project_name, project_id=project_id) def build_v3_tags_payload(self, resource, resource_type, project_name): return self.general_apis.build_v3_tags_payload( diff --git a/vmware_nsxlib/v3/utils.py b/vmware_nsxlib/v3/utils.py index 45c3752f..75c6b6ee 100644 --- a/vmware_nsxlib/v3/utils.py +++ b/vmware_nsxlib/v3/utils.py @@ -494,15 +494,19 @@ class NsxLibApiBase(object): {'scope': "os-api-version", 'tag': self.nsxlib_config.plugin_ver}] - def build_v3_api_version_project_tag(self, project_name): + def build_v3_api_version_project_tag(self, project_name, project_id=None): if not project_name: project_name = self.nsxlib_config.plugin_tag - return [{'scope': self.nsxlib_config.plugin_scope, + tags = [{'scope': self.nsxlib_config.plugin_scope, 'tag': self.nsxlib_config.plugin_tag}, {'scope': "os-api-version", 'tag': self.nsxlib_config.plugin_ver}, {'scope': 'os-project-name', 'tag': project_name[:MAX_TAG_LEN]}] + if project_id: + tags.append({'scope': 'os-project-id', + 'tag': project_id[:MAX_TAG_LEN]}) + return tags def is_internal_resource(self, nsx_resource): """Indicates whether the passed nsx-resource is internal