我正在将Python 3.8与azure-mgmt-servicebus = v。1.0.0一起使用。我想获取给定名称空间的主题数。我已经尝试了以下...
credential = ServicePrincipalCredentials(self._client_id, self._client_secret, tenant=self._tenant)
sb_client = ServiceBusManagementClient(credential, self._subscription)
...
topics = sb_client.topics.list_by_namespace(
resource_group_name=self._resource_group_name,
namespace_name=namespace
)
num_topics = 0
while topics.current_page:
num_topics += topics.current_page.count
topics.next
logging.info("num topics: %s", num_topics)
尽管我已验证建立了连接(我可以使用相同的连接创建主题),并且在Azure门户中可以看到许多主题,但我的“ num_topics”始终返回零。我以为我没有正确使用API,但不确定哪里会崩溃。如何获得给定名称空间的主题数?
如果要获取给定服务总线名称空间的主题数,则可以使用下面的代码。
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)
检查门户中的主题,结果正确:
更新:
如果不想使用循环,可以将转换topics
为列表,然后使用len()
函数。
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))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句