Azure Service Bus Management Client Library

9.0.0 · active · verified Thu Apr 09

The `azure-mgmt-servicebus` library provides the client-side functionality for managing Azure Service Bus resources, such as namespaces, queues, and topics, through the Azure Resource Manager API. It is part of the broader Azure SDK for Python, currently at version 9.0.0, and follows a consistent release cadence with other Azure management libraries, typically updated monthly or bi-monthly with new API versions and features.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate with Azure using `DefaultAzureCredential` and then use `ServiceBusManagementClient` to list and retrieve Service Bus namespaces within a specified resource group. Ensure `AZURE_SUBSCRIPTION_ID` and `AZURE_RESOURCE_GROUP_NAME` environment variables are set for authentication and resource context.

import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.servicebus import ServiceBusManagementClient

# Set your Azure Subscription ID and Resource Group in environment variables or replace directly.
subscription_id = os.environ.get('AZURE_SUBSCRIPTION_ID', 'YOUR_SUBSCRIPTION_ID')
resource_group_name = os.environ.get('AZURE_RESOURCE_GROUP_NAME', 'my-servicebus-rg')
namespace_name = os.environ.get('AZURE_SERVICEBUS_NAMESPACE', 'my-servicebus-namespace')

if subscription_id == 'YOUR_SUBSCRIPTION_ID':
    raise ValueError("Please set the AZURE_SUBSCRIPTION_ID environment variable.")

# Authenticate using DefaultAzureCredential (looks for env vars, managed identity, etc.)
credential = DefaultAzureCredential()

# Create a Service Bus Management Client
servicebus_client = ServiceBusManagementClient(credential, subscription_id)

try:
    # Example: List all Service Bus namespaces in a resource group
    print(f"Listing Service Bus namespaces in resource group '{resource_group_name}':")
    namespaces = servicebus_client.namespaces.list_by_resource_group(resource_group_name)
    for ns in namespaces:
        print(f"  - {ns.name} (Location: {ns.location}, Status: {ns.status})")

    # Example: Get a specific Service Bus namespace (if it exists)
    print(f"\nGetting Service Bus namespace '{namespace_name}':")
    try:
        ns_details = servicebus_client.namespaces.get(resource_group_name, namespace_name)
        print(f"  Namespace '{ns_details.name}' found. Status: {ns_details.status}")
    except Exception as e:
        print(f"  Namespace '{namespace_name}' not found or error: {e}")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →