|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Net.Sockets; |
| 5 | +using Aspire.Hosting.ApplicationModel; |
| 6 | +using Aspire.Hosting.Publishing; |
| 7 | + |
| 8 | +namespace Aspire.Hosting; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Provides extension methods for adding MongoDB resources to an <see cref="IDistributedApplicationBuilder"/>. |
| 12 | +/// </summary> |
| 13 | +public static class MongoDBBuilderExtensions |
| 14 | +{ |
| 15 | + private const int DefaultContainerPort = 27017; |
| 16 | + private const string PasswordEnvVarName = "MONGO_INITDB_ROOT_PASSWORD"; |
| 17 | + private const string UserNameEnvVarName = "MONGO_INITDB_ROOT_USERNAME"; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Adds a MongoDB container to the application model. The default image is "mongo" and the tag is "latest". |
| 21 | + /// </summary> |
| 22 | + /// <returns></returns> |
| 23 | + /// <param name="builder">The <see cref="IDistributedApplicationBuilder"/>.</param> |
| 24 | + /// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param> |
| 25 | + /// <param name="port">The host port for MongoDB.</param> |
| 26 | + /// <param name="password">The password for the MongoDB root user. Defaults to a random password.</param> |
| 27 | + /// <returns>A reference to the <see cref="IResourceBuilder{MongoDBContainerResource}"/>.</returns> |
| 28 | + public static IResourceBuilder<MongoDBContainerResource> AddMongoDBContainer( |
| 29 | + this IDistributedApplicationBuilder builder, |
| 30 | + string name, |
| 31 | + int? port = null, |
| 32 | + string? password = null) |
| 33 | + { |
| 34 | + password ??= Guid.NewGuid().ToString("N"); |
| 35 | + |
| 36 | + var mongoDBContainer = new MongoDBContainerResource(name, password); |
| 37 | + |
| 38 | + return builder |
| 39 | + .AddResource(mongoDBContainer) |
| 40 | + .WithManifestPublishingCallback(WriteMongoDBContainerToManifest) |
| 41 | + .WithAnnotation(new ServiceBindingAnnotation(ProtocolType.Tcp, port: port, containerPort: DefaultContainerPort)) // Internal port is always 27017. |
| 42 | + .WithAnnotation(new ContainerImageAnnotation { Image = "mongo", Tag = "latest" }) |
| 43 | + .WithEnvironment(PasswordEnvVarName, () => mongoDBContainer.Password) |
| 44 | + .WithEnvironment(UserNameEnvVarName, () => mongoDBContainer.UserName); |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// 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. |
| 49 | + /// </summary> |
| 50 | + /// <param name="builder">The <see cref="IDistributedApplicationBuilder"/>.</param> |
| 51 | + /// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param> |
| 52 | + /// <param name="connectionString">The MongoDB connection string (optional).</param> |
| 53 | + /// <returns>A reference to the <see cref="IResourceBuilder{MongoDBConnectionResource}"/>.</returns> |
| 54 | + public static IResourceBuilder<MongoDBConnectionResource> AddMongoDBConnection(this IDistributedApplicationBuilder builder, string name, string? connectionString = null) |
| 55 | + { |
| 56 | + var mongoDBConnection = new MongoDBConnectionResource(name, connectionString); |
| 57 | + |
| 58 | + return builder |
| 59 | + .AddResource(mongoDBConnection) |
| 60 | + .WithManifestPublishingCallback(context => context.WriteMongoDBConnectionToManifest(mongoDBConnection)); |
| 61 | + } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Adds a MongoDB database to the application model. |
| 65 | + /// </summary> |
| 66 | + /// <param name="builder">The MongoDB server resource builder.</param> |
| 67 | + /// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param> |
| 68 | + /// <returns>A reference to the <see cref="IResourceBuilder{MongoDBDatabaseResource}"/>.</returns> |
| 69 | + public static IResourceBuilder<MongoDBDatabaseResource> AddDatabase(this IResourceBuilder<MongoDBContainerResource> builder, string name) |
| 70 | + { |
| 71 | + var mongoDBDatabase = new MongoDBDatabaseResource(name, builder.Resource); |
| 72 | + |
| 73 | + return builder.ApplicationBuilder |
| 74 | + .AddResource(mongoDBDatabase) |
| 75 | + .WithManifestPublishingCallback(context => context.WriteMongoDBDatabaseToManifest(mongoDBDatabase)); |
| 76 | + } |
| 77 | + |
| 78 | + private static void WriteMongoDBContainerToManifest(this ManifestPublishingContext context) |
| 79 | + { |
| 80 | + context.Writer.WriteString("type", "mongodb.server.v0"); |
| 81 | + } |
| 82 | + |
| 83 | + private static void WriteMongoDBConnectionToManifest(this ManifestPublishingContext context, MongoDBConnectionResource mongoDbConnection) |
| 84 | + { |
| 85 | + context.Writer.WriteString("type", "mongodb.connection.v0"); |
| 86 | + context.Writer.WriteString("connectionString", mongoDbConnection.GetConnectionString()); |
| 87 | + } |
| 88 | + |
| 89 | + private static void WriteMongoDBDatabaseToManifest(this ManifestPublishingContext context, MongoDBDatabaseResource mongoDbDatabase) |
| 90 | + { |
| 91 | + context.Writer.WriteString("type", "mongodb.database.v0"); |
| 92 | + context.Writer.WriteString("parent", mongoDbDatabase.Parent.Name); |
| 93 | + } |
| 94 | +} |
0 commit comments