Example #1
0
        public async Task <Tenant> UpdateTenantAsync(
            Tenant tenant)
        {
            using (var ctx = new AdminEntities(this.connectionString))
            {
                var entity = await ctx.Tenants.GetAsync(
                    tenant.SubscriptionId,
                    tenant.ResourceGroupName,
                    tenant.AccountName);

                // Update-able properties: Tags, State and Address
                entity.Tags         = JsonConvert.SerializeObject(tenant.Tags);
                entity.State        = tenant.State.ToString();
                entity.Address      = tenant.Address;
                entity.LastModified = DateTime.UtcNow;

                if (tenant.IsDisabled.HasValue)
                {
                    entity.IsDisabled = tenant.IsDisabled;
                }

                await ctx.SaveChangesAsync();

                return(entity.ToTenant());
            }
        }
Example #2
0
        public async Task RemoveQuotaAsync(
            string engagementAccount,
            string quotaName)
        {
            using (var ctx = new AdminEntities(this.connectionString))
            {
                var existing = await ctx.Quotas.SingleOrDefaultAsync(
                    q => q.AccountName == engagementAccount &&
                    q.QuotaName == quotaName);

                if (existing != null)
                {
                    ctx.Quotas.Remove(existing);
                    await ctx.SaveChangesAsync();
                }
            }
        }
Example #3
0
        public async Task DeleteTenantAsync(
            string subscriptionId,
            string resourceGroupName,
            string accountName)
        {
            using (var ctx = new AdminEntities(this.connectionString))
            {
                var entity = await ctx.Tenants.GetAsync(
                    subscriptionId,
                    resourceGroupName,
                    accountName);

                ctx.Tenants.Remove(entity);

                var quotaEntities = ctx.Quotas.Where(e => e.AccountName == accountName);
                ctx.Quotas.RemoveRange(quotaEntities);

                await ctx.SaveChangesAsync();
            }
        }
Example #4
0
        private async Task <Tenant> WithTenantDescriptionAsync(
            string subscriptionId,
            string resourceGroupName,
            string accountName,
            Action <TenantDescription> action,
            int maxRetry)
        {
            var retry = 0;

            while (true)
            {
                using (var ctx = new AdminEntities(this.connectionString))
                {
                    var entity = await ctx.Tenants.GetAsync(
                        subscriptionId,
                        resourceGroupName,
                        accountName);

                    var description = JsonConvert.DeserializeObject <TenantDescription>(entity.ResourceDescription);

                    action(description);

                    entity.ResourceDescription = JsonConvert.SerializeObject(description);
                    entity.LastModified        = DateTime.UtcNow;

                    try
                    {
                        await ctx.SaveChangesAsync();

                        return(entity.ToTenant());
                    }
                    catch (DbUpdateConcurrencyException)
                    {
                        if (++retry >= maxRetry)
                        {
                            throw new ConcurrencyException("Failed due to concurrency update on the same entity. Please try it again later");
                        }
                    }
                }
            }
        }
Example #5
0
        public async Task PushQuotaRemindingAsync(
            QuotaMetadata metadata,
            int reminding,
            DateTime synchronizeTime)
        {
            using (var ctx = new AdminEntities(this.connectionString))
            {
                var entity = await ctx.Quotas.SingleOrDefaultAsync(t => t.AccountName == metadata.AccountName && t.QuotaName == metadata.QuotaName);

                if (entity == null)
                {
                    throw new ResourceNotFoundException($"No quota {metadata.AccountName}/{metadata.QuotaName}");
                }

                if (metadata.IsCurrentOrNewerSlot(entity.LastUpdatedTime))
                {
                    entity.Remaining       = reminding;
                    entity.LastUpdatedTime = synchronizeTime;
                    await ctx.SaveChangesAsync();
                }
            }
        }
Example #6
0
        public async Task <QuotaEntity> CreateOrUpdateQuotaAsync(
            string engagementAccount,
            string quotaName,
            int quota)
        {
            using (var ctx = new AdminEntities(this.connectionString))
            {
                var existing = await ctx.Quotas.SingleOrDefaultAsync(
                    q => q.AccountName == engagementAccount &&
                    q.QuotaName == quotaName);

                var now = DateTime.UtcNow;
                if (existing != null)
                {
                    existing.Quota           = quota;
                    existing.LastUpdatedTime = now;
                }
                else
                {
                    existing = new QuotaEntity
                    {
                        AccountName     = engagementAccount,
                        QuotaName       = quotaName,
                        Remaining       = quota,
                        Quota           = quota,
                        CreatedTime     = now,
                        LastUpdatedTime = now
                    };
                    ctx.Quotas.Add(existing);
                }

                await ctx.SaveChangesAsync();

                return(existing);
            }
        }
Example #7
0
        public async Task <Tenant> CreateOrUpdateTenantAsync(
            Tenant tenant,
            IEnumerable <AuthenticationRule> authenticationRules,
            IReadOnlyDictionary <string, int> quotas)
        {
            using (var ctx = new AdminEntities(this.connectionString))
            {
                var now    = DateTime.UtcNow;
                var entity = await ctx.Tenants.GetAsync(
                    tenant.SubscriptionId,
                    tenant.ResourceGroupName,
                    tenant.AccountName,
                    false);

                if (entity == null)
                {
                    entity = new TenantEntity
                    {
                        SubscriptionId      = tenant.SubscriptionId,
                        ResourceGroup       = tenant.ResourceGroupName,
                        AccountName         = tenant.AccountName,
                        Location            = tenant.Location,
                        SKU                 = tenant.SKU,
                        Tags                = JsonConvert.SerializeObject(tenant.Tags),
                        State               = tenant.State.ToString(),
                        Created             = now,
                        LastModified        = now,
                        Address             = tenant.Address,
                        ResourceDescription = JsonConvert.SerializeObject(new TenantDescription
                        {
                            AuthenticationRules = authenticationRules.ToList(),
                            ChannelSettings     = new List <ChannelSetting>()
                        }),
                        ResourceId = tenant.ResourceId
                    };
                    ctx.Tenants.Add(entity);

                    ctx.Quotas.AddRange(quotas.Select(pair => new QuotaEntity
                    {
                        AccountName     = tenant.AccountName,
                        QuotaName       = pair.Key,
                        Remaining       = pair.Value,
                        Quota           = pair.Value,
                        CreatedTime     = now,
                        LastUpdatedTime = now
                    }));
                }
                else
                {
                    // Update-able properties: Tags, State and Address
                    entity.Tags         = JsonConvert.SerializeObject(tenant.Tags);
                    entity.State        = tenant.State.ToString();
                    entity.Address      = tenant.Address;
                    entity.LastModified = now;
                }

                await ctx.SaveChangesAsync();

                return(entity.ToTenant());
            }
        }