Skip to content

Commit 1e6ab4f

Browse files
commit
1 parent 10aac80 commit 1e6ab4f

File tree

5 files changed

+88
-1
lines changed

5 files changed

+88
-1
lines changed

examples/src/YC/CmdOptions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using CommandLine;
2+
3+
namespace YcCloud;
4+
5+
internal class CmdOptions
6+
{
7+
[Option('h', "host", Required = true, HelpText = "Database host")]
8+
public string Host { get; set; } = null!;
9+
10+
[Option('d', "database", Required = true, HelpText = "Database name")]
11+
public string Database { get; set; } = null!;
12+
13+
[Option("saFilePath", Required = true, HelpText = "Sa Key")]
14+
public string SaFilePath { get; set; } = null!;
15+
}

examples/src/YC/Program.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using CommandLine;
2+
using Microsoft.Extensions.Logging;
3+
using YcCloud;
4+
using Ydb.Sdk.Ado;
5+
using Ydb.Sdk.Yc;
6+
7+
await Parser.Default.ParseArguments<CmdOptions>(args).WithParsedAsync(async cmd =>
8+
{
9+
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole().SetMinimumLevel(LogLevel.Information));
10+
11+
var saProvider = new ServiceAccountProvider(saFilePath: cmd.SaFilePath, loggerFactory: loggerFactory);
12+
await saProvider.Initialize();
13+
14+
var cert = YcCerts.GetDefaultServerCertificate();
15+
16+
var builder = new YdbConnectionStringBuilder
17+
{
18+
UseTls = true,
19+
Host = cmd.Host,
20+
Port = 2135,
21+
Database = cmd.Database,
22+
CredentialsProvider = saProvider,
23+
LoggerFactory = loggerFactory,
24+
CustomCertificate = cert
25+
};
26+
27+
await using var ydbConnection = new YdbConnection(builder);
28+
await ydbConnection.OpenAsync();
29+
30+
Console.WriteLine(await new YdbCommand(ydbConnection) { CommandText = "SELECT 'Hello YDB!'u" }.ExecuteScalarAsync());
31+
});

examples/src/YC/YC.csproj

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<RootNamespace>YcCloud</RootNamespace>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="CommandLineParser" Version="2.8.0" />
13+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
14+
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
15+
<PackageReference Include="Ydb.Protos" Version="1.1.1" />
16+
</ItemGroup>
17+
<ItemGroup>
18+
<ProjectReference Include="..\..\..\src\Ydb.Sdk\src\Ydb.Sdk.csproj" />
19+
<ProjectReference Include="..\..\..\..\ydb-dotnet-yc\src\Ydb.Sdk.Yc.Auth\src\Ydb.Sdk.Yc.Auth.csproj" />
20+
</ItemGroup>
21+
</Project>

examples/src/YdbExamples.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdoNet", "AdoNet\AdoNet.csp
1313
EndProject
1414
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DapperExample", "DapperExample\DapperExample.csproj", "{AC8F1B10-31EB-4A29-BF35-7F49B06E1FA8}"
1515
EndProject
16+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YC", "YC\YC.csproj", "{753E4F33-CB08-47B9-864F-4CC037B278C4}"
17+
EndProject
1618
Global
1719
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1820
Debug|Any CPU = Debug|Any CPU
@@ -39,6 +41,10 @@ Global
3941
{AC8F1B10-31EB-4A29-BF35-7F49B06E1FA8}.Debug|Any CPU.Build.0 = Debug|Any CPU
4042
{AC8F1B10-31EB-4A29-BF35-7F49B06E1FA8}.Release|Any CPU.ActiveCfg = Release|Any CPU
4143
{AC8F1B10-31EB-4A29-BF35-7F49B06E1FA8}.Release|Any CPU.Build.0 = Release|Any CPU
44+
{753E4F33-CB08-47B9-864F-4CC037B278C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
45+
{753E4F33-CB08-47B9-864F-4CC037B278C4}.Debug|Any CPU.Build.0 = Debug|Any CPU
46+
{753E4F33-CB08-47B9-864F-4CC037B278C4}.Release|Any CPU.ActiveCfg = Release|Any CPU
47+
{753E4F33-CB08-47B9-864F-4CC037B278C4}.Release|Any CPU.Build.0 = Release|Any CPU
4248
EndGlobalSection
4349
GlobalSection(SolutionProperties) = preSolution
4450
HideSolutionNode = FALSE

src/Ydb.Sdk/src/Ado/YdbConnectionStringBuilder.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,19 @@ public string? RootCertificate
141141

142142
public ICredentialsProvider? CredentialsProvider { get; set; }
143143

144+
public X509Certificate? CustomCertificate
145+
{
146+
get => _customCertificate;
147+
set
148+
{
149+
RootCertificate = null;
150+
151+
_customCertificate = value;
152+
}
153+
}
154+
155+
private X509Certificate? _customCertificate;
156+
144157
private void SaveValue(string propertyName, object? value)
145158
{
146159
if (value == null)
@@ -183,7 +196,8 @@ internal Task<Driver> BuildDriver()
183196
{
184197
var credentialsProvider = CredentialsProvider ??
185198
(User != null ? new StaticCredentialsProvider(User, Password) : null);
186-
var cert = RootCertificate != null ? X509Certificate.CreateFromCertFile(RootCertificate) : null;
199+
var cert = CustomCertificate ??
200+
(RootCertificate != null ? X509Certificate.CreateFromCertFile(RootCertificate) : null);
187201

188202
return Driver.CreateInitialized(new DriverConfig(
189203
endpoint: Endpoint,

0 commit comments

Comments
 (0)