Azure Event Hub Management Client
The `azure-mgmt-eventhub` library provides a client for managing Azure Event Hub resources, including Event Hub namespaces, Event Hubs, Consumer Groups, and Disaster Recovery configurations. It is part of the Azure SDK for Python, currently at version 11.2.0, and receives regular updates for bug fixes, new features, and API changes.
Warnings
- breaking Older versions (prior to v4/v5) of Azure management libraries used `msrestazure.azure_active_directory` for authentication. Current stable versions (v11+) require `azure.identity` for credential handling.
- breaking The API surface and method signatures for creating and updating resources have significantly changed across major versions, particularly from older track 1 SDKs to the current track 2 SDKs. Object models for resource properties are also different.
- gotcha Many list operations (e.g., `list_by_resource_group`, `list_by_namespace`) return `azure.core.paging.ItemPaged` objects, which are iterators. If you expect a plain list, you might need to convert it (e.g., `list(client.namespaces.list_by_resource_group(...))`) if you need random access or length upfront.
- gotcha Resource creation methods (e.g., `namespaces.begin_create_or_update`) are often long-running operations and return a poller object. You must call `.result()` on the poller to wait for the operation to complete and get the final resource object.
Install
-
pip install azure-mgmt-eventhub azure-identity
Imports
- EventHubManagementClient
from azure.mgmt.eventhub import EventHubManagementClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.eventhub import EventHubManagementClient
# Set these environment variables or replace with actual values
# AZURE_SUBSCRIPTION_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_TENANT_ID
# Or log in via Azure CLI: `az login`
SUBSCRIPTION_ID = os.environ.get("AZURE_SUBSCRIPTION_ID", "<your-subscription-id>")
RESOURCE_GROUP_NAME = os.environ.get("AZURE_RESOURCE_GROUP_NAME", "<existing-resource-group>")
EVENTHUB_NAMESPACE_NAME = os.environ.get("AZURE_EVENTHUB_NAMESPACE_NAME", "<existing-eventhub-namespace>")
def main():
if SUBSCRIPTION_ID == "<your-subscription-id>" or \
RESOURCE_GROUP_NAME == "<existing-resource-group>" or \
EVENTHUB_NAMESPACE_NAME == "<existing-eventhub-namespace>":
print("Please set AZURE_SUBSCRIPTION_ID, AZURE_RESOURCE_GROUP_NAME, and AZURE_EVENTHUB_NAMESPACE_NAME environment variables or replace placeholders.")
return
credential = DefaultAzureCredential()
client = EventHubManagementClient(credential, SUBSCRIPTION_ID)
print(f"Listing Event Hub Namespaces in resource group '{RESOURCE_GROUP_NAME}':")
try:
namespaces = client.namespaces.list_by_resource_group(RESOURCE_GROUP_NAME)
for namespace in namespaces:
print(f"- {namespace.name} (Location: {namespace.location})")
# List Event Hubs within this namespace
print(f" Listing Event Hubs in namespace '{namespace.name}':")
event_hubs = client.event_hubs.list_by_namespace(RESOURCE_GROUP_NAME, namespace.name)
for event_hub in event_hubs:
print(f" - {event_hub.name} (Partitions: {event_hub.partition_count})")
except Exception as e:
print(f"An error occurred: {e}")
print("Ensure the resource group and namespace exist and your credentials have access.")
if __name__ == "__main__":
main()