Skip to content

Commit 5ea071f

Browse files
authored
Added 3 configs to config framework (#18162)
* add configs to config framework * use signed common build * Fix configs set on process scope get lost * Get-AzConfig supports filter by Scope * use official common build v1.3.58
1 parent 38d92a8 commit 5ea071f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+979
-314
lines changed

src/Accounts/Accounts/Account/ConnectAzureRmAccount.cs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@
3535
using Microsoft.Azure.Commands.Profile.Properties;
3636
using Microsoft.Azure.Commands.ResourceManager.Common;
3737
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
38+
using Microsoft.Azure.Commands.Shared.Config;
3839
using Microsoft.Azure.PowerShell.Authenticators;
3940
using Microsoft.Azure.PowerShell.Authenticators.Factories;
41+
using Microsoft.Azure.PowerShell.Common.Config;
4042
using Microsoft.Identity.Client;
4143
using Microsoft.WindowsAzure.Commands.Common;
4244
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
@@ -316,11 +318,27 @@ public override void ExecuteCmdlet()
316318
}
317319

318320
}
321+
else if (AzureSession.Instance.TryGetComponent<IConfigManager>(nameof(IConfigManager), out var configManager))
322+
{
323+
string subscriptionFromConfig = configManager.GetConfigValue<string>(ConfigKeys.DefaultSubscriptionForLogin);
324+
if (!string.IsNullOrEmpty(subscriptionFromConfig))
325+
{
326+
// user doesn't specify subscript; but DefaultSubscriptionForLogin is found in config
327+
WriteDebugWithTimestamp($"[ConnectAzureRmAccountCommand] Using default subscription \"{subscriptionFromConfig}\" from config.");
328+
if (Guid.TryParse(subscriptionFromConfig, out subscriptionIdGuid))
329+
{
330+
subscriptionId = subscriptionFromConfig;
331+
}
332+
else
333+
{
334+
subscriptionName = subscriptionFromConfig;
335+
}
336+
}
337+
}
319338

320339
if(ClientAssertionParameterSet.Equals(ParameterSetName, StringComparison.OrdinalIgnoreCase))
321340
{
322-
string suppressWarningOrErrorValue = System.Environment.GetEnvironmentVariable(BreakingChangeAttributeHelper.SUPPRESS_ERROR_OR_WARNING_MESSAGE_ENV_VARIABLE_NAME);
323-
bool.TryParse(suppressWarningOrErrorValue, out bool suppressWarningOrError);
341+
bool suppressWarningOrError = AzureSession.Instance.TryGetComponent<IConfigManager>(nameof(IConfigManager), out var configManager) && configManager.GetConfigValue<bool>(ConfigKeys.DisplayBreakingChangeWarning);
324342
if (!suppressWarningOrError)
325343
{
326344
WriteWarning("The feature related to parameter name 'FederatedToken' is under preview.");
@@ -416,16 +434,8 @@ public override void ExecuteCmdlet()
416434
&& SendCertificateChain)
417435
{
418436
azureAccount.SetProperty(AzureAccount.Property.SendCertificateChain, SendCertificateChain.ToString());
419-
bool supressWarningOrError = false;
420-
try
421-
{
422-
supressWarningOrError = bool.Parse(System.Environment.GetEnvironmentVariable(BreakingChangeAttributeHelper.SUPPRESS_ERROR_OR_WARNING_MESSAGE_ENV_VARIABLE_NAME));
423-
}
424-
catch
425-
{
426-
//if value of env variable is invalid, use default value of supressWarningOrError
427-
}
428-
if (!supressWarningOrError)
437+
bool suppressWarningOrError = AzureSession.Instance.TryGetComponent<IConfigManager>(nameof(IConfigManager), out var configManager) && configManager.GetConfigValue<bool>(ConfigKeys.DisplayBreakingChangeWarning);
438+
if (!suppressWarningOrError)
429439
{
430440
WriteWarning(Resources.PreviewFunctionMessage);
431441
}

src/Accounts/Accounts/ChangeLog.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
-->
2020

2121
## Upcoming Release
22-
* Added a preview feature allowing user to control the configurations of Azure PowerShell by using the following cmdlets:
23-
- `Get-AzConfig`
24-
- `Update-AzConfig`
25-
- `Clear-AzConfig`
22+
* Added a preview feature allowing user to control the following configurations by using `Get-AzConfig`, `Update-AzConfig` and `Clear-AzConfig`:
23+
- `DefaultSubscriptionForLogin`: Subscription name or GUID. Sets the default context for Azure PowerShell when logging in without specifying a subscription.
24+
- `DisplayBreakingChangeWarning`: Controls if warning messages for breaking changes are displayed or suppressed.
25+
- `EnableDataCollection`: When enabled, Azure PowerShell cmdlets send telemetry data to Microsoft to improve the customer experience.
2626
* Upgraded System.Reflection.DispatchProxy on Windows PowerShell [#17856]
2727
* Upgraded Azure.Identity to 1.6.0 and Azure.Core to 1.24.0
2828

src/Accounts/Accounts/CommonModule/TelemetryProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ private static AzurePSDataCollectionProfile CreateDataCollectionProfile(Action<s
223223
}
224224

225225
warningLogger(Resources.DataCollectionEnabledWarning);
226-
return new AzurePSDataCollectionProfile(true);
226+
return new AzurePSDataCollectionProfile();
227227
}
228228

229229
public void Dispose()

src/Accounts/Accounts/Config/ClearConfigCommand.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using Microsoft.Azure.Commands.Common.Exceptions;
1516
using Microsoft.Azure.PowerShell.Common.Config;
1617
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
1718
using Microsoft.WindowsAzure.Commands.Utilities.Common;
@@ -23,21 +24,19 @@
2324

2425
namespace Microsoft.Azure.Commands.Common.Authentication.Config
2526
{
26-
[Cmdlet("Clear", "AzConfig", SupportsShouldProcess = true)]
27+
[Cmdlet("Clear", "AzConfig", SupportsShouldProcess = true, DefaultParameterSetName = ClearAll)]
2728
[OutputType(typeof(bool))]
2829
[CmdletPreview(PreviewMessage)]
2930
public class ClearConfigCommand : ConfigCommandBase, IDynamicParameters
3031
{
3132
private const string ClearByKey = "ClearByKey";
3233
private const string ClearAll = "ClearAll";
3334

34-
private const string ProcessMessage = "Clear the configs that apply to \"{0}\" by the following keys: {1}.";
35+
private const string ProcessMessage = "Clear the configs that apply to {0} by the following keys: {1}.";
3536

36-
private string ContinueMessage => $"Clear all the configs that apply to \"{AppliesTo}\" in scope {Scope}?";
37-
private string ProcessTarget => $"Configs in scope {Scope}";
37+
private string ContinueMessageForClearAll => $"Clear all the configs that apply to {AppliesTo ?? "all the modules and cmdlets"} in scope {Scope}.";
3838

39-
[Parameter(ParameterSetName = ClearAll, Mandatory = true, HelpMessage = "Clear all configs.")]
40-
public SwitchParameter All { get; set; }
39+
private string ProcessTarget => $"{Scope} scope";
4140

4241
[Parameter(ParameterSetName = ClearAll, HelpMessage = "Do not ask for confirmation when clearing all configs.")]
4342
public SwitchParameter Force { get; set; }
@@ -59,6 +58,15 @@ public class ClearConfigCommand : ConfigCommandBase, IDynamicParameters
5958
}));
6059
}
6160

61+
protected override void ValidateParameters()
62+
{
63+
base.ValidateParameters();
64+
if (Scope != ConfigScope.Process && Scope != ConfigScope.CurrentUser)
65+
{
66+
throw new AzPSArgumentException($"When clearing configs, {nameof(Scope)} must be either {ConfigScope.Process} or {ConfigScope.CurrentUser}", nameof(Scope));
67+
}
68+
}
69+
6270
public override void ExecuteCmdlet()
6371
{
6472
switch (ParameterSetName)
@@ -87,7 +95,7 @@ private void ClearConfigByKey()
8795
return;
8896
}
8997
base.ConfirmAction(
90-
string.Format(ProcessMessage, AppliesTo, string.Join(", ", configKeysFromInput)),
98+
string.Format(ProcessMessage, AppliesTo ?? "all the modules and cmdlets", string.Join(", ", configKeysFromInput)),
9199
ProcessTarget,
92100
() => configKeysFromInput.ForEach(ClearConfigByKey));
93101
}
@@ -102,7 +110,7 @@ private void ClearConfigByKey(string key)
102110

103111
private void ClearAllConfigs()
104112
{
105-
ConfirmAction(Force, ContinueMessage, ContinueMessage, ProcessTarget, () =>
113+
ConfirmAction(Force, ContinueMessageForClearAll, ContinueMessageForClearAll, ProcessTarget, () =>
106114
{
107115
ConfigManager.ClearConfig(new ClearConfigOptions(null, Scope)
108116
{

src/Accounts/Accounts/Config/ConfigCommandBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public ConfigCommandBase() : base()
5151
ConfigManager = configManager;
5252
}
5353

54-
[Parameter(HelpMessage = "Specifies what part of Azure PowerShell the config applies to. Possible values are:\n- \"" + ConfigFilter.GlobalAppliesTo + "\": the config applies to all modules and cmdlets of Azure PowerShell. \n- Module name: the config applies to a certain module of Azure PowerShell. For example, \"Az.Storage\".\n- Cmdlet name: the config applies to a certain cmdlet of Azure PowerShell. For example, \"Get-AzKeyVault\".\nIf not specified, when getting configs, output will be all of the above; when updating, it defaults to \"" + ConfigFilter.GlobalAppliesTo + "\"; when clearing, configs applying to any targets are cleared.")]
54+
[Parameter(HelpMessage = "Specifies what part of Azure PowerShell the config applies to. Possible values are:\n- \"" + ConfigFilter.GlobalAppliesTo + "\": the config applies to all modules and cmdlets of Azure PowerShell.\n- Module name: the config applies to a certain module of Azure PowerShell. For example, \"Az.Storage\".\n- Cmdlet name: the config applies to a certain cmdlet of Azure PowerShell. For example, \"Get-AzKeyVault\".\nIf not specified, when getting or clearing configs, it defaults to all the above; when updating, it defaults to \"" + ConfigFilter.GlobalAppliesTo + "\".")]
5555
[ValidateNotNullOrEmpty]
5656
public string AppliesTo { get; set; }
5757

src/Accounts/Accounts/Config/GetConfigCommand.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.Azure.Commands.ResourceManager.Common;
1717
using Microsoft.Azure.PowerShell.Common.Config;
1818
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
19+
using Microsoft.WindowsAzure.Commands.Utilities.Common;
1920
using System;
2021
using System.Collections.Generic;
2122
using System.Collections.ObjectModel;
@@ -57,6 +58,10 @@ public override void ExecuteCmdlet()
5758
private ConfigFilter CreateConfigFilter()
5859
{
5960
ConfigFilter filter = new ConfigFilter() { AppliesTo = AppliesTo };
61+
if (this.IsParameterBound(c => c.Scope))
62+
{
63+
filter.Scope = Scope;
64+
}
6065
IEnumerable<string> configKeysFromInput = GetConfigsSpecifiedByUser()
6166
.Where(x => (SwitchParameter)x.Value)
6267
.Select(x => x.Key);

src/Accounts/Accounts/Config/UpdateConfigCommand.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15+
using Microsoft.Azure.Commands.Common.Exceptions;
1516
using Microsoft.Azure.Commands.Profile.Models;
1617
using Microsoft.Azure.PowerShell.Common.Config;
1718
using Microsoft.WindowsAzure.Commands.Common.CustomAttributes;
@@ -49,6 +50,15 @@ protected override void BeginProcessing()
4950
}
5051
}
5152

53+
protected override void ValidateParameters()
54+
{
55+
base.ValidateParameters();
56+
if (Scope != ConfigScope.Process && Scope != ConfigScope.CurrentUser)
57+
{
58+
throw new AzPSArgumentException($"When updating configs, {nameof(Scope)} must be either {ConfigScope.Process} or {ConfigScope.CurrentUser}", nameof(Scope));
59+
}
60+
}
61+
5262
public override void ExecuteCmdlet()
5363
{
5464
var configsFromInput = GetConfigsSpecifiedByUser();

src/Accounts/Accounts/DataCollection/EnableAzureRMDataCollection.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
1717
using Microsoft.Azure.Commands.Profile.Properties;
1818
using Microsoft.Azure.Commands.ResourceManager.Common;
19-
using Microsoft.WindowsAzure.Commands.Common;
19+
using Microsoft.Azure.Commands.Shared.Config;
20+
using Microsoft.Azure.PowerShell.Common.Config;
2021
using System.Management.Automation;
2122

2223
namespace Microsoft.Azure.Commands.Profile
@@ -31,7 +32,7 @@ protected override void BeginProcessing()
3132

3233
public override void ExecuteCmdlet()
3334
{
34-
if (ShouldProcess(Resources.EnableDataCollection, Resources.DataCollectionEnabledWarning,
35+
if (ShouldProcess(Resources.EnableDataCollection, Resources.DataCollectionEnabledWarning,
3536
string.Empty))
3637
{
3738

@@ -41,11 +42,10 @@ public override void ExecuteCmdlet()
4142

4243
protected void SetDataCollectionProfile(bool enable)
4344
{
44-
var profile = _dataCollectionProfile;
45-
profile.EnableAzureDataCollection = enable;
45+
// update config
4646
var session = AzureSession.Instance;
47-
DataCollectionController.WritePSDataCollectionProfile(session, profile);
48-
AzureSession.Instance.RegisterComponent(DataCollectionController.RegistryKey, () => DataCollectionController.Create(profile), true);
47+
session.TryGetComponent<IConfigManager>(nameof(IConfigManager), out var configManager);
48+
configManager.UpdateConfig(ConfigKeys.EnableDataCollection, enable, ConfigScope.CurrentUser);
4949
}
5050
}
5151
}

src/Accounts/Accounts/Feedback/SendFeedback.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,6 @@ private bool CheckIfInteractive()
8080
interactive = false;
8181
}
8282
}
83-
84-
if (!interactive && _dataCollectionProfile != null && !_dataCollectionProfile.EnableAzureDataCollection.HasValue)
85-
{
86-
_dataCollectionProfile.EnableAzureDataCollection = false;
87-
}
8883
return interactive;
8984
}
9085

src/Accounts/Accounts/Models/RMProfileClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,11 @@ public AzureRmProfile Login(
284284
{
285285
if (subscriptionId != null)
286286
{
287-
throw new PSInvalidOperationException(String.Format(ResourceMessages.SubscriptionIdNotFound, account.Id, subscriptionId));
287+
throw new PSInvalidOperationException(String.Format(ResourceMessages.SubscriptionIdNotFound, account.Id, subscriptionId) + " " + ProfileMessages.SubscriptionNotFouldPleaseCheckConfig);
288288
}
289289
else if (subscriptionName != null)
290290
{
291-
throw new PSInvalidOperationException(String.Format(ResourceMessages.SubscriptionNameNotFound, account.Id, subscriptionName));
291+
throw new PSInvalidOperationException(String.Format(ResourceMessages.SubscriptionNameNotFound, account.Id, subscriptionName) + " " + ProfileMessages.SubscriptionNotFouldPleaseCheckConfig);
292292
}
293293

294294
var newContext = new AzureContext(account, environment, newTenant);

0 commit comments

Comments
 (0)