Exemple #1
0
        public async Task <KeyDescription> RegenerateKeyAsync(
            string requestId,
            string subscriptionId,
            string resourceGroupName,
            string accountName,
            RegenerateKeyParameter parameter)
        {
            await this.ValidateSubscriptionRegistration(subscriptionId);

            var accountKey = new AccountKey
            {
                Name         = parameter.Name,
                IsPrimaryKey = parameter.Rank == KeyRank.PrimaryKey,
                Value        = SASHelper.GenerateKey(DefaultSASKeys.DefaultKeyLength),
            };

            var updated = await this.tenantCacheClient.ResetKeyAsync(
                requestId,
                subscriptionId,
                resourceGroupName,
                accountName,
                accountKey);

            return(new KeyDescription
            {
                Name = updated.Name,
                Rank = updated.IsPrimaryKey ? KeyRank.PrimaryKey : KeyRank.SecondaryKey,
                Value = updated.Value
            });
        }
Exemple #2
0
        public async Task <Account> CreateOrUpdateAccountAsync(
            string requestId,
            string subscriptionId,
            string resourceGroupName,
            string accountName,
            Account account)
        {
            await this.ValidateSubscriptionRegistration(subscriptionId);

            string message;

            if (!ValidateAccountName(accountName, out message))
            {
                throw new InvalidArgumentException($"Invalid account name: {message}");
            }

            var skuDescription = SkuStore.Descriptions.FirstOrDefault(description => string.Equals(description.Name, account.SKU.Name, StringComparison.OrdinalIgnoreCase));

            if (skuDescription == null)
            {
                throw new InvalidArgumentException($"SKU {account.SKU.Name} is invalid. Supported SKUs are {string.Join(", ", SkuStore.Descriptions.Select(d => d.Name))}");
            }

            var normalizedLocation = account.Location
                                     .Replace(" ", string.Empty)
                                     .ToLowerInvariant();

            if (!skuDescription.Locations.Contains(normalizedLocation, StringComparer.OrdinalIgnoreCase))
            {
                throw new InvalidArgumentException($"Location {account.Location} is invalid for SKU {account.SKU.Name}. Supported locations are {string.Join(", ", skuDescription.Locations)}");
            }

            var sku = new SKU
            {
                Name = skuDescription.Name,
                Tier = skuDescription.Tier
            };

            IReadOnlyDictionary <string, int> quotas;

            if (!SkuStore.Quotas.TryGetValue(skuDescription.Name, out quotas))
            {
                quotas = new Dictionary <string, int>();
            }

            var paramTenant = new Tenant
            {
                SubscriptionId    = subscriptionId,
                ResourceGroupName = resourceGroupName,
                AccountName       = accountName,
                Location          = normalizedLocation,
                SKU        = sku.Name,
                Tags       = account.Tags ?? new Dictionary <string, string>(),
                State      = TenantState.Active,
                ResourceId = ResourceIdHelper.GetAccountId(
                    subscriptionId,
                    resourceGroupName,
                    accountName)
            };

            var tenant = await this.tenantCacheClient.CreateOrUpdateTenantAsync(
                requestId,
                paramTenant,
                DefaultSASKeys.DefaultKeyNames.Select(name => new AuthenticationRule
            {
                KeyName = name,
                PrimaryKey = SASHelper.GenerateKey(DefaultSASKeys.DefaultKeyLength),
                SecondaryKey = SASHelper.GenerateKey(DefaultSASKeys.DefaultKeyLength),
            }).ToArray(),
                quotas.ToDictionary(pair => pair.Key, pair => pair.Value));

            // Check subscription registration state to handle racing condition
            if (!await this.store.IsSubscriptionRegisteredAsync(subscriptionId))
            {
                tenant.IsDisabled = true;
                await this.tenantCacheClient.UpdateTenantAsync(
                    requestId,
                    tenant);
            }

            return(await Task.FromResult(new Account
            {
                Id = ResourceIdHelper.GetAccountId(
                    tenant.SubscriptionId,
                    tenant.ResourceGroupName,
                    tenant.AccountName),
                Name = tenant.AccountName,
                Type = NameStore.FullyQualifiedAccountResourceType,
                Location = tenant.Location,
                SKU = SkuStore.GetSKU(tenant.SKU),
                Tags = tenant.Tags
            }));
        }