Skip to content

Commit 2ebb223

Browse files
Per Kopsdavidkallesen
authored andcommitted
feat: add support for connectionstrings in KustoClientProvider/AtcKustoOptions incl. unit-tests
1 parent 43c496e commit 2ebb223

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

src/Atc.Kusto/Options/AtcKustoOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ public class AtcKustoOptions
77
public string? DatabaseName { get; set; }
88

99
public TokenCredential? Credential { get; set; }
10+
11+
public string? ConnectionString { get; set; }
1012
}

src/Atc.Kusto/Providers/Internal/KustoClientProvider.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ private KustoConnectionStringBuilder GetConnectionString(ClientCacheKey clientCa
4444
.WithAadAzureTokenCredentialsAuthentication(cred),
4545
{ HostAddress: { } host, DatabaseName: { } db } =>
4646
new KustoConnectionStringBuilder(host.AbsoluteUri, clientCacheKey.DatabaseName ?? db),
47+
{ ConnectionString: { } cs, DatabaseName: { } db, Credential: { } cred }
48+
=> new KustoConnectionStringBuilder($"{cs};Database={db}")
49+
.WithAadAzureTokenCredentialsAuthentication(cred),
50+
{ ConnectionString: { } cs, DatabaseName: { } db }
51+
=> new KustoConnectionStringBuilder($"{cs};Database={db}"),
52+
{ ConnectionString: { } cs, Credential: { } cred }
53+
=> new KustoConnectionStringBuilder(cs)
54+
.WithAadAzureTokenCredentialsAuthentication(cred),
55+
{ ConnectionString: { } cs }
56+
=> new KustoConnectionStringBuilder(cs),
4757
_ => throw new InvalidOperationException(
4858
$"Missing configuration for kusto connection `{clientCacheKey.ConnectionName}`"),
4959
};

test/Atc.Kusto.Tests/Providers/Internal/KustoClientProviderTests.cs

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,84 @@ internal void GetQueryClient_Returns_Client_With_Connection_And_Database_Without
147147
Assert.Equal(databaseName, client.DefaultDatabaseName);
148148
}
149149

150+
[Theory, AutoNSubstituteDataWithAtcKustoOptions(withCredential: true)]
151+
internal void GetQueryClient_Returns_Client_With_ConnectionString_And_Database_With_Credential(
152+
[Frozen] IOptionsMonitor<AtcKustoOptions> monitor,
153+
AtcKustoOptions options,
154+
KustoClientProvider sut)
155+
{
156+
// Arrange
157+
options.HostAddress = null;
158+
options.ConnectionString = $"https://{Guid.NewGuid():N}.kusto.windows.net";
159+
monitor.Get(null).Returns(options);
160+
161+
// Act
162+
var client = sut.GetQueryClient();
163+
164+
// Assert
165+
Assert.NotNull(client);
166+
Assert.Equal(options.DatabaseName, client.DefaultDatabaseName);
167+
}
168+
169+
[Theory, AutoNSubstituteDataWithAtcKustoOptions(withCredential: false)]
170+
internal void GetQueryClient_Returns_Client_With_ConnectionString_And_Database_Without_Credential(
171+
[Frozen] IOptionsMonitor<AtcKustoOptions> monitor,
172+
AtcKustoOptions options,
173+
KustoClientProvider sut)
174+
{
175+
// Arrange
176+
options.HostAddress = null;
177+
options.ConnectionString = $"https://{Guid.NewGuid():N}.kusto.windows.net";
178+
monitor.Get(null).Returns(options);
179+
180+
// Act
181+
var client = sut.GetQueryClient();
182+
183+
// Assert
184+
Assert.NotNull(client);
185+
Assert.Equal(options.DatabaseName, client.DefaultDatabaseName);
186+
}
187+
188+
[Theory, AutoNSubstituteDataWithAtcKustoOptions(withCredential: true)]
189+
internal void GetQueryClient_Returns_Client_With_ConnectionString_With_Credential(
190+
[Frozen] IOptionsMonitor<AtcKustoOptions> monitor,
191+
AtcKustoOptions options,
192+
KustoClientProvider sut)
193+
{
194+
// Arrange
195+
options.HostAddress = null;
196+
options.ConnectionString = $"https://{Guid.NewGuid():N}.kusto.windows.net";
197+
options.DatabaseName = null;
198+
monitor.Get(null).Returns(options);
199+
200+
// Act
201+
var client = sut.GetQueryClient();
202+
203+
// Assert
204+
Assert.NotNull(client);
205+
Assert.Equal("NetDefaultDB", client.DefaultDatabaseName); // Apparently the library still sets a default database name
206+
}
207+
208+
[Theory, AutoNSubstituteDataWithAtcKustoOptions(withCredential: false)]
209+
internal void GetQueryClient_Returns_Client_With_ConnectionString_Without_Credential(
210+
[Frozen] IOptionsMonitor<AtcKustoOptions> monitor,
211+
AtcKustoOptions options,
212+
KustoClientProvider sut)
213+
{
214+
// Arrange
215+
options.HostAddress = null;
216+
options.ConnectionString = $"https://{Guid.NewGuid():N}.kusto.windows.net";
217+
options.DatabaseName = null;
218+
monitor.Get(null).Returns(options);
219+
220+
// Act
221+
var client = sut.GetQueryClient();
222+
223+
// Assert
224+
Assert.NotNull(client);
225+
Assert.Equal("NetDefaultDB", client.DefaultDatabaseName); // Apparently the library still sets a default database name
226+
}
227+
150228
[Theory, AutoNSubstituteDataWithAtcKustoOptions(withCredential: true)]
151229
internal void GetAdminClient_Returns_Client_With_Credential(
152230
[Frozen] IOptionsMonitor<AtcKustoOptions> monitor,
@@ -289,4 +367,82 @@ internal void GetAdminClient_Returns_Client_With_Connection_And_Database_Without
289367
Assert.NotNull(client);
290368
Assert.Equal(databaseName, client.DefaultDatabaseName);
291369
}
370+
371+
[Theory, AutoNSubstituteDataWithAtcKustoOptions(withCredential: true)]
372+
internal void GetAdminClient_Returns_Client_With_ConnectionString_And_Database_With_Credential(
373+
[Frozen] IOptionsMonitor<AtcKustoOptions> monitor,
374+
AtcKustoOptions options,
375+
KustoClientProvider sut)
376+
{
377+
// Arrange
378+
options.HostAddress = null;
379+
options.ConnectionString = $"https://{Guid.NewGuid():N}.kusto.windows.net";
380+
monitor.Get(null).Returns(options);
381+
382+
// Act
383+
var client = sut.GetAdminClient();
384+
385+
// Assert
386+
Assert.NotNull(client);
387+
Assert.Equal(options.DatabaseName, client.DefaultDatabaseName);
388+
}
389+
390+
[Theory, AutoNSubstituteDataWithAtcKustoOptions(withCredential: false)]
391+
internal void GetAdminClient_Returns_Client_With_ConnectionString_And_Database_Without_Credential(
392+
[Frozen] IOptionsMonitor<AtcKustoOptions> monitor,
393+
AtcKustoOptions options,
394+
KustoClientProvider sut)
395+
{
396+
// Arrange
397+
options.HostAddress = null;
398+
options.ConnectionString = $"https://{Guid.NewGuid():N}.kusto.windows.net";
399+
monitor.Get(null).Returns(options);
400+
401+
// Act
402+
var client = sut.GetAdminClient();
403+
404+
// Assert
405+
Assert.NotNull(client);
406+
Assert.Equal(options.DatabaseName, client.DefaultDatabaseName);
407+
}
408+
409+
[Theory, AutoNSubstituteDataWithAtcKustoOptions(withCredential: true)]
410+
internal void GetAdminClient_Returns_Client_With_ConnectionString_With_Credential(
411+
[Frozen] IOptionsMonitor<AtcKustoOptions> monitor,
412+
AtcKustoOptions options,
413+
KustoClientProvider sut)
414+
{
415+
// Arrange
416+
options.HostAddress = null;
417+
options.ConnectionString = $"https://{Guid.NewGuid():N}.kusto.windows.net";
418+
options.DatabaseName = null;
419+
monitor.Get(null).Returns(options);
420+
421+
// Act
422+
var client = sut.GetAdminClient();
423+
424+
// Assert
425+
Assert.NotNull(client);
426+
Assert.Equal("NetDefaultDB", client.DefaultDatabaseName); // Apparently the library still sets a default database name
427+
}
428+
429+
[Theory, AutoNSubstituteDataWithAtcKustoOptions(withCredential: false)]
430+
internal void GetAdminClient_Returns_Client_With_ConnectionString_Without_Credential(
431+
[Frozen] IOptionsMonitor<AtcKustoOptions> monitor,
432+
AtcKustoOptions options,
433+
KustoClientProvider sut)
434+
{
435+
// Arrange
436+
options.HostAddress = null;
437+
options.ConnectionString = $"https://{Guid.NewGuid():N}.kusto.windows.net";
438+
options.DatabaseName = null;
439+
monitor.Get(null).Returns(options);
440+
441+
// Act
442+
var client = sut.GetAdminClient();
443+
444+
// Assert
445+
Assert.NotNull(client);
446+
Assert.Equal("NetDefaultDB", client.DefaultDatabaseName); // Apparently the library still sets a default database name
447+
}
292448
}

0 commit comments

Comments
 (0)