Create a topic with authorization rule using azure-messaging-servicebus

Viewed 90

I'm trying to create a topic using ServiceBusAdministrationClient connected through a namespace's root manage access key (com.azure:azure-messaging-servicebus:7.8.0)

def createOptions = new CreateTopicOptions()
def authRule = new SharedAccessAuthorizationRule(authRuleName, [AccessRights.SEND, AccessRights.LISTEN])
createOptions.authorizationRules.add(authRule)
def topicProps = administrationClient.createTopic(topicName, createOptions)

but there are no AuthorizationRules associated with the topic. I did the same thing when creating a queue and it worked. Any ideas what I might be missing?

2 Answers

For the authorization rule, you need to use Azure Resource Management library Below is a sample to create an authorization rule

final AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);

final TokenCredential credential =
    new DefaultAzureCredentialBuilder()
        .authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
        .build();

var azureResourceManager =
    AzureResourceManager.configure()
        .withLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS)
        .authenticate(credential, profile)
        .withSubscription("subscription");

ServiceBusManager manager = azureResourceManager.serviceBusNamespaces().manager();

SBAuthorizationRuleInner authorizationRule =
    manager
        .serviceClient()
        .getTopics()
        .createOrUpdateAuthorizationRule(
            "resourceGroupName", "namespaceName", "topicName", "authorizationRuleName");
Related