-
Notifications
You must be signed in to change notification settings - Fork 722
Add mongodb container and connection support #986
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
Merged
eerhardt
merged 16 commits into
dotnet:main
from
ailtonguitar:features/add-mongodb-support
Dec 6, 2023
Merged
Changes from 2 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
fc7fc08
feat: add mongodb container and connection support
ailtonguitar bc3d5bf
Move package reference to correct section
sebastienros 4a08605
Use standard HealthChecks property name
sebastienros 49835c2
refactor: change methods to void and small fixes in AspireMongoDBDriv…
ailtonguitar 5d6461e
Update tests/Aspire.MongoDB.Driver.Tests/AspireMongoDBDriverExtension…
ailtonguitar 9ce8ef2
refactor: mongodb conformancetests refactoring
ailtonguitar 89cecae
refactor: mongodb connectionString builder
ailtonguitar b880d73
chore: update mongodb readme
ailtonguitar b551170
refactor: refactoring on AspireMongoDBDriverExtensions.cs
ailtonguitar c7adf69
refactor: refactoring on AspireMongoDBDriverExtensions.cs
ailtonguitar c027e51
chore: Update tests/testproject/TestProject.IntegrationServiceA/TestP…
ailtonguitar e405fde
fix: mongodb conformanceTests to check if the server is available
ailtonguitar efc6886
fix: remove unnecessary usings and spaces
ailtonguitar 4a5eecc
Merge branch 'main' into features/add-mongodb-support
sebastienros 0f52a59
Remove authentication altogether
sebastienros fbcc1ad
Fix unit tests
sebastienros File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.