Skip to content

Commit

Permalink
Consolidate RabbitMQ resource types for P4. (dotnet#2078)
Browse files Browse the repository at this point in the history
* Consolidate RabbitMQ resource types for P4.

* Add host port back in.
  • Loading branch information
mitchdenny authored and radical committed Feb 6, 2024
1 parent eb7a8f2 commit 78e947c
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 61 deletions.
2 changes: 1 addition & 1 deletion playground/eShopLite/AppHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
.WithReference(catalogDb)
.WithReplicas(2);

var messaging = builder.AddRabbitMQContainer("messaging");
var messaging = builder.AddRabbitMQ("messaging").PublishAsContainer();

var basketService = builder.AddProject("basketservice", @"..\BasketService\BasketService.csproj")
.WithReference(basketCache)
Expand Down
37 changes: 11 additions & 26 deletions src/Aspire.Hosting/RabbitMQ/RabbitMQBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@ namespace Aspire.Hosting;
public static class RabbitMQBuilderExtensions
{
/// <summary>
/// Adds a RabbitMQ container to the application. The default image name is "rabbitmq" and the default tag is "3-management".
/// Adds a RabbitMQ resource to the application. A container is used for local development.
/// </summary>
/// <param name="builder">The <see cref="IDistributedApplicationBuilder"/>.</param>
/// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param>
/// <param name="port">The host port of RabbitMQ.</param>
/// <param name="password">The password for RabbitMQ.</param>
/// <param name="port">The host port that the underlying container is bound to when running locally.</param>
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<RabbitMQContainerResource> AddRabbitMQContainer(this IDistributedApplicationBuilder builder, string name, int? port = null, string? password = null)
public static IResourceBuilder<RabbitMQServerResource> AddRabbitMQ(this IDistributedApplicationBuilder builder, string name, int? port = null)
{
password ??= Guid.NewGuid().ToString("N");
var rabbitMq = new RabbitMQContainerResource(name, password);
var password = Guid.NewGuid().ToString("N");
var rabbitMq = new RabbitMQServerResource(name, password);
return builder.AddResource(rabbitMq)
.WithAnnotation(new EndpointAnnotation(ProtocolType.Tcp, port: port, containerPort: 5672))
.WithAnnotation(new ContainerImageAnnotation { Image = "rabbitmq", Tag = "3" })
.WithManifestPublishingCallback(context => WriteRabbitMQContainerToManifest(context, rabbitMq))
.WithManifestPublishingCallback(WriteRabbitMQServerToManifest)
.WithEnvironment("RABBITMQ_DEFAULT_USER", "guest")
.WithEnvironment(context =>
{
Expand All @@ -40,33 +39,19 @@ public static IResourceBuilder<RabbitMQContainerResource> AddRabbitMQContainer(t
context.EnvironmentVariables.Add("RABBITMQ_DEFAULT_PASS", rabbitMq.Password);
}
});

}

/// <summary>
/// Adds a RabbitMQ resource to the application. A container is used for local development.
/// </summary>
/// <param name="builder">The <see cref="IDistributedApplicationBuilder"/>.</param>
/// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param>
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<RabbitMQServerResource> AddRabbitMQ(this IDistributedApplicationBuilder builder, string name)
private static void WriteRabbitMQServerToManifest(ManifestPublishingContext context)
{
var password = Guid.NewGuid().ToString("N");
var rabbitMq = new RabbitMQServerResource(name, password);
return builder.AddResource(rabbitMq)
.WithAnnotation(new EndpointAnnotation(ProtocolType.Tcp, containerPort: 5672))
.WithAnnotation(new ContainerImageAnnotation { Image = "rabbitmq", Tag = "3" })
.WithManifestPublishingCallback(WriteRabbitMQServerToManifest)
.WithEnvironment("RABBITMQ_DEFAULT_USER", "guest")
.WithEnvironment("RABBITMQ_DEFAULT_PASS", () => rabbitMq.Password);
context.Writer.WriteString("type", "rabbitmq.server.v0");
}

private static void WriteRabbitMQServerToManifest(ManifestPublishingContext context)
public static IResourceBuilder<RabbitMQServerResource> PublishAsContainer(this IResourceBuilder<RabbitMQServerResource> builder)
{
context.Writer.WriteString("type", "rabbitmq.server.v0");
return builder.WithManifestPublishingCallback(context => WriteRabbitMQContainerToManifest(context, builder.Resource));
}

private static void WriteRabbitMQContainerToManifest(ManifestPublishingContext context, RabbitMQContainerResource resource)
private static void WriteRabbitMQContainerToManifest(ManifestPublishingContext context, RabbitMQServerResource resource)
{
context.WriteContainer(resource);
context.Writer.WriteString( // "connectionString": "...",
Expand Down
32 changes: 0 additions & 32 deletions src/Aspire.Hosting/RabbitMQ/RabbitMQContainerResource.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Aspire.Hosting/RabbitMQ/RabbitMQServerResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Aspire.Hosting.ApplicationModel;
/// </summary>
/// <param name="name">The name of the resource.</param>
/// <param name="password">The RabbitMQ server password.</param>
public class RabbitMQServerResource(string name, string password) : Resource(name), IResourceWithConnectionString, IResourceWithEnvironment
public class RabbitMQServerResource(string name, string password) : ContainerResource(name), IResourceWithConnectionString, IResourceWithEnvironment
{
/// <summary>
/// The RabbitMQ server password.
Expand Down
2 changes: 1 addition & 1 deletion tests/Aspire.Hosting.Tests/ManifestGenerationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public void EnsureAllRabbitMQManifestTypesHaveVersion0Suffix()
var program = CreateTestProgramJsonDocumentManifestPublisher();

program.AppBuilder.AddRabbitMQ("rabbitabstract");
program.AppBuilder.AddRabbitMQContainer("rabbitcontainer");
program.AppBuilder.AddRabbitMQ("rabbitcontainer").PublishAsContainer();

// Build AppHost so that publisher can be resolved.
program.Build();
Expand Down

0 comments on commit 78e947c

Please sign in to comment.