Ejemplo n.º 1
0
        protected async Task <IEnumerable <IAzureSubscriptionContext> > GetSubscriptionContextsAsync()
        {
            IEnumerable <IAzureSubscriptionContext> subscriptions = Enumerable.Empty <IAzureSubscriptionContext>();

            Account account = await this.GetAccountAsync();

            if (account != null && !account.NeedsReauthentication)
            {
                IAzureUserAccount azureUserAccount = this.accountPickerViewModel.AuthenticationManager.UserAccounts.FirstOrDefault(a => a.UniqueId == account.UniqueId);

                if (azureUserAccount != null)
                {
                    try
                    {
                        subscriptions = await azureUserAccount.GetSubscriptionsAsync(false).ConfigureAwait(false);
                    }
                    catch (Exception)
                    {
                        // User cancelled out of the login prompt, etc. - ignore exception and return no subscriptions
                    }
                }
            }

            return(subscriptions);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Returns true if given user account equals this class
 /// </summary>
 public bool Equals(IAzureUserAccount other)
 {
     return(other != null &&
            CommonUtil.SameString(other.UniqueId, UniqueId) &&
            CommonUtil.SameString(other.TenantId, TenantId));
     // TODO probably should check the AllTenants field
 }
 /// <summary>
 /// Default constructor to initialize the subscription identifier
 /// </summary>
 public AzureSubscriptionIdentifier(IAzureUserAccount userAccount, string tenantId, string subscriptionId, Uri serviceManagementEndpoint)
 {
     UserAccount               = userAccount;
     TenantId                  = tenantId;
     SubscriptionId            = subscriptionId;
     ServiceManagementEndpoint = serviceManagementEndpoint;
 }
 private void CopyFrom(IAzureUserAccount azureUserAccount)
 {
     this.DisplayInfo           = new AzureUserAccountDisplayInfo(azureUserAccount.DisplayInfo);
     this.NeedsReauthentication = azureUserAccount.NeedsReauthentication;
     this.TenantId   = azureUserAccount.TenantId;
     this.AllTenants = azureUserAccount.AllTenants;
     this.UniqueId   = azureUserAccount.UniqueId;
     AzureUserAccount account = azureUserAccount as AzureUserAccount;
 }
Ejemplo n.º 5
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>());
        }
Ejemplo n.º 6
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>());
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Default constructor to initializes user session
 /// </summary>
 public AzureUserAccount(IAzureUserAccount azureUserAccount)
 {
     CopyFrom(azureUserAccount);
 }
Ejemplo n.º 8
0
 public static bool SameUserAccount(IAzureUserAccount value1, IAzureUserAccount value2)
 {
     return((value1 == null && value2 == null) || (value2 != null && value2.Equals(value1)));
 }
Ejemplo n.º 9
0
        /// <summary>
        /// Gets all subscription contexts under a specific user account. Queries all tenants for the account and uses these to log in
        /// and retrieve subscription information as needed
        /// </summary>
        public async Task <IEnumerable <IAzureUserAccountSubscriptionContext> > GetSubscriptionContextsAsync(IAzureUserAccount userAccount)
        {
            List <IAzureUserAccountSubscriptionContext> contexts = new List <IAzureUserAccountSubscriptionContext>();
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            ServiceResponse <IAzureUserAccountSubscriptionContext> response = await AzureUtil.ExecuteGetAzureResourceAsParallel(
                userAccount, userAccount.AllTenants, string.Empty, CancellationToken.None, GetSubscriptionsForTentantAsync);

            if (response.HasError)
            {
                var ex = response.Errors.First();
                throw new AzureResourceFailedException(
                          string.Format(CultureInfo.CurrentCulture, SR.FailedToGetAzureSubscriptionsErrorMessage, ex.Message));
            }
            contexts.AddRange(response.Data);
            stopwatch.Stop();
            TraceEvent(TraceEventType.Verbose, (int)TraceId.AzureResource, "Time taken to get all subscriptions was {0}ms", stopwatch.ElapsedMilliseconds.ToString());
            return(contexts);
        }