Exemplo n.º 1
0
        private static void ConfigureSkuStore(StatelessServiceContext serviceContext)
        {
            var serializedLocations = serviceContext
                                      .CodePackageActivationContext
                                      .GetConfig <string>("ResourceProviderWebService", "Locations");
            var serializedSKUs = serviceContext
                                 .CodePackageActivationContext
                                 .GetConfig <string>("ResourceProviderWebService", "SKUs");

            IEnumerable <string> locations;

            try
            {
                locations = JsonConvert.DeserializeObject <IEnumerable <string> >(serializedLocations);
            }
            catch
            {
                locations = new string[] { string.Empty };
            }

            IEnumerable <string> skus;

            try
            {
                skus = JsonConvert.DeserializeObject <IEnumerable <string> >(serializedSKUs);
            }
            catch
            {
                skus = new string[] { string.Empty };
            }

            SkuStore.Initialize(locations, skus);
        }
Exemplo n.º 2
0
        public async Task <Account> GetAccountAsync(
            string requestId,
            string subscriptionId,
            string resourceGroupName,
            string accountName)
        {
            var tenant = await this.tenantCacheClient.GetTenantAsync(
                requestId,
                subscriptionId,
                resourceGroupName,
                accountName);

            return(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
            });
        }
Exemplo n.º 3
0
        public async Task <Account> UpdateAccountAsync(
            string requestId,
            string subscriptionId,
            string resourceGroupName,
            string accountName,
            AccountPatch accountPatch)
        {
            await this.ValidateSubscriptionRegistration(subscriptionId);

            var paramTenant = new Tenant
            {
                SubscriptionId    = subscriptionId,
                ResourceGroupName = resourceGroupName,
                AccountName       = accountName,
                Tags  = accountPatch.Tags ?? new Dictionary <string, string>(),
                State = TenantState.Active
            };

            var tenant = await this.tenantCacheClient.UpdateTenantAsync(
                requestId,
                paramTenant);

            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
            }));
        }
Exemplo n.º 4
0
        public async Task <IEnumerable <Account> > ListAccountsAsync(
            string requestId,
            string subscriptionId)
        {
            var tenants = await this.tenantCacheClient.ListTenantsAsync(
                requestId,
                subscriptionId);

            return(tenants.Select(tenant => 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
            }));
        }
Exemplo n.º 5
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
            }));
        }