From 94936bc61647a71586257b2cba21640e03ebb283 Mon Sep 17 00:00:00 2001 From: Adit Sarfaty Date: Mon, 22 Oct 2018 13:32:48 +0300 Subject: [PATCH] Add utility to generate tags with api version & project name Those tags can be used for policy resources, where there is no need to add the os resource id to the tags, as it is the same as the NSX id. Change-Id: I4e9644957dbfd16194f556cbff5447f2a2e49669 --- vmware_nsxlib/tests/unit/v3/test_utils.py | 11 +++++++++++ vmware_nsxlib/v3/__init__.py | 3 +++ vmware_nsxlib/v3/utils.py | 10 ++++++++++ 3 files changed, 24 insertions(+) diff --git a/vmware_nsxlib/tests/unit/v3/test_utils.py b/vmware_nsxlib/tests/unit/v3/test_utils.py index da261610..a9a9fc20 100644 --- a/vmware_nsxlib/tests/unit/v3/test_utils.py +++ b/vmware_nsxlib/tests/unit/v3/test_utils.py @@ -66,6 +66,17 @@ class TestNsxV3Utils(nsxlib_testcase.NsxClientTestCase): 'tag': nsxlib_testcase.PLUGIN_VER}] self.assertEqual(expected, result) + def test_build_v3_api_version_project_tag(self): + proj = 'project_x' + result = self.nsxlib.build_v3_api_version_project_tag(proj) + 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}] + 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 cf4283ea..b3d87e7b 100644 --- a/vmware_nsxlib/v3/__init__.py +++ b/vmware_nsxlib/v3/__init__.py @@ -93,6 +93,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_tags_payload(self, resource, resource_type, project_name): return self.general_apis.build_v3_tags_payload( resource, resource_type, project_name) diff --git a/vmware_nsxlib/v3/utils.py b/vmware_nsxlib/v3/utils.py index b5ff2f00..45c3752f 100644 --- a/vmware_nsxlib/v3/utils.py +++ b/vmware_nsxlib/v3/utils.py @@ -494,6 +494,16 @@ class NsxLibApiBase(object): {'scope': "os-api-version", 'tag': self.nsxlib_config.plugin_ver}] + def build_v3_api_version_project_tag(self, project_name): + if not project_name: + project_name = self.nsxlib_config.plugin_tag + return [{'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]}] + def is_internal_resource(self, nsx_resource): """Indicates whether the passed nsx-resource is internal