From 245ae6e758cb7fbd1f3b6f8e590929825dea4349 Mon Sep 17 00:00:00 2001 From: Peter Gill Date: Wed, 23 Aug 2023 14:00:48 -0230 Subject: [PATCH] Settle on a new TenantSettings config "TenantSettings": { "ConfigPairs": [ { "Id": 1, "ConfigReaderUrls": [ "http://localhost:5000/api/ConfigReader" ], "ConfigReaderUrlBearerToken": "PLACEHOLDER", "DecryptionKey": "PLACEHOLDER", "UniqueIdDbPattern": ".*_Web", "SqlUniqueIdLookup": "SELECT Top 1 Id FROM ExampleTable" } ], "UserAgent": "TownSuite-MultiTenant-Console Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/115.0" } --- Readme.md | 38 +++++++----- TownSuite.MultiTenant.Console/AppSettings.cs | 7 --- TownSuite.MultiTenant.Console/Program.cs | 58 ++++++++---------- .../TownSuite.MultiTenant.Console.csproj | 2 +- .../appsettings.json | 4 +- .../AppSettingsConfigReader_Tests.cs | 11 +--- TownSuite.MultiTenant.Tests/FakeHttpClient.cs | 21 ++++--- .../HttpConfigReader_Tests.cs | 10 +-- TownSuite.MultiTenant.Tests/IdFaker.cs | 2 +- .../TenantResolver_Tests.cs | 10 +-- .../appsettings_reader_test.json | 13 +++- .../http_reader_test.json | 20 +++--- .../AppSettingsConfigPairs.cs | 4 +- .../AppSettingsConfigReader.cs | 13 ++-- TownSuite.MultiTenant/ConfigReader.cs | 6 +- TownSuite.MultiTenant/HttpConfigReader.cs | 61 ++++++++++--------- TownSuite.MultiTenant/IUniqueIdRetriever.cs | 2 +- TownSuite.MultiTenant/Settings.cs | 5 +- ...IdRetriever.cs => SqlUniqueIdRetriever.cs} | 13 +--- .../TownSuite.MultiTenant.csproj | 2 +- TownSuite.MultiTenant/TsWebClient.cs | 13 ++-- 21 files changed, 146 insertions(+), 169 deletions(-) delete mode 100644 TownSuite.MultiTenant.Console/AppSettings.cs rename {TownSuite.MultiTenant.Console => TownSuite.MultiTenant}/AppSettingsConfigPairs.cs (75%) rename TownSuite.MultiTenant/{UniqueIdRetriever.cs => SqlUniqueIdRetriever.cs} (53%) diff --git a/Readme.md b/Readme.md index 8cb77f2..a655563 100644 --- a/Readme.md +++ b/Readme.md @@ -69,10 +69,17 @@ Read tenant information from a appsettings.json file. "tenant2_app1": "Server=tcp:myserver.example.townsuite.com,1433;Initial Catalog=mydatabase2;Persist Security Info=False;User ID=myuser;Password=mypassword;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" }, "TenantSettings": { - "UniqueIdDbPattern": ".*_Web", - "SqlUniqueIdLookup": "SELECT Top 1 Id FROM ExampleTable" - }, - "DecryptionKey": "PLACEHOLDER" + // AppSettingsConfigReader supports only 1 record in the ConfigPairs + "ConfigPairs": [ + { + "Id": 1, + "DecryptionKey": "PLACEHOLDER", + "UniqueIdDbPattern": ".*_Web", + "SqlUniqueIdLookup": "SELECT Top 1 Id FROM ExampleTable" + } + ], + "UserAgent": "TownSuite-MultiTenant-Console Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/115.0" + } } ``` @@ -118,17 +125,20 @@ public class ExampleController : ControllerBase Settings that are required to make an http call and read the output ```json -{ - "TenantSettings": { - "UniqueIdDbPattern": ".*_Web", - "SqlUniqueIdLookup": "SELECT Top 1 Id FROM ExampleTable", - "ConfigReaderUrl": [ - "http://localhost:5000/api/ConfigReader", - "https://localhost:5001/api/ConfigReader" +"TenantSettings": { + "ConfigPairs": [ + { + "Id": 1, + "ConfigReaderUrls": [ + "http://localhost:5000/api/ConfigReader" + ], + "ConfigReaderUrlBearerToken": "PLACEHOLDER", + "DecryptionKey": "PLACEHOLDER", + "UniqueIdDbPattern": ".*_Web", + "SqlUniqueIdLookup": "SELECT Top 1 Id FROM ExampleTable" + } ], - "ConfigReaderUrlBearerToken": "PLACEHOLDER", - "DecryptionKey": "PLACEHOLDER" - } + "UserAgent": "TownSuite-MultiTenant-Console Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/115.0" } ``` diff --git a/TownSuite.MultiTenant.Console/AppSettings.cs b/TownSuite.MultiTenant.Console/AppSettings.cs deleted file mode 100644 index 6b2892d..0000000 --- a/TownSuite.MultiTenant.Console/AppSettings.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace TownSuite.MultiTenant.Console; - -public class AppSettings -{ - public AppSettingsConfigPairs[] ConfigPairs { get; init; } - public string UserAgent { get; init; } -} \ No newline at end of file diff --git a/TownSuite.MultiTenant.Console/Program.cs b/TownSuite.MultiTenant.Console/Program.cs index 204d6ed..b757ff6 100644 --- a/TownSuite.MultiTenant.Console/Program.cs +++ b/TownSuite.MultiTenant.Console/Program.cs @@ -6,7 +6,6 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using TownSuite.MultiTenant; -using TownSuite.MultiTenant.Console; // See https://aka.ms/new-console-template for more information @@ -64,7 +63,7 @@ .AddEnvironmentVariables(); var configurationRoot = builder.Build(); -var settings = configurationRoot.GetSection("AppSettings").Get(); +var settings = configurationRoot.GetSection("TenantSettings").Get(); using var loggerFactory = LoggerFactory.Create(builder => { @@ -86,45 +85,36 @@ using var writer = new StreamWriter(outputFile); using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture); - -foreach (var lookup in settings.ConfigPairs) +try { - try - { - var resolver = new TenantResolver(loggerResolver, - new HttpConfigReader(loggerReader, new UniqueIdRetriever(lookup.SqlUniqueIdLookup), - new TsWebClient(new HttpClient(), lookup.ConfigReaderUrlBearerToken, settings.UserAgent), - new Settings() - { - ConfigReaderUrls = lookup.ConfigReaderUrl, - DecryptionKey = lookup.DecryptionKey, - UniqueIdDbPattern = lookup.UniqueIdDbPattern - })); - resolver.Clear(); - await resolver.ResolveAll(); + var resolver = new TenantResolver(loggerResolver, + new HttpConfigReader(loggerReader, new SqlUniqueIdRetriever(), + new TsWebClient(new HttpClient(), settings.UserAgent), + settings)); + resolver.Clear(); + await resolver.ResolveAll(); - foreach (var tenant in resolver.Tenants.OrderBy(p=>p.Key)) + foreach (var tenant in resolver.Tenants.OrderBy(p => p.Key)) + { + try { - try - { - // See the centralconfig for appNames - await using var conn = tenant.Value.CreateConnection("WebService"); - await conn.OpenAsync(); - var records = await conn.QueryAsync(sql); + // See the centralconfig for appNames + await using var conn = tenant.Value.CreateConnection("WebService"); + await conn.OpenAsync(); + var records = await conn.QueryAsync(sql); - csv.WriteRecords(records); - } - catch (Exception ex) - { - logger.LogError(ex, $"Failed to retrieve for tenant {tenant.Key}"); - } + csv.WriteRecords(records); + } + catch (Exception ex) + { + logger.LogError(ex, $"Failed to retrieve for tenant {tenant.Key}"); } } - catch (Exception e) - { - logger.LogError(e, $"Failed tenant resolver, config pair {lookup.Id}."); - } +} +catch (Exception e) +{ + logger.LogError(e, $"Failed tenant resolver."); } static void PrintHelp() diff --git a/TownSuite.MultiTenant.Console/TownSuite.MultiTenant.Console.csproj b/TownSuite.MultiTenant.Console/TownSuite.MultiTenant.Console.csproj index 287a9a3..68148c0 100644 --- a/TownSuite.MultiTenant.Console/TownSuite.MultiTenant.Console.csproj +++ b/TownSuite.MultiTenant.Console/TownSuite.MultiTenant.Console.csproj @@ -5,7 +5,7 @@ net6.0 enable enable - 1.0.7 + 2.0.0 diff --git a/TownSuite.MultiTenant.Console/appsettings.json b/TownSuite.MultiTenant.Console/appsettings.json index f0a922d..8042990 100644 --- a/TownSuite.MultiTenant.Console/appsettings.json +++ b/TownSuite.MultiTenant.Console/appsettings.json @@ -6,11 +6,11 @@ "Microsoft": "Information" } }, - "AppSettings": { + "TenantSettings": { "ConfigPairs": [ { "Id": 1, - "ConfigReaderUrl": [ + "ConfigReaderUrls": [ "PLACEHOLDER" ], "ConfigReaderUrlBearerToken": "PLACEHOLDER", diff --git a/TownSuite.MultiTenant.Tests/AppSettingsConfigReader_Tests.cs b/TownSuite.MultiTenant.Tests/AppSettingsConfigReader_Tests.cs index 714375d..c39b68e 100644 --- a/TownSuite.MultiTenant.Tests/AppSettingsConfigReader_Tests.cs +++ b/TownSuite.MultiTenant.Tests/AppSettingsConfigReader_Tests.cs @@ -16,15 +16,8 @@ public void Setup() .AddJsonFile("appsettings_reader_test.json") .AddEnvironmentVariables() .Build(); - - string pattern = config.GetSection("TenantSettings").GetSection("UniqueIdDbPattern").Value; - string sql = config.GetSection("TenantSettings").GetSection("SqlUniqueIdLookup").Value; - string decryptionKey = config.GetSection("TenantSettings").GetSection("DecryptionKey").Value; - settings = new Settings() - { - UniqueIdDbPattern = pattern, - DecryptionKey = decryptionKey - }; + + settings = config.GetSection("TenantSettings").Get(); } [Test] diff --git a/TownSuite.MultiTenant.Tests/FakeHttpClient.cs b/TownSuite.MultiTenant.Tests/FakeHttpClient.cs index 05ebc38..abafee3 100644 --- a/TownSuite.MultiTenant.Tests/FakeHttpClient.cs +++ b/TownSuite.MultiTenant.Tests/FakeHttpClient.cs @@ -2,12 +2,13 @@ namespace TownSuite.MultiTenant.Tests; public class FakeHttpClient : TsWebClient { - public FakeHttpClient(HttpClient httpClient, string bearerToken, string userAgent) : base(httpClient, bearerToken, + public FakeHttpClient(HttpClient httpClient, string bearerToken, string userAgent) : base(httpClient, userAgent) { } - public override Task> GetAsync(string url, CancellationToken cancellationToken) + public override Task> GetAsync(string url, string bearerToken, + CancellationToken cancellationToken) { var response = new List(); @@ -19,17 +20,17 @@ public override Task> GetAsync(string url, Cancel }; tenant1.Connections.Add(new KeyValuePairOfStringAndString() { - Key="tenant1_app1", + Key = "tenant1_app1", Value = "PLACEHOLDER1" }); tenant1.Connections.Add(new KeyValuePairOfStringAndString() { - Key="tenant1_app2", + Key = "tenant1_app2", Value = "PLACEHOLDER2" }); tenant1.Connections.Add(new KeyValuePairOfStringAndString() { - Key="a.dns.record.as.tenant.townsuite.com_app1", + Key = "a.dns.record.as.tenant.townsuite.com_app1", Value = "tenant 1 alias" }); var tenant2 = new WebSearchResponse() @@ -40,17 +41,17 @@ public override Task> GetAsync(string url, Cancel }; tenant2.Connections.Add(new KeyValuePairOfStringAndString() { - Key="tenant2_app1", + Key = "tenant2_app1", Value = "PLACEHOLDER3" }); tenant2.Connections.Add(new KeyValuePairOfStringAndString() { - Key="tenant2_app2", + Key = "tenant2_app2", Value = "PLACEHOLDER4" }); tenant2.Connections.Add(new KeyValuePairOfStringAndString() { - Key="second.dns.record.as.tenant.townsuite.com_app1", + Key = "second.dns.record.as.tenant.townsuite.com_app1", Value = "tenant 2 alias" }); var tenant3 = new WebSearchResponse() @@ -61,10 +62,10 @@ public override Task> GetAsync(string url, Cancel }; tenant3.Connections.Add(new KeyValuePairOfStringAndString() { - Key="tenant3_app1", + Key = "tenant3_app1", Value = "PLACEHOLDER5" }); - + response.Add(tenant1); response.Add(tenant2); response.Add(tenant3); diff --git a/TownSuite.MultiTenant.Tests/HttpConfigReader_Tests.cs b/TownSuite.MultiTenant.Tests/HttpConfigReader_Tests.cs index 5b7cb18..a3411f9 100644 --- a/TownSuite.MultiTenant.Tests/HttpConfigReader_Tests.cs +++ b/TownSuite.MultiTenant.Tests/HttpConfigReader_Tests.cs @@ -17,15 +17,7 @@ public void Setup() .AddEnvironmentVariables() .Build(); - string pattern = config.GetSection("TenantSettings").GetSection("UniqueIdDbPattern").Value; - string sql = config.GetSection("TenantSettings").GetSection("SqlUniqueIdLookup").Value; - string decryptionKey = config.GetSection("TenantSettings").GetSection("DecryptionKey").Value; - settings = new Settings() - { - UniqueIdDbPattern = pattern, - DecryptionKey = decryptionKey, - ConfigReaderUrls = config.GetSection("TenantSettings").GetSection("ConfigReaderUrl").Get() - }; + settings = config.GetSection("TenantSettings").Get(); } [Test] diff --git a/TownSuite.MultiTenant.Tests/IdFaker.cs b/TownSuite.MultiTenant.Tests/IdFaker.cs index 947b107..3cdbdb5 100644 --- a/TownSuite.MultiTenant.Tests/IdFaker.cs +++ b/TownSuite.MultiTenant.Tests/IdFaker.cs @@ -2,7 +2,7 @@ namespace TownSuite.MultiTenant.Tests; public class IdFaker : IUniqueIdRetriever { - public Task GetUniqueId(ConnectionStrings con) + public Task GetUniqueId(ConnectionStrings con, AppSettingsConfigPairs configPairs) { if (con.Name.StartsWith("a.dns.record")) diff --git a/TownSuite.MultiTenant.Tests/TenantResolver_Tests.cs b/TownSuite.MultiTenant.Tests/TenantResolver_Tests.cs index 9e5d03c..9b2f32f 100644 --- a/TownSuite.MultiTenant.Tests/TenantResolver_Tests.cs +++ b/TownSuite.MultiTenant.Tests/TenantResolver_Tests.cs @@ -17,15 +17,7 @@ public void Setup() .AddEnvironmentVariables() .Build(); - string pattern = config.GetSection("TenantSettings").GetSection("UniqueIdDbPattern").Value; - string sql = config.GetSection("TenantSettings").GetSection("SqlUniqueIdLookup").Value; - string decryptionKey = config.GetSection("TenantSettings").GetSection("DecryptionKey").Value; - settings = new Settings() - { - UniqueIdDbPattern = pattern, - DecryptionKey = decryptionKey, - ConfigReaderUrls = config.GetSection("TenantSettings").GetSection("ConfigReaderUrl").Get() - }; + settings = config.GetSection("TenantSettings").Get(); } [Test] diff --git a/TownSuite.MultiTenant.Tests/appsettings_reader_test.json b/TownSuite.MultiTenant.Tests/appsettings_reader_test.json index 1790149..f173060 100644 --- a/TownSuite.MultiTenant.Tests/appsettings_reader_test.json +++ b/TownSuite.MultiTenant.Tests/appsettings_reader_test.json @@ -17,8 +17,15 @@ "tenant3_app1": "PLACEHOLDER5" }, "TenantSettings": { - "UniqueIdDbPattern": ".*_app1", - "SqlUniqueIdLookup": "SELECT TOP 1 [Id] FROM [dbo].[UniqueId] WHERE [Name] = @name", - "DecryptionKey": "" + // AppSettingsConfigReader supports only 1 record in the ConfigPairs + "ConfigPairs": [ + { + "Id": 1, + "DecryptionKey": "PLACEHOLDER", + "UniqueIdDbPattern": ".*_app1", + "SqlUniqueIdLookup": "SELECT TOP 1 [Id] FROM [dbo].[UniqueId] WHERE [Name] = @name" + } + ], + "UserAgent": "TownSuite-MultiTenant-Console Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/115.0" } } \ No newline at end of file diff --git a/TownSuite.MultiTenant.Tests/http_reader_test.json b/TownSuite.MultiTenant.Tests/http_reader_test.json index a7f4331..feadda0 100644 --- a/TownSuite.MultiTenant.Tests/http_reader_test.json +++ b/TownSuite.MultiTenant.Tests/http_reader_test.json @@ -7,13 +7,19 @@ } }, "TenantSettings": { - "UniqueIdDbPattern": ".*_app1", - "SqlUniqueIdLookup": "SELECT TOP 1 [Id] FROM [dbo].[UniqueId] WHERE [Name] = @name", - "ConfigReaderUrl": [ - "http://localhost:5000/api/ConfigReader", - "https://localhost:5001/api/ConfigReader" + "ConfigPairs": [ + { + "Id": 1, + "ConfigReaderUrls": [ + "http://localhost:5000/api/ConfigReader", + "https://localhost:5001/api/ConfigReader" + ], + "ConfigReaderUrlBearerToken": "PLACEHOLDER", + "DecryptionKey": "", + "UniqueIdDbPattern": ".*_app1", + "SqlUniqueIdLookup": "SELECT TOP 1 [Id] FROM [dbo].[UniqueId] WHERE [Name] = @name" + } ], - "ConfigReaderUrlBearerToken": "PLACEHOLDER", - "DecryptionKey": "" + "UserAgent": "TownSuite-MultiTenant-Console Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/115.0" } } \ No newline at end of file diff --git a/TownSuite.MultiTenant.Console/AppSettingsConfigPairs.cs b/TownSuite.MultiTenant/AppSettingsConfigPairs.cs similarity index 75% rename from TownSuite.MultiTenant.Console/AppSettingsConfigPairs.cs rename to TownSuite.MultiTenant/AppSettingsConfigPairs.cs index 1be2698..bde5dd9 100644 --- a/TownSuite.MultiTenant.Console/AppSettingsConfigPairs.cs +++ b/TownSuite.MultiTenant/AppSettingsConfigPairs.cs @@ -1,9 +1,9 @@ -namespace TownSuite.MultiTenant.Console; +namespace TownSuite.MultiTenant; public class AppSettingsConfigPairs { public string Id { get; init; } - public string[] ConfigReaderUrl { get; init; } + public string[] ConfigReaderUrls { get; init; } public string ConfigReaderUrlBearerToken { get; init; } public string DecryptionKey { get; init; } public string UniqueIdDbPattern { get; init; } diff --git a/TownSuite.MultiTenant/AppSettingsConfigReader.cs b/TownSuite.MultiTenant/AppSettingsConfigReader.cs index cb0b9d5..7178d9b 100644 --- a/TownSuite.MultiTenant/AppSettingsConfigReader.cs +++ b/TownSuite.MultiTenant/AppSettingsConfigReader.cs @@ -9,7 +9,6 @@ public class AppSettingsConfigReader : ConfigReader { private readonly Microsoft.Extensions.Configuration.IConfiguration _configuration; private readonly ILogger _logger; - private readonly IUniqueIdRetriever _uniqueIdRetriever; public AppSettingsConfigReader(Microsoft.Extensions.Configuration.IConfiguration configuration, ILogger logger, IUniqueIdRetriever uniqueIdRetriever, @@ -17,7 +16,6 @@ public AppSettingsConfigReader(Microsoft.Extensions.Configuration.IConfiguration { _configuration = configuration; _logger = logger; - _uniqueIdRetriever = uniqueIdRetriever; } public override string GetConnection(string tenant, string appType) @@ -39,14 +37,17 @@ public override async Task Refresh() _connections = new ConcurrentDictionary>(); var conns = new List(); - string pattern = _settings.UniqueIdDbPattern; - + var firstSettingsRecord = _settings.ConfigPairs.FirstOrDefault(); + + string pattern = firstSettingsRecord.UniqueIdDbPattern; + var tasks = new List(); foreach (var connection in connections) { - var con = new ConnectionStrings(_settings.DecryptionKey) { Name = connection.Key, ConnStr = connection.Value }; + var con = new ConnectionStrings(firstSettingsRecord.DecryptionKey) + { Name = connection.Key, ConnStr = connection.Value }; conns.Add(con); - tasks.Add(InitializeUniqueIds(con, pattern)); + tasks.Add(InitializeUniqueIds(con, pattern, firstSettingsRecord)); } foreach (var task in tasks) diff --git a/TownSuite.MultiTenant/ConfigReader.cs b/TownSuite.MultiTenant/ConfigReader.cs index b059c2a..463e5ab 100644 --- a/TownSuite.MultiTenant/ConfigReader.cs +++ b/TownSuite.MultiTenant/ConfigReader.cs @@ -11,7 +11,7 @@ namespace TownSuite.MultiTenant; public abstract class ConfigReader : IConfigReader { protected static ConcurrentDictionary> _connections; - private readonly IUniqueIdRetriever _uniqueIdRetriever; + protected readonly IUniqueIdRetriever _uniqueIdRetriever; protected readonly Settings _settings; public List Exceptions { get; private set; } = new List(); @@ -51,7 +51,7 @@ public bool IsSetup() return _connections != null && _connections.Any(); } - protected async Task InitializeUniqueIds(ConnectionStrings con, string pattern) + protected async Task InitializeUniqueIds(ConnectionStrings con, string pattern, AppSettingsConfigPairs configPairs) { string? tenant = con.Name.Split("_").FirstOrDefault(); if (string.IsNullOrWhiteSpace(tenant)) @@ -67,7 +67,7 @@ protected async Task InitializeUniqueIds(ConnectionStrings con, string pattern) try { - string uniqueId = await _uniqueIdRetriever.GetUniqueId(con); + string uniqueId = await _uniqueIdRetriever.GetUniqueId(con, configPairs); AddOrUpdateCons(con, uniqueId); } diff --git a/TownSuite.MultiTenant/HttpConfigReader.cs b/TownSuite.MultiTenant/HttpConfigReader.cs index 30a941f..dc37adf 100644 --- a/TownSuite.MultiTenant/HttpConfigReader.cs +++ b/TownSuite.MultiTenant/HttpConfigReader.cs @@ -18,7 +18,6 @@ public HttpConfigReader(ILogger logger, IUniqueIdRetriever uni _webClient = webClient; } - public override string GetConnection(string tenant, string appType) { var connectionString = _connections[tenant] @@ -34,43 +33,47 @@ public override string GetConnection(string tenant, string appType) /// public override async Task Refresh() { - var configReaderUrls = _settings.ConfigReaderUrls; - _connections?.Clear(); - _connections = new ConcurrentDictionary>(); - - foreach (var configReaderUrl in configReaderUrls) + foreach (var configPair in _settings.ConfigPairs) { - var tenants = await _webClient.GetAsync(configReaderUrl, System.Threading.CancellationToken.None); - var conns = new List(); + var configReaderUrls = configPair.ConfigReaderUrls; + _connections?.Clear(); + _connections = new ConcurrentDictionary>(); - string pattern = _settings.UniqueIdDbPattern; - - var tasks = new List(); - foreach (var tenant in tenants) + foreach (var configReaderUrl in configReaderUrls) { - foreach (var connection in tenant.Connections) + var tenants = await _webClient.GetAsync(configReaderUrl, configPair.ConfigReaderUrlBearerToken, + System.Threading.CancellationToken.None); + var conns = new List(); + + string pattern = configPair.UniqueIdDbPattern; + + var tasks = new List(); + foreach (var tenant in tenants) { - var con = new ConnectionStrings(_settings.DecryptionKey) - { Name = $"{connection.Key}", ConnStr = connection.Value }; - conns.Add(con); - tasks.Add(InitializeUniqueIds(con, pattern)); + foreach (var connection in tenant.Connections) + { + var con = new ConnectionStrings(configPair.DecryptionKey) + { Name = $"{connection.Key}", ConnStr = connection.Value }; + conns.Add(con); + tasks.Add(InitializeUniqueIds(con, pattern, configPair)); + } } - } - foreach (var task in tasks) - { - await task; - } - - if (Exceptions.Any()) - { - foreach(var ex in Exceptions) + foreach (var task in tasks) { - _logger.LogError(ex.Message, ex); + await task; } - } - GroupDatabasesByTenant(conns); + if (Exceptions.Any()) + { + foreach (var ex in Exceptions) + { + _logger.LogError(ex.Message, ex); + } + } + + GroupDatabasesByTenant(conns); + } } } } \ No newline at end of file diff --git a/TownSuite.MultiTenant/IUniqueIdRetriever.cs b/TownSuite.MultiTenant/IUniqueIdRetriever.cs index c29b96a..5beaf42 100644 --- a/TownSuite.MultiTenant/IUniqueIdRetriever.cs +++ b/TownSuite.MultiTenant/IUniqueIdRetriever.cs @@ -2,5 +2,5 @@ namespace TownSuite.MultiTenant; public interface IUniqueIdRetriever { - Task GetUniqueId(ConnectionStrings con); + Task GetUniqueId(ConnectionStrings con, AppSettingsConfigPairs appSettingsConfigPairs); } \ No newline at end of file diff --git a/TownSuite.MultiTenant/Settings.cs b/TownSuite.MultiTenant/Settings.cs index 454d0e2..920bc0f 100644 --- a/TownSuite.MultiTenant/Settings.cs +++ b/TownSuite.MultiTenant/Settings.cs @@ -2,7 +2,6 @@ namespace TownSuite.MultiTenant; public class Settings { - public string UniqueIdDbPattern { get; init; } - public string DecryptionKey { get; init; } - public string[] ConfigReaderUrls { get; init; } + public AppSettingsConfigPairs[] ConfigPairs { get; init; } + public string UserAgent { get; init; } } \ No newline at end of file diff --git a/TownSuite.MultiTenant/UniqueIdRetriever.cs b/TownSuite.MultiTenant/SqlUniqueIdRetriever.cs similarity index 53% rename from TownSuite.MultiTenant/UniqueIdRetriever.cs rename to TownSuite.MultiTenant/SqlUniqueIdRetriever.cs index 7e4d30d..af4fbf8 100644 --- a/TownSuite.MultiTenant/UniqueIdRetriever.cs +++ b/TownSuite.MultiTenant/SqlUniqueIdRetriever.cs @@ -3,21 +3,14 @@ namespace TownSuite.MultiTenant; -public class UniqueIdRetriever : IUniqueIdRetriever +public class SqlUniqueIdRetriever : IUniqueIdRetriever { - private readonly string _sql; - - public UniqueIdRetriever(string sql) - { - _sql = sql; - } - - public async Task GetUniqueId(ConnectionStrings con) + public async Task GetUniqueId(ConnectionStrings con, AppSettingsConfigPairs configPairs) { await using var cn = new SqlConnection(con.ConnStr); await cn.OpenAsync(); - string uniqueId = await cn.QueryFirstOrDefaultAsync(_sql); + string uniqueId = await cn.QueryFirstOrDefaultAsync(configPairs.SqlUniqueIdLookup); return uniqueId; } } \ No newline at end of file diff --git a/TownSuite.MultiTenant/TownSuite.MultiTenant.csproj b/TownSuite.MultiTenant/TownSuite.MultiTenant.csproj index 182f5b5..cc4d7a4 100644 --- a/TownSuite.MultiTenant/TownSuite.MultiTenant.csproj +++ b/TownSuite.MultiTenant/TownSuite.MultiTenant.csproj @@ -6,7 +6,7 @@ enable TownSuite.MultiTenant true - 1.0.7 + 2.0.0 A small dotnet library to assist in single instance applications with multi tenant databases. diff --git a/TownSuite.MultiTenant/TsWebClient.cs b/TownSuite.MultiTenant/TsWebClient.cs index efd2e22..6046ec9 100644 --- a/TownSuite.MultiTenant/TsWebClient.cs +++ b/TownSuite.MultiTenant/TsWebClient.cs @@ -6,21 +6,17 @@ namespace TownSuite.MultiTenant; public class TsWebClient { private System.Net.Http.HttpClient _httpClient; - private readonly string _bearerToken; private readonly string _userAgent; public TsWebClient(System.Net.Http.HttpClient httpClient, - string bearerToken, string userAgent) + string userAgent) { _httpClient = httpClient; - _bearerToken = bearerToken; _userAgent = userAgent; } - void PrepareRequest(System.Net.Http.HttpClient client, System.Net.Http.HttpRequestMessage request, string url) + void PrepareRequest(System.Net.Http.HttpClient client, System.Net.Http.HttpRequestMessage request) { - request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _bearerToken); - if (string.IsNullOrWhiteSpace(_userAgent)) { throw new TownSuiteException("User-Agent is required."); @@ -30,7 +26,7 @@ void PrepareRequest(System.Net.Http.HttpClient client, System.Net.Http.HttpReque } public virtual async System.Threading.Tasks.Task> GetAsync( - string url, System.Threading.CancellationToken cancellationToken) + string url, string bearerToken, System.Threading.CancellationToken cancellationToken) { var client_ = _httpClient; @@ -44,8 +40,9 @@ void PrepareRequest(System.Net.Http.HttpClient client, System.Net.Http.HttpReque System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json")); request_.RequestUri = new System.Uri(url, System.UriKind.RelativeOrAbsolute); + request_.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", bearerToken); - PrepareRequest(client_, request_, url); + PrepareRequest(client_, request_); var response_ = await client_ .SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken)