From 78d21ea871b99a63188817aa86c5caaa30ba43b6 Mon Sep 17 00:00:00 2001 From: asarfaty Date: Sun, 28 Jun 2020 15:13:16 +0200 Subject: [PATCH] Add new certificate api find_cert_with_pem and fix policy certificate resource type Change-Id: I0b87ea9b6563ef6d046659ff12015db1eba42283 --- .../tests/unit/v3/policy/test_resources.py | 16 ++++++++++++++++ vmware_nsxlib/v3/policy/core_defs.py | 2 +- vmware_nsxlib/v3/policy/core_resources.py | 8 ++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/vmware_nsxlib/tests/unit/v3/policy/test_resources.py b/vmware_nsxlib/tests/unit/v3/policy/test_resources.py index 5ab1d6ad..6a937be3 100644 --- a/vmware_nsxlib/tests/unit/v3/policy/test_resources.py +++ b/vmware_nsxlib/tests/unit/v3/policy/test_resources.py @@ -5191,6 +5191,22 @@ class TestPolicyCertificate(NsxPolicyLibTestCase): ) self.assert_called_with_def(update_call, expected_def) + def test_find_cert_with_pem(self): + id1 = '1' + id2 = '2' + pem1 = '111' + pem2 = '222' + with mock.patch.object(self.policy_api, "list", + return_value={'results': [ + {'id': id1, 'pem_encoded': pem1}, + {'id': id2, 'pem_encoded': pem2}]}) as api: + cert_ids = self.resourceApi.find_cert_with_pem( + pem1, tenant=TEST_TENANT) + self.assertEqual(1, len(cert_ids)) + self.assertEqual(id1, cert_ids[0]) + expected_def = core_defs.CertificateDef(tenant=TEST_TENANT) + self.assert_called_with_def(api, expected_def) + class TestPolicyExcludeList(NsxPolicyLibTestCase): diff --git a/vmware_nsxlib/v3/policy/core_defs.py b/vmware_nsxlib/v3/policy/core_defs.py index df88f9a6..a1c371d7 100644 --- a/vmware_nsxlib/v3/policy/core_defs.py +++ b/vmware_nsxlib/v3/policy/core_defs.py @@ -2089,7 +2089,7 @@ class CertificateDef(ResourceDef): @staticmethod def resource_type(): - return "TlsTrustData" + return "TlsCertificate" def get_obj_dict(self): body = super(CertificateDef, self).get_obj_dict() diff --git a/vmware_nsxlib/v3/policy/core_resources.py b/vmware_nsxlib/v3/policy/core_resources.py index 0b0a6949..137fac51 100644 --- a/vmware_nsxlib/v3/policy/core_resources.py +++ b/vmware_nsxlib/v3/policy/core_resources.py @@ -4150,6 +4150,14 @@ class NsxPolicyCertApi(NsxPolicyResourceBase): certificate_def = self.entry_def(tenant=tenant) return self._list(certificate_def) + def find_cert_with_pem(self, cert_pem, + tenant=constants.POLICY_INFRA_TENANT): + # Find certificate with cert_pem + certs = self.list(tenant=tenant) + cert_ids = [cert['id'] for cert in certs + if cert['pem_encoded'] == cert_pem] + return cert_ids + def update(self, certificate_id, name=IGNORE, pem_encoded=IGNORE, private_key=IGNORE, passphrase=IGNORE, key_algo=IGNORE, description=IGNORE,