Skip to content

Commit 1844f0f

Browse files
committed
Refactor AddDbGate method to use default name
Removed the `name` parameter from the `AddDbGate` method across various builder extension classes, simplifying the method signature to only require the `port` parameter. The default name "dbgate" is now used internally. Updated tests to reflect the new method signature and default naming convention, ensuring assertions check for the new default name. Adjusted environment variable handling in `ConfigureDbGateContainer` methods to accommodate these changes. These updates streamline the API for adding a DbGate container resource while maintaining functionality for different database types.
1 parent 30a4b63 commit 1844f0f

File tree

18 files changed

+244
-309
lines changed

18 files changed

+244
-309
lines changed

src/CommunityToolkit.Aspire.Hosting.DbGate/DbGateBuilderExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ public static IResourceBuilder<DbGateContainerResource> WithDataBindMount(this I
5757
/// Adds a DbGate container resource to the application.
5858
/// </summary>
5959
/// <param name="builder">The resource builder.</param>
60-
/// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param>
60+
/// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency. Optional; defaults to <c>dbgate</c>.</param>
6161
/// <param name="port">The host port to bind the underlying container to.</param>
6262
/// <remarks>
6363
/// Multiple <see cref="AddDbGate(IDistributedApplicationBuilder, string, int?)"/> calls will return the same resource builder instance.
6464
/// </remarks>
6565
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
66-
public static IResourceBuilder<DbGateContainerResource> AddDbGate(this IDistributedApplicationBuilder builder, [ResourceName] string name, int? port = null)
66+
public static IResourceBuilder<DbGateContainerResource> AddDbGate(this IDistributedApplicationBuilder builder, [ResourceName] string name = "dbgate", int? port = null)
6767
{
6868
ArgumentNullException.ThrowIfNull(builder);
6969
ArgumentNullException.ThrowIfNull(name);
Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Aspire.Hosting.ApplicationModel;
2+
using System.Diagnostics.Metrics;
23
using System.Text;
34

45
namespace Aspire.Hosting;
@@ -37,57 +38,45 @@ public static IResourceBuilder<MongoDBServerResource> WithDbGate(this IResourceB
3738
{
3839
ArgumentNullException.ThrowIfNull(builder);
3940

40-
containerName ??= $"{builder.Resource.Name}-dbgate";
41+
containerName ??= "dbgate";
4142

42-
var dbGateBuilder = DbGateBuilderExtensions.AddDbGate(builder.ApplicationBuilder, containerName);
43+
var dbGateBuilder = builder.ApplicationBuilder.AddDbGate(containerName);
4344

4445
dbGateBuilder
45-
.WithEnvironment(context => ConfigureDbGateContainer(context, builder.ApplicationBuilder));
46+
.WithEnvironment(context => ConfigureDbGateContainer(context, builder))
47+
.WaitFor(builder);
4648

4749
configureContainer?.Invoke(dbGateBuilder);
4850

4951
return builder;
5052
}
5153

52-
private static void ConfigureDbGateContainer(EnvironmentCallbackContext context, IDistributedApplicationBuilder applicationBuilder)
54+
private static void ConfigureDbGateContainer(EnvironmentCallbackContext context, IResourceBuilder<MongoDBServerResource> builder)
5355
{
54-
var mongoDBInstances = applicationBuilder.Resources.OfType<MongoDBServerResource>();
56+
var mongoDBServer = builder.Resource;
5557

56-
var counter = 1;
58+
var name = mongoDBServer.Name;
59+
var label = $"LABEL_{name}";
5760

5861
// Multiple WithDbGate calls will be ignored
59-
if (context.EnvironmentVariables.ContainsKey($"LABEL_mongodb{counter}"))
62+
if (context.EnvironmentVariables.ContainsKey(label))
6063
{
6164
return;
6265
}
6366

64-
foreach (var mongoDBServer in mongoDBInstances)
65-
{
66-
// DbGate assumes MongoDB is being accessed over a default Aspire container network and hardcodes the resource address
67-
// This will need to be refactored once updated service discovery APIs are available
68-
context.EnvironmentVariables.Add($"LABEL_mongodb{counter}", mongoDBServer.Name);
69-
context.EnvironmentVariables.Add($"URL_mongodb{counter}", mongoDBServer.ConnectionStringExpression);
70-
context.EnvironmentVariables.Add($"ENGINE_mongodb{counter}", "mongo@dbgate-plugin-mongo");
67+
// DbGate assumes MongoDB is being accessed over a default Aspire container network and hardcodes the resource address
68+
// This will need to be refactored once updated service discovery APIs are available
69+
context.EnvironmentVariables.Add(label, name);
70+
context.EnvironmentVariables.Add($"URL_{name}", mongoDBServer.ConnectionStringExpression);
71+
context.EnvironmentVariables.Add($"ENGINE_{name}", "mongo@dbgate-plugin-mongo");
7172

72-
counter++;
73+
if (context.EnvironmentVariables.GetValueOrDefault("CONNECTIONS") is string { Length: > 0 } connections)
74+
{
75+
context.EnvironmentVariables["CONNECTIONS"] = $"{connections},{name}";
7376
}
74-
75-
var instancesCount = mongoDBInstances.Count();
76-
if (instancesCount > 0)
77+
else
7778
{
78-
var strBuilder = new StringBuilder();
79-
strBuilder.AppendJoin(',', Enumerable.Range(1, instancesCount).Select(i => $"mongodb{i}"));
80-
var connections = strBuilder.ToString();
81-
82-
string CONNECTIONS = context.EnvironmentVariables.GetValueOrDefault("CONNECTIONS")?.ToString() ?? string.Empty;
83-
if (string.IsNullOrEmpty(CONNECTIONS))
84-
{
85-
context.EnvironmentVariables["CONNECTIONS"] = connections;
86-
}
87-
else
88-
{
89-
context.EnvironmentVariables["CONNECTIONS"] += $",{connections}";
90-
}
79+
context.EnvironmentVariables["CONNECTIONS"] = name;
9180
}
9281
}
9382
}

src/CommunityToolkit.Aspire.Hosting.MySql.Extensions/MySqlBuilderExtensions.cs

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -79,59 +79,47 @@ public static IResourceBuilder<MySqlServerResource> WithDbGate(this IResourceBui
7979
{
8080
ArgumentNullException.ThrowIfNull(builder);
8181

82-
containerName ??= $"{builder.Resource.Name}-dbgate";
82+
containerName ??= "dbgate";
8383

8484
var dbGateBuilder = DbGateBuilderExtensions.AddDbGate(builder.ApplicationBuilder, containerName);
8585

8686
dbGateBuilder
87-
.WithEnvironment(context => ConfigureDbGateContainer(context, builder.ApplicationBuilder));
87+
.WithEnvironment(context => ConfigureDbGateContainer(context, builder))
88+
.WaitFor(builder);
8889

8990
configureContainer?.Invoke(dbGateBuilder);
9091

9192
return builder;
9293
}
9394

94-
private static void ConfigureDbGateContainer(EnvironmentCallbackContext context, IDistributedApplicationBuilder applicationBuilder)
95+
private static void ConfigureDbGateContainer(EnvironmentCallbackContext context, IResourceBuilder<MySqlServerResource> builder)
9596
{
96-
var mysqlInstances = applicationBuilder.Resources.OfType<MySqlServerResource>();
97+
var mySqlServer = builder.Resource;
9798

98-
var counter = 1;
99+
var name = mySqlServer.Name;
100+
var label = $"LABEL_{name}";
99101

100102
// Multiple WithDbGate calls will be ignored
101-
if (context.EnvironmentVariables.ContainsKey($"LABEL_mysql{counter}"))
103+
if (context.EnvironmentVariables.ContainsKey(label))
102104
{
103105
return;
104106
}
105107

106-
foreach (var mySqlServerResource in mysqlInstances)
108+
// DbGate assumes MySql is being accessed over a default Aspire container network and hardcodes the resource address
109+
context.EnvironmentVariables.Add(label, name);
110+
context.EnvironmentVariables.Add($"SERVER_{name}", name);
111+
context.EnvironmentVariables.Add($"USER_{name}", "root");
112+
context.EnvironmentVariables.Add($"PASSWORD_{name}", mySqlServer.PasswordParameter);
113+
context.EnvironmentVariables.Add($"PORT_{name}", mySqlServer.PrimaryEndpoint.TargetPort!.ToString()!);
114+
context.EnvironmentVariables.Add($"ENGINE_{name}", "mysql@dbgate-plugin-mysql");
115+
116+
if (context.EnvironmentVariables.GetValueOrDefault("CONNECTIONS") is string { Length: > 0 } connections)
107117
{
108-
// DbGate assumes MySql is being accessed over a default Aspire container network and hardcodes the resource address
109-
context.EnvironmentVariables.Add($"LABEL_mysql{counter}", mySqlServerResource.Name);
110-
context.EnvironmentVariables.Add($"SERVER_mysql{counter}", mySqlServerResource.Name);
111-
context.EnvironmentVariables.Add($"USER_mysql{counter}", "root");
112-
context.EnvironmentVariables.Add($"PASSWORD_mysql{counter}", mySqlServerResource.PasswordParameter);
113-
context.EnvironmentVariables.Add($"PORT_mysql{counter}", mySqlServerResource.PrimaryEndpoint.TargetPort!.ToString()!);
114-
context.EnvironmentVariables.Add($"ENGINE_mysql{counter}", "mysql@dbgate-plugin-mysql");
115-
116-
counter++;
118+
context.EnvironmentVariables["CONNECTIONS"] = $"{connections},{name}";
117119
}
118-
119-
var instancesCount = mysqlInstances.Count();
120-
if (instancesCount > 0)
120+
else
121121
{
122-
var strBuilder = new StringBuilder();
123-
strBuilder.AppendJoin(',', Enumerable.Range(1, instancesCount).Select(i => $"mysql{i}"));
124-
var connections = strBuilder.ToString();
125-
126-
string CONNECTIONS = context.EnvironmentVariables.GetValueOrDefault("CONNECTIONS")?.ToString() ?? string.Empty;
127-
if (string.IsNullOrEmpty(CONNECTIONS))
128-
{
129-
context.EnvironmentVariables["CONNECTIONS"] = connections;
130-
}
131-
else
132-
{
133-
context.EnvironmentVariables["CONNECTIONS"] += $",{connections}";
134-
}
122+
context.EnvironmentVariables["CONNECTIONS"] = name;
135123
}
136124
}
137125

src/CommunityToolkit.Aspire.Hosting.PostgreSQL.Extensions/PostgresBuilderExtensions.cs

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ public static IResourceBuilder<PostgresServerResource> WithDbGate(this IResource
3939
{
4040
ArgumentNullException.ThrowIfNull(builder);
4141

42-
containerName ??= $"{builder.Resource.Name}-dbgate";
42+
containerName ??= "dbgate";
4343

4444
var dbGateBuilder = DbGateBuilderExtensions.AddDbGate(builder.ApplicationBuilder, containerName);
4545

4646
dbGateBuilder
47-
.WithEnvironment(context => ConfigureDbGateContainer(context, builder.ApplicationBuilder));
47+
.WithEnvironment(context => ConfigureDbGateContainer(context, builder))
48+
.WaitFor(builder);
4849

4950
configureContainer?.Invoke(dbGateBuilder);
5051

@@ -91,52 +92,39 @@ public static IResourceBuilder<PostgresServerResource> WithAdminer(this IResourc
9192
return builder;
9293
}
9394

94-
private static void ConfigureDbGateContainer(EnvironmentCallbackContext context, IDistributedApplicationBuilder applicationBuilder)
95+
private static void ConfigureDbGateContainer(EnvironmentCallbackContext context, IResourceBuilder<PostgresServerResource> builder)
9596
{
96-
var postgresInstances = applicationBuilder.Resources.OfType<PostgresServerResource>();
97+
var postgresServer = builder.Resource;
9798

98-
var counter = 1;
99+
var name = postgresServer.Name;
100+
var label = $"LABEL_{name}";
99101

100102
// Multiple WithDbGate calls will be ignored
101-
if (context.EnvironmentVariables.ContainsKey($"LABEL_postgres{counter}"))
103+
if (context.EnvironmentVariables.ContainsKey(label))
102104
{
103105
return;
104106
}
105107

106-
foreach (var postgresServer in postgresInstances)
108+
var userParameter = postgresServer.UserNameParameter is null
109+
? ReferenceExpression.Create($"postgres")
110+
: ReferenceExpression.Create($"{postgresServer.UserNameParameter}");
111+
112+
// DbGate assumes Postgres is being accessed over a default Aspire container network and hardcodes the resource address
113+
// This will need to be refactored once updated service discovery APIs are available
114+
context.EnvironmentVariables.Add($"LABEL_{name}", postgresServer.Name);
115+
context.EnvironmentVariables.Add($"SERVER_{name}", postgresServer.Name);
116+
context.EnvironmentVariables.Add($"USER_{name}", userParameter);
117+
context.EnvironmentVariables.Add($"PASSWORD_{name}", postgresServer.PasswordParameter);
118+
context.EnvironmentVariables.Add($"PORT_{name}", postgresServer.PrimaryEndpoint.TargetPort!.ToString()!);
119+
context.EnvironmentVariables.Add($"ENGINE_{name}", "postgres@dbgate-plugin-postgres");
120+
121+
if (context.EnvironmentVariables.GetValueOrDefault("CONNECTIONS") is string { Length: > 0 } connections)
107122
{
108-
var userParameter = postgresServer.UserNameParameter is null
109-
? ReferenceExpression.Create($"postgres")
110-
: ReferenceExpression.Create($"{postgresServer.UserNameParameter}");
111-
112-
// DbGate assumes Postgres is being accessed over a default Aspire container network and hardcodes the resource address
113-
// This will need to be refactored once updated service discovery APIs are available
114-
context.EnvironmentVariables.Add($"LABEL_postgres{counter}", postgresServer.Name);
115-
context.EnvironmentVariables.Add($"SERVER_postgres{counter}", postgresServer.Name);
116-
context.EnvironmentVariables.Add($"USER_postgres{counter}", userParameter);
117-
context.EnvironmentVariables.Add($"PASSWORD_postgres{counter}", postgresServer.PasswordParameter);
118-
context.EnvironmentVariables.Add($"PORT_postgres{counter}", postgresServer.PrimaryEndpoint.TargetPort!.ToString()!);
119-
context.EnvironmentVariables.Add($"ENGINE_postgres{counter}", "postgres@dbgate-plugin-postgres");
120-
121-
counter++;
123+
context.EnvironmentVariables["CONNECTIONS"] = $"{connections},{name}";
122124
}
123-
124-
var instancesCount = postgresInstances.Count();
125-
if (instancesCount > 0)
125+
else
126126
{
127-
var strBuilder = new StringBuilder();
128-
strBuilder.AppendJoin(',', Enumerable.Range(1, instancesCount).Select(i => $"postgres{i}"));
129-
var connections = strBuilder.ToString();
130-
131-
string CONNECTIONS = context.EnvironmentVariables.GetValueOrDefault("CONNECTIONS")?.ToString() ?? string.Empty;
132-
if (string.IsNullOrEmpty(CONNECTIONS))
133-
{
134-
context.EnvironmentVariables["CONNECTIONS"] = connections;
135-
}
136-
else
137-
{
138-
context.EnvironmentVariables["CONNECTIONS"] += $",{connections}";
139-
}
127+
context.EnvironmentVariables["CONNECTIONS"] = name;
140128
}
141129
}
142130

src/CommunityToolkit.Aspire.Hosting.Redis.Extensions/RedisBuilderExtensions.cs

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -36,61 +36,42 @@ public static IResourceBuilder<RedisResource> WithDbGate(this IResourceBuilder<R
3636
{
3737
ArgumentNullException.ThrowIfNull(builder);
3838

39-
containerName ??= $"{builder.Resource.Name}-dbgate";
39+
containerName ??= "dbgate";
4040

41-
var dbGateBuilder = DbGateBuilderExtensions.AddDbGate(builder.ApplicationBuilder, containerName);
41+
var dbGateBuilder = builder.ApplicationBuilder.AddDbGate(containerName);
4242

4343
dbGateBuilder
44-
.WithEnvironment(context => ConfigureDbGateContainer(context, builder.ApplicationBuilder));
44+
.WithEnvironment(context => ConfigureDbGateContainer(context, builder))
45+
.WaitFor(builder);
4546

4647
configureContainer?.Invoke(dbGateBuilder);
4748

4849
return builder;
4950
}
5051

51-
private static void ConfigureDbGateContainer(EnvironmentCallbackContext context, IDistributedApplicationBuilder applicationBuilder)
52+
private static void ConfigureDbGateContainer(EnvironmentCallbackContext context, IResourceBuilder<RedisResource> builder)
5253
{
53-
var reidsInstances = applicationBuilder.Resources.OfType<RedisResource>();
54+
var redisResource = builder.Resource;
5455

55-
var counter = 1;
56+
var name = redisResource.Name;
57+
var lalbel = $"LABEL_{name}";
5658

57-
// Multiple WithDbGate calls will be ignored
58-
if (context.EnvironmentVariables.ContainsKey($"LABEL_redis{counter}"))
59-
{
60-
return;
61-
}
62-
63-
foreach (var redisResource in reidsInstances)
64-
{
59+
// DbGate assumes Redis is being accessed over a default Aspire container network and hardcodes the resource address
60+
var redisUrl = redisResource.PasswordParameter is not null ?
61+
ReferenceExpression.Create($"redis://:{redisResource.PasswordParameter}@{name}:{redisResource.PrimaryEndpoint.TargetPort?.ToString()}") :
62+
ReferenceExpression.Create($"redis://{name}:{redisResource.PrimaryEndpoint.TargetPort?.ToString()}");
6563

66-
// DbGate assumes Redis is being accessed over a default Aspire container network and hardcodes the resource address
67-
var redisUrl = redisResource.PasswordParameter is not null ?
68-
ReferenceExpression.Create($"redis://:{redisResource.PasswordParameter}@{redisResource.Name}:{redisResource.PrimaryEndpoint.TargetPort?.ToString()}") :
69-
ReferenceExpression.Create($"redis://{redisResource.Name}:{redisResource.PrimaryEndpoint.TargetPort?.ToString()}");
64+
context.EnvironmentVariables.Add(lalbel, name);
65+
context.EnvironmentVariables.Add($"URL_{name}", redisUrl);
66+
context.EnvironmentVariables.Add($"ENGINE_{name}", "redis@dbgate-plugin-redis");
7067

71-
context.EnvironmentVariables.Add($"LABEL_redis{counter}", redisResource.Name);
72-
context.EnvironmentVariables.Add($"URL_redis{counter}", redisUrl);
73-
context.EnvironmentVariables.Add($"ENGINE_redis{counter}", "redis@dbgate-plugin-redis");
74-
75-
counter++;
68+
if (context.EnvironmentVariables.GetValueOrDefault("CONNECTIONS") is string { Length: > 0 } connections)
69+
{
70+
context.EnvironmentVariables["CONNECTIONS"] = $"{connections},{name}";
7671
}
77-
78-
var instancesCount = reidsInstances.Count();
79-
if (instancesCount > 0)
72+
else
8073
{
81-
var strBuilder = new StringBuilder();
82-
strBuilder.AppendJoin(',', Enumerable.Range(1, instancesCount).Select(i => $"redis{i}"));
83-
var connections = strBuilder.ToString();
84-
85-
string CONNECTIONS = context.EnvironmentVariables.GetValueOrDefault("CONNECTIONS")?.ToString() ?? string.Empty;
86-
if (string.IsNullOrEmpty(CONNECTIONS))
87-
{
88-
context.EnvironmentVariables["CONNECTIONS"] = connections;
89-
}
90-
else
91-
{
92-
context.EnvironmentVariables["CONNECTIONS"] += $",{connections}";
93-
}
74+
context.EnvironmentVariables["CONNECTIONS"] = name;
9475
}
9576
}
9677
}

0 commit comments

Comments
 (0)