Skip to content

Commit 73e852c

Browse files
committed
Password/user/port parameters api changed
1 parent df9bc1f commit 73e852c

File tree

4 files changed

+141
-31
lines changed

4 files changed

+141
-31
lines changed

src/CommunityToolkit.Aspire.Hosting.Minio/MinioBuilderExtensions.cs

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,16 @@ public static class MinioBuilderExtensions
1818
/// </summary>
1919
/// <param name="builder">The <see cref="IDistributedApplicationBuilder"/>.</param>
2020
/// <param name="name">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param>
21-
/// <param name="minioConsolePort">The host port for MinioO Admin.</param>
22-
/// <param name="minioPort">The host port for MiniO.</param>
23-
/// <param name="rootUser">The root user for the MiniO server.</param>
24-
/// <param name="rootPassword">The password for the MiniO root user.</param>
21+
/// <param name="port">The host port for MiniO.</param>
22+
/// <param name="rootUser">The parameter used to provide the root user name for the MiniO resource. If <see langword="null"/> a default value will be used.</param>
23+
/// <param name="rootPassword">The parameter used to provide the administrator password for the MiniO resource. If <see langword="null"/> a random password will be generated.</param>
2524
/// <returns>A reference to the <see cref="IResourceBuilder{MinioContainerResource}"/>.</returns>
2625
public static IResourceBuilder<MinioContainerResource> AddMinioContainer(
2726
this IDistributedApplicationBuilder builder,
28-
string name,
27+
[ResourceName] string name,
2928
IResourceBuilder<ParameterResource>? rootUser = null,
3029
IResourceBuilder<ParameterResource>? rootPassword = null,
31-
int minioPort = 9000,
32-
int minioConsolePort = 9001)
30+
int? port = null)
3331
{
3432
ArgumentNullException.ThrowIfNull(builder);
3533
ArgumentException.ThrowIfNullOrEmpty(name);
@@ -39,18 +37,16 @@ public static IResourceBuilder<MinioContainerResource> AddMinioContainer(
3937

4038
var rootUserParameter = rootUser?.Resource ?? new ParameterResource("user", _ => MinioContainerResource.DefaultUserName);
4139

42-
var minioContainer = new MinioContainerResource(name, rootUserParameter, rootPasswordParameter);
40+
var resource = new MinioContainerResource(name, rootUserParameter, rootPasswordParameter);
4341

4442
var builderWithResource = builder
45-
.AddResource(minioContainer)
43+
.AddResource(resource)
4644
.WithImage(MinioContainerImageTags.Image, MinioContainerImageTags.Tag)
4745
.WithImageRegistry(MinioContainerImageTags.Registry)
48-
.WithHttpEndpoint(targetPort: 9000, port: minioPort, name: MinioContainerResource.PrimaryEndpointName)
49-
.WithHttpEndpoint(targetPort: 9001, port: minioConsolePort, name: "console")
50-
.WithEnvironment("MINIO_ADDRESS", $":{minioPort.ToString()}")
51-
.WithEnvironment("MINIO_CONSOLE_ADDRESS", $":{minioConsolePort.ToString()}")
52-
.WithEnvironment(RootUserEnvVarName, minioContainer.RootUser.Value)
53-
.WithEnvironment(RootPasswordEnvVarName, minioContainer.RootPassword.Value)
46+
.WithHttpEndpoint(targetPort: 9000, port: port, name: MinioContainerResource.PrimaryEndpointName)
47+
.WithHttpEndpoint(targetPort: 9001, name: MinioContainerResource.ConsoleEndpointName)
48+
.WithEnvironment(RootUserEnvVarName, resource.RootUser.Value)
49+
.WithEnvironment(RootPasswordEnvVarName, resource.PasswordParameter.Value)
5450
.WithArgs("server", "/data");
5551

5652
var endpoint = builderWithResource.Resource.GetEndpoint(MinioContainerResource.PrimaryEndpointName);
@@ -74,6 +70,52 @@ public static IResourceBuilder<MinioContainerResource> AddMinioContainer(
7470
return builderWithResource;
7571
}
7672

73+
74+
/// <summary>
75+
/// Configures the user name that the Minio resource uses.
76+
/// </summary>
77+
/// <param name="builder">The resource builder.</param>
78+
/// <param name="userName">The parameter used to provide the user name for the PostgreSQL resource.</param>
79+
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
80+
public static IResourceBuilder<MinioContainerResource> WithUserName(this IResourceBuilder<MinioContainerResource> builder, IResourceBuilder<ParameterResource> userName)
81+
{
82+
ArgumentNullException.ThrowIfNull(builder);
83+
ArgumentNullException.ThrowIfNull(userName);
84+
85+
builder.Resource.RootUser = userName.Resource;
86+
return builder;
87+
}
88+
89+
/// <summary>
90+
/// Configures the password that the MiniO resource is used.
91+
/// </summary>
92+
/// <param name="builder">The resource builder.</param>
93+
/// <param name="password">The parameter used to provide the password for the MiniO resource. If <see langword="null"/>, no password will be configured.</param>
94+
/// <returns>The <see cref="IResourceBuilder{T}"/>.</returns>
95+
public static IResourceBuilder<MinioContainerResource> WithPassword(this IResourceBuilder<MinioContainerResource> builder, IResourceBuilder<ParameterResource> password)
96+
{
97+
ArgumentNullException.ThrowIfNull(builder);
98+
99+
builder.Resource.SetPassword(password.Resource);
100+
return builder;
101+
}
102+
103+
/// <summary>
104+
/// Configures the host port that the PGAdmin resource is exposed on instead of using randomly assigned port.
105+
/// </summary>
106+
/// <param name="builder">The resource builder for PGAdmin.</param>
107+
/// <param name="port">The port to bind on the host. If <see langword="null"/> is used, a random port will be assigned.</param>
108+
/// <returns>The resource builder for PGAdmin.</returns>
109+
public static IResourceBuilder<MinioContainerResource> WithHostPort(this IResourceBuilder<MinioContainerResource> builder, int? port)
110+
{
111+
ArgumentNullException.ThrowIfNull(builder);
112+
113+
return builder.WithEndpoint("http", endpoint =>
114+
{
115+
endpoint.Port = port;
116+
});
117+
}
118+
77119
/// <summary>
78120
/// Adds a named volume for the data folder to a Minio container resource.
79121
/// </summary>

src/CommunityToolkit.Aspire.Hosting.Minio/MinioContainerResource.cs

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,33 @@
33
/// <summary>
44
/// A resource that represents a MiniO storage
55
/// </summary>
6-
/// <param name="name">The name of the resource</param>
7-
/// <param name="rootUser"> A parameter that contains the MiniO server admin user name, or null to</param>
8-
/// <param name="rootPassword"> A parameter that contains the Minio server admin password</param>
9-
public sealed class MinioContainerResource(
10-
string name,
11-
ParameterResource rootUser,
12-
ParameterResource rootPassword) : ContainerResource(name),
13-
IResourceWithConnectionString
6+
public sealed class MinioContainerResource : ContainerResource, IResourceWithConnectionString
147
{
158
internal const string PrimaryEndpointName = "http";
9+
internal const string ConsoleEndpointName = "console";
1610
internal const string DefaultUserName = "minioadmin";
1711

12+
/// <summary>
13+
/// Initializes a new instance of the <see cref="MinioContainerResource"/> class.
14+
/// </summary>
15+
/// <param name="name">The name of the resource.</param>
16+
/// <param name="user">A parameter that contains the Minio server root user name.</param>
17+
/// <param name="password">A parameter that contains the Minio server root password.</param>
18+
public MinioContainerResource(string name, ParameterResource user, ParameterResource password) : base(name)
19+
{
20+
RootUser = user;
21+
PasswordParameter = password;
22+
}
23+
1824
/// <summary>
1925
/// The MiniO root user.
2026
/// </summary>
21-
public ParameterResource RootUser { get; set; } = rootUser;
27+
public ParameterResource RootUser { get; set; }
2228

2329
/// <summary>
2430
/// The MiniO root password.
2531
/// </summary>
26-
public ParameterResource RootPassword { get; } = rootPassword;
32+
public ParameterResource PasswordParameter { get; set; }
2733

2834
private EndpointReference? _primaryEndpoint;
2935

@@ -36,17 +42,40 @@ public sealed class MinioContainerResource(
3642
/// Gets the connection string expression for the Minio
3743
/// </summary>
3844
public ReferenceExpression ConnectionStringExpression => GetConnectionString();
39-
45+
46+
/// <summary>
47+
/// Gets the connection string for the MiniO server.
48+
/// </summary>
49+
/// <param name="cancellationToken"> A <see cref="CancellationToken"/> to observe while waiting for the task to complete.</param>
50+
/// <returns>A connection string for the PostgreSQL server in the form "Host=host;Port=port;Username=postgres;Password=password".</returns>
51+
public ValueTask<string?> GetConnectionStringAsync(CancellationToken cancellationToken = default)
52+
{
53+
if (this.TryGetLastAnnotation<ConnectionStringRedirectAnnotation>(out var connectionStringAnnotation))
54+
{
55+
return connectionStringAnnotation.Resource.GetConnectionStringAsync(cancellationToken);
56+
}
57+
58+
return ConnectionStringExpression.GetValueAsync(cancellationToken);
59+
}
60+
61+
/// <summary>
62+
/// Gets the connection string for the MiniO server.
63+
/// </summary>
4064
private ReferenceExpression GetConnectionString()
4165
{
4266
var builder = new ReferenceExpressionBuilder();
4367

4468
builder.Append(
4569
$"Endpoint=http://{PrimaryEndpoint.Property(EndpointProperty.Host)}:{PrimaryEndpoint.Property(EndpointProperty.Port)}");
46-
70+
4771
builder.Append($";AccessKey={RootUser.Value}");
48-
builder.Append($";SecretKey={RootPassword.Value}");
72+
builder.Append($";SecretKey={PasswordParameter.Value}");
4973

5074
return builder.Build();
5175
}
76+
77+
internal void SetPassword(ParameterResource password)
78+
{
79+
PasswordParameter = password;
80+
}
5281
}

tests/CommunityToolkit.Aspire.Hosting.Minio.Tests/MinioFunctionalTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public async Task StorageGetsCreatedAndUsable()
2727
.AddMinioContainer("minio",
2828
distributedApplicationBuilder.AddParameter("username", rootUser),
2929
rootPasswordParameter,
30-
minioPort: port);
30+
port: port);
3131

3232
await using var app = await distributedApplicationBuilder.BuildAsync();
3333

@@ -77,7 +77,7 @@ public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume)
7777
var minio = builder1.AddMinioContainer("minio",
7878
builder1.AddParameter("username", rootUser),
7979
rootPasswordParameter,
80-
minioPort: port);
80+
port: port);
8181

8282
if (useVolume)
8383
{
@@ -134,7 +134,7 @@ public async Task WithDataShouldPersistStateBetweenUsages(bool useVolume)
134134
var minio2 = builder2.AddMinioContainer("minio",
135135
builder2.AddParameter("username", rootUser),
136136
rootPasswordParameter2,
137-
minioPort: port);
137+
port: port);
138138

139139
if (useVolume)
140140
{

tests/CommunityToolkit.Aspire.Hosting.Minio.Tests/MinioPublicApiTests.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,44 @@ public void WithDataBindMountShouldThrowWhenSourceIsNull()
6868
var exception = Assert.Throws<ArgumentNullException>(action);
6969
Assert.Equal(nameof(source), exception.ParamName);
7070
}
71+
72+
[Fact]
73+
public void VerifyMinioContainerResourceWithHostPort()
74+
{
75+
var builder = DistributedApplication.CreateBuilder();
76+
builder.AddMinioContainer("minio")
77+
.WithHostPort(1000);
78+
79+
var resource = Assert.Single(builder.Resources.OfType<MinioContainerResource>());
80+
var endpoint = Assert.Single(resource.Annotations.OfType<EndpointAnnotation>(), x => x.Name == "http");
81+
Assert.Equal(1000, endpoint.Port);
82+
}
83+
84+
[Fact]
85+
public async Task VerifyMinioContainerResourceWithPassword()
86+
{
87+
var builder = DistributedApplication.CreateBuilder();
88+
var password = "p@ssw0rd1";
89+
var pass = builder.AddParameter("pass", password);
90+
var minio = builder.AddMinioContainer("minio")
91+
.WithPassword(pass)
92+
.WithEndpoint("http", e => e.AllocatedEndpoint = new AllocatedEndpoint(e, "localhost", 2000));
93+
94+
var connectionString = await minio.Resource.GetConnectionStringAsync();
95+
Assert.Equal("Endpoint=http://localhost:2000;AccessKey=minioadmin;SecretKey=p@ssw0rd1", connectionString);
96+
}
7197

98+
[Fact]
99+
public async Task VerifyMinioContainerResourceWithUserName()
100+
{
101+
var builder = DistributedApplication.CreateBuilder();
102+
var user = "user1";
103+
var pass = builder.AddParameter("user", user);
104+
var postgres = builder.AddMinioContainer("minio")
105+
.WithUserName(pass)
106+
.WithEndpoint("http", e => e.AllocatedEndpoint = new AllocatedEndpoint(e, "localhost", 2000));
107+
108+
var connectionString = await postgres.Resource.GetConnectionStringAsync();
109+
Assert.Equal($"Endpoint=http://localhost:2000;AccessKey=user1;SecretKey={postgres.Resource.PasswordParameter.Value}", connectionString);
110+
}
72111
}

0 commit comments

Comments
 (0)