|
| 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 System.Text.Json; |
| 6 | +using Aspire.Hosting.ApplicationModel; |
| 7 | + |
| 8 | +namespace Aspire.Hosting.MongoDb; |
| 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 DefaultPassword = "password"; |
| 17 | + private const string PasswordEnvVarName = "MONGO_INITDB_ROOT_PASSWORD"; |
| 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 'password' 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 = DefaultContainerPort, |
| 32 | + string password = DefaultPassword) |
| 33 | + { |
| 34 | + var mongoDbContainer = new MongoDbContainerResource(name, password); |
| 35 | + |
| 36 | + return builder |
| 37 | + .AddResource(mongoDbContainer) |
| 38 | + .WithAnnotation(new ManifestPublishingCallbackAnnotation(WriteMongoDbContainerToManifest)) |
| 39 | + .WithAnnotation(new ServiceBindingAnnotation(ProtocolType.Tcp, port: port, containerPort: DefaultContainerPort)) // Internal port is always 27017. |
| 40 | + .WithAnnotation(new ContainerImageAnnotation { Image = "mongo", Tag = "latest" }) |
| 41 | + .WithEnvironment(PasswordEnvVarName, () => mongoDbContainer.Password); |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// 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. |
| 46 | + /// </summary> |
| 47 | + /// <param name="builder">The <see cref="IDistributedApplicationBuilder"/>.</param> |
| 48 | + /// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param> |
| 49 | + /// <param name="connectionString">The MongoDB connection string (optional).</param> |
| 50 | + /// <returns>A reference to the <see cref="IResourceBuilder{MongoDbConnectionResource}"/>.</returns> |
| 51 | + public static IResourceBuilder<MongoDbConnectionResource> AddMongoDbConnection(this IDistributedApplicationBuilder builder, string name, string? connectionString = null) |
| 52 | + { |
| 53 | + var mongoDbConnection = new MongoDbConnectionResource(name, connectionString); |
| 54 | + |
| 55 | + return builder |
| 56 | + .AddResource(mongoDbConnection) |
| 57 | + .WithAnnotation(new ManifestPublishingCallbackAnnotation((json) => WriteMongoDbConnectionToManifest(json, mongoDbConnection))); |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Adds a MongoDB database to the application model. |
| 62 | + /// </summary> |
| 63 | + /// <param name="builder">The MongoDB server resource builder.</param> |
| 64 | + /// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param> |
| 65 | + /// <returns>A reference to the <see cref="IResourceBuilder{MongoDbDatabaseResource}"/>.</returns> |
| 66 | + public static IResourceBuilder<MongoDbDatabaseResource> AddDatabase(this IResourceBuilder<MongoDbContainerResource> builder, string name) |
| 67 | + { |
| 68 | + var mongoDbDatabase = new MongoDbDatabaseResource(name, builder.Resource); |
| 69 | + |
| 70 | + return builder.ApplicationBuilder |
| 71 | + .AddResource(mongoDbDatabase) |
| 72 | + .WithAnnotation(new ManifestPublishingCallbackAnnotation( |
| 73 | + (json) => WriteMongoDbDatabaseToManifest(json, mongoDbDatabase))); |
| 74 | + } |
| 75 | + |
| 76 | + private static void WriteMongoDbContainerToManifest(Utf8JsonWriter jsonWriter) |
| 77 | + { |
| 78 | + jsonWriter.WriteString("type", "mongodb.server.v0"); |
| 79 | + } |
| 80 | + |
| 81 | + private static void WriteMongoDbConnectionToManifest(Utf8JsonWriter jsonWriter, MongoDbConnectionResource mongoDbConnection) |
| 82 | + { |
| 83 | + jsonWriter.WriteString("type", "mongodb.connection.v0"); |
| 84 | + jsonWriter.WriteString("connectionString", mongoDbConnection.GetConnectionString()); |
| 85 | + } |
| 86 | + |
| 87 | + private static void WriteMongoDbDatabaseToManifest(Utf8JsonWriter json, MongoDbDatabaseResource mongoDbDatabase) |
| 88 | + { |
| 89 | + json.WriteString("type", "mongodb.database.v0"); |
| 90 | + json.WriteString("parent", mongoDbDatabase.Parent.Name); |
| 91 | + } |
| 92 | +} |
0 commit comments