Example #1
0
        private List <AzureTenant> ListAccountTenants(AzureAccount account, AzureEnvironment environment, SecureString password, ShowDialog promptBehavior)
        {
            List <AzureTenant> result = new List <AzureTenant>();

            try
            {
                var commonTenantToken = AcquireAccessToken(account, environment, AuthenticationFactory.CommonAdTenant,
                                                           password, promptBehavior);

                using (var subscriptionClient = AzureSession.ClientFactory.CreateCustomClient <SubscriptionClient>(
                           new TokenCloudCredentials(commonTenantToken.AccessToken),
                           environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager)))
                {
                    //TODO: Fix subscription client to not require subscriptionId
                    result = account.MergeTenants(subscriptionClient.Tenants.List().TenantIds, commonTenantToken);
                }
            }
            catch
            {
                WriteWarningMessage(string.Format(Microsoft.Azure.Commands.Profile.Properties.Resources.UnableToAqcuireToken, AuthenticationFactory.CommonAdTenant));
                if (account.IsPropertySet(AzureAccount.Property.Tenants))
                {
                    result =
                        account.GetPropertyAsArray(AzureAccount.Property.Tenants)
                        .Select(ti => new AzureTenant()
                    {
                        Id     = new Guid(ti),
                        Domain = AccessTokenExtensions.GetDomain(account.Id)
                    }).ToList();
                }
            }

            return(result);
        }
        private List <AzureTenant> ListAccountTenants(
            IAzureAccount account,
            IAzureEnvironment environment,
            SecureString password,
            string promptBehavior,
            Action <string> promptAction)
        {
            IList <AzureTenant> result = new List <AzureTenant>();
            var commonTenant           = account.GetCommonTenant();

            try
            {
                var commonTenantToken = AcquireAccessToken(
                    account,
                    environment,
                    commonTenant,
                    password,
                    promptBehavior,
                    promptAction);

                result = SubscriptionAndTenantClient?.ListAccountTenants(commonTenantToken, environment);
            }
            catch
            {
                WriteWarningMessage(string.Format(ProfileMessages.UnableToAqcuireToken, commonTenant));
                if (account.IsPropertySet(AzureAccount.Property.Tenants))
                {
                    result =
                        account.GetPropertyAsArray(AzureAccount.Property.Tenants)
                        .Select(ti =>
                    {
                        var tenant = new AzureTenant();

                        Guid guid;
                        if (Guid.TryParse(ti, out guid))
                        {
                            tenant.Id        = ti;
                            tenant.Directory = AccessTokenExtensions.GetDomain(account.Id);
                        }
                        else
                        {
                            tenant.Directory = ti;
                        }

                        return(tenant);
                    }).ToList();
                }
                if (!result.Any())
                {
                    throw;
                }
            }

            return(result.ToList());
        }
Example #3
0
        private List <AzureTenant> ListAccountTenants(
            IAzureAccount account,
            IAzureEnvironment environment,
            SecureString password,
            string promptBehavior,
            Action <string> promptAction)
        {
            List <AzureTenant> result = new List <AzureTenant>();
            var commonTenant          = GetCommonTenant(account);

            try
            {
                var commonTenantToken = AcquireAccessToken(
                    account,
                    environment,
                    commonTenant,
                    password,
                    promptBehavior,
                    promptAction);

                SubscriptionClient subscriptionClient = null;
                try
                {
                    subscriptionClient = AzureSession.Instance.ClientFactory.CreateCustomArmClient <SubscriptionClient>(
                        environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager),
                        new TokenCredentials(commonTenantToken.AccessToken) as ServiceClientCredentials,
                        AzureSession.Instance.ClientFactory.GetCustomHandlers());
                    //TODO: Fix subscription client to not require subscriptionId
                    result = account.MergeTenants(subscriptionClient.Tenants.List(), commonTenantToken);
                }
                finally
                {
                    // In test mode, we are reusing the client since disposing of it will
                    // fail some tests (due to HttpClient being null)
                    if (subscriptionClient != null && !TestMockSupport.RunningMocked)
                    {
                        subscriptionClient.Dispose();
                    }
                }
            }
            catch
            {
                WriteWarningMessage(string.Format(ProfileMessages.UnableToAqcuireToken, commonTenant));
                if (account.IsPropertySet(AzureAccount.Property.Tenants))
                {
                    result =
                        account.GetPropertyAsArray(AzureAccount.Property.Tenants)
                        .Select(ti =>
                    {
                        var tenant = new AzureTenant();

                        Guid guid;
                        if (Guid.TryParse(ti, out guid))
                        {
                            tenant.Id        = ti;
                            tenant.Directory = AccessTokenExtensions.GetDomain(account.Id);
                        }
                        else
                        {
                            tenant.Directory = ti;
                        }

                        return(tenant);
                    }).ToList();
                }
                if (!result.Any())
                {
                    throw;
                }
            }

            return(result);
        }