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

Add InputAnnotation #2546

Merged
merged 3 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions src/Aspire.Hosting/ApplicationModel/InputAnnotation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using Aspire.Hosting.Publishing;

namespace Aspire.Hosting.ApplicationModel;

/// <summary>
/// Represents a input annotation that describes an input value.
/// </summary>
/// <remarks>
/// This class is used to specify generated passwords, usernames, etc.
/// </remarks>
[DebuggerDisplay("Type = {GetType().Name,nq}, Name = {Name}")]
public sealed class InputAnnotation : IResourceAnnotation
{
/// <summary>
/// Initializes a new instance of <see cref="InputAnnotation"/>.
/// </summary>
/// <param name="name">The name of the input.</param>
/// <param name="type">An optional type name of the input. "string" is the default, if not specified.</param>
/// <param name="secret">A flag indicating whether the input is secret.</param>
public InputAnnotation(string name, string? type = null, bool secret = false)
{
Name = name;
Type = type ?? "string";
Secret = secret;
}

/// <summary>
/// Name of the input.
/// </summary>
public string Name { get; set; }

/// <summary>
/// The type of the input.
/// </summary>
public string Type { get; set; }

/// <summary>
/// Indicates if the input is a secret.
/// </summary>
public bool Secret { get; set; }

/// <summary>
/// Represents how the default value of the input should be retrieved.
/// </summary>
public InputDefault? Default { get; set; }
}

/// <summary>
/// Represents how a default value should be retrieved.
/// </summary>
public abstract class InputDefault
{
/// <summary>
/// Writes the current <see cref="InputDefault"/> to the manifest context.
/// </summary>
/// <param name="context">The context for the manifest publishing operation.</param>
public abstract void WriteToManifest(ManifestPublishingContext context);
}

/// <summary>
/// Represents that a default value should be generated.
/// </summary>
public sealed class GenerateInputDefault : InputDefault
{
/// <summary>
/// The minimum length of the generated value.
/// </summary>
public int MinLength { get; set; }

/// <inheritdoc/>
public override void WriteToManifest(ManifestPublishingContext context)
{
context.Writer.WriteStartObject("generate");
context.Writer.WriteNumber("minLength", MinLength);
context.Writer.WriteEndObject();
}
}
20 changes: 20 additions & 0 deletions src/Aspire.Hosting/ApplicationModel/InputAnnotationExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Aspire.Hosting.ApplicationModel;

/// <summary>
/// Provides extension methods for <see cref="InputAnnotation"/>.
/// </summary>
internal static class InputAnnotationExtensions
{
internal static IResourceBuilder<T> WithDefaultPassword<T>(this IResourceBuilder<T> builder) where T : IResource
{
builder.WithAnnotation(new InputAnnotation("password", secret: true)
{
Default = new GenerateInputDefault { MinLength = 10 }
});

return builder;
}
}
25 changes: 19 additions & 6 deletions src/Aspire.Hosting/ApplicationModel/ParameterResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,33 @@ namespace Aspire.Hosting.ApplicationModel;
/// <summary>
/// Represents a parameter resource.
/// </summary>
/// <param name="name">The name of the parameter resource.</param>
/// <param name="callback">The callback function to retrieve the value of the parameter.</param>
/// <param name="secret">A flag indicating whether the parameter is secret.</param>
public sealed class ParameterResource(string name, Func<string> callback, bool secret = false) : Resource(name)
public sealed class ParameterResource : Resource
{
private readonly Func<string> _callback;

/// <summary>
/// Initializes a new instance of <see cref="ParameterResource"/>.
/// </summary>
/// <param name="name">The name of the parameter resource.</param>
/// <param name="callback">The callback function to retrieve the value of the parameter.</param>
/// <param name="secret">A flag indicating whether the parameter is secret.</param>
public ParameterResource(string name, Func<string> callback, bool secret = false) : base(name)
{
_callback = callback;
Secret = secret;

Annotations.Add(new InputAnnotation("value", secret: secret));
}

/// <summary>
/// Gets the value of the parameter.
/// </summary>
public string Value => callback();
public string Value => _callback();

/// <summary>
/// Gets a value indicating whether the parameter is secret.
/// </summary>
public bool Secret { get; } = secret;
public bool Secret { get; }

/// <summary>
/// Gets the expression used in the manifest to reference the value of the parameter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,7 @@ private static void WriteParameterResourceToManifest(ManifestPublishingContext c
}

context.Writer.WriteString("value", $"{{{resource.Name}.inputs.value}}");
context.Writer.WriteStartObject("inputs");
context.Writer.WriteStartObject("value");
context.Writer.WriteString("type", "string");

if (resource.Secret)
{
context.Writer.WriteBoolean("secret", resource.Secret);
}

context.Writer.WriteEndObject();
context.Writer.WriteEndObject();
context.WriteInputs(resource);
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion src/Aspire.Hosting/MySql/MySqlBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static IResourceBuilder<MySqlServerResource> AddMySql(this IDistributedAp
return builder.AddResource(resource)
.WithAnnotation(new EndpointAnnotation(ProtocolType.Tcp, port: port, containerPort: 3306)) // Internal port is always 3306.
.WithAnnotation(new ContainerImageAnnotation { Image = "mysql", Tag = "8.3.0" })
.WithDefaultPassword()
.WithEnvironment(context =>
{
if (context.ExecutionContext.IsPublishMode)
Expand Down Expand Up @@ -99,6 +100,6 @@ public static IResourceBuilder<T> WithPhpMyAdmin<T>(this IResourceBuilder<T> bui
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<MySqlServerResource> PublishAsContainer(this IResourceBuilder<MySqlServerResource> builder)
{
return builder.WithManifestPublishingCallback(builder.Resource.WriteToManifestAsync);
return builder.WithManifestPublishingCallback(context => context.WriteContainerAsync(builder.Resource));
}
}
18 changes: 0 additions & 18 deletions src/Aspire.Hosting/MySql/MySqlServerResource.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// 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.Publishing;
using Aspire.Hosting.Utils;

namespace Aspire.Hosting.ApplicationModel;
Expand Down Expand Up @@ -52,21 +51,4 @@ internal void AddDatabase(string name, string databaseName)
{
_databases.TryAdd(name, databaseName);
}

internal async Task WriteToManifestAsync(ManifestPublishingContext context)
{
await context.WriteContainerAsync(this).ConfigureAwait(false);

context.Writer.WriteStartObject("inputs"); // "inputs": {
context.Writer.WriteStartObject("password"); // "password": {
context.Writer.WriteString("type", "string"); // "type": "string",
context.Writer.WriteBoolean("secret", true); // "secret": true,
context.Writer.WriteStartObject("default"); // "default": {
context.Writer.WriteStartObject("generate"); // "generate": {
context.Writer.WriteNumber("minLength", 10); // "minLength": 10,
context.Writer.WriteEndObject(); // }
context.Writer.WriteEndObject(); // }
context.Writer.WriteEndObject(); // }
context.Writer.WriteEndObject(); // }
}
}
3 changes: 2 additions & 1 deletion src/Aspire.Hosting/Oracle/OracleDatabaseBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static IResourceBuilder<OracleDatabaseServerResource> AddOracleDatabase(t
return builder.AddResource(oracleDatabaseServer)
.WithAnnotation(new EndpointAnnotation(ProtocolType.Tcp, port: port, containerPort: 1521))
.WithAnnotation(new ContainerImageAnnotation { Image = "database/free", Tag = "23.3.0.0", Registry = "container-registry.oracle.com" })
.WithDefaultPassword()
.WithEnvironment(context =>
{
if (context.ExecutionContext.IsPublishMode)
Expand Down Expand Up @@ -69,6 +70,6 @@ public static IResourceBuilder<OracleDatabaseResource> AddDatabase(this IResourc
/// <returns></returns>
public static IResourceBuilder<OracleDatabaseServerResource> PublishAsContainer(this IResourceBuilder<OracleDatabaseServerResource> builder)
{
return builder.WithManifestPublishingCallback(builder.Resource.WriteToManifestAsync);
return builder.WithManifestPublishingCallback(context => context.WriteContainerAsync(builder.Resource));
}
}
18 changes: 0 additions & 18 deletions src/Aspire.Hosting/Oracle/OracleDatabaseServerResource.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// 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.Publishing;
using Aspire.Hosting.Utils;

namespace Aspire.Hosting.ApplicationModel;
Expand Down Expand Up @@ -52,21 +51,4 @@ internal void AddDatabase(string name, string databaseName)
{
_databases.TryAdd(name, databaseName);
}

internal async Task WriteToManifestAsync(ManifestPublishingContext context)
{
await context.WriteContainerAsync(this).ConfigureAwait(false);

context.Writer.WriteStartObject("inputs"); // "inputs": {
context.Writer.WriteStartObject("password"); // "password": {
context.Writer.WriteString("type", "string"); // "type": "string",
context.Writer.WriteBoolean("secret", true); // "secret": true,
context.Writer.WriteStartObject("default"); // "default": {
context.Writer.WriteStartObject("generate"); // "generate": {
context.Writer.WriteNumber("minLength", 10); // "minLength": 10,
context.Writer.WriteEndObject(); // }
context.Writer.WriteEndObject(); // }
context.Writer.WriteEndObject(); // }
context.Writer.WriteEndObject(); // }
}
}
3 changes: 2 additions & 1 deletion src/Aspire.Hosting/Postgres/PostgresBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static IResourceBuilder<PostgresServerResource> AddPostgres(this IDistrib
return builder.AddResource(postgresServer)
.WithAnnotation(new EndpointAnnotation(ProtocolType.Tcp, port: port, containerPort: 5432)) // Internal port is always 5432.
.WithAnnotation(new ContainerImageAnnotation { Image = "postgres", Tag = "16.2" })
.WithDefaultPassword()
.WithEnvironment("POSTGRES_HOST_AUTH_METHOD", "scram-sha-256")
.WithEnvironment("POSTGRES_INITDB_ARGS", "--auth-host=scram-sha-256 --auth-local=scram-sha-256")
.WithEnvironment(context =>
Expand Down Expand Up @@ -113,6 +114,6 @@ private static void SetPgAdminEnvironmentVariables(EnvironmentCallbackContext co
/// <returns></returns>
public static IResourceBuilder<PostgresServerResource> PublishAsContainer(this IResourceBuilder<PostgresServerResource> builder)
{
return builder.WithManifestPublishingCallback(builder.Resource.WriteToManifestAsync);
return builder.WithManifestPublishingCallback(context => context.WriteContainerAsync(builder.Resource));
}
}
18 changes: 0 additions & 18 deletions src/Aspire.Hosting/Postgres/PostgresServerResource.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// 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.Publishing;
using Aspire.Hosting.Utils;

namespace Aspire.Hosting.ApplicationModel;
Expand Down Expand Up @@ -82,21 +81,4 @@ internal void AddDatabase(string name, string databaseName)
{
_databases.TryAdd(name, databaseName);
}

internal async Task WriteToManifestAsync(ManifestPublishingContext context)
{
await context.WriteContainerAsync(this).ConfigureAwait(false);

context.Writer.WriteStartObject("inputs"); // "inputs": {
context.Writer.WriteStartObject("password"); // "password": {
context.Writer.WriteString("type", "string"); // "type": "string",
context.Writer.WriteBoolean("secret", true); // "secret": true,
context.Writer.WriteStartObject("default"); // "default": {
context.Writer.WriteStartObject("generate"); // "generate": {
context.Writer.WriteNumber("minLength", 10); // "minLength": 10,
context.Writer.WriteEndObject(); // }
context.Writer.WriteEndObject(); // }
context.Writer.WriteEndObject(); // }
context.Writer.WriteEndObject(); // }
}
}
33 changes: 33 additions & 0 deletions src/Aspire.Hosting/Publishing/ManifestPublishingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public async Task WriteContainerAsync(ContainerResource container)

await WriteEnvironmentVariablesAsync(container).ConfigureAwait(false);
WriteBindings(container, emitContainerPort: true);
WriteInputs(container);
}

/// <summary>
Expand Down Expand Up @@ -185,6 +186,38 @@ public async Task WriteEnvironmentVariablesAsync(IResource resource)
}
}

/// <summary>
/// Writes the "inputs" annotations for the underlying resource.
/// </summary>
/// <param name="resource">The resource to write inputs for.</param>
public void WriteInputs(IResource resource)
{
if (resource.TryGetAnnotationsOfType<InputAnnotation>(out var inputs))
{
Writer.WriteStartObject("inputs");
foreach (var input in inputs)
{
Writer.WriteStartObject(input.Name);
Writer.WriteString("type", input.Type);

if (input.Secret)
{
Writer.WriteBoolean("secret", true);
}

if (input.Default is not null)
{
Writer.WriteStartObject("default");
input.Default.WriteToManifest(this);
Writer.WriteEndObject();
}

Writer.WriteEndObject();
}
Writer.WriteEndObject();
}
}

/// <summary>
/// TODO: Doc Comments
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion src/Aspire.Hosting/RabbitMQ/RabbitMQBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static IResourceBuilder<RabbitMQServerResource> AddRabbitMQ(this IDistrib
return builder.AddResource(rabbitMq)
.WithAnnotation(new EndpointAnnotation(ProtocolType.Tcp, port: port, containerPort: 5672))
.WithAnnotation(new ContainerImageAnnotation { Image = "rabbitmq", Tag = "3" })
.WithDefaultPassword()
.WithEnvironment("RABBITMQ_DEFAULT_USER", "guest")
.WithEnvironment(context =>
{
Expand All @@ -49,6 +50,6 @@ public static IResourceBuilder<RabbitMQServerResource> AddRabbitMQ(this IDistrib
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<RabbitMQServerResource> PublishAsContainer(this IResourceBuilder<RabbitMQServerResource> builder)
{
return builder.WithManifestPublishingCallback(builder.Resource.WriteToManifestAsync);
return builder.WithManifestPublishingCallback(context => context.WriteContainerAsync(builder.Resource));
}
}
19 changes: 0 additions & 19 deletions src/Aspire.Hosting/RabbitMQ/RabbitMQServerResource.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// 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.Publishing;

namespace Aspire.Hosting.ApplicationModel;

/// <summary>
Expand Down Expand Up @@ -37,21 +35,4 @@ public class RabbitMQServerResource(string name, string password) : ContainerRes
var endpoint = allocatedEndpoints.Where(a => a.Name != "management").Single();
return $"amqp://guest:{Password}@{endpoint.EndPointString}";
}

internal async Task WriteToManifestAsync(ManifestPublishingContext context)
{
await context.WriteContainerAsync(this).ConfigureAwait(false);

context.Writer.WriteStartObject("inputs"); // "inputs": {
context.Writer.WriteStartObject("password"); // "password": {
context.Writer.WriteString("type", "string"); // "type": "string",
context.Writer.WriteBoolean("secret", true); // "secret": true,
context.Writer.WriteStartObject("default"); // "default": {
context.Writer.WriteStartObject("generate"); // "generate": {
context.Writer.WriteNumber("minLength", 10); // "minLength": 10,
context.Writer.WriteEndObject(); // }
context.Writer.WriteEndObject(); // }
context.Writer.WriteEndObject(); // }
context.Writer.WriteEndObject(); // }
}
}
Loading