Skip to content

Commit

Permalink
WithPgAdmin - Respect UserName Parameter (dotnet#5805)
Browse files Browse the repository at this point in the history
* WithPgAdmin - Respect UserName Parameter

* tests
  • Loading branch information
cmeyertons authored Oct 13, 2024
1 parent 230dd3b commit 4e7e0de
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/Aspire.Hosting.PostgreSQL/PostgresBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// 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.Json;
using System.Text;
using System.Text.Json;
using Aspire.Hosting.ApplicationModel;
using Aspire.Hosting.Postgres;
using Aspire.Hosting.Utils;
Expand Down Expand Up @@ -69,7 +69,7 @@ public static IResourceBuilder<PostgresServerResource> AddPostgres(this IDistrib
//
// https://github.com/npgsql/npgsql/blob/c3b31c393de66a4b03fba0d45708d46a2acb06d2/src/Npgsql/NpgsqlConnection.cs#L445
//
connection.ConnectionString = connection.ConnectionString + ";Database=postgres;";
connection.ConnectionString += ";Database=postgres;";
});

return builder.AddResource(postgresServer)
Expand Down Expand Up @@ -183,7 +183,7 @@ public static IResourceBuilder<T> WithPgAdmin<T>(this IResourceBuilder<T> builde
// This will need to be refactored once updated service discovery APIs are available
writer.WriteString("Host", endpoint.Resource.Name);
writer.WriteNumber("Port", (int)endpoint.TargetPort!);
writer.WriteString("Username", "postgres");
writer.WriteString("Username", postgresInstance.UserNameParameter?.Value ?? "postgres");
writer.WriteString("SSLMode", "prefer");
writer.WriteString("MaintenanceDB", "postgres");
writer.WriteString("PasswordExecCommand", $"echo '{postgresInstance.PasswordParameter.Value}'"); // HACK: Generating a pass file and playing around with chmod is too painful.
Expand Down
5 changes: 3 additions & 2 deletions tests/Aspire.Hosting.PostgreSQL.Tests/AddPostgresTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,9 @@ public void WithPostgresTwiceEndsUpWithOneContainer()
public async Task WithPostgresProducesValidServersJsonFile()
{
var builder = DistributedApplication.CreateBuilder();
var username = builder.AddParameter("pg-user", "myuser");
var pg1 = builder.AddPostgres("mypostgres1").WithPgAdmin(pga => pga.WithHostPort(8081));
var pg2 = builder.AddPostgres("mypostgres2").WithPgAdmin(pga => pga.WithHostPort(8081));
var pg2 = builder.AddPostgres("mypostgres2", username).WithPgAdmin(pga => pga.WithHostPort(8081));

// Add fake allocated endpoints.
pg1.WithEndpoint("tcp", e => e.AllocatedEndpoint = new AllocatedEndpoint(e, "localhost", 5001));
Expand Down Expand Up @@ -486,7 +487,7 @@ public async Task WithPostgresProducesValidServersJsonFile()
Assert.Equal("Servers", servers.GetProperty("2").GetProperty("Group").GetString());
Assert.Equal("mypostgres2", servers.GetProperty("2").GetProperty("Host").GetString());
Assert.Equal(5432, servers.GetProperty("2").GetProperty("Port").GetInt32());
Assert.Equal("postgres", servers.GetProperty("2").GetProperty("Username").GetString());
Assert.Equal("myuser", servers.GetProperty("2").GetProperty("Username").GetString());
Assert.Equal("prefer", servers.GetProperty("2").GetProperty("SSLMode").GetString());
Assert.Equal("postgres", servers.GetProperty("2").GetProperty("MaintenanceDB").GetString());
Assert.Equal($"echo '{pg2.Resource.PasswordParameter.Value}'", servers.GetProperty("2").GetProperty("PasswordExecCommand").GetString());
Expand Down

0 comments on commit 4e7e0de

Please sign in to comment.