Azure ServiceBus: created Shared Access Policy for queue programmatically

84 views Asked by At

I need to create some entities in Azure ServiceBus using Python.

Creating a Queue works like a charm:

adminClient.create_queue('<name>', default_message_time_to_live='P8D', uplicate_detection_history_time_window='PT1H', lock_duration='PT1M', max_delivery_count=100, max_size_in_megabytes=4096, max_message_size_in_kilobytes=256)

The Queue gets created without a Shared access policy, so I want to achieve that in code as well:

adminClient.create_queue('<name>', authorization_rules=[ AuthorizationRule(type='SharedAccessPolicy',key_name='...',rights=[ 'Send' ]) ], default_message_time_to_live='P8D', uplicate_detection_history_time_window='PT1H', lock_duration='PT1M', max_delivery_count=100, max_size_in_megabytes=4096, max_message_size_in_kilobytes=256)

Running this code throws azure.core.exceptions.HttpResponseError: SubCode=50000. Internal Server Error. I don't even know if SharedAccessPolicy is correct as type. I tried adding primary_Key='...' and secondary_key='...', but got the same result.

I am aware of the documentation found at https://azuresdkdocs.blob.core.windows.net/$web/python/azure-servicebus/latest/azure.servicebus.management.html#azure.servicebus.management.AuthorizationRule, but it doesn't really help.

What am I missing?

1

There are 1 answers

0
Vivek Vaibhav Shandilya On

This worked for me.

I have used azure.mgmt.servicebus from azure-mgmt-servicebus package.

For reference check this document.

My Code:

app.py:

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


credentials = DefaultAzureCredential()
sub_id = "xxxxxxxxxxxxxxxxxxxx"
resource_group = "xxxxxxxxxxxxxx"
client = ServiceBusManagementClient(credential=credentials,subscription_id=sub_id)

#create queue
client.queues.create_or_update(resource_group,"pysdksb","sdkqueue",parameters={"properties":{"enablePartitioning": True}})

# create an authorization rules in created queue.
client.queues.create_or_update_authorization_rule(resource_group,"pysdksb","sdkqueue",authorization_rule_name="sdkauth",
        parameters={"properties": {"rights": ["Listen", "Send"]}},)

print("Queue created with Shared Access Policy.")

OUTPUT: