0

Is it possible to create a new event hub in an event hub namespace programatically? The EventHubsNamespaceResource Class from the Azure.ResourceManager.EventHubs library does not seem to support this. Is there some other way?

ResourceGroupResource _resourceGroup;

public EventHubResource GetOrCreateEventHub(string eventHubNamespaceName, string eventHubName)
{
    var eventHubNamespace = _resourceGroup.GetEventHubsNamespace(eventHubNamespaceName).Value;

    try
    {
        return eventHubNamespace.GetEventHub(eventHubName).Value;
    }
    catch (Exception ex)
    {
        // TODO: create a new event hub
    }
}
Martin Staufcik
  • 8,295
  • 4
  • 44
  • 63

1 Answers1

0

The EventHubCollection class has CreateOrUpdate method that can be used to create a new event hub.

var eventHubNamespace = _resourceGroup.GetEventHubsNamespace(eventHubNamespaceName).Value;
var eventHubName = "some_name";

var coll = eventHubNamespace.GetEventHubs();
var data = new EventHubData()
{
    CaptureDescription = new CaptureDescription()
    {
        Destination = new EventHubDestination()
        {
            ArchiveNameFormat = "some/path/{Year}{Month}{Day}/{Namespace}-{EventHub}-{PartitionId}-{Hour}{Minute}{Second}",
            BlobContainer = "mycontainer",
            Name = "EventHubArchive.AzureBlockBlob",
            StorageAccountResourceId = new Azure.Core.ResourceIdentifier(storageAccountResourceId),
        },
        Enabled = true,
        Encoding = EncodingCaptureDescription.Avro,
        IntervalInSeconds = 900,
        SizeLimitInBytes = 314572800,
    },
    MessageRetentionInDays = 2,
};
return coll.CreateOrUpdate(WaitUntil.Completed, eventHubName, data).Value;
Martin Staufcik
  • 8,295
  • 4
  • 44
  • 63
  • You are allowed to accept your own answer if your question is answered with it. That would make sure that the question doesn't show up anymore with the unanswered filter. – Silvan Jul 05 '23 at 15:45