Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom application model for EventHubs emulator #7005

Merged
merged 14 commits into from
Jan 10, 2025
2 changes: 1 addition & 1 deletion playground/AspireEventHub/EventHubs.AppHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

var eventHub = builder.AddAzureEventHubs("eventhubns")
.RunAsEmulator()
.AddEventHub("hub");
.WithHub("hub");

builder.AddProject<Projects.EventHubsConsumer>("consumer")
.WithReference(eventHub).WaitFor(eventHub)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var storage = builder.AddAzureStorage("storage").RunAsEmulator();
var queue = storage.AddQueues("queue");
var blob = storage.AddBlobs("blob");
var eventHubs = builder.AddAzureEventHubs("eventhubs").RunAsEmulator().AddEventHub("myhub");
var eventHubs = builder.AddAzureEventHubs("eventhubs").RunAsEmulator().WithHub("myhub");
var serviceBus = builder.AddAzureServiceBus("messaging").RunAsEmulator().WithQueue("myqueue");

var funcApp = builder.AddAzureFunctionsProject<Projects.AzureFunctionsEndToEnd_Functions>("funcapp")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Aspire.Hosting.ApplicationModel;
/// Represents an Azure OpenAI resource.
/// </summary>
/// <param name="name">The name of the resource.</param>
/// <param name="configureInfrastructure">Configures the underlying Azure resource using the CDK.</param>
/// <param name="configureInfrastructure">Configures the underlying Azure resource using Azure.Provisioning.</param>
public class AzureOpenAIResource(string name, Action<AzureResourceInfrastructure> configureInfrastructure) :
AzureProvisioningResource(name, configureInfrastructure),
IResourceWithConnectionString
Expand Down
222 changes: 163 additions & 59 deletions src/Aspire.Hosting.Azure.EventHubs/AzureEventHubsExtensions.cs

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/Aspire.Hosting.Azure.EventHubs/AzureEventHubsResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Azure.EventHubs;

namespace Aspire.Hosting.Azure;

Expand All @@ -27,7 +28,7 @@ public class AzureEventHubsResource(string name, Action<AzureResourceInfrastruct

private const string ConnectionKeyPrefix = "Aspire__Azure__Messaging__EventHubs";

internal List<string> Hubs { get; } = [];
internal List<EventHub> Hubs { get; } = [];

/// <summary>
/// Gets the "eventHubsEndpoint" output reference from the bicep template for the Azure Event Hubs resource.
Expand Down
20 changes: 20 additions & 0 deletions src/Aspire.Hosting.Azure.EventHubs/ConfigJsonAnnotation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json.Nodes;
using Aspire.Hosting.ApplicationModel;

namespace Aspire.Hosting.Azure.EventHubs;

/// <summary>
/// Represents an annotation for updating the JSON content of a mounted document.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does "a mounted document" mean?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Config.json file (json document) that is mounted to the container using ContainerMountType.BindMount.

/// </summary>
internal sealed class ConfigJsonAnnotation : IResourceAnnotation
{
public ConfigJsonAnnotation(Action<JsonNode> configure)
{
Configure = configure;
}

public Action<JsonNode> Configure { get; }
}
101 changes: 101 additions & 0 deletions src/Aspire.Hosting.Azure.EventHubs/EventHub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json;
using Azure.Provisioning;

namespace Aspire.Hosting.Azure.EventHubs;

/// <summary>
/// Represents an Event Hub.
/// </summary>
/// <remarks>
/// Use <see cref="AzureProvisioningResourceExtensions.ConfigureInfrastructure{T}(ApplicationModel.IResourceBuilder{T}, Action{AzureResourceInfrastructure})"/> to configure specific <see cref="Azure.Provisioning"/> properties.
/// </remarks>
public class EventHub
{
/// <summary>
/// Initializes a new instance of the <see cref="EventHub"/> class.
/// </summary>
public EventHub(string name)
{
Name = name;
}

/// <summary>
/// The event hub name.
/// </summary>
public string Name { get; set; }

/// <summary>
/// Number of partitions created for the Event Hub, allowed values are from
/// 1 to 32 partitions.
/// </summary>
public long? PartitionCount { get; set; }

/// <summary>
/// The consumer groups for this hub.
/// </summary>
public List<EventHubConsumerGroup> ConsumerGroups { get; } = [];

/// <summary>
/// Converts the current instance to a provisioning entity.
/// </summary>
/// <returns>A <see cref="global::Azure.Provisioning.EventHubs.EventHub"/> instance.</returns>
internal global::Azure.Provisioning.EventHubs.EventHub ToProvisioningEntity()
{
var hub = new global::Azure.Provisioning.EventHubs.EventHub(Infrastructure.NormalizeBicepIdentifier(Name));

hub.Name = Name;

if (PartitionCount.HasValue)
{
hub.PartitionCount = PartitionCount.Value;
}

return hub;
}

/// <summary>
/// Converts the current instance to a JSON object.
/// </summary>
/// <param name="writer">The Utf8JsonWriter to write the JSON object to.</param>
internal void WriteJsonObjectProperties(Utf8JsonWriter writer)
{
var hub = this;

writer.WriteString(nameof(Name), hub.Name);

if (hub.PartitionCount.HasValue)
{
writer.WriteNumber(nameof(PartitionCount), hub.PartitionCount.Value);
}
else
{
// Value is required. We don't assign it by default in case
// we need to detect if the value was never set or if the defaults
// in Azure.Provisioning change.

writer.WriteNumber(nameof(PartitionCount), 1);
}

#pragma warning disable CA1507 // Use nameof to express symbol names: there is no direct link between the property name and the JSON representation
writer.WriteStartArray("ConsumerGroups");

// The default consumer group ('$default') is automatically created by the
// emulator. We don't need to create it explicitly.

if (hub.ConsumerGroups.Count >= 0)
{
foreach (var consumerGroup in hub.ConsumerGroups)
{
writer.WriteStartObject();
consumerGroup.WriteJsonObjectProperties(writer);
writer.WriteEndObject();
}
}
writer.WriteEndArray();
}
#pragma warning restore CA1507 // Use nameof to express symbol names

}
51 changes: 51 additions & 0 deletions src/Aspire.Hosting.Azure.EventHubs/EventHubConsumerGroup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json;
using Azure.Provisioning;

namespace Aspire.Hosting.Azure.EventHubs;

/// <summary>
/// Represents a Consumer Group.
/// </summary>
/// <remarks>
/// Use <see cref="AzureProvisioningResourceExtensions.ConfigureInfrastructure{T}(ApplicationModel.IResourceBuilder{T}, Action{AzureResourceInfrastructure})"/> to configure specific <see cref="Azure.Provisioning"/> properties.
/// </remarks>
public class EventHubConsumerGroup
{
/// <summary>
/// Initializes a new instance of the <see cref="EventHubConsumerGroup"/> class.
/// </summary>
public EventHubConsumerGroup(string name)
{
Name = name;
}

/// <summary>
/// The event hub name.
/// </summary>
public string Name { get; set; }

/// <summary>
/// Converts the current instance to a provisioning entity.
/// </summary>
/// <returns>A <see cref="global::Azure.Provisioning.EventHubs.EventHub"/> instance.</returns>
internal global::Azure.Provisioning.EventHubs.EventHubsConsumerGroup ToProvisioningEntity()
{
var consumerGroup = new global::Azure.Provisioning.EventHubs.EventHubsConsumerGroup(Infrastructure.NormalizeBicepIdentifier(Name));

consumerGroup.Name = Name;

return consumerGroup;
}

/// <summary>
/// Converts the current instance to a JSON object.
/// </summary>
/// <param name="writer">The Utf8JsonWriter to write the JSON object to.</param>
internal void WriteJsonObjectProperties(Utf8JsonWriter writer)
sebastienros marked this conversation as resolved.
Show resolved Hide resolved
{
writer.WriteString(nameof(Name), Name);
}
}
15 changes: 15 additions & 0 deletions src/Aspire.Hosting.Azure.EventHubs/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,24 @@ Aspire.Hosting.Azure.AzureEventHubsEmulatorResource
Aspire.Hosting.Azure.AzureEventHubsEmulatorResource.AzureEventHubsEmulatorResource(Aspire.Hosting.Azure.AzureEventHubsResource! innerResource) -> void
Aspire.Hosting.Azure.AzureEventHubsResource.AzureEventHubsResource(string! name, System.Action<Aspire.Hosting.Azure.AzureResourceInfrastructure!>! configureInfrastructure) -> void
Aspire.Hosting.Azure.AzureEventHubsResource.IsEmulator.get -> bool
Aspire.Hosting.Azure.EventHubs.EventHub
Aspire.Hosting.Azure.EventHubs.EventHub.ConsumerGroups.get -> System.Collections.Generic.List<Aspire.Hosting.Azure.EventHubs.EventHubConsumerGroup!>!
Aspire.Hosting.Azure.EventHubs.EventHub.EventHub(string! name) -> void
Aspire.Hosting.Azure.EventHubs.EventHub.Name.get -> string!
Aspire.Hosting.Azure.EventHubs.EventHub.Name.set -> void
Aspire.Hosting.Azure.EventHubs.EventHub.PartitionCount.get -> long?
Aspire.Hosting.Azure.EventHubs.EventHub.PartitionCount.set -> void
Aspire.Hosting.Azure.EventHubs.EventHubConsumerGroup
Aspire.Hosting.Azure.EventHubs.EventHubConsumerGroup.EventHubConsumerGroup(string! name) -> void
Aspire.Hosting.Azure.EventHubs.EventHubConsumerGroup.Name.get -> string!
Aspire.Hosting.Azure.EventHubs.EventHubConsumerGroup.Name.set -> void
override Aspire.Hosting.Azure.AzureEventHubsEmulatorResource.Annotations.get -> Aspire.Hosting.ApplicationModel.ResourceAnnotationCollection!
override Aspire.Hosting.Azure.AzureEventHubsEmulatorResource.Name.get -> string!
static Aspire.Hosting.AzureEventHubsExtensions.ConfigureEmulator(this Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Azure.AzureEventHubsEmulatorResource!>! builder, System.Action<System.Text.Json.Nodes.JsonNode!>! configJson) -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Azure.AzureEventHubsEmulatorResource!>!
static Aspire.Hosting.AzureEventHubsExtensions.RunAsEmulator(this Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Azure.AzureEventHubsResource!>! builder, System.Action<Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Azure.AzureEventHubsEmulatorResource!>!>? configureContainer = null) -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Azure.AzureEventHubsResource!>!
static Aspire.Hosting.AzureEventHubsExtensions.WithConfigurationFile(this Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Azure.AzureEventHubsEmulatorResource!>! builder, string! path) -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Azure.AzureEventHubsEmulatorResource!>!
static Aspire.Hosting.AzureEventHubsExtensions.WithDataBindMount(this Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Azure.AzureEventHubsEmulatorResource!>! builder, string? path = null) -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Azure.AzureEventHubsEmulatorResource!>!
static Aspire.Hosting.AzureEventHubsExtensions.WithDataVolume(this Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Azure.AzureEventHubsEmulatorResource!>! builder, string? name = null) -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Azure.AzureEventHubsEmulatorResource!>!
static Aspire.Hosting.AzureEventHubsExtensions.WithGatewayPort(this Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Azure.AzureEventHubsEmulatorResource!>! builder, int? port) -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Azure.AzureEventHubsEmulatorResource!>!
static Aspire.Hosting.AzureEventHubsExtensions.WithHostPort(this Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Azure.AzureEventHubsEmulatorResource!>! builder, int? port) -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Azure.AzureEventHubsEmulatorResource!>!
static Aspire.Hosting.AzureEventHubsExtensions.WithHub(this Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Azure.AzureEventHubsResource!>! builder, string! name, System.Action<Aspire.Hosting.Azure.EventHubs.EventHub!>? configure = null) -> Aspire.Hosting.ApplicationModel.IResourceBuilder<Aspire.Hosting.Azure.AzureEventHubsResource!>!
2 changes: 1 addition & 1 deletion src/Aspire.Hosting.Azure.Search/AzureSearchExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void ConfigureSearch(AzureResourceInfrastructure infrastructure)
infrastructure.Add(search.CreateRoleAssignment(SearchBuiltInRole.SearchIndexDataContributor, principalTypeParameter, principalIdParameter));
infrastructure.Add(search.CreateRoleAssignment(SearchBuiltInRole.SearchServiceContributor, principalTypeParameter, principalIdParameter));

// TODO: The endpoint format should move into the CDK so we can maintain this
// TODO: The endpoint format should move into Azure.Provisioning so we can maintain this
// logic in a single location and have a better chance at supporting more than
// just public Azure in the future. https://github.com/Azure/azure-sdk-for-net/issues/42640
infrastructure.Add(new ProvisioningOutput("connectionString", typeof(string))
Expand Down
Loading
Loading