Skip to content

Commit 3c9899c

Browse files
[Account] Remove extra table from interactive context selection process (#25520)
* remove extra table * refine code * refine code * refine code * fix test cases --------- Co-authored-by: Yabo Hu <[email protected]>
1 parent 3b6e3c7 commit 3c9899c

File tree

5 files changed

+24
-56
lines changed

5 files changed

+24
-56
lines changed

src/Accounts/Accounts.Test/AzureRMProfileTestsForMultitenant.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,8 @@ public void LoginByTenant()
318318
null,
319319
false,
320320
mockOpenIDConfig.Object,
321-
null);
321+
null,
322+
IsInteractiveContextSelectionEnabled: false);
322323

323324
Assert.Equal("2021-01-01", client.SubscriptionAndTenantClient.ApiVersion);
324325
Assert.Equal(tenantA, azureRmProfile.DefaultContext.Tenant.Id.ToString());

src/Accounts/Accounts/Account/ConnectAzureRmAccount.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ public override void ExecuteCmdlet()
538538
}
539539

540540
profileClient.WarningLog = (message) => _tasks.Enqueue(new Task(() => this.WriteWarning(message)));
541-
profileClient.InformationLog = (message) => _tasks.Enqueue(new Task(() => WriteInteractiveInformation(message)));
541+
profileClient.InteractiveInformationLog = (message) => _tasks.Enqueue(new Task(() => WriteInteractiveInformation(message)));
542542
profileClient.DebugLog = (message) => _tasks.Enqueue(new Task(() => this.WriteDebugWithTimestamp(message)));
543543
profileClient.PromptAndReadLine = (message) =>
544544
{
@@ -561,7 +561,6 @@ public override void ExecuteCmdlet()
561561
shouldPopulateContextList,
562562
MaxContextPopulation,
563563
resourceId,
564-
IsInteractiveAuthenticationFlow(),
565564
IsInteractiveContextSelectionEnabled()));
566565
task.Start();
567566
while (!task.IsCompleted)
@@ -614,11 +613,6 @@ private bool IsInteractiveContextSelectionEnabled()
614613
return AzureSession.Instance.TryGetComponent<IConfigManager>(nameof(IConfigManager), out IConfigManager configManager) ? configManager.GetConfigValue<LoginExperienceConfig>(ConfigKeys.LoginExperienceV2).Equals(LoginExperienceConfig.On) : true;
615614
}
616615

617-
private bool IsInteractiveAuthenticationFlow()
618-
{
619-
return ParameterSetName.Equals(UserParameterSet);
620-
}
621-
622616
private bool IsPopUpInteractiveAuthenticationFlow()
623617
{
624618
return ParameterSetName.Equals(UserParameterSet) && UseDeviceAuthentication.IsPresent == false;
@@ -634,7 +628,7 @@ private void ValidateActionRequiredMessageCanBePresented()
634628

635629
private void WriteInteractiveInformation(string message)
636630
{
637-
if (IsInteractiveAuthenticationFlow())
631+
if (ParameterSetName.Equals(UserParameterSet))
638632
{
639633
this.WriteInformation(message, false);
640634
}

src/Accounts/Accounts/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
-->
2020

2121
## Upcoming Release
22+
* Removed the informational table about selected context to avoid duplication with output table.
2223

2324
## Version 3.0.2
2425
* Fixed bug handling GUID type subscription Id.

src/Accounts/Accounts/Models/RMProfileClient.cs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,21 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
14-
using Microsoft.Azure.Commands.Common;
1514
using Microsoft.Azure.Commands.Common.Authentication;
1615
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
1716
using Microsoft.Azure.Commands.Common.Authentication.Models;
1817
using Microsoft.Azure.Commands.Common.Authentication.ResourceManager;
19-
using Microsoft.Azure.Commands.Common.Exceptions;
2018
using Microsoft.Azure.Commands.Profile.Models;
2119
using Microsoft.Azure.Commands.Profile.Properties;
2220
using Microsoft.Azure.Commands.Profile.Utilities;
23-
using Microsoft.Azure.Management.Profiles.Storage.Version2019_06_01.Models;
24-
using Microsoft.Identity.Client.NativeInterop;
2521
using Microsoft.Rest.Azure;
2622
using Microsoft.WindowsAzure.Commands.Common;
2723

2824
using System;
2925
using System.Collections.Generic;
3026
using System.Linq;
3127
using System.Management.Automation;
32-
using System.Runtime.InteropServices;
3328
using System.Security;
34-
using System.Security.Authentication.ExtendedProtection;
35-
using System.Text;
3629

3730
using AuthenticationMessages = Microsoft.Azure.Commands.Common.Authentication.Properties.Resources;
3831
using ProfileMessages = Microsoft.Azure.Commands.Profile.Properties.Resources;
@@ -46,7 +39,7 @@ public class RMProfileClient
4639
private IAzureTokenCache _cache;
4740
public Action<string> WarningLog;
4841
public Action<string> DebugLog;
49-
public Action<string> InformationLog;
42+
public Action<string> InteractiveInformationLog;
5043
internal Func<string, string> PromptAndReadLine;
5144

5245
private List<AzureTenant> _queriedTenants = new List<AzureTenant>();
@@ -137,18 +130,21 @@ public AzureRmProfile Login(
137130
bool shouldPopulateContextList = true,
138131
int maxContextPopulation = Profile.ConnectAzureRmAccountCommand.DefaultMaxContextPopulation,
139132
string authScope = null,
140-
bool isInteractiveAuthenticationFlow = false,
141133
bool IsInteractiveContextSelectionEnabled = true)
142134
{
143-
if(isInteractiveAuthenticationFlow) WriteInformationMessage($"{PSStyle.ForegroundColor.BrightYellow}{Resources.PleaseSelectAccount}{PSStyle.Reset}{System.Environment.NewLine}");
135+
136+
WriteInteractiveInformationMessage($"{PSStyle.ForegroundColor.BrightYellow}{Resources.PleaseSelectAccount}{PSStyle.Reset}{System.Environment.NewLine}");
144137

145138
IAzureSubscription defaultSubscription = null;
146139
IAzureTenant defaultTenant = null;
147140
List<AzureSubscription> subscriptions = new List<AzureSubscription>();
148141
List<AzureSubscription> tempSubscriptions = null;
149142
string tenantName = null;
150143

151-
bool selectSubscriptionFromList = isInteractiveAuthenticationFlow && IsInteractiveContextSelectionEnabled && string.IsNullOrEmpty(subscriptionId) && string.IsNullOrEmpty(subscriptionName);
144+
bool selectSubscriptionFromList = AzureAccount.AccountType.User.Equals(account.Type) &&
145+
IsInteractiveContextSelectionEnabled &&
146+
string.IsNullOrEmpty(subscriptionId) &&
147+
string.IsNullOrEmpty(subscriptionName);
152148
var lastUsedSubscription = selectSubscriptionFromList ? _profile?.DefaultContext?.Subscription : null;
153149

154150
string promptBehavior =
@@ -242,7 +238,7 @@ public AzureRmProfile Login(
242238
throw new ArgumentNullException(string.Format($"{e.Message}{Environment.NewLine}{baseMessage} {typeMessage}"), e);
243239
}
244240

245-
WriteInformationMessage(Resources.RetrievingSubscription);
241+
WriteInteractiveInformationMessage(Resources.RetrievingSubscription);
246242
tempSubscriptions = null;
247243
if (TryGetTenantSubscription(
248244
token,
@@ -253,8 +249,7 @@ public AzureRmProfile Login(
253249
true,
254250
out defaultSubscription,
255251
out defaultTenant,
256-
out tempSubscriptions,
257-
isInteractiveAuthenticationFlow))
252+
out tempSubscriptions))
258253
{
259254
account.SetOrAppendProperty(AzureAccount.Property.Tenants, new[] { defaultTenant.Id.ToString() });
260255

@@ -267,7 +262,7 @@ public AzureRmProfile Login(
267262
{
268263
InteractiveSubscriptionSelectionHelper.SelectSubscriptionFromList(
269264
subscriptions, _queriedTenants, tenantIdOrName, tenantName, lastUsedSubscription,
270-
Prompt, WriteInformationMessage,
265+
Prompt, WriteInteractiveInformationMessage,
271266
ref defaultSubscription, ref defaultTenant);
272267
}
273268
}
@@ -283,7 +278,7 @@ public AzureRmProfile Login(
283278
IAzureSubscription tempSubscription = null;
284279
tempSubscriptions = null;
285280

286-
WriteInformationMessage(Resources.RetrievingSubscription);
281+
WriteInteractiveInformationMessage(Resources.RetrievingSubscription);
287282

288283
foreach (var tenant in _queriedTenants)
289284
{
@@ -323,7 +318,7 @@ public AzureRmProfile Login(
323318

324319
if (token != null &&
325320
(defaultTenant == null || selectSubscriptionFromList) &&
326-
TryGetTenantSubscription(token, account, environment, subscriptionId, subscriptionName, false, out tempSubscription, out tempTenant, out tempSubscriptions, isInteractiveAuthenticationFlow))
321+
TryGetTenantSubscription(token, account, environment, subscriptionId, subscriptionName, false, out tempSubscription, out tempTenant, out tempSubscriptions))
327322
{
328323
// If no subscription found for the given token/tenant,discard tempTenant value.
329324
// Continue to look for matched subscripitons until one subscription retrived by its home tenant is found.
@@ -350,7 +345,7 @@ public AzureRmProfile Login(
350345
{
351346
InteractiveSubscriptionSelectionHelper.SelectSubscriptionFromList(
352347
subscriptions, _queriedTenants, tenantIdOrName, tenantName, lastUsedSubscription,
353-
Prompt, WriteInformationMessage,
348+
Prompt, WriteInteractiveInformationMessage,
354349
ref defaultSubscription, ref defaultTenant);
355350
}
356351
}
@@ -385,7 +380,7 @@ public AzureRmProfile Login(
385380
WriteWarningMessage(string.Format(ProfileMessages.CannotSetDefaultContext, defaultContext.ToString()));
386381
}
387382

388-
if (!skipValidation && !defaultSubscription.State.Equals("Enabled", StringComparison.OrdinalIgnoreCase))
383+
if (!skipValidation && null != defaultSubscription.State && !defaultSubscription.State.Equals("Enabled", StringComparison.OrdinalIgnoreCase))
389384
{
390385
WriteWarningMessage(string.Format(
391386
ProfileMessages.SelectedSubscriptionNotActive,
@@ -707,9 +702,9 @@ private bool TryGetTenantSubscription(IAccessToken accessToken,
707702
bool isTenantPresent,
708703
out IAzureSubscription subscription,
709704
out IAzureTenant tenant,
710-
out List<AzureSubscription> subscriptions,
711-
bool isInteractiveAuthentication)
705+
out List<AzureSubscription> subscriptions)
712706
{
707+
bool isInteractiveAuthentication = AzureAccount.AccountType.User.Equals(account.Type);
713708
subscriptions = new List<AzureSubscription>();
714709
subscription = null;
715710
if (accessToken != null)
@@ -881,11 +876,11 @@ private void WriteDebugMessage(string message)
881876
DebugLog(message);
882877
}
883878
}
884-
private void WriteInformationMessage(string message)
879+
private void WriteInteractiveInformationMessage(string message)
885880
{
886-
if (InformationLog != null)
881+
if (InteractiveInformationLog != null)
887882
{
888-
InformationLog(message);
883+
InteractiveInformationLog(message);
889884
}
890885
}
891886

src/Accounts/Accounts/Utilities/InteractiveSubscriptionSelectionHelper.cs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ internal static void SelectSubscriptionFromList(IEnumerable<IAzureSubscription>
7676
{
7777
defaultTenant.ExtendedProperties.Add(AzureTenant.Property.DisplayName, tenantName);
7878
}
79-
WriteSelectedSubscriptionTable(defaultSubscription?.Name ?? defaultSubscription?.Id,
80-
defaultTenant?.GetProperty(AzureTenant.Property.DisplayName) ?? tenantName ?? defaultTenant?.Id,
81-
outputAction, columnSubNameWidth, columnTenantWidth, columnIndentsWidth);
8279
}
8380
catch (ArgumentOutOfRangeException)
8481
{
@@ -149,26 +146,6 @@ private static void WriteSubscriptionSelectionTableRow(int subIndex, IAzureSubsc
149146

150147
}
151148

152-
private static void WriteSelectedSubscriptionTable(string subscription, string tenant,
153-
Action<string> outputAction, int columnSubNameWidth, int columnTenantWidth, int columnIndentsWidth)
154-
{
155-
string columnSubNameTab = "Subscription name", columnTenantTab = "Tenant name";
156-
string separator = "-",
157-
ColumSubNameSeparator = new StringBuilder().Insert(0, separator, columnSubNameWidth).ToString(),
158-
ColumTenantSeparator = new StringBuilder().Insert(0, separator, columnTenantWidth).ToString();
159-
160-
outputAction("");
161-
outputAction($"{String.Format($"{{0,-{columnSubNameWidth + columnIndentsWidth}}}", columnSubNameTab)}" +
162-
$"{String.Format($"{{0,-{columnTenantWidth + columnIndentsWidth}}}", columnTenantTab)}");
163-
outputAction($"{String.Format($"{{0,-{columnSubNameWidth + columnIndentsWidth}}}", ColumSubNameSeparator)}" +
164-
$"{String.Format($"{{0,-{columnTenantWidth}}}", ColumTenantSeparator)}");
165-
string truncatedSubName = subscription?.Length > columnSubNameWidth ? $"{subscription.Substring(0, columnSubNameWidth - 3)}..." : subscription;
166-
string truncatedTenantName = tenant?.Length > columnTenantWidth ? $"{tenant.Substring(0, columnTenantWidth - 3)}..." : tenant;
167-
string subNameRowValue = String.Format($"{{0,-{columnSubNameWidth + columnIndentsWidth}}}", truncatedSubName),
168-
tenantDomainNameRowValue = String.Format($"{{0,-{columnTenantWidth}}}", truncatedTenantName);
169-
outputAction($"{subNameRowValue}{tenantDomainNameRowValue}");
170-
}
171-
172149
public static IAzureTenant GetDetailedTenantFromQueryHistory(List<AzureTenant> queriedTenants, string tenantId)
173150
{
174151
return queriedTenants?.Where(t => t.Id.Equals(tenantId, StringComparison.OrdinalIgnoreCase))?.FirstOrDefault();

0 commit comments

Comments
 (0)