-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Config framework implementation #17545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8efa0f9
Modified code from Microsot.Extensions.Configuration
isra-fel e54b3cb
implementation of config framework
isra-fel 3f59938
cmdlet related
isra-fel c1e967f
test related
isra-fel 678bef2
changelog; 3rd party license
isra-fel ceebb4a
move PSConfig model to Accounts proj; feedback link
isra-fel 1a6a670
Move preview attribute into child classes
isra-fel 4f6a903
fix get/update by config key
isra-fel 1283ed6
correct cmdlet regex
isra-fel 86ef6eb
Merge branch 'main' of https://github.com/Azure/azure-powershell into…
isra-fel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| // ---------------------------------------------------------------------------------- | ||
| // | ||
| // Copyright Microsoft Corporation | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // ---------------------------------------------------------------------------------- | ||
|
|
||
| using Microsoft.Azure.PowerShell.Common.Config; | ||
| using Microsoft.WindowsAzure.Commands.Common.CustomAttributes; | ||
| using Microsoft.WindowsAzure.Commands.Utilities.Common; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Collections.ObjectModel; | ||
| using System.Linq; | ||
| using System.Management.Automation; | ||
|
|
||
| namespace Microsoft.Azure.Commands.Common.Authentication.Config | ||
| { | ||
| [Cmdlet("Clear", "AzConfig", SupportsShouldProcess = true)] | ||
| [OutputType(typeof(bool))] | ||
| [CmdletPreview(PreviewMessage)] | ||
| public class ClearConfigCommand : ConfigCommandBase, IDynamicParameters | ||
| { | ||
| private const string ClearByKey = "ClearByKey"; | ||
| private const string ClearAll = "ClearAll"; | ||
|
|
||
| private const string ProcessMessage = "Clear the configs that apply to \"{0}\" by the following keys: {1}."; | ||
|
|
||
| private string ContinueMessage => $"Clear all the configs that apply to \"{AppliesTo}\" in scope {Scope}?"; | ||
| private string ProcessTarget => $"Configs in scope {Scope}"; | ||
|
|
||
| [Parameter(ParameterSetName = ClearAll, Mandatory = true, HelpMessage = "Clear all configs.")] | ||
| public SwitchParameter All { get; set; } | ||
|
|
||
| [Parameter(ParameterSetName = ClearAll, HelpMessage = "Do not ask for confirmation when clearing all configs.")] | ||
| public SwitchParameter Force { get; set; } | ||
|
|
||
| [Parameter(HelpMessage = "Returns true if cmdlet executes correctly.")] | ||
| public SwitchParameter PassThru { get; set; } | ||
|
|
||
| public new object GetDynamicParameters() | ||
| { | ||
| return GetDynamicParameters((ConfigDefinition config) => | ||
| new RuntimeDefinedParameter( | ||
| config.Key, | ||
| typeof(SwitchParameter), | ||
| new Collection<Attribute>() { | ||
| new ParameterAttribute { | ||
| ParameterSetName = ClearByKey, | ||
| HelpMessage = config.HelpMessage | ||
| } | ||
| })); | ||
| } | ||
|
|
||
| public override void ExecuteCmdlet() | ||
| { | ||
| switch (ParameterSetName) | ||
| { | ||
| case ClearByKey: | ||
| ClearConfigByKey(); | ||
| break; | ||
| case ClearAll: | ||
| ClearAllConfigs(); | ||
| break; | ||
| } | ||
| if (PassThru) | ||
| { | ||
| WriteObject(true); | ||
| } | ||
| } | ||
|
|
||
| private void ClearConfigByKey() | ||
| { | ||
| IEnumerable<string> configKeysFromInput = GetConfigsSpecifiedByUser() | ||
| .Where(x => (SwitchParameter)x.Value) | ||
| .Select(x => x.Key); | ||
| if (!configKeysFromInput.Any()) | ||
| { | ||
| WriteWarning($"Please specify the key(s) of the configs to clear. Run `help {MyInvocation.MyCommand.Name}` for more information."); | ||
| return; | ||
| } | ||
| base.ConfirmAction( | ||
| string.Format(ProcessMessage, AppliesTo, string.Join(", ", configKeysFromInput)), | ||
| ProcessTarget, | ||
| () => configKeysFromInput.ForEach(ClearConfigByKey)); | ||
| } | ||
|
|
||
| private void ClearConfigByKey(string key) | ||
| { | ||
| ConfigManager.ClearConfig(new ClearConfigOptions(key, Scope) | ||
| { | ||
| AppliesTo = AppliesTo | ||
| }); | ||
| } | ||
|
|
||
| private void ClearAllConfigs() | ||
| { | ||
| ConfirmAction(Force, ContinueMessage, ContinueMessage, ProcessTarget, () => | ||
| { | ||
| ConfigManager.ClearConfig(new ClearConfigOptions(null, Scope) | ||
| { | ||
| AppliesTo = AppliesTo | ||
| }); | ||
| }); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| // ---------------------------------------------------------------------------------- | ||
| // | ||
| // Copyright Microsoft Corporation | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // ---------------------------------------------------------------------------------- | ||
|
|
||
| using Microsoft.Azure.Commands.Common.Exceptions; | ||
| using Microsoft.Azure.Commands.ResourceManager.Common; | ||
| using Microsoft.Azure.PowerShell.Common.Config; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Management.Automation; | ||
|
|
||
| namespace Microsoft.Azure.Commands.Common.Authentication.Config | ||
| { | ||
| public abstract class ConfigCommandBase : AzureRMCmdlet | ||
| { | ||
| protected const string PreviewMessage = "The cmdlet group \"AzConfig\" is in preview. Feedback is welcome: https://aka.ms/azpsissue"; | ||
|
|
||
| private readonly RuntimeDefinedParameterDictionary _dynamicParameters = new RuntimeDefinedParameterDictionary(); | ||
|
|
||
| protected IConfigManager ConfigManager { get; } | ||
| protected IEnumerable<ConfigDefinition> ConfigDefinitions | ||
| { | ||
| get | ||
| { | ||
| if (_configDefinitions == null) | ||
| { | ||
| _configDefinitions = ConfigManager.ListConfigDefinitions(); | ||
| } | ||
| return _configDefinitions; | ||
| } | ||
| } | ||
| private IEnumerable<ConfigDefinition> _configDefinitions; | ||
|
|
||
| public ConfigCommandBase() : base() | ||
| { | ||
| if (!AzureSession.Instance.TryGetComponent<IConfigManager>(nameof(IConfigManager), out var configManager)) | ||
| { | ||
| throw new AzPSApplicationException($"Unexpected error: {nameof(IConfigManager)} has not been registered to the current session."); | ||
| } | ||
| ConfigManager = configManager; | ||
| } | ||
|
|
||
| [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.")] | ||
| [ValidateNotNullOrEmpty] | ||
| public string AppliesTo { get; set; } | ||
|
|
||
| [Parameter(HelpMessage = "Determines the scope of config changes, for example, whether changes apply only to the current process, or to all sessions started by this user. By default it is CurrentUser.")] | ||
| public ConfigScope Scope { get; set; } = ConfigScope.CurrentUser; | ||
|
|
||
| protected override void BeginProcessing() | ||
| { | ||
| base.BeginProcessing(); | ||
| ValidateParameters(); | ||
| } | ||
|
|
||
| protected virtual void ValidateParameters() | ||
| { | ||
| if (!AppliesToHelper.TryParseAppliesTo(AppliesTo, out _)) | ||
| { | ||
| throw new AzPSArgumentException($"{nameof(AppliesTo)} must be a valid module name, a cmdlet name, or \"{ConfigFilter.GlobalAppliesTo}\"", nameof(AppliesTo)); | ||
| } | ||
| } | ||
|
|
||
| protected object GetDynamicParameters(Func<ConfigDefinition, RuntimeDefinedParameter> mapConfigToParameter) | ||
| { | ||
| _dynamicParameters.Clear(); | ||
| foreach (var config in ConfigDefinitions) | ||
| { | ||
| _dynamicParameters.Add(config.Key, mapConfigToParameter(config)); | ||
| } | ||
| return _dynamicParameters; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the dynamic parameters and their values if specified. | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| protected IEnumerable<(string Key, object Value)> GetConfigsSpecifiedByUser() | ||
| { | ||
| var configs = new Dictionary<string, object>(); | ||
| foreach (var param in _dynamicParameters.Values.Where(p => p.IsSet)) | ||
| { | ||
| yield return (param.Name, param.Value); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.