From 89f12bf9876e22ad205df0f9f68472cf3fd5e60c Mon Sep 17 00:00:00 2001 From: Bhaskar Mallapragada Date: Fri, 2 Dec 2022 15:52:39 -0800 Subject: [PATCH 1/2] Set the database name through configuration --- src/Service.Tests/Configuration/ConfigurationTests.cs | 6 +++++- src/Service/Configurations/RuntimeConfigProvider.cs | 10 +++++++++- src/Service/Controllers/ConfigurationController.cs | 7 +++++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Service.Tests/Configuration/ConfigurationTests.cs b/src/Service.Tests/Configuration/ConfigurationTests.cs index d1ecd6d0f6..8629bccf4b 100644 --- a/src/Service.Tests/Configuration/ConfigurationTests.cs +++ b/src/Service.Tests/Configuration/ConfigurationTests.cs @@ -43,6 +43,7 @@ public class ConfigurationTests private const string POST_STARTUP_CONFIG_ENTITY = "Book"; private const string POST_STARTUP_CONFIG_ENTITY_SOURCE = "books"; private const string POST_STARTUP_CONFIG_ROLE = "PostStartupConfigRole"; + private const string COSMOS_DATABASE_NAME = "config_db"; private const int RETRY_COUNT = 5; private const int RETRY_WAIT_SECONDS = 1; @@ -428,6 +429,8 @@ public async Task TestSettingConfigurationCreatesCorrectClasses() Assert.AreEqual(DatabaseType.cosmos, configuration.DatabaseType, "Expected cosmos database type after configuring the runtime with cosmos settings."); Assert.AreEqual(config.Schema, configuration.DataSource.CosmosDbNoSql.GraphQLSchema, "Expected the schema in the configuration to match the one sent to the configuration endpoint."); Assert.AreEqual(config.ConnectionString, configuration.ConnectionString, "Expected the connection string in the configuration to match the one sent to the configuration endpoint."); + string db = configProvider.GetRuntimeConfiguration().DataSource.CosmosDbNoSql.Database; + Assert.AreEqual(COSMOS_DATABASE_NAME, db, "Expect the database in the runtime config to match the one sent to the configuration endpoint."); } [TestMethod("Validates that an exception is thrown if there's a null model in filter parser.")] @@ -916,7 +919,8 @@ private static ConfigurationPostParameters GetCosmosConfigurationParameters() File.ReadAllText(cosmosFile), File.ReadAllText("schema.gql"), "AccountEndpoint=https://localhost:8081/;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", - AccessToken: null); + AccessToken: null, + Database: COSMOS_DATABASE_NAME); } /// diff --git a/src/Service/Configurations/RuntimeConfigProvider.cs b/src/Service/Configurations/RuntimeConfigProvider.cs index 6510c2167d..daf728d86a 100644 --- a/src/Service/Configurations/RuntimeConfigProvider.cs +++ b/src/Service/Configurations/RuntimeConfigProvider.cs @@ -213,12 +213,14 @@ public static bool LoadRuntimeConfigValue( /// The GraphQL Schema. Can be left null for SQL configurations. /// The connection string to the database. /// The string representation of a managed identity access token + /// The name of the database to be used for Cosmos /// useful to connect to the database. public void Initialize( string configuration, string? schema, string connectionString, - string? accessToken) + string? accessToken, + string? database = null) { if (string.IsNullOrEmpty(connectionString)) { @@ -247,6 +249,12 @@ public void Initialize( } CosmosDbOptions? cosmosDb = RuntimeConfiguration.DataSource.CosmosDbNoSql! with { GraphQLSchema = schema }; + + if (!string.IsNullOrEmpty(database)) + { + cosmosDb = cosmosDb with { Database = database }; + } + DataSource dataSource = RuntimeConfiguration.DataSource with { CosmosDbNoSql = cosmosDb }; RuntimeConfiguration = RuntimeConfiguration with { DataSource = dataSource }; } diff --git a/src/Service/Controllers/ConfigurationController.cs b/src/Service/Controllers/ConfigurationController.cs index 73f5a6f6b1..52d6990256 100644 --- a/src/Service/Controllers/ConfigurationController.cs +++ b/src/Service/Controllers/ConfigurationController.cs @@ -32,7 +32,8 @@ public ActionResult Index([FromBody] ConfigurationPostParameters configuration) configuration.Configuration, configuration.Schema, configuration.ConnectionString, - configuration.AccessToken); + configuration.AccessToken, + configuration.Database); return new OkResult(); } @@ -45,10 +46,12 @@ public ActionResult Index([FromBody] ConfigurationPostParameters configuration) /// The GraphQL schema. Can be left empty for SQL databases. /// The database connection string. /// The managed identity access token (if any) used to connect to the database. + /// The name of the database to be used for Cosmos public record class ConfigurationPostParameters( string Configuration, string? Schema, string ConnectionString, - string? AccessToken) + string? AccessToken, + string? Database = null) { } } From ff8901b89173d9821095911089d06c9e66c0e21f Mon Sep 17 00:00:00 2001 From: Bhaskar Mallapragada Date: Fri, 2 Dec 2022 16:26:02 -0800 Subject: [PATCH 2/2] Modifying test string Co-authored-by: Sean Leonard --- src/Service.Tests/Configuration/ConfigurationTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Service.Tests/Configuration/ConfigurationTests.cs b/src/Service.Tests/Configuration/ConfigurationTests.cs index 8629bccf4b..fad77091b6 100644 --- a/src/Service.Tests/Configuration/ConfigurationTests.cs +++ b/src/Service.Tests/Configuration/ConfigurationTests.cs @@ -430,7 +430,7 @@ public async Task TestSettingConfigurationCreatesCorrectClasses() Assert.AreEqual(config.Schema, configuration.DataSource.CosmosDbNoSql.GraphQLSchema, "Expected the schema in the configuration to match the one sent to the configuration endpoint."); Assert.AreEqual(config.ConnectionString, configuration.ConnectionString, "Expected the connection string in the configuration to match the one sent to the configuration endpoint."); string db = configProvider.GetRuntimeConfiguration().DataSource.CosmosDbNoSql.Database; - Assert.AreEqual(COSMOS_DATABASE_NAME, db, "Expect the database in the runtime config to match the one sent to the configuration endpoint."); + Assert.AreEqual(COSMOS_DATABASE_NAME, db, "Expected the database name in the runtime config to match the one sent to the configuration endpoint."); } [TestMethod("Validates that an exception is thrown if there's a null model in filter parser.")]