Skip to content

Commit 12bfe67

Browse files
committed
Merge pull request Azure#1594 from hovsepm/dev
[#110120350] Subscriptions and tenants can be listed correctly without specifying -tenantID
2 parents 13dc0e2 + 9497268 commit 12bfe67

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public AzureRMProfile Login(
6868
var token = AcquireAccessToken(account, environment, tenantId, password, promptBehavior);
6969
if(TryGetTenantSubscription(token, account, environment, tenantId, subscriptionId, subscriptionName, out newSubscription, out newTenant))
7070
{
71-
account.SetProperty(AzureAccount.Property.Tenants, new[] { newTenant.Id.ToString() });
71+
account.SetOrAppendProperty(AzureAccount.Property.Tenants, new[] { newTenant.Id.ToString() });
7272
}
7373
}
7474
// (tenant is not provided and subscription is present) OR
@@ -240,7 +240,7 @@ private void SwitchSubscription(AzureSubscription subscription)
240240

241241
public List<AzureTenant> ListTenants(string tenant)
242242
{
243-
return ListAccountTenants(_profile.Context.Account, _profile.Context.Environment, null, ShowDialog.Auto)
243+
return ListAccountTenants(_profile.Context.Account, _profile.Context.Environment, null, ShowDialog.Never)
244244
.Where(t => tenant == null ||
245245
tenant.Equals(t.Id.ToString(), StringComparison.OrdinalIgnoreCase) ||
246246
tenant.Equals(t.Domain, StringComparison.OrdinalIgnoreCase))
@@ -380,7 +380,9 @@ public IEnumerable<AzureSubscription> ListSubscriptions()
380380
{
381381
try
382382
{
383-
subscriptions.AddRange(ListSubscriptions(tenant.Id.ToString()));
383+
subscriptions.AddRange(
384+
ListSubscriptions(
385+
(tenant.Id == Guid.Empty) ? tenant.Domain:tenant.Id.ToString()));
384386
}
385387
catch (AadAuthenticationException)
386388
{
@@ -481,19 +483,24 @@ private bool TryGetTenantSubscription(IAccessToken accessToken,
481483
}
482484
else
483485
{
484-
var subscriptions = subscriptionClient.Subscriptions.List().Subscriptions;
485-
if (subscriptions != null && subscriptions.Any())
486+
var subscriptions = (subscriptionClient.Subscriptions.List().Subscriptions ??
487+
new List<Microsoft.Azure.Subscriptions.Models.Subscription>())
488+
.Where(s => "enabled".Equals(s.State, StringComparison.OrdinalIgnoreCase) ||
489+
"warned".Equals(s.State, StringComparison.OrdinalIgnoreCase));
490+
491+
if (subscriptions.Any())
486492
{
487493
if (subscriptionName != null)
488494
{
489-
subscriptionFromServer = subscriptions.FirstOrDefault(s => s.DisplayName.Equals(subscriptionName, StringComparison.OrdinalIgnoreCase));
495+
subscriptionFromServer = subscriptions.FirstOrDefault(
496+
s => s.DisplayName.Equals(subscriptionName, StringComparison.OrdinalIgnoreCase));
490497
}
491498
else
492499
{
493-
if (subscriptions.Count > 1)
500+
if (subscriptions.Count() > 1)
494501
{
495502
WriteWarningMessage(string.Format(
496-
"TenantId '{0}' contains more than one subscription. First one will be selected for further use. " +
503+
"TenantId '{0}' contains more than one active subscription. First one will be selected for further use. " +
497504
"To select another subscription, use Set-AzureRmContext.",
498505
tenantId));
499506
}
@@ -569,10 +576,21 @@ private List<AzureTenant> ListAccountTenants(AzureAccount account, AzureEnvironm
569576
{
570577
result =
571578
account.GetPropertyAsArray(AzureAccount.Property.Tenants)
572-
.Select( ti => new AzureTenant()
573-
{
574-
Id = new Guid(ti),
575-
Domain = AccessTokenExtensions.GetDomain(account.Id)
579+
.Select( ti => {
580+
var tenant = new AzureTenant();
581+
582+
Guid guid;
583+
if(Guid.TryParse(ti, out guid))
584+
{
585+
tenant.Id = guid;
586+
tenant.Domain = AccessTokenExtensions.GetDomain(account.Id);
587+
}
588+
else
589+
{
590+
tenant.Domain = ti;
591+
}
592+
593+
return tenant;
576594
}).ToList();
577595
}
578596

@@ -608,7 +626,7 @@ private IEnumerable<AzureSubscription> ListSubscriptionsForTenant(AzureAccount a
608626
subscriptions.Subscriptions.Select(
609627
(s) =>
610628
s.ToAzureSubscription(new AzureContext(_profile.Context.Subscription, account,
611-
environment, CreateTenantFromString(tenantId))));
629+
environment, CreateTenantFromString(tenantId, accessToken.TenantId))));
612630
}
613631

614632
return new List<AzureSubscription>();
@@ -623,7 +641,7 @@ private void WriteWarningMessage(string message)
623641
}
624642
}
625643

626-
private static AzureTenant CreateTenantFromString(string tenantOrDomain)
644+
private static AzureTenant CreateTenantFromString(string tenantOrDomain, string accessTokenTenantId)
627645
{
628646
AzureTenant result = new AzureTenant();
629647
Guid id;
@@ -633,6 +651,7 @@ private static AzureTenant CreateTenantFromString(string tenantOrDomain)
633651
}
634652
else
635653
{
654+
result.Id = Guid.Parse(accessTokenTenantId);
636655
result.Domain = tenantOrDomain;
637656
}
638657

0 commit comments

Comments
 (0)