Skip to content

Commit 9fc2ef4

Browse files
feat EF: Supported Connect to YC
1 parent dd5de8b commit 9fc2ef4

File tree

9 files changed

+94
-15
lines changed

9 files changed

+94
-15
lines changed

examples/src/EF_YC/CmdOptions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using CommandLine;
2+
3+
namespace EF_YC;
4+
5+
internal class CmdOptions
6+
{
7+
[Option('c', "connectionString", Required = true, HelpText = "Connection string")]
8+
public string ConnectionString { get; set; } = null!;
9+
10+
[Option("saFilePath", Required = true, HelpText = "Sa Key")]
11+
public string SaFilePath { get; set; } = null!;
12+
}

examples/src/YC/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ await Parser.Default.ParseArguments<CmdOptions>(args).WithParsedAsync(async cmd
99
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole().SetMinimumLevel(LogLevel.Information));
1010

1111
var saProvider = new ServiceAccountProvider(saFilePath: cmd.SaFilePath, loggerFactory: loggerFactory);
12-
await saProvider.Initialize();
1312

1413
var builder = new YdbConnectionStringBuilder
1514
{

examples/src/YC/YC.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
1414
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
1515
<PackageReference Include="Ydb.Protos" Version="1.1.1" />
16-
<PackageReference Include="Ydb.Sdk.Yc.Auth" Version="0.1.0" />
16+
<PackageReference Include="Ydb.Sdk.Yc.Auth" Version="0.2.0" />
1717
</ItemGroup>
1818
<ItemGroup>
1919
<ProjectReference Include="..\..\..\src\Ydb.Sdk\src\Ydb.Sdk.csproj" />

examples/src/YdbExamples.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Topic", "Topic\Topic.csproj
1919
EndProject
2020
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EF", "EF\EF.csproj", "{0CE9DF93-1411-4E73-BA88-A66018FAB948}"
2121
EndProject
22+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EF_YC", "EF_YC\EF_YC.csproj", "{31A2E7BF-45BB-4C63-9DEC-58DD06491EF1}"
23+
EndProject
2224
Global
2325
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2426
Debug|Any CPU = Debug|Any CPU
@@ -57,6 +59,10 @@ Global
5759
{0CE9DF93-1411-4E73-BA88-A66018FAB948}.Debug|Any CPU.Build.0 = Debug|Any CPU
5860
{0CE9DF93-1411-4E73-BA88-A66018FAB948}.Release|Any CPU.ActiveCfg = Release|Any CPU
5961
{0CE9DF93-1411-4E73-BA88-A66018FAB948}.Release|Any CPU.Build.0 = Release|Any CPU
62+
{31A2E7BF-45BB-4C63-9DEC-58DD06491EF1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
63+
{31A2E7BF-45BB-4C63-9DEC-58DD06491EF1}.Debug|Any CPU.Build.0 = Debug|Any CPU
64+
{31A2E7BF-45BB-4C63-9DEC-58DD06491EF1}.Release|Any CPU.ActiveCfg = Release|Any CPU
65+
{31A2E7BF-45BB-4C63-9DEC-58DD06491EF1}.Release|Any CPU.Build.0 = Release|Any CPU
6066
EndGlobalSection
6167
GlobalSection(SolutionProperties) = preSolution
6268
HideSolutionNode = FALSE

src/EfCore.Ydb/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Supported connect to Yandex Cloud
Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
using System.Collections.Generic;
2+
using System.Security.Cryptography.X509Certificates;
23
using EfCore.Ydb.Extensions;
34
using Microsoft.EntityFrameworkCore.Infrastructure;
45
using Microsoft.Extensions.DependencyInjection;
6+
using Ydb.Sdk.Auth;
57

68
namespace EfCore.Ydb.Infrastructure.Internal;
79

810
public class YdbOptionsExtension : RelationalOptionsExtension
911
{
12+
public ICredentialsProvider? CredentialsProvider { get; private set; }
13+
14+
public X509Certificate2Collection? ServerCertificates { get; private set; }
15+
1016
private DbContextOptionsExtensionInfo? _info;
1117

1218
public YdbOptionsExtension()
@@ -15,22 +21,40 @@ public YdbOptionsExtension()
1521

1622
private YdbOptionsExtension(YdbOptionsExtension copyFrom) : base(copyFrom)
1723
{
24+
CredentialsProvider = copyFrom.CredentialsProvider;
25+
ServerCertificates = copyFrom.ServerCertificates;
26+
}
27+
28+
protected override RelationalOptionsExtension Clone() => new YdbOptionsExtension(this);
29+
30+
public override void ApplyServices(IServiceCollection services) => services.AddEntityFrameworkYdb();
31+
32+
public override DbContextOptionsExtensionInfo Info => _info ??= new ExtensionInfo(this);
33+
34+
public YdbOptionsExtension WithCredentialsProvider(ICredentialsProvider? credentialsProvider)
35+
{
36+
var clone = (YdbOptionsExtension)Clone();
37+
38+
clone.CredentialsProvider = credentialsProvider;
39+
40+
return clone;
1841
}
1942

20-
protected override RelationalOptionsExtension Clone()
21-
=> new YdbOptionsExtension(this);
43+
public YdbOptionsExtension WithServerCertificates(X509Certificate2Collection? serverCertificates)
44+
{
45+
var clone = (YdbOptionsExtension)Clone();
2246

23-
public override void ApplyServices(IServiceCollection services)
24-
=> services.AddEntityFrameworkYdb();
47+
clone.ServerCertificates = serverCertificates;
2548

26-
public override DbContextOptionsExtensionInfo Info
27-
=> _info ??= new ExtensionInfo(this);
49+
return clone;
50+
}
2851

29-
private sealed class ExtensionInfo(IDbContextOptionsExtension extension) : RelationalExtensionInfo(extension)
52+
private sealed class ExtensionInfo(YdbOptionsExtension extension) : RelationalExtensionInfo(extension)
3053
{
3154
public override bool IsDatabaseProvider => true;
3255

33-
// TODO: Right now it's stub
34-
public override void PopulateDebugInfo(IDictionary<string, string> debugInfo) => debugInfo["Hello"] = "World!";
56+
public override void PopulateDebugInfo(IDictionary<string, string> debugInfo)
57+
{
58+
}
3559
}
3660
}
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1+
using System;
2+
using System.Security.Cryptography.X509Certificates;
13
using EfCore.Ydb.Infrastructure.Internal;
24
using Microsoft.EntityFrameworkCore;
35
using Microsoft.EntityFrameworkCore.Infrastructure;
6+
using Ydb.Sdk.Ado;
7+
using Ydb.Sdk.Auth;
48

59
namespace EfCore.Ydb.Infrastructure;
610

711
public class YdbDbContextOptionsBuilder(DbContextOptionsBuilder optionsBuilder)
8-
: RelationalDbContextOptionsBuilder<YdbDbContextOptionsBuilder, YdbOptionsExtension>(optionsBuilder);
12+
: RelationalDbContextOptionsBuilder<YdbDbContextOptionsBuilder, YdbOptionsExtension>(optionsBuilder)
13+
{
14+
public YdbDbContextOptionsBuilder WithCredentialsProvider(ICredentialsProvider? credentialsProvider) =>
15+
WithOption(optionsBuilder => optionsBuilder.WithCredentialsProvider(credentialsProvider));
16+
17+
public YdbDbContextOptionsBuilder WithServerCertificates(X509Certificate2Collection? serverCertificates) =>
18+
WithOption(optionsBuilder => optionsBuilder.WithServerCertificates(serverCertificates));
19+
}

src/EfCore.Ydb/src/Storage/Internal/YdbRelationalConnection.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,38 @@
11
using System.Data.Common;
2+
using System.Security.Cryptography.X509Certificates;
23
using EfCore.Ydb.Extensions;
4+
using EfCore.Ydb.Infrastructure.Internal;
35
using Microsoft.EntityFrameworkCore;
46
using Microsoft.EntityFrameworkCore.Storage;
57
using Ydb.Sdk.Ado;
8+
using Ydb.Sdk.Auth;
69

710
namespace EfCore.Ydb.Storage.Internal;
811

9-
public class YdbRelationalConnection(RelationalConnectionDependencies dependencies)
10-
: RelationalConnection(dependencies), IYdbRelationalConnection
12+
public class YdbRelationalConnection : RelationalConnection, IYdbRelationalConnection
1113
{
12-
protected override DbConnection CreateDbConnection() => new YdbConnection(GetValidatedConnectionString());
14+
private readonly ICredentialsProvider? _credentialsProvider;
15+
private readonly X509Certificate2Collection? _serverCertificates;
16+
17+
public YdbRelationalConnection(RelationalConnectionDependencies dependencies) : base(dependencies)
18+
{
19+
var ydbOptionsExtension = dependencies.ContextOptions.FindExtension<YdbOptionsExtension>() ??
20+
new YdbOptionsExtension();
21+
22+
_credentialsProvider = ydbOptionsExtension.CredentialsProvider;
23+
_serverCertificates = ydbOptionsExtension.ServerCertificates;
24+
}
25+
26+
protected override DbConnection CreateDbConnection()
27+
{
28+
var ydbConnectionStringBuilder = new YdbConnectionStringBuilder(GetValidatedConnectionString())
29+
{
30+
CredentialsProvider = _credentialsProvider,
31+
ServerCertificates = _serverCertificates
32+
};
33+
34+
return new YdbConnection(ydbConnectionStringBuilder);
35+
}
1336

1437
public IYdbRelationalConnection Clone()
1538
{

src/YdbSdk.sln

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ EndProject
1414
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Ydb.Sdk\tests\Tests.csproj", "{A27FD249-6ACB-4392-B00F-CD08FB727C98}"
1515
EndProject
1616
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "EfCore.Ydb", "EfCore.Ydb", "{5E7B167B-5FC7-41BD-8819-16B02ED9B961}"
17+
ProjectSection(SolutionItems) = preProject
18+
EfCore.Ydb\CHANGELOG.md = EfCore.Ydb\CHANGELOG.md
19+
EndProjectSection
1720
EndProject
1821
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{4A4EE5F3-CC9C-4166-A8F5-3ACFDD2A75F3}"
1922
EndProject

0 commit comments

Comments
 (0)