Search code examples
c#pulumipulumi-azure

Retrieving Connection String when Creating ServiceBus with Pulumi (AzureNative)


I create a Servicebus-Namespace using AzureNative on Pulumi:

public void CreateNamespace(string namespaceName, SkuName skuname, SkuTier tier)
{
  var namespace = new Namespace(namespaceName, new NamespaceArgs
  {
    Location = _resourceGroup.Location,
    NamespaceName = namespaceName,
    ResourceGroupName = _resourceGroup.Name,
    Sku = new Pulumi.AzureNative.ServiceBus.Inputs.SBSkuArgs
    {
      Name = skuname,
      Tier = tier
    }
  });
}

The Servicebus Namespace is created correctly. After creating the Servicebus-Namespace I need to retrieve the ConnectionString for this resource. Either for the automatically created RootManageSharedAccessKey or alternatively by creating a specific additional policy for that task.

Within the Azure Portal I can retrieve the Key by navigating through Settings/Shared access policies/Policy/ and copying the Primary access key from there.

Screenshot Azure Portal

I did not find any property or function within the AzureNative.ServiceBus - Namespace that seem to lead to that key. Any way to retrieve that property?


Solution

  • I solved it by creating a new NamespaceRule and return ListNamespaceKeys-Properties:

    var namespaceRule = new NamespaceAuthorizationRule(rulename, new NamespaceAuthorizationRuleArgs
    {
      AuthorizationRuleName = rulename,
      NamespaceName = namespace.Name,
      ResourceGroupName = _resourceGroup.Name,
      Rights = new[]
      {
        AccessRights.Listen,
        AccessRights.Send
      }
    });
    
    var nameSpaceKeys = Output
    .Tuple(namespace.Name, namespaceRule.Name)
    .Apply(t => ListNamespaceKeys.InvokeAsync(new ListNamespaceKeysArgs
    {
      NamespaceName = t.Item1,
      AuthorizationRuleName = t.Item2,
      ResourceGroupName = _resourceGroup.GetResourceName()
    }));
    

    Now NamespaceKeys contains all the required Properties like PrimaryConnectionString etc.