// ReSharper disable once UnusedMember.Global // ReSharper disable once MemberCanBePrivate.Global public async Task CreateTenantAsync(IOspSession systemSession, string databaseName, string tenantId) { ArgumentValidation.Validate(nameof(systemSession), systemSession); ArgumentValidation.ValidateString(nameof(databaseName), databaseName); ArgumentValidation.ValidateString(nameof(tenantId), tenantId); var normalizedDatabaseName = databaseName.ToLower(); var normalizedTenantId = databaseName.MakeKey(); if (await IsTenantExistingAsync(systemSession, normalizedTenantId)) { throw new TenantException($"Tenant '{normalizedTenantId}' already exists."); } if (await IsDatabaseAlreadyExistingAsync(normalizedDatabaseName)) { throw new DatabaseException($"Database '{normalizedDatabaseName}' already exists."); } await _repositoryClient.CreateRepositoryAsync(normalizedDatabaseName); await _repositoryClient.CreateUser(systemSession, _systemConfiguration.AuthenticationDatabaseName, normalizedDatabaseName, string.Format(_systemConfiguration.DatabaseUser, normalizedDatabaseName), _systemConfiguration.DatabaseUserPassword); var repository = _repositoryClient.GetRepository(normalizedDatabaseName); await repository.CreateCollectionIfNotExistsAsync <CkAttribute>(); await repository.CreateCollectionIfNotExistsAsync <CkEntity>(); await repository.CreateCollectionIfNotExistsAsync <CkEntityAssociation>(); await repository.CreateCollectionIfNotExistsAsync <CkEntityInheritance>(); await repository.CreateCollectionIfNotExistsAsync <RtAssociation>(); var ospTenant = new SystemEntities.OspTenant { TenantId = normalizedTenantId, DatabaseName = normalizedDatabaseName }; await _tenantCollection.InsertAsync(systemSession, ospTenant); await RestoreTenantSystemCkModelAsync(systemSession, ospTenant); }
// ReSharper disable once UnusedMember.Global // ReSharper disable once MemberCanBePrivate.Global public async Task AttachTenantAsync(IOspSession systemSession, string databaseName, string tenantId) { ArgumentValidation.Validate(nameof(systemSession), systemSession); ArgumentValidation.ValidateString(nameof(databaseName), databaseName); ArgumentValidation.ValidateString(nameof(tenantId), tenantId); if (await IsTenantExistingAsync(systemSession, tenantId)) { throw new TenantException($"Tenant '{tenantId}' already exists."); } if (!await IsDatabaseAlreadyExistingAsync(databaseName)) { throw new DatabaseException($"Database '{databaseName}' does not exist."); } var ospTenant = new SystemEntities.OspTenant { TenantId = tenantId, DatabaseName = databaseName }; await _tenantCollection.InsertAsync(systemSession, ospTenant); }
private async Task RestoreTenantSystemCkModelAsync(IOspSession systemSession, SystemEntities.OspTenant ospTenant) { var ckModelFilePath = Path.Combine(Helper.AssemblyDirectory, "CKModel.json"); Logger.Info("Importing construction kit model '{0}'", ckModelFilePath); await ImportCkModelAsync(systemSession, ospTenant.TenantId, ScopeIds.System, ckModelFilePath, null); Logger.Info("Construction kit model imported."); }