Skip to content

[#110120350] Subscriptions and tenants can be listed correctly without specifying -tenantID #1594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 7, 2016
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public AzureRMProfile Login(
var token = AcquireAccessToken(account, environment, tenantId, password, promptBehavior);
if(TryGetTenantSubscription(token, account, environment, tenantId, subscriptionId, subscriptionName, out newSubscription, out newTenant))
{
account.SetProperty(AzureAccount.Property.Tenants, new[] { newTenant.Id.ToString() });
account.SetOrAppendProperty(AzureAccount.Property.Tenants, new[] { newTenant.Id.ToString() });
}
}
// (tenant is not provided and subscription is present) OR
Expand Down Expand Up @@ -240,7 +240,7 @@ private void SwitchSubscription(AzureSubscription subscription)

public List<AzureTenant> ListTenants(string tenant)
{
return ListAccountTenants(_profile.Context.Account, _profile.Context.Environment, null, ShowDialog.Auto)
return ListAccountTenants(_profile.Context.Account, _profile.Context.Environment, null, ShowDialog.Never)
.Where(t => tenant == null ||
tenant.Equals(t.Id.ToString(), StringComparison.OrdinalIgnoreCase) ||
tenant.Equals(t.Domain, StringComparison.OrdinalIgnoreCase))
Expand Down Expand Up @@ -380,7 +380,9 @@ public IEnumerable<AzureSubscription> ListSubscriptions()
{
try
{
subscriptions.AddRange(ListSubscriptions(tenant.Id.ToString()));
subscriptions.AddRange(
ListSubscriptions(
(tenant.Id == Guid.Empty) ? tenant.Domain:tenant.Id.ToString()));
}
catch (AadAuthenticationException)
{
Expand Down Expand Up @@ -481,19 +483,24 @@ private bool TryGetTenantSubscription(IAccessToken accessToken,
}
else
{
var subscriptions = subscriptionClient.Subscriptions.List().Subscriptions;
if (subscriptions != null && subscriptions.Any())
var subscriptions = (subscriptionClient.Subscriptions.List().Subscriptions ??
new List<Microsoft.Azure.Subscriptions.Models.Subscription>())
.Where(s => "enabled".Equals(s.State, StringComparison.OrdinalIgnoreCase) ||
"warned".Equals(s.State, StringComparison.OrdinalIgnoreCase));

if (subscriptions.Any())
{
if (subscriptionName != null)
{
subscriptionFromServer = subscriptions.FirstOrDefault(s => s.DisplayName.Equals(subscriptionName, StringComparison.OrdinalIgnoreCase));
subscriptionFromServer = subscriptions.FirstOrDefault(
s => s.DisplayName.Equals(subscriptionName, StringComparison.OrdinalIgnoreCase));
}
else
{
if (subscriptions.Count > 1)
if (subscriptions.Count() > 1)
{
WriteWarningMessage(string.Format(
"TenantId '{0}' contains more than one subscription. First one will be selected for further use. " +
"TenantId '{0}' contains more than one active subscription. First one will be selected for further use. " +
"To select another subscription, use Set-AzureRmContext.",
tenantId));
}
Expand Down Expand Up @@ -569,10 +576,21 @@ private List<AzureTenant> ListAccountTenants(AzureAccount account, AzureEnvironm
{
result =
account.GetPropertyAsArray(AzureAccount.Property.Tenants)
.Select( ti => new AzureTenant()
{
Id = new Guid(ti),
Domain = AccessTokenExtensions.GetDomain(account.Id)
.Select( ti => {
var tenant = new AzureTenant();

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

return tenant;
}).ToList();
}

Expand Down Expand Up @@ -608,7 +626,7 @@ private IEnumerable<AzureSubscription> ListSubscriptionsForTenant(AzureAccount a
subscriptions.Subscriptions.Select(
(s) =>
s.ToAzureSubscription(new AzureContext(_profile.Context.Subscription, account,
environment, CreateTenantFromString(tenantId))));
environment, CreateTenantFromString(tenantId, accessToken.TenantId))));
}

return new List<AzureSubscription>();
Expand All @@ -623,7 +641,7 @@ private void WriteWarningMessage(string message)
}
}

private static AzureTenant CreateTenantFromString(string tenantOrDomain)
private static AzureTenant CreateTenantFromString(string tenantOrDomain, string accessTokenTenantId)
{
AzureTenant result = new AzureTenant();
Guid id;
Expand All @@ -633,6 +651,7 @@ private static AzureTenant CreateTenantFromString(string tenantOrDomain)
}
else
{
result.Id = Guid.Parse(accessTokenTenantId);
result.Domain = tenantOrDomain;
}

Expand Down