-
Notifications
You must be signed in to change notification settings - Fork 321
/
SqlServerServerResource.cs
82 lines (68 loc) · 3.15 KB
/
SqlServerServerResource.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// 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 SQL Server container.
/// </summary>
public class SqlServerServerResource : ContainerResource, IResourceWithConnectionString
{
internal const string PrimaryEndpointName = "tcp";
/// <summary>
/// Initializes a new instance of the <see cref="SqlServerServerResource"/> class.
/// </summary>
/// <param name="name">The name of the resource.</param>
/// <param name="password">A parameter that contains the SQL Sever password.</param>
public SqlServerServerResource(string name, ParameterResource password) : base(name)
{
ArgumentNullException.ThrowIfNull(password);
PrimaryEndpoint = new(this, PrimaryEndpointName);
PasswordParameter = password;
}
/// <summary>
/// Gets the primary endpoint for the Redis server.
/// </summary>
public EndpointReference PrimaryEndpoint { get; }
/// <summary>
/// Gets the parameter that contains the PostgreSQL server password.
/// </summary>
public ParameterResource PasswordParameter { get; }
private ReferenceExpression ConnectionString =>
ReferenceExpression.Create(
$"Server={PrimaryEndpoint.Property(EndpointProperty.IPV4Host)},{PrimaryEndpoint.Property(EndpointProperty.Port)};User ID=sa;Password={PasswordParameter};TrustServerCertificate=true");
/// <summary>
/// Gets the connection string expression for the SQL Server.
/// </summary>
public ReferenceExpression ConnectionStringExpression
{
get
{
if (this.TryGetLastAnnotation<ConnectionStringRedirectAnnotation>(out var connectionStringAnnotation))
{
return connectionStringAnnotation.Resource.ConnectionStringExpression;
}
return ConnectionString;
}
}
/// <summary>
/// Gets the connection string for the SQL Server.
/// </summary>
/// <param name="cancellationToken"> A <see cref="CancellationToken"/> to observe while waiting for the task to complete.</param>
/// <returns>A connection string for the SQL Server in the form "Server=host,port;User ID=sa;Password=password;TrustServerCertificate=true".</returns>
public ValueTask<string?> GetConnectionStringAsync(CancellationToken cancellationToken = default)
{
if (this.TryGetLastAnnotation<ConnectionStringRedirectAnnotation>(out var connectionStringAnnotation))
{
return connectionStringAnnotation.Resource.GetConnectionStringAsync(cancellationToken);
}
return ConnectionString.GetValueAsync(cancellationToken);
}
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);
}
}