diff --git a/src/Service.Tests/Configuration/ConfigurationTests.cs b/src/Service.Tests/Configuration/ConfigurationTests.cs index 5692311c73..7965fed602 100644 --- a/src/Service.Tests/Configuration/ConfigurationTests.cs +++ b/src/Service.Tests/Configuration/ConfigurationTests.cs @@ -44,6 +44,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; @@ -429,6 +430,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, "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.")] @@ -975,7 +978,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) { } }