Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TenantedCosmosWorkflowStoreFactory"/> class.
        /// </summary>
        /// <param name="containerFactory">
        /// The <see cref="ICosmosContainerSourceWithTenantLegacyTransition"/> that will be used to
        /// create underlying <see cref="Container"/> instances for the content stores.
        /// </param>
        /// <param name="optionsFactory">
        /// Gets Cosmos Client options with suitable serialization configuration.
        /// </param>
        /// <param name="logicalDatabaseName">
        /// The logical Cosmos database name to use when creating or retrieving containers.
        /// </param>
        /// <param name="logicalDefinitionContainerName">
        /// The logical name for the Cosmos container that holds workflow definitions.
        /// </param>
        /// <param name="definitionPartitionKeyPath">
        /// The partition key path for the workflow definition Cosmos container.
        /// </param>
        public TenantedCosmosWorkflowStoreFactory(
            ICosmosContainerSourceWithTenantLegacyTransition containerFactory,
            ICosmosOptionsFactory optionsFactory,
            string logicalDatabaseName,
            string logicalDefinitionContainerName,
            string definitionPartitionKeyPath)
        {
            this.containerFactory               = containerFactory;
            this.logicalDatabaseName            = logicalDatabaseName;
            this.logicalDefinitionContainerName = logicalDefinitionContainerName;
            this.definitionPartitionKeyPath     = definitionPartitionKeyPath;

            this.clientOptions = optionsFactory.CreateCosmosClientOptions();
        }
        public static void SetupCosmosDbRepository(FeatureContext featureContext)
        {
            IServiceProvider serviceProvider = ContainerBindings.GetServiceProvider(featureContext);
            ICosmosContainerSourceWithTenantLegacyTransition factory = serviceProvider.GetRequiredService <ICosmosContainerSourceWithTenantLegacyTransition>();
            ICosmosOptionsFactory optionsFactory = serviceProvider.GetRequiredService <ICosmosOptionsFactory>();
            ITenantProvider       tenantProvider = serviceProvider.GetRequiredService <ITenantProvider>();
            IConfiguration        configuration  = serviceProvider.GetRequiredService <IConfiguration>();

            ITenant rootTenant = tenantProvider.Root;

            LegacyV2CosmosContainerConfiguration cosmosConfig =
                configuration.GetSection("TestCosmosConfiguration").Get <LegacyV2CosmosContainerConfiguration>()
                ?? new LegacyV2CosmosContainerConfiguration();

            cosmosConfig.DatabaseName          = "endjinspecssharedthroughput";
            cosmosConfig.DisableTenantIdPrefix = true;

            tenantProvider.Root.UpdateProperties(data => data.Append(new KeyValuePair <string, object>(
                                                                         $"StorageConfiguration__{TenantedCosmosWorkflowStoreServiceCollectionExtensions.WorkflowStoreLogicalDatabaseName}__{TenantedCosmosWorkflowStoreServiceCollectionExtensions.WorkflowDefinitionStoreLogicalContainerName}",
                                                                         cosmosConfig)));

            tenantProvider.Root.UpdateProperties(data => data.Append(new KeyValuePair <string, object>(
                                                                         $"StorageConfiguration__{TenantedCosmosWorkflowStoreServiceCollectionExtensions.WorkflowStoreLogicalDatabaseName}__{TenantedCosmosWorkflowStoreServiceCollectionExtensions.WorkflowInstanceStoreLogicalContainerName}",
                                                                         cosmosConfig)));

            tenantProvider.Root.UpdateProperties(data => data.Append(new KeyValuePair <string, object>(
                                                                         "StorageConfiguration__workflow__testdocuments",
                                                                         cosmosConfig)));

            Newtonsoft.Json.JsonSerializerSettings jsonSettings = serviceProvider.GetRequiredService <IJsonSerializerSettingsProvider>().Instance;
            Container testDocumentsRepository = WorkflowRetryHelper.ExecuteWithStandardTestRetryRulesAsync(
                async() => await factory.GetContainerForTenantAsync(
                    rootTenant,
                    "StorageConfiguration__workflow__testdocuments",
                    "NotUsed",
                    "workflow",
                    "testdocuments",
                    "/id",
                    cosmosClientOptions: optionsFactory.CreateCosmosClientOptions())).Result;

            featureContext.Set(testDocumentsRepository, TestDocumentsRepository);
        }
        public static void SetupDatabases(FeatureContext featureContext)
        {
            IServiceProvider serviceProvider = ContainerBindings.GetServiceProvider(featureContext);
            ISqlConnectionFromDynamicConfiguration           sqlConnectionFactory = serviceProvider.GetRequiredService <ISqlConnectionFromDynamicConfiguration>();
            ICosmosContainerSourceWithTenantLegacyTransition factory = serviceProvider.GetRequiredService <ICosmosContainerSourceWithTenantLegacyTransition>();
            ICosmosOptionsFactory optionsFactory = serviceProvider.GetRequiredService <ICosmosOptionsFactory>();
            ITenantProvider       tenantProvider = serviceProvider.GetRequiredService <ITenantProvider>();
            IConfiguration        configuration  = serviceProvider.GetRequiredService <IConfiguration>();

            string containerBase = Guid.NewGuid().ToString();
            string databaseName  = $"workflow-{containerBase}";

            SqlDatabaseConfiguration sqlConfig = new()
            {
                ConnectionStringPlainText = $"Server=(localdb)\\mssqllocaldb;Initial Catalog={databaseName};Trusted_Connection=True;MultipleActiveResultSets=true",
            };

            tenantProvider.Root.UpdateProperties(data => data.AddSqlDatabaseConfiguration(
                                                     TenantedSqlWorkflowStoreServiceCollectionExtensions.WorkflowConnectionKey,
                                                     sqlConfig));

            LegacyV2CosmosContainerConfiguration cosmosConfig =
                configuration.GetSection("TestCosmosConfiguration").Get <LegacyV2CosmosContainerConfiguration>()
                ?? new LegacyV2CosmosContainerConfiguration();

            cosmosConfig.DatabaseName          = "endjinspecssharedthroughput";
            cosmosConfig.DisableTenantIdPrefix = true;

            // Configuring with V2-style configuration because for now, the application uses the
            // ICosmosContainerSourceWithTenantLegacyTransition in the mode where all the tenant
            // configuration remains in V2 mode.
            tenantProvider.Root.UpdateProperties(data => data.Append(new KeyValuePair <string, object>(
                                                                         $"StorageConfiguration__{TenantedCosmosWorkflowStoreServiceCollectionExtensions.WorkflowStoreLogicalDatabaseName}__{TenantedCosmosWorkflowStoreServiceCollectionExtensions.WorkflowDefinitionStoreLogicalContainerName}",
                                                                         cosmosConfig)));

            tenantProvider.Root.UpdateProperties(data => data.Append(new KeyValuePair <string, object>(
                                                                         $"StorageConfiguration__{TenantedCosmosWorkflowStoreServiceCollectionExtensions.WorkflowStoreLogicalDatabaseName}__{TenantedCosmosWorkflowStoreServiceCollectionExtensions.WorkflowInstanceStoreLogicalContainerName}",
                                                                         cosmosConfig)));

            tenantProvider.Root.UpdateProperties(data => data.Append(new KeyValuePair <string, object>(
                                                                         "StorageConfiguration__workflow__testdocuments",
                                                                         cosmosConfig)));

            // This is required by the various bits of the test that work with the "data catalog".
            Container testDocumentsRepository = WorkflowRetryHelper.ExecuteWithStandardTestRetryRulesAsync(
                async() => await factory.GetContainerForTenantAsync(
                    tenantProvider.Root,
                    "StorageConfiguration__workflow__testdocuments",
                    "NotUsed",
                    "workflow",
                    "testdocuments",
                    "/id",
                    cosmosClientOptions: optionsFactory.CreateCosmosClientOptions())).Result;

            featureContext.Set(testDocumentsRepository, TestDocumentsRepository);

            // And now, deploy the sql server for this instance.
#if DEBUG
            const string BUILD = "debug";
#else
            const string BUILD = "release";
#endif

            SqlHelpers.SetupDatabaseFromDacPac(sqlConfig.ConnectionStringPlainText, databaseName, @$ "..\..\..\..\Marain.Workflow.Storage.Sql.Database\bin\{BUILD}\Marain.Workflow.Storage.Sql.Database.dacpac");