Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce WithEnvironment overload for custom connection string keys #3002 #3239

Merged
merged 10 commits into from
Mar 29, 2024
20 changes: 20 additions & 0 deletions src/Aspire.Hosting/ResourceBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,26 @@ public static IResourceBuilder<T> WithEnvironment<T>(this IResourceBuilder<T> bu
});
}

/// <summary>
/// Adds an environment variable to the resource with the connection string from the referenced resource.
/// </summary>
/// <typeparam name="T">The destination resource type.</typeparam>
/// <param name="builder">The destination resource builder to which the environment variable will be added.</param>
/// <param name="envVarName">The name of the environment variable under which the connection string will be set.</param>
/// <param name="resource">The resource builder of the referenced service from which to pull the connection string.</param>
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<T> WithEnvironment<T>(
this IResourceBuilder<T> builder,
string envVarName,
IResourceBuilder<IResourceWithConnectionString> resource)
where T : IResourceWithEnvironment
{
return builder.WithEnvironment(context =>
{
context.EnvironmentVariables[envVarName] = new ConnectionStringReference(resource.Resource, optional: false);
});
}

/// <summary>
/// Adds the arguments to be passed to a container resource when the container is started.
/// </summary>
Expand Down
28 changes: 28 additions & 0 deletions tests/Aspire.Hosting.Tests/WithEnvironmentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,34 @@ public async Task EnvironmentVariableExpressions()
Assert.Equal("{test.connectionString};name=1", manifestConfig["HOST"]);
}

[Fact]
public async Task EnvironmentWithConnectionStringSetsProperEnvironmentVariable()
{
// Arrange
const string sourceCon = "sourceConnectionString";
using var testProgram = CreateTestProgram();
var sourceBuilder = testProgram.AppBuilder.AddResource(new TestResource("sourceService", sourceCon));
var targetBuilder = testProgram.AppBuilder.AddContainer("targetContainer", "targetImage");

string envVarName = "CUSTOM_CONNECTION_STRING";

// Act
targetBuilder.WithEnvironment(envVarName, sourceBuilder);
testProgram.Build();

// Call environment variable callbacks for the Run operation.
var runConfig = await EnvironmentVariableEvaluator.GetEnvironmentVariablesAsync(targetBuilder.Resource, DistributedApplicationOperation.Run);

// Assert
Assert.Single(runConfig, kvp => kvp.Key == envVarName && kvp.Value == sourceCon);

// Call environment variable callbacks for the Publish operation.
var publishConfig = await EnvironmentVariableEvaluator.GetEnvironmentVariablesAsync(targetBuilder.Resource, DistributedApplicationOperation.Publish);

// Assert
Assert.Single(publishConfig, kvp => kvp.Key == envVarName && kvp.Value == "{sourceService.connectionString}");
}

private sealed class TestResource(string name, string connectionString) : Resource(name), IResourceWithConnectionString
{
public ReferenceExpression ConnectionStringExpression =>
Expand Down