Skip to content

Commit 6f79281

Browse files
author
Hovsep Mkrtchyan
committed
Fixed [#110120350] Subscriptions and tenants can be listed correctly without specifying -tenantID
1 parent dd01fcd commit 6f79281

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

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

Lines changed: 23 additions & 9 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
{
@@ -569,10 +571,21 @@ private List<AzureTenant> ListAccountTenants(AzureAccount account, AzureEnvironm
569571
{
570572
result =
571573
account.GetPropertyAsArray(AzureAccount.Property.Tenants)
572-
.Select( ti => new AzureTenant()
573-
{
574-
Id = new Guid(ti),
575-
Domain = AccessTokenExtensions.GetDomain(account.Id)
574+
.Select( ti => {
575+
var tenant = new AzureTenant();
576+
577+
Guid guid;
578+
if(Guid.TryParse(ti, out guid))
579+
{
580+
tenant.Id = guid;
581+
tenant.Domain = AccessTokenExtensions.GetDomain(account.Id);
582+
}
583+
else
584+
{
585+
tenant.Domain = ti;
586+
}
587+
588+
return tenant;
576589
}).ToList();
577590
}
578591

@@ -608,7 +621,7 @@ private IEnumerable<AzureSubscription> ListSubscriptionsForTenant(AzureAccount a
608621
subscriptions.Subscriptions.Select(
609622
(s) =>
610623
s.ToAzureSubscription(new AzureContext(_profile.Context.Subscription, account,
611-
environment, CreateTenantFromString(tenantId))));
624+
environment, CreateTenantFromString(tenantId, accessToken.TenantId))));
612625
}
613626

614627
return new List<AzureSubscription>();
@@ -623,7 +636,7 @@ private void WriteWarningMessage(string message)
623636
}
624637
}
625638

626-
private static AzureTenant CreateTenantFromString(string tenantOrDomain)
639+
private static AzureTenant CreateTenantFromString(string tenantOrDomain, string accessTokenTenantId)
627640
{
628641
AzureTenant result = new AzureTenant();
629642
Guid id;
@@ -633,6 +646,7 @@ private static AzureTenant CreateTenantFromString(string tenantOrDomain)
633646
}
634647
else
635648
{
649+
result.Id = Guid.Parse(accessTokenTenantId);
636650
result.Domain = tenantOrDomain;
637651
}
638652

0 commit comments

Comments
 (0)