File: OracleDatabaseServerResource.cs
Web Access
Project: src\src\Aspire.Hosting.Oracle\Aspire.Hosting.Oracle.csproj (Aspire.Hosting.Oracle)
// 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 an Oracle Database container.
/// </summary>
public class OracleDatabaseServerResource : ContainerResource, IResourceWithConnectionString
{
    internal const string PrimaryEndpointName = "tcp";
    private const string DefaultUserName = "system";
 
    /// <summary>
    /// Initializes a new instance of the <see cref="OracleDatabaseServerResource"/> class.
    /// </summary>
    /// <param name="name">The name of the resource.</param>
    /// <param name="password">A parameter that contains the Oracle Database server password.</param>
    public OracleDatabaseServerResource(string name, ParameterResource password) : base(name)
    {
        ArgumentNullException.ThrowIfNull(password);
 
        PrimaryEndpoint = new(this, PrimaryEndpointName);
        PasswordParameter = password;
    }
 
    /// <summary>
    /// Gets the primary endpoint for the Oracle server.
    /// </summary>
    public EndpointReference PrimaryEndpoint { get; }
 
    /// <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 Oracle Database server password.
    /// </summary>
    public ParameterResource PasswordParameter { get; }
 
    /// <summary>
    /// Gets the connection string expression for the Oracle Database server.
    /// </summary>
    public ReferenceExpression ConnectionStringExpression =>
        ReferenceExpression.Create(
            $"user id={DefaultUserName};password={PasswordParameter};data source={PrimaryEndpoint.Property(EndpointProperty.HostAndPort)}");
 
    /// <summary>
    /// Gets a reference to the user name for the Oracle server.
    /// </summary>
    /// <remarks>
    /// Returns the user name parameter if specified, otherwise returns the default user name "system".
    /// </remarks>
    public ReferenceExpression UserNameReference => ReferenceExpression.Create($"{DefaultUserName}");
 
    internal ReferenceExpression BuildJdbcConnectionString(string? databaseName = null)
    {
        var builder = new ReferenceExpressionBuilder();
        builder.AppendLiteral("jdbc:oracle:thin:");
        builder.Append($"{UserNameReference:uri}");
        builder.AppendLiteral("/");
        builder.Append($"{PasswordParameter:uri}");
        builder.AppendLiteral("@//");
        builder.Append($"{Host}");
        builder.AppendLiteral(":");
        builder.Append($"{Port}");
 
        if (!string.IsNullOrEmpty(databaseName))
        {
            var databaseNameExpression = ReferenceExpression.Create($"{databaseName}");
            builder.Append($"/{databaseNameExpression:uri}");
        }
 
        return builder.Build();
    }
    /// <summary>
    /// Gets the JDBC connection string for the Oracle Database server.
    /// </summary>
    /// <remarks>
    /// Format: <c>jdbc:oracle:thin:{user}/{password}@//{host}:{port}</c>.
    /// </remarks>
    public ReferenceExpression JdbcConnectionString => BuildJdbcConnectionString();
 
    private readonly Dictionary<string, string> _databases = new(StringComparers.ResourceName);
 
    /// <summary>
    /// A dictionary where the key is the resource name and the value is the database name.
    /// </summary>
    public IReadOnlyDictionary<string, string> Databases => _databases;
 
    internal void AddDatabase(string name, string databaseName)
    {
        _databases.TryAdd(name, databaseName);
    }
 
    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("JdbcConnectionString", JdbcConnectionString);
    }
}