Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demonstrate closing async credentials in Key Vault docs #16793

Merged
merged 2 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions sdk/keyvault/azure-keyvault-certificates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ See
[azure-core documentation](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/core/azure-core/CLIENT_LIBRARY_DEVELOPER.md#transport)
for more information.

Async clients should be closed when they're no longer needed. Each async
client is an async context manager and defines an async `close` method. For
Async clients and credentials should be closed when they're no longer needed. These
objects are async context managers and define async `close` methods. For
example:

```py
Expand All @@ -276,15 +276,17 @@ from azure.keyvault.certificates.aio import CertificateClient

credential = DefaultAzureCredential()

# call close when the client is no longer needed
# call close when the client and credential are no longer needed
client = CertificateClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
...
await client.close()
await credential.close()

# alternatively, use the client as an async context manager
# alternatively, use them as async context managers (contextlib.AsyncExitStack can help)
client = CertificateClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
async with client:
...
async with credential:
...
```

### Asynchronously create a Certificate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from azure.keyvault.certificates import CertificatePolicy, CertificateContentType, WellKnownIssuerNames
from azure.keyvault.certificates.aio import CertificateClient
from devtools_testutils import ResourceGroupPreparer, KeyVaultPreparer
import pytest

from _shared.preparer_async import KeyVaultClientPreparer as _KeyVaultClientPreparer
from _shared.test_case_async import KeyVaultTestCase
Expand All @@ -20,7 +21,8 @@ def print(*args):
assert all(arg is not None for arg in args)


def test_create_certificate():
@pytest.mark.asyncio
async def test_create_certificate():
vault_url = "vault_url"
# pylint:disable=unused-variable
# [START create_certificate_client]
Expand All @@ -30,6 +32,11 @@ def test_create_certificate():
# Create a KeyVaultCertificate using default Azure credentials
credential = DefaultAzureCredential()
certificate_client = CertificateClient(vault_url=vault_url, credential=credential)

# the client and credential should be closed when no longer needed
# (both are also async context managers)
await certificate_client.close()
await credential.close()
# [END create_certificate_client]


Expand Down
12 changes: 7 additions & 5 deletions sdk/keyvault/azure-keyvault-keys/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ See
[azure-core documentation](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/core/azure-core/CLIENT_LIBRARY_DEVELOPER.md#transport)
for more information.

Async clients should be closed when they're no longer needed. Each async
client is an async context manager and defines an async `close` method. For
Async clients and credentials should be closed when they're no longer needed. These
objects are async context managers and define async `close` methods. For
example:

```py
Expand All @@ -291,15 +291,17 @@ from azure.keyvault.keys.aio import KeyClient

credential = DefaultAzureCredential()

# call close when the client is no longer needed
# call close when the client and credential are no longer needed
client = KeyClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
...
await client.close()
await credential.close()

# alternatively, use the client as an async context manager
# alternatively, use them as async context managers (contextlib.AsyncExitStack can help)
client = KeyClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
async with client:
...
async with credential:
...
```

### Asynchronously create a Key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ async def test_encrypt_decrypt_async(self, azure_keyvault_url, **kwargs):
# or a key's id, which must include a version
key_id = "https://<your vault>.vault.azure.net/keys/<key name>/fe4fdcab688c479a9aa80f01ffeac26"
crypto_client = CryptographyClient(key_id, credential)

# the client and credential should be closed when no longer needed
# (both are also async context managers)
await crypto_client.close()
await credential.close()
# [END create_client]

client = CryptographyClient(key, credential)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from azure.keyvault.keys.aio import KeyClient
from azure.keyvault.keys._shared import HttpChallengeCache
from devtools_testutils import PowerShellPreparer
import pytest

from _shared.test_case_async import KeyVaultTestCase

Expand All @@ -24,7 +25,8 @@ def print(*args):
assert all(arg is not None for arg in args)


def test_create_key_client():
@pytest.mark.asyncio
async def test_create_key_client():
vault_url = "vault_url"
# pylint:disable=unused-variable
# [START create_key_client]
Expand All @@ -34,6 +36,11 @@ def test_create_key_client():
# Create a KeyClient using default Azure credentials
credential = DefaultAzureCredential()
key_client = KeyClient(vault_url, credential)

# the client and credential should be closed when no longer needed
# (both are also async context managers)
await key_client.close()
await credential.close()
# [END create_key_client]


Expand Down
12 changes: 7 additions & 5 deletions sdk/keyvault/azure-keyvault-secrets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ See
[azure-core documentation](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/core/azure-core/CLIENT_LIBRARY_DEVELOPER.md#transport)
for more information.

Async clients should be closed when they're no longer needed. Each async
client is an async context manager and defines an async `close` method. For
Async clients and credentials should be closed when they're no longer needed. These
objects are async context managers and define async `close` methods. For
example:

```py
Expand All @@ -273,15 +273,17 @@ from azure.keyvault.secrets.aio import SecretClient

credential = DefaultAzureCredential()

# call close when the client is no longer needed
# call close when the client and credential are no longer needed
client = SecretClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
...
await client.close()
await credential.close()

# alternatively, use the client as an async context manager
# alternatively, use them as async context managers (contextlib.AsyncExitStack can help)
client = SecretClient(vault_url="https://my-key-vault.vault.azure.net/", credential=credential)
async with client:
...
async with credential:
...
```

### Asynchronously create a secret
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def test_create_secret_client():
from azure.keyvault.secrets import SecretClient

# Create a SecretClient using default Azure credentials
credentials = DefaultAzureCredential()
secret_client = SecretClient(vault_url, credentials)
credential = DefaultAzureCredential()
secret_client = SecretClient(vault_url, credential)
# [END create_secret_client]


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from azure.keyvault.secrets.aio import SecretClient
from devtools_testutils import ResourceGroupPreparer, KeyVaultPreparer
import pytest

from _shared.preparer_async import KeyVaultClientPreparer as _KeyVaultClientPreparer
from _shared.test_case_async import KeyVaultTestCase
Expand All @@ -20,16 +21,22 @@ def print(*args):
assert all(arg is not None for arg in args)


def test_create_secret_client():
@pytest.mark.asyncio
async def test_create_secret_client():
vault_url = "vault_url"
# pylint:disable=unused-variable
# [START create_secret_client]
from azure.identity.aio import DefaultAzureCredential
from azure.keyvault.secrets.aio import SecretClient

# Create a SecretClient using default Azure credentials
credentials = DefaultAzureCredential()
secret_client = SecretClient(vault_url, credentials)
credential = DefaultAzureCredential()
secret_client = SecretClient(vault_url, credential)

# the client and credential should be closed when no longer needed
# (both are also async context managers)
await secret_client.close()
await credential.close()
# [END create_secret_client]


Expand Down