Skip to content

Commit 755712c

Browse files
authored
Changed the group way from TenantId to TenantName+Id for Get-AzContext (#23959)
* group by tenantname (tenantid) * enable test cases
1 parent 0481a4f commit 755712c

File tree

8 files changed

+60
-47
lines changed

8 files changed

+60
-47
lines changed

src/Accounts/Accounts.Test/AzureRMProfileTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ public void AzurePSComletMessageQueue()
824824
}
825825

826826

827-
[Fact(Skip = "Skip as the order of subscription changes")]
827+
[Fact]
828828
[Trait(Category.AcceptanceType, Category.CheckIn)]
829829
public void GetAzureRmSubscriptionPaginatedResult()
830830
{
@@ -854,10 +854,11 @@ public void GetAzureRmSubscriptionPaginatedResult()
854854
cmdlt.InvokeEndProcessing();
855855

856856
var subscriptionName = MockSubscriptionClientFactory.GetSubscriptionNameFromId(secondList[0]);
857+
var targetSubscription = (PSAzureSubscription)commandRuntimeMock.OutputPipeline.Where(sub => ((PSAzureSubscription)sub).Id.Equals(secondList[0])).FirstOrDefault();
857858

858859
Assert.True(commandRuntimeMock.OutputPipeline.Count == 7);
859-
Assert.Equal("Disabled", ((PSAzureSubscription)commandRuntimeMock.OutputPipeline[2]).State);
860-
Assert.Equal(subscriptionName, ((PSAzureSubscription)commandRuntimeMock.OutputPipeline[2]).Name);
860+
Assert.Equal("Disabled", targetSubscription?.State);
861+
Assert.Equal(subscriptionName, targetSubscription?.Name);
861862
}
862863

863864
[Fact]

src/Accounts/Accounts.Test/SubscriptionCmdletTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void PipingWithRmContextWorks()
4646
TestRunner.RunTestScript("Test-PipingWithContext");
4747
}
4848

49-
[Fact(Skip = "Skip as the order of subscription changes")]
49+
[Fact]
5050
[Trait(Category.AcceptanceType, Category.CheckIn)]
5151
public void SetAzureRmContextWithoutSubscription()
5252
{

src/Accounts/Accounts.Test/SubscriptionCmdletTests.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ function Test-SetAzureRmContextWithoutSubscription
119119
Assert-True { $context.Subscription -ne $null }
120120
Assert-True { $context.Tenant -ne $null }
121121
Assert-AreEqual $context.Tenant.Id $firstSubscription.HomeTenantId
122-
Assert-AreEqual $context.Subscription.Id $firstSubscription.Id
123122
}
124123

125124
<#

src/Accounts/Accounts.Test/TenantCmdletMockTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public void GetTenantWithDomainParameter()
199199
Assert.Equal("https://secure.fakesite.com/xxxxx-yyyy/logintenantbranding/0/bannerlogo?ts=0000000000", ((PSAzureTenant)OutputPipeline.First()).TenantBrandingLogoUrl);
200200
}
201201

202-
[Fact(Skip = "Skip as the order of subscription changes")]
202+
[Fact]
203203
[Trait(Category.AcceptanceType, Category.CheckIn)]
204204
public void GetTenantWithoutParameters()
205205
{
@@ -240,8 +240,8 @@ public void GetTenantWithoutParameters()
240240

241241
//Verify
242242
Assert.Equal(2, OutputPipeline.Count);
243-
var tenantA = ((PSAzureTenant)OutputPipeline.First());
244-
var tenantB = ((PSAzureTenant)OutputPipeline.Last());
243+
var tenantA = (PSAzureTenant)OutputPipeline.Where(tenant => ((PSAzureTenant)tenant).Name.Equals("Microsoft"))?.FirstOrDefault();
244+
var tenantB = (PSAzureTenant)OutputPipeline.Where(tenant => ((PSAzureTenant)tenant).Name.Equals("Macrohard"))?.FirstOrDefault();
245245

246246
Assert.Equal(tenantId, tenantA.Id.ToString());
247247
Assert.Equal("Home", tenantA.TenantCategory);

src/Accounts/Accounts.Test/TenantCmdletTests.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
1717
using Microsoft.Azure.Commands.Common.Authentication.Models;
1818
using Microsoft.Azure.Commands.Profile.Models;
19+
using Microsoft.Azure.Commands.Profile.Models.Core;
1920
using Microsoft.Azure.Commands.ScenarioTest;
2021
using Microsoft.Azure.ServiceManagement.Common.Models;
2122
using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
2223
using Microsoft.WindowsAzure.Commands.ScenarioTest;
2324
using Microsoft.WindowsAzure.Commands.Utilities.Common;
25+
using System.Linq;
26+
2427
using Xunit;
2528
using Xunit.Abstractions;
2629

@@ -57,7 +60,8 @@ public void GetTenantWithTenantParameter()
5760
cmdlt.InvokeEndProcessing();
5861

5962
Assert.True(CommandRuntimeMock.OutputPipeline.Count == 2);
60-
Assert.Equal(TenantId, ((PSAzureTenant)CommandRuntimeMock.OutputPipeline[1]).Id.ToString());
63+
// skip first item as it's PSAzureContext
64+
Assert.Contains(TenantId, CommandRuntimeMock.OutputPipeline.Skip(1).Select(tenant => ((PSAzureTenant)tenant).Id));
6165
}
6266

6367
[Fact]
@@ -76,7 +80,8 @@ public void GetTenantWithDomainParameter()
7680
cmdlt.InvokeEndProcessing();
7781

7882
Assert.True(CommandRuntimeMock.OutputPipeline.Count >= 2);
79-
Assert.Equal(TenantId, ((PSAzureTenant)CommandRuntimeMock.OutputPipeline[1]).Id.ToString());
83+
// skip first item as it's PSAzureContext
84+
Assert.Contains(TenantId, CommandRuntimeMock.OutputPipeline.Skip(1).Select(tenant => ((PSAzureTenant)tenant).Id));
8085
}
8186

8287
[Fact]
@@ -94,7 +99,8 @@ public void GetTenantWithoutParameters()
9499
cmdlt.InvokeEndProcessing();
95100

96101
Assert.True(CommandRuntimeMock.OutputPipeline.Count >= 2);
97-
Assert.Equal(TenantId, ((PSAzureTenant)CommandRuntimeMock.OutputPipeline[1]).Id.ToString());
102+
// skip first item as it's PSAzureContext
103+
Assert.Contains(TenantId, CommandRuntimeMock.OutputPipeline.Skip(1).Select(tenant => ((PSAzureTenant)tenant).Id));
98104
}
99105

100106
private void Login(string subscriptionId, string tenantId)

src/Accounts/Accounts/Accounts.generated.format.ps1xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@
9797
</TableRowEntries>
9898
</TableControl>
9999
<GroupBy>
100-
<ScriptBlock>$_.Tenant.Id</ScriptBlock>
101-
<Label>TenantId</Label>
100+
<ScriptBlock>if([System.String]::IsNullOrEmpty($_.Tenant.Name)){$_.Tenant.Id}else{"$($_.Tenant.Name) ($($_.Tenant.Id))"}</ScriptBlock>
101+
<Label>Tenant</Label>
102102
</GroupBy>
103103
</View>
104104
<View>

src/Accounts/Accounts/Models/RMProfileClient.cs

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ public bool TryRemoveContext(IAzureContext context)
111111
return result;
112112
}
113113

114+
private IAzureTenant GetDetailedTenantFromQueryHistory(List<AzureTenant> tenants, string tenantId)
115+
{
116+
return tenants?.Where(t => t.Id.Equals(tenantId))?.FirstOrDefault(); ;
117+
}
118+
114119
public AzureRmProfile Login(
115120
IAzureAccount account,
116121
IAzureEnvironment environment,
@@ -126,8 +131,9 @@ public AzureRmProfile Login(
126131
int maxContextPopulation = Profile.ConnectAzureRmAccountCommand.DefaultMaxContextPopulation,
127132
string authScope = null)
128133
{
129-
IAzureSubscription newSubscription = null;
130-
IAzureTenant newTenant = null;
134+
IAzureSubscription defaultSubscription = null;
135+
IAzureTenant defaultTenant = null;
136+
List<AzureTenant> tenants = null;
131137
string promptBehavior =
132138
(password == null &&
133139
account.Type != AzureAccount.AccountType.AccessToken &&
@@ -151,15 +157,15 @@ public AzureRmProfile Login(
151157
throw new PSInvalidOperationException(Resources.SubscriptionOrTenantMissing);
152158
}
153159

154-
newSubscription = new AzureSubscription
160+
defaultSubscription = new AzureSubscription
155161
{
156162
Id = subscriptionId
157163
};
158164

159-
newSubscription.SetOrAppendProperty(AzureSubscription.Property.Tenants, tenantIdOrName);
160-
newSubscription.SetOrAppendProperty(AzureSubscription.Property.Account, account.Id);
165+
defaultSubscription.SetOrAppendProperty(AzureSubscription.Property.Tenants, tenantIdOrName);
166+
defaultSubscription.SetOrAppendProperty(AzureSubscription.Property.Account, account.Id);
161167

162-
newTenant = new AzureTenant
168+
defaultTenant = new AzureTenant
163169
{
164170
Id = tenantIdOrName
165171
};
@@ -225,18 +231,17 @@ public AzureRmProfile Login(
225231
subscriptionId,
226232
subscriptionName,
227233
true,
228-
out newSubscription,
229-
out newTenant))
234+
out defaultSubscription,
235+
out defaultTenant))
230236
{
231-
account.SetOrAppendProperty(AzureAccount.Property.Tenants, new[] { newTenant.Id.ToString() });
237+
account.SetOrAppendProperty(AzureAccount.Property.Tenants, new[] { defaultTenant.Id.ToString() });
232238
}
233239
}
234240
// (tenant is not provided and subscription is present) OR
235241
// (tenant is not provided and subscription is not provided)
236242
else
237243
{
238-
var tenants = ListAccountTenants(account, environment, password, promptBehavior, promptAction)
239-
.Select(s => s.Id.ToString()).ToList();
244+
tenants = ListAccountTenants(account, environment, password, promptBehavior, promptAction).ToList();
240245
account.SetProperty(AzureAccount.Property.Tenants, null);
241246
string accountId = null;
242247
IAzureTenant tempTenant = null;
@@ -250,57 +255,57 @@ public AzureRmProfile Login(
250255

251256
try
252257
{
253-
token = AcquireAccessToken(account, environment, tenant, password, ShowDialog.Auto, null);
258+
token = AcquireAccessToken(account, environment, tenant.Id, password, ShowDialog.Auto, null);
254259
if (accountId == null)
255260
{
256261
accountId = account.Id;
257-
account.SetOrAppendProperty(AzureAccount.Property.Tenants, tenant);
262+
account.SetOrAppendProperty(AzureAccount.Property.Tenants, tenant.Id);
258263
}
259264
else if (accountId.Equals(account.Id, StringComparison.OrdinalIgnoreCase))
260265
{
261-
account.SetOrAppendProperty(AzureAccount.Property.Tenants, tenant);
266+
account.SetOrAppendProperty(AzureAccount.Property.Tenants, tenant.Id);
262267
}
263268
else
264269
{ // if account ID is different from the first tenant account id we need to ignore current tenant
265270
WriteWarningMessage(string.Format(
266271
ProfileMessages.AccountIdMismatch,
267272
account.Id,
268-
tenant,
273+
tenant.Id,
269274
accountId));
270275
account.Id = accountId;
271276
token = null;
272277
}
273278
}
274279
catch(Exception e)
275280
{
276-
WriteWarningMessage(string.Format(ProfileMessages.UnableToAqcuireToken, tenant, e.Message));
277-
WriteDebugMessage(string.Format(ProfileMessages.UnableToAqcuireToken, tenant, e.ToString()));
281+
WriteWarningMessage(string.Format(ProfileMessages.UnableToAqcuireToken, tenant.Id, e.Message));
282+
WriteDebugMessage(string.Format(ProfileMessages.UnableToAqcuireToken, tenant.Id, e.ToString()));
278283
}
279284

280285
if (token != null &&
281-
newTenant == null &&
286+
defaultTenant == null &&
282287
TryGetTenantSubscription(token, account, environment, subscriptionId, subscriptionName, false, out tempSubscription, out tempTenant))
283288
{
284289
// If no subscription found for the given token/tenant,discard tempTenant value.
285290
// Continue to look for matched subscripitons until one subscription retrived by its home tenant is found.
286291
if (tempSubscription != null)
287292
{
288-
newSubscription = tempSubscription;
293+
defaultSubscription = tempSubscription;
289294
if (tempSubscription.GetTenant() == tempSubscription.GetHomeTenant())
290295
{
291-
newTenant = tempTenant;
296+
defaultTenant = tempTenant;
292297
}
293298
}
294299
}
295300
}
296-
newSubscription = newSubscription ?? tempSubscription;
297-
newTenant = newTenant ??
298-
(newSubscription != null ? new AzureTenant() { Id = newSubscription.GetTenant() } : tempTenant);
301+
defaultSubscription = defaultSubscription ?? tempSubscription;
302+
defaultTenant = defaultTenant ??
303+
(defaultSubscription != null ? new AzureTenant() { Id = defaultSubscription.GetTenant() } : tempTenant);
299304
}
300305
}
301306

302307
shouldPopulateContextList &= _profile.DefaultContext?.Account == null;
303-
if (newSubscription == null)
308+
if (defaultSubscription == null)
304309
{
305310
if (subscriptionId != null)
306311
{
@@ -311,25 +316,27 @@ public AzureRmProfile Login(
311316
throw new PSInvalidOperationException(String.Format(ResourceMessages.SubscriptionNameNotFound, account.Id, subscriptionName) + " " + ProfileMessages.SubscriptionNotFouldPleaseCheckConfig);
312317
}
313318

314-
var newContext = new AzureContext(account, environment, newTenant);
315-
if (!_profile.TrySetDefaultContext(name, newContext))
319+
defaultTenant = GetDetailedTenantFromQueryHistory(tenants, defaultTenant.Id) ?? defaultTenant;
320+
var defaultContext = new AzureContext(account, environment, defaultTenant);
321+
if (!_profile.TrySetDefaultContext(name, defaultContext))
316322
{
317-
WriteWarningMessage(string.Format(ProfileMessages.CannotSetDefaultContext, newContext.ToString()));
323+
WriteWarningMessage(string.Format(ProfileMessages.CannotSetDefaultContext, defaultContext.ToString()));
318324
}
319325
}
320326
else
321327
{
322-
var newContext = new AzureContext(newSubscription, account, environment, newTenant);
323-
if (!_profile.TrySetDefaultContext(name, newContext))
328+
defaultTenant = GetDetailedTenantFromQueryHistory(tenants, defaultTenant.Id) ?? defaultTenant; ;
329+
var defaultContext = new AzureContext(defaultSubscription, account, environment, defaultTenant);
330+
if (!_profile.TrySetDefaultContext(name, defaultContext))
324331
{
325-
WriteWarningMessage(string.Format(ProfileMessages.CannotSetDefaultContext, newContext.ToString()));
332+
WriteWarningMessage(string.Format(ProfileMessages.CannotSetDefaultContext, defaultContext.ToString()));
326333
}
327334

328-
if (!skipValidation && !newSubscription.State.Equals("Enabled", StringComparison.OrdinalIgnoreCase))
335+
if (!skipValidation && !defaultSubscription.State.Equals("Enabled", StringComparison.OrdinalIgnoreCase))
329336
{
330337
WriteWarningMessage(string.Format(
331338
ProfileMessages.SelectedSubscriptionNotActive,
332-
newSubscription.State));
339+
defaultSubscription.State));
333340
}
334341
}
335342

@@ -341,7 +348,7 @@ public AzureRmProfile Login(
341348

342349
foreach (var subscription in subscriptions)
343350
{
344-
IAzureTenant tempTenant = new AzureTenant()
351+
IAzureTenant tempTenant = GetDetailedTenantFromQueryHistory(tenants, subscription.GetProperty(AzureSubscription.Property.Tenants)) ?? new AzureTenant()
345352
{
346353
Id = subscription.GetProperty(AzureSubscription.Property.Tenants)
347354
};

src/Accounts/Authentication.ResourceManager/Models/PSAzureContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public PSAzureContext(PSObject other)
161161
/// <summary>
162162
/// The targeted tenant in Azure.
163163
/// </summary>
164-
[Ps1Xml(Label = "TenantId", GroupByThis = true, Target = ViewControl.Table, ScriptBlock = "$_.Tenant.Id")]
164+
[Ps1Xml(Label = "Tenant", GroupByThis = true, Target = ViewControl.Table, ScriptBlock = "if([System.String]::IsNullOrEmpty($_.Tenant.Name)){$_.Tenant.Id}else{\"$($_.Tenant.Name) ($($_.Tenant.Id))\"}")]
165165
public IAzureTenant Tenant { get; set; }
166166

167167
public IAzureTokenCache TokenCache { get; set; } = null;

0 commit comments

Comments
 (0)