Search code examples
c#azureazure-resource-manager

Azure resource management library for C# - create an Event Hub


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
    }
}

Solution

  • 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;