-
Notifications
You must be signed in to change notification settings - Fork 536
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
69937e2
commit b1be7c1
Showing
14 changed files
with
468 additions
and
1 deletion.
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
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,92 @@ | ||
// 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 System.Text.Json; | ||
using Aspire.Hosting.ApplicationModel; | ||
|
||
namespace Aspire.Hosting.MongoDb; | ||
|
||
/// <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 DefaultPassword = "password"; | ||
private const string PasswordEnvVarName = "MONGO_INITDB_ROOT_PASSWORD"; | ||
|
||
/// <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 'password' password.</param> | ||
/// <returns>A reference to the <see cref="IResourceBuilder{MongoDbContainerResource}"/>.</returns> | ||
public static IResourceBuilder<MongoDbContainerResource> AddMongoDbContainer( | ||
this IDistributedApplicationBuilder builder, | ||
string name, | ||
int port = DefaultContainerPort, | ||
string password = DefaultPassword) | ||
{ | ||
var mongoDbContainer = new MongoDbContainerResource(name, password); | ||
|
||
return builder | ||
.AddResource(mongoDbContainer) | ||
.WithAnnotation(new ManifestPublishingCallbackAnnotation(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); | ||
} | ||
|
||
/// <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) | ||
.WithAnnotation(new ManifestPublishingCallbackAnnotation((json) => WriteMongoDbConnectionToManifest(json, 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) | ||
.WithAnnotation(new ManifestPublishingCallbackAnnotation( | ||
(json) => WriteMongoDbDatabaseToManifest(json, mongoDbDatabase))); | ||
} | ||
|
||
private static void WriteMongoDbContainerToManifest(Utf8JsonWriter jsonWriter) | ||
{ | ||
jsonWriter.WriteString("type", "mongodb.server.v0"); | ||
} | ||
|
||
private static void WriteMongoDbConnectionToManifest(Utf8JsonWriter jsonWriter, MongoDbConnectionResource mongoDbConnection) | ||
{ | ||
jsonWriter.WriteString("type", "mongodb.connection.v0"); | ||
jsonWriter.WriteString("connectionString", mongoDbConnection.GetConnectionString()); | ||
} | ||
|
||
private static void WriteMongoDbDatabaseToManifest(Utf8JsonWriter json, MongoDbDatabaseResource mongoDbDatabase) | ||
{ | ||
json.WriteString("type", "mongodb.database.v0"); | ||
json.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), IMySqlResource | ||
{ | ||
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; | ||
} |
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,30 @@ | ||
// 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 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 | ||
{ | ||
public string Password { get; } = password; | ||
|
||
/// <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 $"mongodb://root:{Password}@{allocatedEndpoint.Address}:{allocatedEndpoint.Port}"; | ||
} | ||
} |
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,29 @@ | ||
// 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 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) | ||
{ | ||
return $"{connectionString}/{Name}"; | ||
} | ||
|
||
throw new DistributedApplicationException("Parent resource connection string was null."); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
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,17 @@ | ||
<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> | ||
<PackageReference Include="MongoDB.Driver" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" /> | ||
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.