Skip to content

Commit

Permalink
Settle on a new TenantSettings config
Browse files Browse the repository at this point in the history
"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"
}
  • Loading branch information
majorsilence committed Aug 23, 2023
1 parent 0ea69c8 commit 245ae6e
Show file tree
Hide file tree
Showing 21 changed files with 146 additions and 169 deletions.
38 changes: 24 additions & 14 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
```

Expand Down Expand Up @@ -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"
}
```

Expand Down
7 changes: 0 additions & 7 deletions TownSuite.MultiTenant.Console/AppSettings.cs

This file was deleted.

58 changes: 24 additions & 34 deletions TownSuite.MultiTenant.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -64,7 +63,7 @@
.AddEnvironmentVariables();
var configurationRoot = builder.Build();

var settings = configurationRoot.GetSection("AppSettings").Get<AppSettings>();
var settings = configurationRoot.GetSection("TenantSettings").Get<TownSuite.MultiTenant.Settings>();

using var loggerFactory = LoggerFactory.Create(builder =>
{
Expand All @@ -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<dynamic>(sql);
// See the centralconfig for appNames
await using var conn = tenant.Value.CreateConnection("WebService");
await conn.OpenAsync();
var records = await conn.QueryAsync<dynamic>(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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.0.7</Version>
<Version>2.0.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions TownSuite.MultiTenant.Console/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
"Microsoft": "Information"
}
},
"AppSettings": {
"TenantSettings": {
"ConfigPairs": [
{
"Id": 1,
"ConfigReaderUrl": [
"ConfigReaderUrls": [
"PLACEHOLDER"
],
"ConfigReaderUrlBearerToken": "PLACEHOLDER",
Expand Down
11 changes: 2 additions & 9 deletions TownSuite.MultiTenant.Tests/AppSettingsConfigReader_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Settings>();
}

[Test]
Expand Down
21 changes: 11 additions & 10 deletions TownSuite.MultiTenant.Tests/FakeHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ICollection<WebSearchResponse>> GetAsync(string url, CancellationToken cancellationToken)
public override Task<ICollection<WebSearchResponse>> GetAsync(string url, string bearerToken,
CancellationToken cancellationToken)
{
var response = new List<WebSearchResponse>();

Expand All @@ -19,17 +20,17 @@ public override Task<ICollection<WebSearchResponse>> 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()
Expand All @@ -40,17 +41,17 @@ public override Task<ICollection<WebSearchResponse>> 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()
Expand All @@ -61,10 +62,10 @@ public override Task<ICollection<WebSearchResponse>> 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);
Expand Down
10 changes: 1 addition & 9 deletions TownSuite.MultiTenant.Tests/HttpConfigReader_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]>()
};
settings = config.GetSection("TenantSettings").Get<Settings>();
}

[Test]
Expand Down
2 changes: 1 addition & 1 deletion TownSuite.MultiTenant.Tests/IdFaker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace TownSuite.MultiTenant.Tests;

public class IdFaker : IUniqueIdRetriever
{
public Task<string> GetUniqueId(ConnectionStrings con)
public Task<string> GetUniqueId(ConnectionStrings con, AppSettingsConfigPairs configPairs)
{

if (con.Name.StartsWith("a.dns.record"))
Expand Down
10 changes: 1 addition & 9 deletions TownSuite.MultiTenant.Tests/TenantResolver_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]>()
};
settings = config.GetSection("TenantSettings").Get<Settings>();
}

[Test]
Expand Down
13 changes: 10 additions & 3 deletions TownSuite.MultiTenant.Tests/appsettings_reader_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
20 changes: 13 additions & 7 deletions TownSuite.MultiTenant.Tests/http_reader_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace TownSuite.MultiTenant.Console;
namespace TownSuite.MultiTenant;

public class AppSettingsConfigPairs
{
public string Id { get; init; }

Check warning on line 5 in TownSuite.MultiTenant/AppSettingsConfigPairs.cs

View workflow job for this annotation

GitHub Actions / linux-build

Non-nullable property 'Id' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 5 in TownSuite.MultiTenant/AppSettingsConfigPairs.cs

View workflow job for this annotation

GitHub Actions / linux-build

Non-nullable property 'Id' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 5 in TownSuite.MultiTenant/AppSettingsConfigPairs.cs

View workflow job for this annotation

GitHub Actions / mac-build

Non-nullable property 'Id' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 5 in TownSuite.MultiTenant/AppSettingsConfigPairs.cs

View workflow job for this annotation

GitHub Actions / mac-build

Non-nullable property 'Id' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 5 in TownSuite.MultiTenant/AppSettingsConfigPairs.cs

View workflow job for this annotation

GitHub Actions / windows-build

Non-nullable property 'Id' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 5 in TownSuite.MultiTenant/AppSettingsConfigPairs.cs

View workflow job for this annotation

GitHub Actions / windows-build

Non-nullable property 'Id' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string[] ConfigReaderUrl { get; init; }
public string[] ConfigReaderUrls { get; init; }

Check warning on line 6 in TownSuite.MultiTenant/AppSettingsConfigPairs.cs

View workflow job for this annotation

GitHub Actions / linux-build

Non-nullable property 'ConfigReaderUrls' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 6 in TownSuite.MultiTenant/AppSettingsConfigPairs.cs

View workflow job for this annotation

GitHub Actions / linux-build

Non-nullable property 'ConfigReaderUrls' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 6 in TownSuite.MultiTenant/AppSettingsConfigPairs.cs

View workflow job for this annotation

GitHub Actions / mac-build

Non-nullable property 'ConfigReaderUrls' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 6 in TownSuite.MultiTenant/AppSettingsConfigPairs.cs

View workflow job for this annotation

GitHub Actions / mac-build

Non-nullable property 'ConfigReaderUrls' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 6 in TownSuite.MultiTenant/AppSettingsConfigPairs.cs

View workflow job for this annotation

GitHub Actions / windows-build

Non-nullable property 'ConfigReaderUrls' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 6 in TownSuite.MultiTenant/AppSettingsConfigPairs.cs

View workflow job for this annotation

GitHub Actions / windows-build

Non-nullable property 'ConfigReaderUrls' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string ConfigReaderUrlBearerToken { get; init; }

Check warning on line 7 in TownSuite.MultiTenant/AppSettingsConfigPairs.cs

View workflow job for this annotation

GitHub Actions / linux-build

Non-nullable property 'ConfigReaderUrlBearerToken' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 7 in TownSuite.MultiTenant/AppSettingsConfigPairs.cs

View workflow job for this annotation

GitHub Actions / linux-build

Non-nullable property 'ConfigReaderUrlBearerToken' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 7 in TownSuite.MultiTenant/AppSettingsConfigPairs.cs

View workflow job for this annotation

GitHub Actions / mac-build

Non-nullable property 'ConfigReaderUrlBearerToken' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 7 in TownSuite.MultiTenant/AppSettingsConfigPairs.cs

View workflow job for this annotation

GitHub Actions / mac-build

Non-nullable property 'ConfigReaderUrlBearerToken' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 7 in TownSuite.MultiTenant/AppSettingsConfigPairs.cs

View workflow job for this annotation

GitHub Actions / windows-build

Non-nullable property 'ConfigReaderUrlBearerToken' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 7 in TownSuite.MultiTenant/AppSettingsConfigPairs.cs

View workflow job for this annotation

GitHub Actions / windows-build

Non-nullable property 'ConfigReaderUrlBearerToken' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string DecryptionKey { get; init; }

Check warning on line 8 in TownSuite.MultiTenant/AppSettingsConfigPairs.cs

View workflow job for this annotation

GitHub Actions / linux-build

Non-nullable property 'DecryptionKey' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 8 in TownSuite.MultiTenant/AppSettingsConfigPairs.cs

View workflow job for this annotation

GitHub Actions / linux-build

Non-nullable property 'DecryptionKey' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 8 in TownSuite.MultiTenant/AppSettingsConfigPairs.cs

View workflow job for this annotation

GitHub Actions / mac-build

Non-nullable property 'DecryptionKey' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 8 in TownSuite.MultiTenant/AppSettingsConfigPairs.cs

View workflow job for this annotation

GitHub Actions / mac-build

Non-nullable property 'DecryptionKey' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 8 in TownSuite.MultiTenant/AppSettingsConfigPairs.cs

View workflow job for this annotation

GitHub Actions / windows-build

Non-nullable property 'DecryptionKey' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string UniqueIdDbPattern { get; init; }

Check warning on line 9 in TownSuite.MultiTenant/AppSettingsConfigPairs.cs

View workflow job for this annotation

GitHub Actions / linux-build

Non-nullable property 'UniqueIdDbPattern' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 9 in TownSuite.MultiTenant/AppSettingsConfigPairs.cs

View workflow job for this annotation

GitHub Actions / linux-build

Non-nullable property 'UniqueIdDbPattern' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 9 in TownSuite.MultiTenant/AppSettingsConfigPairs.cs

View workflow job for this annotation

GitHub Actions / mac-build

Non-nullable property 'UniqueIdDbPattern' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 9 in TownSuite.MultiTenant/AppSettingsConfigPairs.cs

View workflow job for this annotation

GitHub Actions / mac-build

Non-nullable property 'UniqueIdDbPattern' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 9 in TownSuite.MultiTenant/AppSettingsConfigPairs.cs

View workflow job for this annotation

GitHub Actions / windows-build

Non-nullable property 'UniqueIdDbPattern' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
Expand Down
Loading

0 comments on commit 245ae6e

Please sign in to comment.