-
Notifications
You must be signed in to change notification settings - Fork 533
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add mongodb container and connection support
- Loading branch information
1 parent
42979ba
commit fc7fc08
Showing
24 changed files
with
1,193 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Aspire.Hosting.ApplicationModel; | ||
|
||
/// <summary> | ||
/// Represents a MongoDB resource that requires a connection string. | ||
/// </summary> | ||
public interface IMongoDBResource : IResourceWithConnectionString | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Net.Sockets; | ||
using Aspire.Hosting.ApplicationModel; | ||
using Aspire.Hosting.Publishing; | ||
|
||
namespace Aspire.Hosting; | ||
|
||
/// <summary> | ||
/// Provides extension methods for adding MongoDB resources to an <see cref="IDistributedApplicationBuilder"/>. | ||
/// </summary> | ||
public static class MongoDBBuilderExtensions | ||
{ | ||
private const int DefaultContainerPort = 27017; | ||
private const string PasswordEnvVarName = "MONGO_INITDB_ROOT_PASSWORD"; | ||
private const string UserNameEnvVarName = "MONGO_INITDB_ROOT_USERNAME"; | ||
|
||
/// <summary> | ||
/// Adds a MongoDB container to the application model. The default image is "mongo" and the tag is "latest". | ||
/// </summary> | ||
/// <returns></returns> | ||
/// <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 for MongoDB.</param> | ||
/// <param name="password">The password for the MongoDB root user. Defaults to a random password.</param> | ||
/// <returns>A reference to the <see cref="IResourceBuilder{MongoDBContainerResource}"/>.</returns> | ||
public static IResourceBuilder<MongoDBContainerResource> AddMongoDBContainer( | ||
this IDistributedApplicationBuilder builder, | ||
string name, | ||
int? port = null, | ||
string? password = null) | ||
{ | ||
password ??= Guid.NewGuid().ToString("N"); | ||
|
||
var mongoDBContainer = new MongoDBContainerResource(name, password); | ||
|
||
return builder | ||
.AddResource(mongoDBContainer) | ||
.WithManifestPublishingCallback(WriteMongoDBContainerToManifest) | ||
.WithAnnotation(new ServiceBindingAnnotation(ProtocolType.Tcp, port: port, containerPort: DefaultContainerPort)) // Internal port is always 27017. | ||
.WithAnnotation(new ContainerImageAnnotation { Image = "mongo", Tag = "latest" }) | ||
.WithEnvironment(PasswordEnvVarName, () => mongoDBContainer.Password) | ||
.WithEnvironment(UserNameEnvVarName, () => mongoDBContainer.UserName); | ||
} | ||
|
||
/// <summary> | ||
/// Adds a MongoDB connection to the application model. Connection strings can also be read from the connection string section of the configuration using the name of the resource. | ||
/// </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="connectionString">The MongoDB connection string (optional).</param> | ||
/// <returns>A reference to the <see cref="IResourceBuilder{MongoDBConnectionResource}"/>.</returns> | ||
public static IResourceBuilder<MongoDBConnectionResource> AddMongoDBConnection(this IDistributedApplicationBuilder builder, string name, string? connectionString = null) | ||
{ | ||
var mongoDBConnection = new MongoDBConnectionResource(name, connectionString); | ||
|
||
return builder | ||
.AddResource(mongoDBConnection) | ||
.WithManifestPublishingCallback(context => context.WriteMongoDBConnectionToManifest(mongoDBConnection)); | ||
} | ||
|
||
/// <summary> | ||
/// Adds a MongoDB database to the application model. | ||
/// </summary> | ||
/// <param name="builder">The MongoDB server resource builder.</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{MongoDBDatabaseResource}"/>.</returns> | ||
public static IResourceBuilder<MongoDBDatabaseResource> AddDatabase(this IResourceBuilder<MongoDBContainerResource> builder, string name) | ||
{ | ||
var mongoDBDatabase = new MongoDBDatabaseResource(name, builder.Resource); | ||
|
||
return builder.ApplicationBuilder | ||
.AddResource(mongoDBDatabase) | ||
.WithManifestPublishingCallback(context => context.WriteMongoDBDatabaseToManifest(mongoDBDatabase)); | ||
} | ||
|
||
private static void WriteMongoDBContainerToManifest(this ManifestPublishingContext context) | ||
{ | ||
context.Writer.WriteString("type", "mongodb.server.v0"); | ||
} | ||
|
||
private static void WriteMongoDBConnectionToManifest(this ManifestPublishingContext context, MongoDBConnectionResource mongoDbConnection) | ||
{ | ||
context.Writer.WriteString("type", "mongodb.connection.v0"); | ||
context.Writer.WriteString("connectionString", mongoDbConnection.GetConnectionString()); | ||
} | ||
|
||
private static void WriteMongoDBDatabaseToManifest(this ManifestPublishingContext context, MongoDBDatabaseResource mongoDbDatabase) | ||
{ | ||
context.Writer.WriteString("type", "mongodb.database.v0"); | ||
context.Writer.WriteString("parent", mongoDbDatabase.Parent.Name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
namespace Aspire.Hosting.ApplicationModel; | ||
|
||
/// <summary> | ||
/// A resource that represents a MongoDB connection. | ||
/// </summary> | ||
/// <param name="name">The name of the resource.</param> | ||
/// <param name="connectionString">The MongoDB connection string.</param> | ||
public class MongoDBConnectionResource(string name, string? connectionString) : Resource(name), IMongoDBResource | ||
{ | ||
private readonly string? _connectionString = connectionString; | ||
|
||
/// <summary> | ||
/// Gets the connection string for the MongoDB server. | ||
/// </summary> | ||
/// <returns>The specified connection string.</returns> | ||
public string? GetConnectionString() => _connectionString; | ||
} |
62 changes: 62 additions & 0 deletions
62
src/Aspire.Hosting/MongoDB/MongoDBConnectionStringBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Aspire.Hosting.MongoDB; | ||
|
||
internal class MongoDBConnectionStringBuilder | ||
{ | ||
private const string Scheme = "mongodb"; | ||
|
||
private string? _server; | ||
private int _port; | ||
private string? _userName; | ||
private string? _password; | ||
|
||
public MongoDBConnectionStringBuilder WithServer(string server) | ||
{ | ||
ArgumentNullException.ThrowIfNullOrWhiteSpace(server, nameof(server)); | ||
|
||
_server = server; | ||
|
||
return this; | ||
} | ||
|
||
public MongoDBConnectionStringBuilder WithPort(int port) | ||
{ | ||
_port = port; | ||
|
||
return this; | ||
} | ||
|
||
public MongoDBConnectionStringBuilder WithUserName(string userName) | ||
{ | ||
ArgumentNullException.ThrowIfNullOrWhiteSpace(userName, nameof(userName)); | ||
|
||
_userName = userName; | ||
|
||
return this; | ||
} | ||
|
||
public MongoDBConnectionStringBuilder WithPassword(string password) | ||
{ | ||
ArgumentNullException.ThrowIfNullOrWhiteSpace(password, nameof(password)); | ||
|
||
_password = password; | ||
|
||
return this; | ||
} | ||
|
||
public string Build() | ||
{ | ||
var builder = new UriBuilder | ||
{ | ||
Scheme = Scheme, | ||
Host = _server, | ||
Port = _port, | ||
UserName = _userName, | ||
Password = _password | ||
}; | ||
|
||
return builder.ToString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Aspire.Hosting.MongoDB; | ||
|
||
namespace Aspire.Hosting.ApplicationModel; | ||
|
||
/// <summary> | ||
/// A resource that represents a MongoDB container. | ||
/// </summary> | ||
/// <param name="name">The name of the resource.</param> | ||
/// <param name="password">The MongoDB root password.</param> | ||
public class MongoDBContainerResource(string name, string password) : ContainerResource(name), IMongoDBResource | ||
{ | ||
private const string DefaultUserName = "root"; | ||
|
||
public string Password { get; } = password; | ||
|
||
public string UserName { get; } = DefaultUserName; | ||
|
||
/// <summary> | ||
/// Gets the connection string for the MongoDB server. | ||
/// </summary> | ||
/// <returns>A connection string for the MongoDB server in the form "mongodb://host:port".</returns> | ||
public string? GetConnectionString() | ||
{ | ||
if (!this.TryGetAllocatedEndPoints(out var allocatedEndpoints)) | ||
{ | ||
throw new DistributedApplicationException("Expected allocated endpoints!"); | ||
} | ||
|
||
var allocatedEndpoint = allocatedEndpoints.Single(); | ||
|
||
return new MongoDBConnectionStringBuilder() | ||
.WithServer(allocatedEndpoint.Address) | ||
.WithPort(allocatedEndpoint.Port) | ||
.WithUserName(UserName) | ||
.WithPassword(Password) | ||
.Build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// 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; | ||
|
||
namespace Aspire.Hosting.ApplicationModel; | ||
|
||
/// <summary> | ||
/// A resource that represents a MongoDB database. This is a child resource of a <see cref="MongoDBContainerResource"/>. | ||
/// </summary> | ||
/// <param name="name">The name of the resource.</param> | ||
/// <param name="mongoDBContainer">The MongoDB server resource associated with this database.</param> | ||
public class MongoDBDatabaseResource(string name, MongoDBContainerResource mongoDBContainer) | ||
: Resource(name), IMongoDBResource, IResourceWithParent<MongoDBContainerResource> | ||
{ | ||
public MongoDBContainerResource Parent => mongoDBContainer; | ||
|
||
/// <summary> | ||
/// Gets the connection string for the MongoDB database. | ||
/// </summary> | ||
/// <returns>A connection string for the MongoDB database.</returns> | ||
public string? GetConnectionString() | ||
{ | ||
if (Parent.GetConnectionString() is { } connectionString) | ||
{ | ||
var builder = new StringBuilder(connectionString); | ||
|
||
if (!connectionString.EndsWith('/')) | ||
{ | ||
builder.Append('/'); | ||
} | ||
|
||
builder.Append(Name); | ||
|
||
return builder.ToString(); | ||
} | ||
|
||
throw new DistributedApplicationException("Parent resource connection string was null."); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Components/Aspire.MongoDB.Driver/Aspire.MongoDB.Driver.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>$(NetCurrent)</TargetFramework> | ||
<IsPackable>true</IsPackable> | ||
<PackageTags>$(ComponentDatabasePackageTags) MongoDB</PackageTags> | ||
<Description>A generic MongoDB client that integrates with Aspire.</Description> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="..\Common\HealthChecksExtensions.cs" Link="HealthChecksExtensions.cs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AspNetCore.HealthChecks.MongoDb" /> | ||
<PackageReference Include="MongoDB.Driver" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" /> | ||
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" /> | ||
<PackageReference Include="MongoDB.Driver.Core.Extensions.DiagnosticSources" /> | ||
<PackageReference Include="OpenTelemetry.Extensions.Hosting" /> | ||
</ItemGroup> | ||
|
||
|
||
</Project> |
Oops, something went wrong.