From e95f3a23538704b4d668ec926892ea99d7a9ef2f Mon Sep 17 00:00:00 2001 From: "Adam Ling (MSFT)" Date: Tue, 6 Apr 2021 11:21:51 -0700 Subject: [PATCH] [EventHubs] Improvement on doc and sample (#17833) --- sdk/eventhub/azure-eventhub/CHANGELOG.md | 2 +- sdk/eventhub/azure-eventhub/samples/README.md | 7 +++++-- .../authenticate_with_azure_sas_credential_async.py | 6 +++--- .../async_samples/authenticate_with_sas_token_async.py | 4 ++-- .../azure-eventhub/samples/sample_dev_requirements.txt | 2 +- .../sync_samples/authenticate_with_azure_sas_credential.py | 5 +---- .../samples/sync_samples/authenticate_with_sas_token.py | 6 +----- 7 files changed, 14 insertions(+), 18 deletions(-) diff --git a/sdk/eventhub/azure-eventhub/CHANGELOG.md b/sdk/eventhub/azure-eventhub/CHANGELOG.md index 028cefcc55a3..b7b15068be25 100644 --- a/sdk/eventhub/azure-eventhub/CHANGELOG.md +++ b/sdk/eventhub/azure-eventhub/CHANGELOG.md @@ -6,7 +6,7 @@ This version follows from version 5.3.1, rather than 5.4.0b1 so that the preview **New Features** -- Added support for using `~azure.core.credentials.AzureSasCredential` as credential for authenticating producer and consumer clients. +- Added support for using `azure.core.credentials.AzureSasCredential` as credential for authenticating producer and consumer clients. - Updated `list_ownership`, `claim_ownership`, `update_checkpoint`, `list_checkpoints` on sync and async `CheckpointStore` to support taking `**kwargs`. - WARNING: Implementing a custom checkpointstore that does not support taking `**kwargs` in the methods listed previously will result in the following pylint error: `W0221: Parameters differ from overridden ________ method (arguments-differ)`. - Updated `update_checkpoint` on sync and async `PartitionContext` to support taking `**kwargs`. diff --git a/sdk/eventhub/azure-eventhub/samples/README.md b/sdk/eventhub/azure-eventhub/samples/README.md index 09795b575cb1..62197853abc1 100644 --- a/sdk/eventhub/azure-eventhub/samples/README.md +++ b/sdk/eventhub/azure-eventhub/samples/README.md @@ -64,10 +64,13 @@ Both [sync version](https://github.com/Azure/azure-sdk-for-python/tree/master/sd - [iot_hub_connection_string_receive_async.py](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/eventhub/azure-eventhub/samples/async_samples/iot_hub_connection_string_receive_async.py) - Examples to receive events from an IoT Hub: - Convert an IoT Hub connection string to the built-in Event Hub endpoint and receive events from it -- [authenticate_with_sas_token.py](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/eventhub/azure-eventhub/samples/sync_samples/authenticate_with_sas_token.py) +- [authenticate_with_sas_token.py](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/eventhub/azure-eventhub/samples/sync_samples/authenticate_with_sas_token.py) ([async_version](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/eventhub/azure-eventhub/samples/async_samples/authenticate_with_sas_token_async.py)) - Examples: - Utilize a SAS token to authenticate when creating an Event Hub client. -- [connection_to_custom_endpoint_address.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventhub/azure-eventhub/samples/sync_samples/connection_to_custom_endpoint_address.py) ([async version](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventhub/azure-eventhub/samples/async_samples/connection_to_custom_endpoint_address_async.py)) - Examples +- [authenticate_with_azure_sas_credential.py](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/eventhub/azure-eventhub/samples/sync_samples/authenticate_with_azure_sas_credential.py) ([async_version](https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/eventhub/azure-eventhub/samples/async_samples/authenticate_with_azure_sas_credential_async.py)) - Examples: + - Utilize `azure.core.credentials.AzureSasCredential` to authenticate when creating an Event Hub client. + +- [connection_to_custom_endpoint_address.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventhub/azure-eventhub/samples/sync_samples/connection_to_custom_endpoint_address.py) ([async version](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/eventhub/azure-eventhub/samples/async_samples/connection_to_custom_endpoint_address_async.py)) - Examples: to create EventHubProducerClient and EventHubConsumerClient that connect to a custom endpoint with a custom certificate. ## Prerequisites diff --git a/sdk/eventhub/azure-eventhub/samples/async_samples/authenticate_with_azure_sas_credential_async.py b/sdk/eventhub/azure-eventhub/samples/async_samples/authenticate_with_azure_sas_credential_async.py index 6349152a1f99..5f6e9f6ffe77 100644 --- a/sdk/eventhub/azure-eventhub/samples/async_samples/authenticate_with_azure_sas_credential_async.py +++ b/sdk/eventhub/azure-eventhub/samples/async_samples/authenticate_with_azure_sas_credential_async.py @@ -6,7 +6,7 @@ # -------------------------------------------------------------------------------------------- """ -Example to demonstrate utilizing azure.core.credentials.AzureSasCredential to authenticate with EventHub +Example to demonstrate utilizing azure.core.credentials.AzureSasCredential to authenticate with Event Hubs. """ # pylint: disable=C0111 @@ -22,7 +22,7 @@ except ImportError: from urllib import pathname2url as url_parse_quote -from azure.core.credentials import AccessToken, AzureSasCredential +from azure.core.credentials import AzureSasCredential from azure.eventhub.aio import EventHubProducerClient from azure.eventhub import EventData @@ -39,12 +39,12 @@ def generate_sas_token(uri, sas_name, sas_value, token_ttl): # Target namespace and hub must also be specified. Consumer group is set to default unless required otherwise. FULLY_QUALIFIED_NAMESPACE = os.environ['EVENT_HUB_HOSTNAME'] EVENTHUB_NAME = os.environ['EVENT_HUB_NAME'] -CONSUMER_GROUP = "$Default" # The following part creates a SAS token. A SAS token is typically given to you after being created. SAS_POLICY = os.environ['EVENT_HUB_SAS_POLICY'] SAS_KEY = os.environ['EVENT_HUB_SAS_KEY'] + async def create_with_sas_token(): uri = "sb://{}/{}".format(FULLY_QUALIFIED_NAMESPACE, EVENTHUB_NAME) token_ttl = 3000 # seconds diff --git a/sdk/eventhub/azure-eventhub/samples/async_samples/authenticate_with_sas_token_async.py b/sdk/eventhub/azure-eventhub/samples/async_samples/authenticate_with_sas_token_async.py index e8dda3260098..4c6d20fd62f5 100644 --- a/sdk/eventhub/azure-eventhub/samples/async_samples/authenticate_with_sas_token_async.py +++ b/sdk/eventhub/azure-eventhub/samples/async_samples/authenticate_with_sas_token_async.py @@ -6,7 +6,7 @@ # -------------------------------------------------------------------------------------------- """ -Example to demonstrate utilizing SAS (Shared Access Signature) tokens to authenticate with ServiceBus +Example to demonstrate utilizing SAS (Shared Access Signature) tokens to authenticate with Event Hubs """ # pylint: disable=C0111 @@ -58,12 +58,12 @@ async def get_token(self, *scopes, **kwargs): # Target namespace and hub must also be specified. Consumer group is set to default unless required otherwise. FULLY_QUALIFIED_NAMESPACE = os.environ['EVENT_HUB_HOSTNAME'] EVENTHUB_NAME = os.environ['EVENT_HUB_NAME'] -CONSUMER_GROUP = "$Default" # The following part creates a SAS token. Users can use any way to create a SAS token. SAS_POLICY = os.environ['EVENT_HUB_SAS_POLICY'] SAS_KEY = os.environ['EVENT_HUB_SAS_KEY'] + async def create_with_sas_token(): uri = "sb://{}/{}".format(FULLY_QUALIFIED_NAMESPACE, EVENTHUB_NAME) token_ttl = 3000 # seconds diff --git a/sdk/eventhub/azure-eventhub/samples/sample_dev_requirements.txt b/sdk/eventhub/azure-eventhub/samples/sample_dev_requirements.txt index 25b5b6ab8297..1f546dd19488 100644 --- a/sdk/eventhub/azure-eventhub/samples/sample_dev_requirements.txt +++ b/sdk/eventhub/azure-eventhub/samples/sample_dev_requirements.txt @@ -1,2 +1,2 @@ azure-eventhub-checkpointstoreblob -azure-eventhub-checkpointstoreblob-aio; python_version >= '3.5' \ No newline at end of file +azure-eventhub-checkpointstoreblob-aio; python_version >= '3.6' \ No newline at end of file diff --git a/sdk/eventhub/azure-eventhub/samples/sync_samples/authenticate_with_azure_sas_credential.py b/sdk/eventhub/azure-eventhub/samples/sync_samples/authenticate_with_azure_sas_credential.py index 6e18d19c592d..0f3628c523ee 100644 --- a/sdk/eventhub/azure-eventhub/samples/sync_samples/authenticate_with_azure_sas_credential.py +++ b/sdk/eventhub/azure-eventhub/samples/sync_samples/authenticate_with_azure_sas_credential.py @@ -6,7 +6,7 @@ # -------------------------------------------------------------------------------------------- """ -Example to demonstrate utilizing azure.core.credentials.AzureSasCredential to authenticate with EventHub +Example to demonstrate utilizing azure.core.credentials.AzureSasCredential to authenticate with Event Hubs. """ # pylint: disable=C0111 @@ -34,13 +34,10 @@ def generate_sas_token(uri, sas_name, sas_value, token_ttl): signature = url_parse_quote(base64.b64encode(signed_hmac_sha256.digest())) return 'SharedAccessSignature sr={}&sig={}&se={}&skn={}'.format(uri, signature, expiry, sas_name) -def on_event(context, event): - print(context.partition_id, ":", event) # Target namespace and hub must also be specified. Consumer group is set to default unless required otherwise. FULLY_QUALIFIED_NAMESPACE = os.environ['EVENT_HUB_HOSTNAME'] EVENTHUB_NAME = os.environ['EVENT_HUB_NAME'] -CONSUMER_GROUP = "$Default" # The following part creates a SAS token. A SAS token is typically given to you after being created. SAS_POLICY = os.environ['EVENT_HUB_SAS_POLICY'] diff --git a/sdk/eventhub/azure-eventhub/samples/sync_samples/authenticate_with_sas_token.py b/sdk/eventhub/azure-eventhub/samples/sync_samples/authenticate_with_sas_token.py index 881f8b6b4974..47b6a0aeb5d7 100644 --- a/sdk/eventhub/azure-eventhub/samples/sync_samples/authenticate_with_sas_token.py +++ b/sdk/eventhub/azure-eventhub/samples/sync_samples/authenticate_with_sas_token.py @@ -6,7 +6,7 @@ # -------------------------------------------------------------------------------------------- """ -Example to demonstrate utilizing SAS (Shared Access Signature) tokens to authenticate with EventHub +Example to demonstrate utilizing SAS (Shared Access Signature) tokens to authenticate with Event Hubs. """ # pylint: disable=C0111 @@ -34,9 +34,6 @@ def generate_sas_token(uri, sas_name, sas_value, token_ttl): signature = url_parse_quote(base64.b64encode(signed_hmac_sha256.digest())) return 'SharedAccessSignature sr={}&sig={}&se={}&skn={}'.format(uri, signature, expiry, sas_name) -def on_event(context, event): - print(context.partition_id, ":", event) - class CustomizedSASCredential(object): def __init__(self, token, expiry): @@ -59,7 +56,6 @@ def get_token(self, *scopes, **kwargs): # Target namespace and hub must also be specified. Consumer group is set to default unless required otherwise. FULLY_QUALIFIED_NAMESPACE = os.environ['EVENT_HUB_HOSTNAME'] EVENTHUB_NAME = os.environ['EVENT_HUB_NAME'] -CONSUMER_GROUP = "$Default" # The following part creates a SAS token. Users can use any way to create a SAS token. SAS_POLICY = os.environ['EVENT_HUB_SAS_POLICY']