Skip to content

Commit f260f28

Browse files
Rohkhann/run time config constructor (#1587)
## Why make this change? Different users of dataapi builder may have different ways in which they want to load in runtimeconfig. For example: There is a scenario where one might want to create the runtimeconfig object dynamically without having a runtime json ready. ## What is this change? - Summary of how your changes work to give reviewers context of your intent. In this pr, we create an abstract class BaseRunTimeConfigLoader that has certain common methods that all derived classes can use. The main TryLoadconfig method will be implemented differently by different classes. The RunTimeConfigProvider by dab loads config from the filesystem. Depending on use case this can be done differently by different consumers. - Also made some name simplifications in test files. ## How was this tested? - [x] Integration Tests yes - [x] Unit Tests yes --------- Co-authored-by: Aaron Powell <me@aaron-powell.com>
1 parent b135913 commit f260f28

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+419
-386
lines changed

src/Cli.Tests/ConfigGeneratorTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ namespace Cli.Tests;
1010
public class ConfigGeneratorTests
1111
{
1212
private IFileSystem? _fileSystem;
13-
private RuntimeConfigLoader? _runtimeConfigLoader;
13+
private FileSystemRuntimeConfigLoader? _runtimeConfigLoader;
1414

1515
[TestInitialize]
1616
public void TestInitialize()
1717
{
1818
_fileSystem = FileSystemUtils.ProvisionMockFileSystem();
1919

20-
_runtimeConfigLoader = new RuntimeConfigLoader(_fileSystem);
20+
_runtimeConfigLoader = new FileSystemRuntimeConfigLoader(_fileSystem);
2121

2222
ILoggerFactory loggerFactory = TestLoggerSupport.ProvisionLoggerFactory();
2323

@@ -49,9 +49,9 @@ public void TryGenerateConfig_WithUserProvidedConfig(
4949

5050
// Mocking logger to assert on logs
5151
Mock<ILogger<ConfigGenerator>> loggerMock = new();
52-
ConfigGenerator.SetLoggerForCliConfigGenerator(loggerMock.Object);
52+
SetLoggerForCliConfigGenerator(loggerMock.Object);
5353

54-
Assert.AreEqual(isConfigGenerationSuccessful, ConfigGenerator.TryGenerateConfig(options, _runtimeConfigLoader!, _fileSystem!));
54+
Assert.AreEqual(isConfigGenerationSuccessful, TryGenerateConfig(options, _runtimeConfigLoader!, _fileSystem!));
5555

5656
if (!isConfigFilePresent)
5757
{
@@ -96,9 +96,9 @@ public void TryGenerateConfig_UsingEnvironmentVariable(
9696

9797
// Mocking logger to assert on logs
9898
Mock<ILogger<ConfigGenerator>> loggerMock = new();
99-
ConfigGenerator.SetLoggerForCliConfigGenerator(loggerMock.Object);
99+
SetLoggerForCliConfigGenerator(loggerMock.Object);
100100

101-
Assert.AreEqual(isConfigGenerationSuccessful, ConfigGenerator.TryGenerateConfig(options, _runtimeConfigLoader!, _fileSystem!));
101+
Assert.AreEqual(isConfigGenerationSuccessful, TryGenerateConfig(options, _runtimeConfigLoader!, _fileSystem!));
102102
if (!isConfigFilePresent)
103103
{
104104
Assert.AreEqual(isConfigGenerationSuccessful, _fileSystem!.File.Exists(configFileName));

src/Cli.Tests/EndToEndTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class EndToEndTests
1313
: VerifyBase
1414
{
1515
private IFileSystem? _fileSystem;
16-
private RuntimeConfigLoader? _runtimeConfigLoader;
16+
private FileSystemRuntimeConfigLoader? _runtimeConfigLoader;
1717
private ILogger<Program>? _cliLogger;
1818

1919
[TestInitialize]
@@ -26,7 +26,7 @@ public void TestInitialize()
2626

2727
_fileSystem = fileSystem;
2828

29-
_runtimeConfigLoader = new RuntimeConfigLoader(_fileSystem);
29+
_runtimeConfigLoader = new FileSystemRuntimeConfigLoader(_fileSystem);
3030

3131
ILoggerFactory loggerFactory = TestLoggerSupport.ProvisionLoggerFactory();
3232

src/Cli.Tests/InitTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ public class InitTests
1111
: VerifyBase
1212
{
1313
private IFileSystem? _fileSystem;
14-
private RuntimeConfigLoader? _runtimeConfigLoader;
14+
private FileSystemRuntimeConfigLoader? _runtimeConfigLoader;
1515

1616
[TestInitialize]
1717
public void TestInitialize()
1818
{
1919
_fileSystem = FileSystemUtils.ProvisionMockFileSystem();
2020

21-
_runtimeConfigLoader = new RuntimeConfigLoader(_fileSystem);
21+
_runtimeConfigLoader = new FileSystemRuntimeConfigLoader(_fileSystem);
2222

2323
ILoggerFactory loggerFactory = TestLoggerSupport.ProvisionLoggerFactory();
2424

src/Cli.Tests/Usings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
global using Microsoft.VisualStudio.TestTools.UnitTesting;
1414
global using Moq;
1515
global using Newtonsoft.Json.Linq;
16-
global using static Azure.DataApiBuilder.Config.RuntimeConfigLoader;
16+
global using static Azure.DataApiBuilder.Config.FileSystemRuntimeConfigLoader;
1717
global using static Cli.ConfigGenerator;
1818
global using static Cli.Tests.TestHelper;
1919
global using static Cli.Utils;

src/Cli.Tests/UtilsTests.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ public void ConstructGraphQLOptionsWithSingularAndPluralWillSetSingularAndPlural
107107
[DataTestMethod]
108108
[DataRow("", "my-config.json", "my-config.json", DisplayName = "user provided the config file and environment variable was not set.")]
109109
[DataRow("Test", "my-config.json", "my-config.json", DisplayName = "user provided the config file and environment variable was set.")]
110-
[DataRow("Test", null, $"{RuntimeConfigLoader.CONFIGFILE_NAME}.Test{RuntimeConfigLoader.CONFIG_EXTENSION}", DisplayName = "config not provided, but environment variable was set.")]
111-
[DataRow("", null, $"{RuntimeConfigLoader.CONFIGFILE_NAME}{RuntimeConfigLoader.CONFIG_EXTENSION}", DisplayName = "neither config was provided, nor environment variable was set.")]
110+
[DataRow("Test", null, $"{CONFIGFILE_NAME}.Test{CONFIG_EXTENSION}", DisplayName = "config not provided, but environment variable was set.")]
111+
[DataRow("", null, $"{CONFIGFILE_NAME}{CONFIG_EXTENSION}", DisplayName = "neither config was provided, nor environment variable was set.")]
112112
public void TestConfigSelectionBasedOnCliPrecedence(
113113
string? environmentValue,
114114
string? userProvidedConfigFile,
@@ -117,13 +117,13 @@ public void TestConfigSelectionBasedOnCliPrecedence(
117117
MockFileSystem fileSystem = new();
118118
fileSystem.AddFile(expectedRuntimeConfigFile, new MockFileData(""));
119119

120-
RuntimeConfigLoader loader = new(fileSystem);
120+
FileSystemRuntimeConfigLoader loader = new(fileSystem);
121121

122-
string? envValueBeforeTest = Environment.GetEnvironmentVariable(RuntimeConfigLoader.RUNTIME_ENVIRONMENT_VAR_NAME);
123-
Environment.SetEnvironmentVariable(RuntimeConfigLoader.RUNTIME_ENVIRONMENT_VAR_NAME, environmentValue);
122+
string? envValueBeforeTest = Environment.GetEnvironmentVariable(RUNTIME_ENVIRONMENT_VAR_NAME);
123+
Environment.SetEnvironmentVariable(RUNTIME_ENVIRONMENT_VAR_NAME, environmentValue);
124124
Assert.IsTrue(TryGetConfigFileBasedOnCliPrecedence(loader, userProvidedConfigFile, out string? actualRuntimeConfigFile));
125125
Assert.AreEqual(expectedRuntimeConfigFile, actualRuntimeConfigFile);
126-
Environment.SetEnvironmentVariable(RuntimeConfigLoader.RUNTIME_ENVIRONMENT_VAR_NAME, envValueBeforeTest);
126+
Environment.SetEnvironmentVariable(RUNTIME_ENVIRONMENT_VAR_NAME, envValueBeforeTest);
127127
}
128128

129129
/// <summary>
@@ -239,14 +239,14 @@ public void TestValidateAudienceAndIssuerForAuthenticationProvider(
239239
public void TestMergeConfig()
240240
{
241241
MockFileSystem fileSystem = new();
242-
fileSystem.AddFile(RuntimeConfigLoader.DEFAULT_CONFIG_FILE_NAME, new MockFileData(BASE_CONFIG));
242+
fileSystem.AddFile(DEFAULT_CONFIG_FILE_NAME, new MockFileData(BASE_CONFIG));
243243
fileSystem.AddFile("dab-config.Test.json", new MockFileData(ENV_BASED_CONFIG));
244244

245-
RuntimeConfigLoader loader = new(fileSystem);
245+
FileSystemRuntimeConfigLoader loader = new(fileSystem);
246246

247-
Environment.SetEnvironmentVariable(RuntimeConfigLoader.RUNTIME_ENVIRONMENT_VAR_NAME, "Test");
247+
Environment.SetEnvironmentVariable(RUNTIME_ENVIRONMENT_VAR_NAME, "Test");
248248

249-
Assert.IsTrue(Cli.ConfigMerger.TryMergeConfigsIfAvailable(fileSystem, loader, new StringLogger(), out string? mergedConfig), "Failed to merge config files");
249+
Assert.IsTrue(ConfigMerger.TryMergeConfigsIfAvailable(fileSystem, loader, new StringLogger(), out string? mergedConfig), "Failed to merge config files");
250250
Assert.AreEqual(mergedConfig, "dab-config.Test.merged.json");
251251
Assert.IsTrue(fileSystem.File.Exists(mergedConfig));
252252
Assert.IsTrue(JToken.DeepEquals(JObject.Parse(MERGED_CONFIG), JObject.Parse(fileSystem.File.ReadAllText(mergedConfig))));
@@ -282,7 +282,7 @@ public void TestMergeConfigAvailability(
282282
MockFileSystem fileSystem = new();
283283

284284
// Setting up the test scenarios
285-
Environment.SetEnvironmentVariable(RuntimeConfigLoader.RUNTIME_ENVIRONMENT_VAR_NAME, environmentValue);
285+
Environment.SetEnvironmentVariable(RUNTIME_ENVIRONMENT_VAR_NAME, environmentValue);
286286
string baseConfig = "dab-config.json";
287287
string envBasedConfig = "dab-config.Test.json";
288288

@@ -296,15 +296,15 @@ public void TestMergeConfigAvailability(
296296
fileSystem.AddFile(envBasedConfig, new("{}"));
297297
}
298298

299-
RuntimeConfigLoader loader = new(fileSystem);
299+
FileSystemRuntimeConfigLoader loader = new(fileSystem);
300300

301301
Assert.AreEqual(
302302
expectedIsMergedConfigAvailable,
303303
ConfigMerger.TryMergeConfigsIfAvailable(fileSystem, loader, new StringLogger(), out string? mergedConfigFile),
304304
"Availability of merge config should match");
305305
Assert.AreEqual(expectedMergedConfigFileName, mergedConfigFile, "Merge config file name should match expected");
306306

307-
Environment.SetEnvironmentVariable(RuntimeConfigLoader.RUNTIME_ENVIRONMENT_VAR_NAME, null);
307+
Environment.SetEnvironmentVariable(RUNTIME_ENVIRONMENT_VAR_NAME, null);
308308
}
309309
}
310310

src/Cli/Commands/AddOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public AddOptions(
5656
[Option("permissions", Required = true, Separator = ':', HelpText = "Permissions required to access the source table or container.")]
5757
public IEnumerable<string> Permissions { get; }
5858

59-
public void Handler(ILogger logger, RuntimeConfigLoader loader, IFileSystem fileSystem)
59+
public void Handler(ILogger logger, FileSystemRuntimeConfigLoader loader, IFileSystem fileSystem)
6060
{
6161
logger.LogInformation($"{PRODUCT_NAME} {ProductInfo.GetProductVersion()}");
6262
if (!IsEntityProvided(Entity, logger, command: "add"))

src/Cli/Commands/InitOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public InitOptions(
103103
[Option("graphql.disabled", Default = false, Required = false, HelpText = "Disables GraphQL endpoint for all entities.")]
104104
public bool GraphQLDisabled { get; }
105105

106-
public void Handler(ILogger logger, RuntimeConfigLoader loader, IFileSystem fileSystem)
106+
public void Handler(ILogger logger, FileSystemRuntimeConfigLoader loader, IFileSystem fileSystem)
107107
{
108108
logger.LogInformation($"{PRODUCT_NAME} {ProductInfo.GetProductVersion()}");
109109
bool isSuccess = ConfigGenerator.TryGenerateConfig(this, loader, fileSystem);

src/Cli/Commands/StartOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public StartOptions(bool verbose, LogLevel? logLevel, bool isHttpsRedirectionDis
3535
[Option("no-https-redirect", Required = false, HelpText = "Disables automatic https redirects.")]
3636
public bool IsHttpsRedirectionDisabled { get; }
3737

38-
public void Handler(ILogger logger, RuntimeConfigLoader loader, IFileSystem fileSystem)
38+
public void Handler(ILogger logger, FileSystemRuntimeConfigLoader loader, IFileSystem fileSystem)
3939
{
4040
logger.LogInformation($"{PRODUCT_NAME} {ProductInfo.GetProductVersion()}");
4141
bool isSuccess = ConfigGenerator.TryStartEngineWithOptions(this, loader, fileSystem);

src/Cli/Commands/UpdateOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public UpdateOptions(
9696
[Option('m', "map", Separator = ',', Required = false, HelpText = "Specify mappings between database fields and GraphQL and REST fields. format: --map \"backendName1:exposedName1,backendName2:exposedName2,...\".")]
9797
public IEnumerable<string>? Map { get; }
9898

99-
public void Handler(ILogger logger, RuntimeConfigLoader loader, IFileSystem fileSystem)
99+
public void Handler(ILogger logger, FileSystemRuntimeConfigLoader loader, IFileSystem fileSystem)
100100
{
101101
logger.LogInformation($"{PRODUCT_NAME} {ProductInfo.GetProductVersion()}");
102102
if (!IsEntityProvided(Entity, logger, command: "update"))

src/Cli/ConfigGenerator.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,21 @@ public static void SetLoggerForCliConfigGenerator(
3535
/// <summary>
3636
/// This method will generate the initial config with databaseType and connection-string.
3737
/// </summary>
38-
public static bool TryGenerateConfig(InitOptions options, RuntimeConfigLoader loader, IFileSystem fileSystem)
38+
public static bool TryGenerateConfig(InitOptions options, FileSystemRuntimeConfigLoader loader, IFileSystem fileSystem)
3939
{
40-
string runtimeConfigFile = RuntimeConfigLoader.DEFAULT_CONFIG_FILE_NAME;
40+
string runtimeConfigFile = FileSystemRuntimeConfigLoader.DEFAULT_CONFIG_FILE_NAME;
4141
if (!string.IsNullOrWhiteSpace(options.Config))
4242
{
4343
_logger.LogInformation("Generating user provided config file with name: {configFileName}", options.Config);
4444
runtimeConfigFile = options.Config;
4545
}
4646
else
4747
{
48-
string? environmentValue = Environment.GetEnvironmentVariable(RuntimeConfigLoader.RUNTIME_ENVIRONMENT_VAR_NAME);
48+
string? environmentValue = Environment.GetEnvironmentVariable(FileSystemRuntimeConfigLoader.RUNTIME_ENVIRONMENT_VAR_NAME);
4949
if (!string.IsNullOrWhiteSpace(environmentValue))
5050
{
51-
_logger.LogInformation("The environment variable {variableName} has a value of {variableValue}", RuntimeConfigLoader.RUNTIME_ENVIRONMENT_VAR_NAME, environmentValue);
52-
runtimeConfigFile = RuntimeConfigLoader.GetEnvironmentFileName(RuntimeConfigLoader.CONFIGFILE_NAME, environmentValue);
51+
_logger.LogInformation("The environment variable {variableName} has a value of {variableValue}", FileSystemRuntimeConfigLoader.RUNTIME_ENVIRONMENT_VAR_NAME, environmentValue);
52+
runtimeConfigFile = FileSystemRuntimeConfigLoader.GetEnvironmentFileName(FileSystemRuntimeConfigLoader.CONFIGFILE_NAME, environmentValue);
5353
_logger.LogInformation("Generating environment config file: {configPath}", fileSystem.Path.GetFullPath(runtimeConfigFile));
5454
}
5555
else
@@ -81,7 +81,7 @@ public static bool TryGenerateConfig(InitOptions options, RuntimeConfigLoader lo
8181
/// <param name="options">Init options</param>
8282
/// <param name="runtimeConfig">Output runtime config json.</param>
8383
/// <returns>True on success. False otherwise.</returns>
84-
public static bool TryCreateRuntimeConfig(InitOptions options, RuntimeConfigLoader loader, IFileSystem fileSystem, [NotNullWhen(true)] out RuntimeConfig? runtimeConfig)
84+
public static bool TryCreateRuntimeConfig(InitOptions options, FileSystemRuntimeConfigLoader loader, IFileSystem fileSystem, [NotNullWhen(true)] out RuntimeConfig? runtimeConfig)
8585
{
8686
runtimeConfig = null;
8787

@@ -225,7 +225,7 @@ public static bool TryCreateRuntimeConfig(InitOptions options, RuntimeConfigLoad
225225
/// This method will add a new Entity with the given REST and GraphQL endpoints, source, and permissions.
226226
/// It also supports fields that needs to be included or excluded for a given role and operation.
227227
/// </summary>
228-
public static bool TryAddEntityToConfigWithOptions(AddOptions options, RuntimeConfigLoader loader, IFileSystem fileSystem)
228+
public static bool TryAddEntityToConfigWithOptions(AddOptions options, FileSystemRuntimeConfigLoader loader, IFileSystem fileSystem)
229229
{
230230
if (!TryGetConfigFileBasedOnCliPrecedence(loader, options.Config, out string runtimeConfigFile))
231231
{
@@ -455,7 +455,7 @@ public static bool TryCreateSourceObjectForNewEntity(
455455
/// This method will update an existing Entity with the given REST and GraphQL endpoints, source, and permissions.
456456
/// It also supports updating fields that need to be included or excluded for a given role and operation.
457457
/// </summary>
458-
public static bool TryUpdateEntityWithOptions(UpdateOptions options, RuntimeConfigLoader loader, IFileSystem fileSystem)
458+
public static bool TryUpdateEntityWithOptions(UpdateOptions options, FileSystemRuntimeConfigLoader loader, IFileSystem fileSystem)
459459
{
460460
if (!TryGetConfigFileBasedOnCliPrecedence(loader, options.Config, out string runtimeConfigFile))
461461
{
@@ -951,7 +951,7 @@ public static bool VerifyCanUpdateRelationship(RuntimeConfig runtimeConfig, stri
951951
/// overrides < environmentConfig < defaultConfig
952952
/// Also preforms validation to check connection string is not null or empty.
953953
/// </summary>
954-
public static bool TryStartEngineWithOptions(StartOptions options, RuntimeConfigLoader loader, IFileSystem fileSystem)
954+
public static bool TryStartEngineWithOptions(StartOptions options, FileSystemRuntimeConfigLoader loader, IFileSystem fileSystem)
955955
{
956956
string? configToBeUsed = options.Config;
957957
if (string.IsNullOrEmpty(configToBeUsed) && ConfigMerger.TryMergeConfigsIfAvailable(fileSystem, loader, _logger, out configToBeUsed))

0 commit comments

Comments
 (0)