From 304884e61b1b1fe8ca96fb97b575195c0470f3af Mon Sep 17 00:00:00 2001 From: Mitch Denny Date: Fri, 6 Oct 2023 14:37:45 +1100 Subject: [PATCH 1/3] Emit database names in manifest. --- .../PostgresContainerBuilderExtensions.cs | 30 +++++++++++++++++-- .../Postgres/PostgresDatabaseAnnotation.cs | 11 +++++++ 2 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 src/Aspire.Hosting/Postgres/PostgresDatabaseAnnotation.cs diff --git a/src/Aspire.Hosting/Postgres/PostgresContainerBuilderExtensions.cs b/src/Aspire.Hosting/Postgres/PostgresContainerBuilderExtensions.cs index 5c6ab360be..7e6b348080 100644 --- a/src/Aspire.Hosting/Postgres/PostgresContainerBuilderExtensions.cs +++ b/src/Aspire.Hosting/Postgres/PostgresContainerBuilderExtensions.cs @@ -16,7 +16,7 @@ public static IDistributedApplicationComponentBuilder WritePostgresComponentToManifestAsync(postgresContainer, writer, ct))) .WithAnnotation(new ServiceBindingAnnotation(ProtocolType.Tcp, port: port, containerPort: 5432)) // Internal port is always 5432. .WithAnnotation(new ContainerImageAnnotation { Image = "postgres", Tag = "latest" }) .WithEnvironment("POSTGRES_HOST_AUTH_METHOD", "trust") @@ -32,9 +32,25 @@ public static IDistributedApplicationComponentBuilder(); + if (databases.Any()) + { + jsonWriter.WriteStartObject("databases"); + + foreach (var database in databases) + { + jsonWriter.WriteStartObject(database.DatabaseName); + jsonWriter.WriteEndObject(); + } + + jsonWriter.WriteEndObject(); + + } + await jsonWriter.FlushAsync(cancellationToken).ConfigureAwait(false); } @@ -46,11 +62,19 @@ public static IDistributedApplicationComponentBuilder WithPostgresDatabase { connectionName = connectionName ?? postgresBuilder.Component.Name; + // We need to capture this here so that when we do manifest generation we know + // how to enumerate the databases. + if (databaseName != null && !postgresBuilder.Component.Annotations.OfType().Any(db => db.DatabaseName == databaseName)) + { + postgresBuilder.WithAnnotation(new PostgresDatabaseAnnotation(databaseName)); + } + return builder.WithEnvironment((context) => { if (context.PublisherName == "manifest") { - context.EnvironmentVariables[$"{ConnectionStringEnvironmentName}{connectionName}"] = $"{{{postgresBuilder.Component.Name}.connectionString}}"; + var manifestConnectionString = databaseName == null ? $"{postgresBuilder.Component.Name}.connectionString" : $"{postgresBuilder.Component.Name}.databases.{databaseName}.connectionString"; + context.EnvironmentVariables[$"{ConnectionStringEnvironmentName}{connectionName}"] = manifestConnectionString; return; } diff --git a/src/Aspire.Hosting/Postgres/PostgresDatabaseAnnotation.cs b/src/Aspire.Hosting/Postgres/PostgresDatabaseAnnotation.cs new file mode 100644 index 0000000000..ce4d9b3410 --- /dev/null +++ b/src/Aspire.Hosting/Postgres/PostgresDatabaseAnnotation.cs @@ -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. + +using Aspire.Hosting.ApplicationModel; + +namespace Aspire.Hosting.Postgres; + +internal sealed class PostgresDatabaseAnnotation(string databaseName) : IDistributedApplicationComponentAnnotation +{ + public string DatabaseName { get; } = databaseName; +} From 041e10d88046409e93dbe1cb394e2b9415ae3c91 Mon Sep 17 00:00:00 2001 From: Mitch Denny Date: Fri, 6 Oct 2023 14:47:15 +1100 Subject: [PATCH 2/3] Missing curlies. --- .../Postgres/PostgresContainerBuilderExtensions.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Aspire.Hosting/Postgres/PostgresContainerBuilderExtensions.cs b/src/Aspire.Hosting/Postgres/PostgresContainerBuilderExtensions.cs index 7e6b348080..a0fecd0b18 100644 --- a/src/Aspire.Hosting/Postgres/PostgresContainerBuilderExtensions.cs +++ b/src/Aspire.Hosting/Postgres/PostgresContainerBuilderExtensions.cs @@ -73,7 +73,7 @@ public static IDistributedApplicationComponentBuilder WithPostgresDatabase { if (context.PublisherName == "manifest") { - var manifestConnectionString = databaseName == null ? $"{postgresBuilder.Component.Name}.connectionString" : $"{postgresBuilder.Component.Name}.databases.{databaseName}.connectionString"; + var manifestConnectionString = databaseName == null ? $"{{{postgresBuilder.Component.Name}.connectionString}}" : $"{{{postgresBuilder.Component.Name}.databases.{databaseName}.connectionString}}"; context.EnvironmentVariables[$"{ConnectionStringEnvironmentName}{connectionName}"] = manifestConnectionString; return; } From fcc3f3026318d872f597885176afb35b58d69630 Mon Sep 17 00:00:00 2001 From: Mitch Denny Date: Mon, 9 Oct 2023 09:48:27 +1100 Subject: [PATCH 3/3] Update git ignore to not checkin aspire-manifest.json --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 4a7f90ae76..47ee7e1de4 100644 --- a/.gitignore +++ b/.gitignore @@ -136,3 +136,4 @@ node_modules/ # vscode python env files .env +samples/DevHost/aspire-manifest-new.json