0

Since I am only allowed 10 event hubs per namespace, I am looking for a good algorithm to create a new namespace for every ten event hubs in the list I have.

NOTE: The list of event hub namespaces are being passed in to the method.

var eventHubResources = GetRequiredService<List<EventHubResource>>();
foreach (var eventHubResource in eventHubResources)
{
    eventHubResource.ResourceGroup = resourceGroup;
    MyNamespace.IEventHub eventHub = new EventHub(logger);
    if (eventHubResource.CaptureSettings == null)
    {
       if (eventHubResources.IndexOf(eventHubResource) <= 9) 
       {
           await eventHub.CreateEventHubAsync(azure, eventHubNamespace[0], eventHubResource, null);
       }
       if ((eventHubResources.IndexOf(eventHubResource) > 9) && (eventHubResources.IndexOf(eventHubResource) <= 19)) 
       {
           await eventHub.CreateEventHubAsync(azure, eventHubNamespace[1], eventHubResource, null);
       }
       // and so on....
    }
    else
    {
       await eventHub.CreateEventHubAsync(azure, eventHubNamespace, eventHubResource, storageAccount);
    }
}

What is a better way to achieve this, compared to what I have above?

TyngeOfTheGinge
  • 524
  • 1
  • 4
  • 14

1 Answers1

0

I didn't test the code but you can get the idea.

public static async Task CreateNamespaces(List<string> eventhubNames, ServiceClientCredentials creds) {

int totalEventHubsInNamespace = 0;

var ehClient = new EventHubManagementClient(creds)
{
    SubscriptionId = "<my subscription id>"
};

foreach (var ehName in eventhubNames)
{
    if (totalEventHubsInNamespace == 0)
    {
        var namespaceParams = new EHNamespace()
        {
            Location = "<datacenter location>"
        };

        // Create namespace
        var namespaceName = "<populate some unique namespace>";
        Console.WriteLine($"Creating namespace... {namespaceName}");
        await ehClient.Namespaces.CreateOrUpdateAsync(resourceGroupName, namespaceName, namespaceParams);
        Console.WriteLine("Created namespace successfully.");
    }

    // Create eventhub.
    Console.WriteLine($"Creating eventhub {ehName}");
    var ehParams = new Eventhub() { }; // Customize you eventhub here if you need.
    await ehClient.EventHubs.CreateOrUpdateAsync(resourceGroupName, namespaceName, ehName, ehParams);
    Console.WriteLine("Created Event Hub successfully.");

    totalEventHubsInNamespace++;
    if (totalEventHubsInNamespace >= 10)
    {
        totalEventHubsInNamespace = 0;
    }
}

}

Serkant Karaca
  • 1,896
  • 9
  • 8
  • If you're interested in an example of using the management library in the way that Serkant is describing, I'd suggest taking a look at the [`EventHubScope`](https://github.com/jsquire/azure-sdk-for-net/blob/master/sdk/eventhub/Azure.Messaging.EventHubs.Shared/src/Testing/EventHubScope.cs) class from the SDK, which is used to dynamically create/remove Event Hub instances during test runs. – Jesse Squire Mar 04 '20 at 14:19