Esempio n. 1
0
        /// <summary>
        /// Creates credential instance for given subscription
        /// </summary>
        private ServiceClientCredentials CreateCredentials(IAzureUserAccountSubscriptionContext subscriptionContext)
        {
            AzureUserAccountSubscriptionContext azureUserSubContext =
                subscriptionContext as AzureUserAccountSubscriptionContext;

            if (azureUserSubContext != null)
            {
                return(azureUserSubContext.Credentials);
            }
            throw new NotSupportedException("This uses an unknown subscription type");
        }
Esempio n. 2
0
        private async Task <ServiceResponse <IAzureUserAccountSubscriptionContext> > GetSubscriptionsForTentantAsync(
            IAzureUserAccount userAccount, IAzureTenant tenant, string lookupKey,
            CancellationToken cancellationToken, CancellationToken internalCancellationToken)
        {
            AzureTenant azureTenant = tenant as AzureTenant;

            if (azureTenant != null)
            {
                ServiceClientCredentials credentials = CreateCredentials(azureTenant);
                using (SubscriptionClient client = new SubscriptionClient(_resourceManagementUri, credentials))
                {
                    IEnumerable <Subscription> subs = await GetSubscriptionsAsync(client);

                    return(new ServiceResponse <IAzureUserAccountSubscriptionContext>(subs.Select(sub =>
                    {
                        AzureSubscriptionIdentifier subId = new AzureSubscriptionIdentifier(userAccount, azureTenant.TenantId, sub.SubscriptionId, _resourceManagementUri);
                        AzureUserAccountSubscriptionContext context = new AzureUserAccountSubscriptionContext(subId, credentials);
                        return context;
                    })));
                }
            }
            return(new ServiceResponse <IAzureUserAccountSubscriptionContext>());
        }
Esempio n. 3
0
        private async Task <ServiceResponse <IAzureUserAccountSubscriptionContext> > GetSubscriptionsForTentantAsync(
            IAzureUserAccount userAccount, IAzureTenant tenant, string lookupKey,
            CancellationToken cancellationToken, CancellationToken internalCancellationToken)
        {
            AzureTenant azureTenant = tenant as AzureTenant;

            if (azureTenant != null)
            {
                ServiceClientCredentials credentials = CreateCredentials(azureTenant);
                string armEndpoint = userAccount.UnderlyingAccount.Properties.ProviderSettings?.Settings?.ArmResource?.Endpoint;
                Uri    armUri      = null;
                if (armEndpoint != null)
                {
                    try
                    {
                        armUri = new Uri(armEndpoint);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine($"Exception while parsing URI: {e.Message}");
                    }
                }
                using (SubscriptionClient client = new SubscriptionClient(armUri ?? _resourceManagementUri, credentials))
                {
                    IEnumerable <Subscription> subs = await GetSubscriptionsAsync(client);

                    return(new ServiceResponse <IAzureUserAccountSubscriptionContext>(subs.Select(sub =>
                    {
                        AzureSubscriptionIdentifier subId = new AzureSubscriptionIdentifier(userAccount, azureTenant.TenantId, sub.SubscriptionId, armUri ?? _resourceManagementUri);
                        AzureUserAccountSubscriptionContext context = new AzureUserAccountSubscriptionContext(subId, credentials);
                        return context;
                    })));
                }
            }
            return(new ServiceResponse <IAzureUserAccountSubscriptionContext>());
        }