Skip to content Skip to sidebar Skip to footer

In The Azure Functions Python Sdk, How Do I Get The Number Of Topics For A Given Namespace?

I'm using Python 3.8 with azure-mgmt-servicebus= v. 1.0.0. I would like to get the number of topics for a given namespace. I have tried the below ... credential = ServicePrinci

Solution 1:

If you want to get the number of the topics for a given service bus namespace, you could use the code below.

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.servicebus import ServiceBusManagementClient

subscription_id = "<subscription-id>"
rg_name = "<resource-group-name>"

tenant_id = "<tenant-id>"
client_id = "<client-id>"
client_secret = "<client-secret>"

credential = ServicePrincipalCredentials(client_id=client_id, secret=client_secret, tenant=tenant_id)
sb_client = ServiceBusManagementClient(credential, subscription_id)
topics = sb_client.topics.list_by_namespace(resource_group_name= rg_name, namespace_name= "servicebusname")

num_topics = 0
for topic in topics:
    num_topics += 1
print(num_topics)

enter image description here

Check the topics in the portal, the result is correct:

enter image description here

Update:

If you don't want to use the loop, you could convert the topics to a list, then use the len() function.

from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.servicebus import ServiceBusManagementClient

subscription_id = "<subscription-id>"
rg_name = "<resource-group-name>"

tenant_id = "<tenant-id>"
client_id = "<client-id>"
client_secret = "<client-secret>"

credential = ServicePrincipalCredentials(client_id=client_id, secret=client_secret, tenant=tenant_id)
sb_client = ServiceBusManagementClient(credential, subscription_id)
topics = sb_client.topics.list_by_namespace(resource_group_name= rg_name, namespace_name= "servicebusname")

testlist = list(topics)
print(len(testlist))

enter image description here

Post a Comment for "In The Azure Functions Python Sdk, How Do I Get The Number Of Topics For A Given Namespace?"