Skip to content

Commit 41f95f1

Browse files
mitchdennyradical
authored andcommitted
Consolidate MongoDB resources. (dotnet#2128)
1 parent aca2658 commit 41f95f1

File tree

8 files changed

+26
-85
lines changed

8 files changed

+26
-85
lines changed

playground/mongo/Mongo.AppHost/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
var builder = DistributedApplication.CreateBuilder(args);
55

6-
var db = builder.AddMongoDBContainer("mongo")
7-
.WithMongoExpress();
6+
var db = builder.AddMongoDB("mongo")
7+
.WithMongoExpress()
8+
.PublishAsContainer();
89

910
builder.AddProject<Projects.Mongo_ApiService>("api")
1011
.WithReference(db);

src/Aspire.Hosting/MongoDB/IMongoDBParentResource.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/Aspire.Hosting/MongoDB/MongoDBBuilderExtensions.cs

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,21 @@ public static class MongoDBBuilderExtensions
1515
{
1616
private const int DefaultContainerPort = 27017;
1717

18-
/// <summary>
19-
/// Adds a MongoDB container to the application model. The default image is "mongo" and the tag is "latest".
20-
/// </summary>
21-
/// <param name="builder">The <see cref="IDistributedApplicationBuilder"/>.</param>
22-
/// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param>
23-
/// <param name="port">The host port for MongoDB.</param>
24-
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
25-
public static IResourceBuilder<MongoDBContainerResource> AddMongoDBContainer(
26-
this IDistributedApplicationBuilder builder,
27-
string name,
28-
int? port = null)
29-
{
30-
var mongoDBContainer = new MongoDBContainerResource(name);
31-
32-
return builder
33-
.AddResource(mongoDBContainer)
34-
.WithManifestPublishingCallback(context => WriteMongoDBContainerToManifest(context, mongoDBContainer))
35-
.WithAnnotation(new EndpointAnnotation(ProtocolType.Tcp, port: port, containerPort: DefaultContainerPort)) // Internal port is always 27017.
36-
.WithAnnotation(new ContainerImageAnnotation { Image = "mongo", Tag = "latest" });
37-
}
38-
3918
/// <summary>
4019
/// Adds a MongoDB resource to the application model. A container is used for local development.
4120
/// </summary>
4221
/// <param name="builder">The <see cref="IDistributedApplicationBuilder"/>.</param>
4322
/// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param>
23+
/// <param name="port">The host port for MongoDB.</param>
4424
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
45-
public static IResourceBuilder<MongoDBServerResource> AddMongoDB(
46-
this IDistributedApplicationBuilder builder,
47-
string name)
25+
public static IResourceBuilder<MongoDBServerResource> AddMongoDB(this IDistributedApplicationBuilder builder, string name, int? port = null)
4826
{
4927
var mongoDBContainer = new MongoDBServerResource(name);
5028

5129
return builder
5230
.AddResource(mongoDBContainer)
5331
.WithManifestPublishingCallback(WriteMongoDBServerToManifest)
54-
.WithAnnotation(new EndpointAnnotation(ProtocolType.Tcp, containerPort: DefaultContainerPort)) // Internal port is always 27017.
32+
.WithAnnotation(new EndpointAnnotation(ProtocolType.Tcp, port: port, containerPort: DefaultContainerPort)) // Internal port is always 27017.
5533
.WithAnnotation(new ContainerImageAnnotation { Image = "mongo", Tag = "latest" });
5634
}
5735

@@ -61,7 +39,7 @@ public static IResourceBuilder<MongoDBServerResource> AddMongoDB(
6139
/// <param name="builder">The MongoDB server resource builder.</param>
6240
/// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param>
6341
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
64-
public static IResourceBuilder<MongoDBDatabaseResource> AddDatabase(this IResourceBuilder<IMongoDBParentResource> builder, string name)
42+
public static IResourceBuilder<MongoDBDatabaseResource> AddDatabase(this IResourceBuilder<MongoDBServerResource> builder, string name)
6543
{
6644
var mongoDBDatabase = new MongoDBDatabaseResource(name, builder.Resource);
6745

@@ -77,7 +55,7 @@ public static IResourceBuilder<MongoDBDatabaseResource> AddDatabase(this IResour
7755
/// <param name="hostPort">The host port for the application ui.</param>
7856
/// <param name="containerName">The name of the container (Optional).</param>
7957
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
80-
public static IResourceBuilder<T> WithMongoExpress<T>(this IResourceBuilder<T> builder, int? hostPort = null, string? containerName = null) where T : IMongoDBParentResource
58+
public static IResourceBuilder<T> WithMongoExpress<T>(this IResourceBuilder<T> builder, int? hostPort = null, string? containerName = null) where T : MongoDBServerResource
8159
{
8260
containerName ??= $"{builder.Resource.Name}-mongoexpress";
8361

@@ -96,6 +74,7 @@ private static void ConfigureMongoExpressContainer(EnvironmentCallbackContext co
9674
var hostPort = GetResourcePort(resource);
9775

9876
context.EnvironmentVariables.Add("ME_CONFIG_MONGODB_URL", $"mongodb://host.docker.internal:{hostPort}/?directConnection=true");
77+
context.EnvironmentVariables.Add("ME_CONFIG_BASICAUTH", "false");
9978

10079
static int GetResourcePort(IResource resource)
10180
{
@@ -109,7 +88,12 @@ static int GetResourcePort(IResource resource)
10988
}
11089
}
11190

112-
private static void WriteMongoDBContainerToManifest(this ManifestPublishingContext context, MongoDBContainerResource resource)
91+
public static IResourceBuilder<MongoDBServerResource> PublishAsContainer(this IResourceBuilder<MongoDBServerResource> builder)
92+
{
93+
return builder.WithManifestPublishingCallback(context => WriteMongoDBContainerToManifest(context, builder.Resource));
94+
}
95+
96+
private static void WriteMongoDBContainerToManifest(this ManifestPublishingContext context, MongoDBServerResource resource)
11397
{
11498
context.WriteContainer(resource);
11599
context.Writer.WriteString( // "connectionString": "...",

src/Aspire.Hosting/MongoDB/MongoDBContainerResource.cs

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/Aspire.Hosting/MongoDB/MongoDBDatabaseResource.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
namespace Aspire.Hosting.ApplicationModel;
55

66
/// <summary>
7-
/// A resource that represents a MongoDB database. This is a child resource of a <see cref="MongoDBContainerResource"/>.
7+
/// A resource that represents a MongoDB database. This is a child resource of a <see cref="MongoDBServerResource"/>.
88
/// </summary>
99
/// <param name="name">The name of the resource.</param>
10-
/// <param name="mongoDBContainer">The MongoDB server resource associated with this database.</param>
11-
public class MongoDBDatabaseResource(string name, IMongoDBParentResource mongoDBContainer)
12-
: Resource(name), IResourceWithParent<IMongoDBParentResource>, IResourceWithConnectionString
10+
/// <param name="parent">The MongoDB server resource associated with this database.</param>
11+
public class MongoDBDatabaseResource(string name, MongoDBServerResource parent) : Resource(name), IResourceWithParent<MongoDBServerResource>, IResourceWithConnectionString
1312
{
14-
public IMongoDBParentResource Parent => mongoDBContainer;
13+
public MongoDBServerResource Parent => parent;
1514

1615
/// <summary>
1716
/// Gets the connection string for the MongoDB database.

src/Aspire.Hosting/MongoDB/MongoDBServerResource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Aspire.Hosting.ApplicationModel;
99
/// A resource that represents a MongoDB container.
1010
/// </summary>
1111
/// <param name="name">The name of the resource.</param>
12-
public class MongoDBServerResource(string name) : Resource(name), IMongoDBParentResource
12+
public class MongoDBServerResource(string name) : ContainerResource(name), IResourceWithConnectionString
1313
{
1414
/// <summary>
1515
/// Gets the connection string for the MongoDB server.

tests/Aspire.Hosting.Tests/MongoDB/AddMongoDBTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ public void AddMongoDBContainerWithDefaultsAddsAnnotationMetadata()
1515
{
1616
var appBuilder = DistributedApplication.CreateBuilder();
1717

18-
appBuilder.AddMongoDBContainer("mongodb");
18+
appBuilder.AddMongoDB("mongodb");
1919

2020
var app = appBuilder.Build();
2121

2222
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>();
2323

24-
var containerResource = Assert.Single(appModel.Resources.OfType<MongoDBContainerResource>());
24+
var containerResource = Assert.Single(appModel.Resources.OfType<MongoDBServerResource>());
2525
Assert.Equal("mongodb", containerResource.Name);
2626

2727
var manifestAnnotation = Assert.Single(containerResource.Annotations.OfType<ManifestPublishingCallbackAnnotation>());
@@ -46,13 +46,13 @@ public void AddMongoDBContainerWithDefaultsAddsAnnotationMetadata()
4646
public void AddMongoDBContainerAddsAnnotationMetadata()
4747
{
4848
var appBuilder = DistributedApplication.CreateBuilder();
49-
appBuilder.AddMongoDBContainer("mongodb", 9813);
49+
appBuilder.AddMongoDB("mongodb", 9813);
5050

5151
var app = appBuilder.Build();
5252

5353
var appModel = app.Services.GetRequiredService<DistributedApplicationModel>();
5454

55-
var containerResource = Assert.Single(appModel.Resources.OfType<MongoDBContainerResource>());
55+
var containerResource = Assert.Single(appModel.Resources.OfType<MongoDBServerResource>());
5656
Assert.Equal("mongodb", containerResource.Name);
5757

5858
var manifestAnnotation = Assert.Single(containerResource.Annotations.OfType<ManifestPublishingCallbackAnnotation>());
@@ -78,7 +78,7 @@ public void MongoDBCreatesConnectionString()
7878
{
7979
var appBuilder = DistributedApplication.CreateBuilder();
8080
appBuilder
81-
.AddMongoDBContainer("mongodb")
81+
.AddMongoDB("mongodb")
8282
.WithAnnotation(
8383
new AllocatedEndpointAnnotation("mybinding",
8484
ProtocolType.Tcp,

tests/Aspire.Hosting.Tests/MongoDB/MongoDBContainerResourceTests.cs renamed to tests/Aspire.Hosting.Tests/MongoDB/MongoDBConnectionStringBuilderTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Aspire.Hosting.Tests.MongoDB;
99

10-
public class MongoDBContainerResourceTests
10+
public class MongoDBConnectionStringBuilderTests
1111
{
1212
[Theory]
1313
[InlineData("password", "mongodb://root:password@myserver:1000/")]

0 commit comments

Comments
 (0)