| File: RabbitMQServerResource.cs | Web Access |
| Project: src\src\Aspire.Hosting.RabbitMQ\Aspire.Hosting.RabbitMQ.csproj (Aspire.Hosting.RabbitMQ) |
// 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> /// A resource that represents a RabbitMQ resource. /// </summary> [AspireExport(ExposeProperties = true)] public class RabbitMQServerResource : ContainerResource, IResourceWithConnectionString, IResourceWithEnvironment { internal const string PrimaryEndpointName = "tcp"; internal const string ManagementEndpointName = "management"; private const string DefaultUserName = "guest"; private EndpointReference? _managementEndpoint; /// <summary> /// Initializes a new instance of the <see cref="RabbitMQServerResource"/> class. /// </summary> /// <param name="name">The name of the resource.</param> /// <param name="userName">A parameter that contains the RabbitMQ server user name, or <see langword="null"/> to use a default value.</param> /// <param name="password">A parameter that contains the RabbitMQ server password.</param> public RabbitMQServerResource(string name, ParameterResource? userName, ParameterResource password) : base(name) { ArgumentNullException.ThrowIfNull(password); PrimaryEndpoint = new(this, PrimaryEndpointName); UserNameParameter = userName; PasswordParameter = password; } /// <summary> /// Gets the primary endpoint for the RabbitMQ server. /// </summary> public EndpointReference PrimaryEndpoint { get; } /// <summary> /// Gets the management endpoint for the RabbitMQ server. /// </summary> public EndpointReference ManagementEndpoint => _managementEndpoint ??= new(this, ManagementEndpointName); /// <summary> /// Gets the host endpoint reference for this resource. /// </summary> public EndpointReferenceExpression Host => PrimaryEndpoint.Property(EndpointProperty.Host); /// <summary> /// Gets the port endpoint reference for this resource. /// </summary> public EndpointReferenceExpression Port => PrimaryEndpoint.Property(EndpointProperty.Port); /// <summary> /// Gets the parameter that contains the RabbitMQ server user name. /// </summary> public ParameterResource? UserNameParameter { get; } /// <summary> /// Gets a reference to the user name for the RabbitMQ server. /// </summary> /// <remarks> /// Returns the user name parameter if specified, otherwise returns the default user name "guest". /// </remarks> public ReferenceExpression UserNameReference => UserNameParameter is not null ? ReferenceExpression.Create($"{UserNameParameter}") : ReferenceExpression.Create($"{DefaultUserName}"); /// <summary> /// Gets the parameter that contains the RabbitMQ server password. /// </summary> public ParameterResource PasswordParameter { get; } /// <summary> /// Gets the connection string expression for the RabbitMQ server. /// </summary> public ReferenceExpression ConnectionStringExpression => UriExpression; /// <summary> /// Gets the connection URI expression for the RabbitMQ server. /// </summary> /// <remarks> /// Format: <c>amqp://{user}:{password}@{host}:{port}</c>. /// </remarks> public ReferenceExpression UriExpression { get { var builder = new ReferenceExpressionBuilder(); builder.AppendLiteral("amqp://"); if (UserNameParameter is not null) { builder.Append($"{UserNameParameter:uri}"); } else { builder.Append($"{DefaultUserName:uri}"); } builder.AppendLiteral(":"); builder.Append($"{PasswordParameter:uri}"); builder.AppendLiteral("@"); builder.Append($"{PrimaryEndpoint.Property(EndpointProperty.HostAndPort)}"); return builder.Build(); } } IEnumerable<KeyValuePair<string, ReferenceExpression>> IResourceWithConnectionString.GetConnectionProperties() { yield return new("Host", ReferenceExpression.Create($"{Host}")); yield return new("Port", ReferenceExpression.Create($"{Port}")); yield return new("Username", UserNameReference); yield return new("Password", ReferenceExpression.Create($"{PasswordParameter}")); yield return new("Uri", UriExpression); } }