From 440274ef7248ea39366a26f7ed80e534708c85c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?McCoy=20Pati=C3=B1o?= <39780829+mccoyp@users.noreply.github.com> Date: Wed, 26 May 2021 19:07:48 -0700 Subject: [PATCH] [Key Vault] Align certificates with other languages (#18767) Co-authored-by: Charles Lowell --- .../azure-keyvault-certificates/CHANGELOG.md | 4 +- .../azure/keyvault/certificates/__init__.py | 10 ++--- .../azure/keyvault/certificates/_models.py | 41 ++++++++++++++++++- .../azure/keyvault/certificates/_parse_id.py | 29 ------------- .../tests/test_certificates_client.py | 10 ++--- .../tests/test_certificates_client_async.py | 10 ++--- .../tests/test_parse_id.py | 8 ++-- 7 files changed, 60 insertions(+), 52 deletions(-) delete mode 100644 sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/_parse_id.py diff --git a/sdk/keyvault/azure-keyvault-certificates/CHANGELOG.md b/sdk/keyvault/azure-keyvault-certificates/CHANGELOG.md index b089632b3c38..05b34267ab8d 100644 --- a/sdk/keyvault/azure-keyvault-certificates/CHANGELOG.md +++ b/sdk/keyvault/azure-keyvault-certificates/CHANGELOG.md @@ -6,8 +6,8 @@ - Updated msrest requirement to >=0.6.21 ### Added -- Added method `parse_key_vault_certificate_id` that parses out a full ID returned by Key Vault, so users can easily -access the certificate's `name`, `vault_url`, and `version`. +- Added class `KeyVaultCertificateIdentifier` that parses out a full ID returned by Key Vault, + so users can easily access the certificate's `name`, `vault_url`, and `version`. ## 4.2.1 (2020-09-08) diff --git a/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/__init__.py b/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/__init__.py index 81aec5e95261..3e8da4143494 100644 --- a/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/__init__.py +++ b/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/__init__.py @@ -22,11 +22,10 @@ DeletedCertificate, IssuerProperties, LifetimeAction, - KeyVaultCertificate + KeyVaultCertificate, + KeyVaultCertificateIdentifier ) -from ._parse_id import parse_key_vault_certificate_id from ._shared.client_base import ApiVersion -from ._shared import KeyVaultResourceId __all__ = [ "ApiVersion", @@ -44,14 +43,13 @@ "KeyCurveName", "KeyType", "KeyVaultCertificate", + "KeyVaultCertificateIdentifier", "KeyUsageType", "LifetimeAction", "CertificateContentType", "WellKnownIssuerNames", "CertificateIssuer", - "IssuerProperties", - "parse_key_vault_certificate_id", - "KeyVaultResourceId" + "IssuerProperties" ] from ._version import VERSION diff --git a/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/_models.py b/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/_models.py index c4d52201e92d..c23efb86464d 100644 --- a/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/_models.py +++ b/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/_models.py @@ -147,7 +147,7 @@ def __init__(self, **kwargs): # type: (**Any) -> None self._attributes = kwargs.pop("attributes", None) self._id = kwargs.pop("cert_id", None) - self._vault_id = parse_key_vault_id(self._id) + self._vault_id = KeyVaultCertificateIdentifier(self._id) self._x509_thumbprint = kwargs.pop("x509_thumbprint", None) self._tags = kwargs.pop("tags", None) @@ -392,6 +392,45 @@ def cer(self): return self._cer +class KeyVaultCertificateIdentifier(object): + """Information about a KeyVaultCertificate parsed from a certificate ID. + + :param str id: the full original identifier of a certificate + :raises ValueError: if the certificate ID is improperly formatted + Example: + .. literalinclude:: ../tests/test_parse_id.py + :start-after: [START parse_key_vault_certificate_id] + :end-before: [END parse_key_vault_certificate_id] + :language: python + :caption: Parse a certificate's ID + :dedent: 8 + """ + + def __init__(self, id): # pylint: disable=W0622 + # type: (str) -> None + self._resource_id = parse_key_vault_id(id) + + @property + def source_id(self): + # type: () -> str + return self._resource_id.source_id + + @property + def vault_url(self): + # type: () -> str + return self._resource_id.vault_url + + @property + def name(self): + # type: () -> str + return self._resource_id.name + + @property + def version(self): + # type: () -> Optional[str] + return self._resource_id.version + + class CertificateOperation(object): # pylint:disable=too-many-instance-attributes """A certificate operation is returned in case of long running requests. diff --git a/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/_parse_id.py b/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/_parse_id.py deleted file mode 100644 index e360b70b5da8..000000000000 --- a/sdk/keyvault/azure-keyvault-certificates/azure/keyvault/certificates/_parse_id.py +++ /dev/null @@ -1,29 +0,0 @@ -# ------------------------------------ -# Copyright (c) Microsoft Corporation. -# Licensed under the MIT License. -# ------------------------------------ - -from ._shared import parse_key_vault_id, KeyVaultResourceId - - -def parse_key_vault_certificate_id(source_id): - # type: (str) -> KeyVaultResourceId - """Parses a certificate's full ID into a class with parsed contents as attributes. - - :param str source_id: the full original identifier of a certificate - :returns: Returns a parsed certificate ID as a :class:`KeyVaultResourceId` - :rtype: ~azure.keyvault.certificates.KeyVaultResourceId - :raises: ValueError - Example: - .. literalinclude:: ../tests/test_parse_id.py - :start-after: [START parse_key_vault_certificate_id] - :end-before: [END parse_key_vault_certificate_id] - :language: python - :caption: Parse a certificate's ID - :dedent: 8 - """ - parsed_id = parse_key_vault_id(source_id) - - return KeyVaultResourceId( - name=parsed_id.name, source_id=parsed_id.source_id, vault_url=parsed_id.vault_url, version=parsed_id.version - ) diff --git a/sdk/keyvault/azure-keyvault-certificates/tests/test_certificates_client.py b/sdk/keyvault/azure-keyvault-certificates/tests/test_certificates_client.py index ce27326c21ae..6de374b49184 100644 --- a/sdk/keyvault/azure-keyvault-certificates/tests/test_certificates_client.py +++ b/sdk/keyvault/azure-keyvault-certificates/tests/test_certificates_client.py @@ -20,11 +20,11 @@ KeyType, KeyCurveName, KeyUsageType, + KeyVaultCertificateIdentifier, CertificateContentType, LifetimeAction, CertificateIssuer, - IssuerProperties, - parse_key_vault_certificate_id + IssuerProperties ) import pytest @@ -88,7 +88,7 @@ def _validate_certificate_operation(self, pending_cert_operation, vault, cert_na self.assertIsNotNone(pending_cert_operation) self.assertIsNotNone(pending_cert_operation.csr) self.assertEqual(original_cert_policy.issuer_name, pending_cert_operation.issuer_name) - pending_id = parse_key_vault_certificate_id(pending_cert_operation.id) + pending_id = KeyVaultCertificateIdentifier(pending_cert_operation.id) self.assertEqual(pending_id.vault_url.strip("/"), vault.strip("/")) self.assertEqual(pending_id.name, cert_name) @@ -349,7 +349,7 @@ def test_recover_and_purge(self, client, **kwargs): client.begin_delete_certificate(certificate_name=cert_name).wait() # validate all our deleted certificates are returned by list_deleted_certificates - deleted = [parse_key_vault_certificate_id(source_id=c.id).name for c in client.list_deleted_certificates()] + deleted = [KeyVaultCertificateIdentifier(id=c.id).name for c in client.list_deleted_certificates()] self.assertTrue(all(c in deleted for c in certs.keys())) # recover select certificates (test resources have a "livekvtest" prefix) @@ -364,7 +364,7 @@ def test_recover_and_purge(self, client, **kwargs): time.sleep(50) # validate none of our deleted certificates are returned by list_deleted_certificates - deleted = [parse_key_vault_certificate_id(source_id=c.id).name for c in client.list_deleted_certificates()] + deleted = [KeyVaultCertificateIdentifier(id=c.id).name for c in client.list_deleted_certificates()] self.assertTrue(not any(c in deleted for c in certs.keys())) # validate the recovered certificates diff --git a/sdk/keyvault/azure-keyvault-certificates/tests/test_certificates_client_async.py b/sdk/keyvault/azure-keyvault-certificates/tests/test_certificates_client_async.py index f3ea86ecefa2..dade32c27428 100644 --- a/sdk/keyvault/azure-keyvault-certificates/tests/test_certificates_client_async.py +++ b/sdk/keyvault/azure-keyvault-certificates/tests/test_certificates_client_async.py @@ -19,11 +19,11 @@ KeyType, KeyCurveName, KeyUsageType, + KeyVaultCertificateIdentifier, CertificateContentType, LifetimeAction, CertificateIssuer, - IssuerProperties, - parse_key_vault_certificate_id + IssuerProperties ) from azure.keyvault.certificates.aio import CertificateClient import pytest @@ -84,7 +84,7 @@ def _validate_certificate_operation(self, pending_cert_operation, vault, cert_na self.assertIsNotNone(pending_cert_operation) self.assertIsNotNone(pending_cert_operation.csr) self.assertEqual(original_cert_policy.issuer_name, pending_cert_operation.issuer_name) - pending_id = parse_key_vault_certificate_id(pending_cert_operation.id) + pending_id = KeyVaultCertificateIdentifier(pending_cert_operation.id) self.assertEqual(pending_id.vault_url.strip("/"), vault.strip("/")) self.assertEqual(pending_id.name, cert_name) @@ -350,7 +350,7 @@ async def test_recover_and_purge(self, client, **kwargs): deleted_certificates = client.list_deleted_certificates() deleted = [] async for c in deleted_certificates: - deleted.append(parse_key_vault_certificate_id(source_id=c.id).name) + deleted.append(KeyVaultCertificateIdentifier(id=c.id).name) self.assertTrue(all(c in deleted for c in certs.keys())) # recover select certificates (test resources have a "livekvtest" prefix) @@ -368,7 +368,7 @@ async def test_recover_and_purge(self, client, **kwargs): deleted_certificates = client.list_deleted_certificates() deleted = [] async for c in deleted_certificates: - deleted.append(parse_key_vault_certificate_id(source_id=c.id).name) + deleted.append(KeyVaultCertificateIdentifier(id=c.id).name) self.assertTrue(not any(c in deleted for c in certs.keys())) # validate the recovered certificates diff --git a/sdk/keyvault/azure-keyvault-certificates/tests/test_parse_id.py b/sdk/keyvault/azure-keyvault-certificates/tests/test_parse_id.py index 249d11df314c..e06b41bedb9b 100644 --- a/sdk/keyvault/azure-keyvault-certificates/tests/test_parse_id.py +++ b/sdk/keyvault/azure-keyvault-certificates/tests/test_parse_id.py @@ -2,7 +2,7 @@ # Copyright (c) Microsoft Corporation. # Licensed under the MIT License. # ------------------------------------- -from azure.keyvault.certificates import CertificateClient, CertificatePolicy, parse_key_vault_certificate_id +from azure.keyvault.certificates import CertificateClient, CertificatePolicy, KeyVaultCertificateIdentifier from devtools_testutils import PowerShellPreparer from _shared.test_case import KeyVaultTestCase @@ -25,7 +25,7 @@ def test_parse_certificate_id_with_version(self, azure_keyvault_url): # [START parse_key_vault_certificate_id] cert = client.get_certificate(cert_name) - parsed_certificate_id = parse_key_vault_certificate_id(cert.id) + parsed_certificate_id = KeyVaultCertificateIdentifier(cert.id) print(parsed_certificate_id.name) print(parsed_certificate_id.vault_url) @@ -40,7 +40,7 @@ def test_parse_certificate_id_with_version(self, azure_keyvault_url): def test_parse_certificate_id_with_pending_version(): source_id = "https://keyvault-name.vault.azure.net/certificates/certificate-name/pending" - parsed_certificate_id = parse_key_vault_certificate_id(source_id) + parsed_certificate_id = KeyVaultCertificateIdentifier(source_id) assert parsed_certificate_id.name == "certificate-name" assert parsed_certificate_id.vault_url == "https://keyvault-name.vault.azure.net" @@ -52,7 +52,7 @@ def test_parse_certificate_id_with_pending_version(): def test_parse_deleted_certificate_id(): source_id = "https://keyvault-name.vault.azure.net/deletedcertificates/deleted-certificate" - parsed_certificate_id = parse_key_vault_certificate_id(source_id) + parsed_certificate_id = KeyVaultCertificateIdentifier(source_id) assert parsed_certificate_id.name == "deleted-certificate" assert parsed_certificate_id.vault_url == "https://keyvault-name.vault.azure.net"