コード例 #1
0
 public TenantInitializeDetails(TenantInitializeDetails other)
     : base(other)
 {
     this.DatabaseServer = other.DatabaseServer;
     this.DatabaseName   = other.DatabaseName;
     this.TenantId       = other.TenantId;
 }
コード例 #2
0
        private async Task AddTenantToShardManagerAsync(TenantInitializeDetails details, string shardMapName)
        {
            var shardMapId = (await Smdb.ShardMapsGlobal.Where(z => z.Name == shardMapName).FirstAsync()).ShardMapId;

            var shardId         = Guid.NewGuid();
            var shardVersion    = Guid.NewGuid();
            var lastOperationId = Guid.NewGuid();

            var finder = new MyDummyTenantFinder(details.DatabaseName, details.TenantId);

            using (var rdb = new TraffkTenantModelDbContext(RdbOptions, finder, new ConfigStringFormatter(finder)
            {
            }))
            {
                rdb.ShardMapsLocal.Add(new ShardMapsLocal {
                    ShardMapId      = shardMapId,
                    Name            = shardMapName,
                    MapType         = 1,
                    KeyType         = 1,
                    LastOperationId = lastOperationId
                });
                rdb.ShardsLocal.Add(new ShardsLocal {
                    ShardId         = shardId,
                    Version         = shardVersion,
                    ShardMapId      = shardMapId,
                    Protocol        = 0,
                    ServerName      = details.DatabaseServer,
                    Port            = 0,
                    DatabaseName    = details.DatabaseName,
                    Status          = 1,
                    LastOperationId = lastOperationId
                });
                await rdb.SaveChangesAsync();
            }

            Smdb.ShardsGlobal.Add(new ShardsGlobal {
                ShardId      = shardId,
                Readable     = true,
                Version      = shardVersion,
                ShardMapId   = shardMapId,
                Protocol     = 0,
                ServerName   = ConfigOptions.Value.FullyQualifiedServerName,
                Port         = 0,
                DatabaseName = details.DatabaseName,
                Status       = 1
            });
            await Smdb.SaveChangesAsync();
        }
コード例 #3
0
        private async Task CreateTenantAsync(TenantCreationDetails details)
        {
            var sqlAzureManagementConfiguration = ConfigOptions.Value;
            var cred = await CredentialFactory.GetAsync(SqlAzureManagementApiResource);

            var c = new SqlManagementClient(cred);

            c.SubscriptionId = sqlAzureManagementConfiguration.SubscriptionId;
            Stuff.Noop(c.Capabilities);
            foreach (var s in c.Servers.List())
            {
                if (s.Name == sqlAzureManagementConfiguration.ServerName)
                {
                    foreach (var db in c.Databases.ListByServer(sqlAzureManagementConfiguration.ResourceGroupName, s.Name))
                    {
                        if (db.Name == sqlAzureManagementConfiguration.TenantModelDatabaseName)
                        {
                            var newDatabaseName = string.Format(sqlAzureManagementConfiguration.NewTenantDatabaseNameFormat, details.TenantName);
                            await CopyDatabaseInitiateAsync(s, db, newDatabaseName);

                            var tenantId = await Tdb.TenantIdReserveAsync(newDatabaseName);

                            var id = new TenantInitializeDetails(details)
                            {
                                DatabaseServer = s.Name,
                                DatabaseName   = newDatabaseName,
                                TenantId       = tenantId
                            };
                            var initJob = BackgroundJob.ContinueWith <ITenantManagementJobs>(JobInfo.JobId.ToString(), j => j.InitializeNewTenantAsync(id));
                            initJob = BackgroundJob.ContinueWith <ITenantManagementJobs>(initJob, j => j.AddTenantToShardManagerAsync(id));
                            var bj = (IBackgroundJobClient) new TenantedBackgroundJobClient(GlobalContext, new HardcodedTraffkTenantFinder(tenantId));
                            bj.ContinueWith <ITenantJobs>(initJob, j => j.ReconfigureFiscalYears(new Bal.Settings.FiscalYearSettings {
                                CalendarMonth = 1, CalendarYear = 2000, FiscalYear = 2000
                            }));
                            return;
                        }
                    }
                }
            }
            throw new Exception($"Could not find Model database [{sqlAzureManagementConfiguration.TenantModelDatabaseName}] on server [{sqlAzureManagementConfiguration.ServerName}]");
        }
コード例 #4
0
        async Task ITenantManagementJobs.InitializeNewTenantAsync(TenantInitializeDetails details)
        {
            var finder = new MyDummyTenantFinder(details.DatabaseName, details.TenantId);

            using (var rdb = new TraffkTenantModelDbContext(RdbOptions, finder, new ConfigStringFormatter(finder)
            {
            }))
            {
                try
                {
                    rdb.Database.OpenConnection();
                }
                catch (Exception sex)
                {
                    throw new Exception("Database has not been created yet", sex);
                }
                using (var trans = await rdb.Database.BeginTransactionAsync())
                {
                    var t = await rdb.Tenants.FindAsync(details.TenantId);

                    if (t == null)
                    {
                        t = new Tenant
                        {
                            TenantId   = details.TenantId,
                            TenantName = details.TenantName,
                            TenantType = Tenant.TenantTypes.Normal,
                        };
                        rdb.Tenants.Add(t);
                        rdb.Apps.Add(new App
                        {
                            Tenant  = t,
                            AppType = AppTypes.Portal,
                            AppName = AppTypes.Portal.ToString(),
                        });
                        await rdb.SaveChangesAsync();

                        var r = new ApplicationRole
                        {
                            Tenant         = t,
                            Name           = "Bootstrap",
                            NormalizedName = "bootstrap",
                        };
                        rdb.Roles.Add(r);
                        var u = new ApplicationUser
                        {
                            Tenant             = t,
                            Email              = details.AdminUsername,
                            NormalizedEmail    = details.AdminUsername.ToUpper(),
                            NormalizedUserName = details.AdminUsername.ToUpper(),
                        };
                        rdb.Users.Add(u);
                        await rdb.SaveChangesAsync();

                        u.PasswordHash = PasswordHasher.HashPassword(u, details.AdminPassword);
                        rdb.UserRoles.Add(new ApplicationUserRole
                        {
                            UserId = u.Id,
                            RoleId = r.Id,
                        });
                        foreach (var p in new[] {
                            PermissionNames.ManageJobs,
                            PermissionNames.ManageUsers,
                            PermissionNames.ManageRoles,
                            PermissionNames.ManageTenants,
                            PermissionNames.CustomerRelationshipData,
                            PermissionNames.DirectMessaging,
                            PermissionNames.ReleaseLog,
                        })
                        {
                            rdb.RoleClaims.Add(new RoleClaim
                            {
                                RoleId     = r.Id,
                                ClaimType  = PermissionHelpers.CreateClaimType(p),
                                ClaimValue = new PermissionClaimValue(true).ToJson()
                            });
                        }
                    }
                    await rdb.SaveChangesAsync();

                    trans.Commit();
                }
            }
        }
コード例 #5
0
 async Task ITenantManagementJobs.AddTenantToShardManagerAsync(TenantInitializeDetails details)
 {
     await AddTenantToShardManagerAsync(details, ConfigOptions.Value.TraffkDirectoryShardMap);
     await AddTenantToShardManagerAsync(details, ConfigOptions.Value.TraffkTenantShardMapName);
 }