Search code examples
python-3.xazureazureservicebusazure-sdk-python

With the new Azure service bus SDK (7.0.0), what's the proper way to create a topic, subscription, and authorization rule?


I'm using Python 3.8 and the new Azure SDK for Python, specifically the azure-mgmt-servicebus 7.0.0 version. With my previous version, whenever I needed to create a service bus topic, subscription, and appropriate authorization rule, I could do

sb_client = self._get_sb_client()
    authorization_rule_rights = [AccessRights.listen]
    sb_client = self._get_sb_client()
    sb_client.topics.create_or_update(
        resource_group_name, namespace_name, topic_name, parameters=TOPIC_PARAMS)
    sb_client.subscriptions.create_or_update(
        resource_group_name, namespace_name, topic_name, 
        SB_SUBSCRIPTION_NAME, parameters=SUBSCRIPTION_PARAMS)

    # Create auth rule
    sb_client.topics.create_or_update_authorization_rule(
        resource_group_name=resource_group_name,
        namespace_name=namespace_name,
        topic_name=topic_name,
        authorization_rule_name=SB_SAS_POLICY_LISTEN,
        parameters=authorization_rule_rights)

However, with the new SDK, my "create_or_update_authorization_rule" now throws a

   msrest.exceptions.SerializationError: Unable to build a model: Unable to deserialize to object: type, AttributeError: 'list' object has no attribute 'get', DeserializationError: Unable to deserialize to object: type, AttributeError: 'list' object has no attribute 'get'

error. The new documentation -- https://learn.microsoft.com/en-us/python/api/overview/azure/servicebus?view=azure-python#service-bus-topics-and-subscriptions, does not detail how to create the topics, only send a message using an existing topic. With the newest SDK, what's the proper way to create the topic, subscription, and authorization rule?


Solution

  • As far as I know, the latest version of azure-mgmt-servicebus is 6.0.0.

    To create authorization rule, you should put the value of authorization_rule_rights in a json format.

    Here is an example:

           authorization_rule_rights= {
              "rights": [
                "Listen",
                "Send"
              ]
            }
    
            result = sb_client.topics.create_or_update_authorization_rule(
                       resource_group_name=RESOURCE_GROUP, 
                       namespace_name=NAMESPACE_NAME, 
                       topic_name=TOPIC_NAME, 
                       authorization_rule_name=AUTHORIZATION_RULE_NAME, 
                       parameters=authorization_rule_rights)
    

    For more details about authorization rule creation, you can refer to this doc.

    For the topic creation, refer to this section.

    For subscription creation, refer to this section.