From 0619ab7c1f49bb36253c62dcf30cf2e6946f3b97 Mon Sep 17 00:00:00 2001
From: Lucas Yao <53558334+LucasYao93@users.noreply.github.com>
Date: Wed, 28 Sep 2022 15:30:29 +0800
Subject: [PATCH] [Account] Use the ArgumentCompleter attribute to replace the
dynamic parameters of Get-AzContext. (#19655)
* [Account] removed dynamic parameters of the Get-AzContext.
* [Account] update changelog.
* Update ChangeLog.md
Co-authored-by: Yunchi Wang <54880216+wyunchi-ms@users.noreply.github.com>
---
src/Accounts/Accounts/ChangeLog.md | 1 +
.../Common/ContextNameCompleterAttribute.cs | 48 +++++++++++++++++++
.../Accounts/Context/GetAzureRMContext.cs | 26 +++-------
3 files changed, 55 insertions(+), 20 deletions(-)
create mode 100644 src/Accounts/Accounts/Common/ContextNameCompleterAttribute.cs
diff --git a/src/Accounts/Accounts/ChangeLog.md b/src/Accounts/Accounts/ChangeLog.md
index cb6c808bdbb9..80509f03bb55 100644
--- a/src/Accounts/Accounts/ChangeLog.md
+++ b/src/Accounts/Accounts/ChangeLog.md
@@ -23,6 +23,7 @@
* Upgraded Azure.Core to 1.25.0 and Azure.Identity to 1.6.1
* Upgraded Microsoft.ApplicationInsights to 2.13.1
* Changed target framework of AuthenticationAssemblyLoadContext to netcoreapp3.1.
+* Used the ArgumentCompleter attribute to replace the dynamic parameters of `Get-AzContext`. [#18041]
* Removed built-in environment of Azure Germany
## Version 2.10.1
diff --git a/src/Accounts/Accounts/Common/ContextNameCompleterAttribute.cs b/src/Accounts/Accounts/Common/ContextNameCompleterAttribute.cs
new file mode 100644
index 000000000000..62da82a7228f
--- /dev/null
+++ b/src/Accounts/Accounts/Common/ContextNameCompleterAttribute.cs
@@ -0,0 +1,48 @@
+using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
+using Microsoft.Azure.Commands.Common.Authentication.Models;
+using Microsoft.Azure.Commands.ResourceManager.Common;
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Linq;
+using System.Management.Automation;
+using System.Management.Automation.Language;
+using System.Text;
+
+namespace Microsoft.Azure.Commands.Profile.Common
+{
+ ///
+ /// This attribute will allow the user to autocomplete the values for valid Azure Context names when applied to context name related cmdlet parameters.
+ ///
+ public class ContextNameCompleterAttribute : ArgumentCompleterAttribute, IArgumentCompleter
+ {
+ ///
+ /// Initializes a new instance of .
+ ///
+ public ContextNameCompleterAttribute():base(typeof(ContextNameCompleterAttribute))
+ {
+
+ }
+
+ ///
+ /// Implementations CompleteArgument function of the .
+ ///
+ public IEnumerable CompleteArgument(string commandName, string parameterName, string wordToComplete, CommandAst commandAst, IDictionary fakeBoundParameters)
+ {
+ var profile = AzureRmProfileProvider.Instance.Profile; // Object profile with DefaultContextKey.
+ AzureRmProfile localProfile = profile as AzureRmProfile;
+ if (localProfile.Contexts != null && localProfile.Contexts.Count > 0)
+ {
+ IEnumerable names = localProfile.Contexts.Keys.ToArray();
+ foreach (string name in names)
+ {
+ yield return new CompletionResult($"'{name}'", $"'{name}'", CompletionResultType.ParameterValue, $"'{name}'");
+ }
+ }
+ else
+ {
+ yield return new CompletionResult($"{localProfile.DefaultContextKey}", $"{localProfile.DefaultContextKey}", CompletionResultType.ParameterValue, $"{localProfile.DefaultContextKey}");
+ }
+ }
+ }
+}
diff --git a/src/Accounts/Accounts/Context/GetAzureRMContext.cs b/src/Accounts/Accounts/Context/GetAzureRMContext.cs
index 4112c18b7a34..9ad4af98986c 100644
--- a/src/Accounts/Accounts/Context/GetAzureRMContext.cs
+++ b/src/Accounts/Accounts/Context/GetAzureRMContext.cs
@@ -20,6 +20,7 @@
using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
using Microsoft.Azure.Commands.Common.Authentication.Models;
+using Microsoft.Azure.Commands.Profile.Common;
using Microsoft.Azure.Commands.Profile.Models.Core;
using Microsoft.Azure.Commands.ResourceManager.Common;
using Microsoft.WindowsAzure.Commands.Utilities.Common;
@@ -34,6 +35,11 @@ namespace Microsoft.Azure.Commands.Profile
public class GetAzureRMContextCommand : AzureRMCmdlet, IDynamicParameters
{
public const string ListAllParameterSet = "ListAllContexts", GetSingleParameterSet = "GetSingleContext";
+
+ [Parameter(Position = 0, Mandatory = false, HelpMessage = "The name of the context", ParameterSetName = GetSingleParameterSet)]
+ [ContextNameCompleter]
+ public string Name { get; set; }
+
///
/// Gets the current default context.
///
@@ -143,25 +149,5 @@ void WriteContext(IAzureContext azureContext, string name)
WriteObject(context);
}
-
- public new object GetDynamicParameters()
- {
- var parameters = base.GetDynamicParameters() as RuntimeDefinedParameterDictionary;
- AzureRmProfile localProfile = DefaultProfile as AzureRmProfile;
- if (localProfile != null && localProfile.Contexts != null && localProfile.Contexts.Count > 0)
- {
- var nameParameter = new RuntimeDefinedParameter(
- "Name", typeof(string),
- new Collection()
- {
- new ParameterAttribute { Position =0, Mandatory=false, HelpMessage="The name of the context", ParameterSetName=GetSingleParameterSet },
- new ValidateSetAttribute((DefaultProfile as AzureRmProfile).Contexts.Keys.ToArray())
- }
- );
- parameters.Add(nameParameter.Name, nameParameter);
- }
-
- return parameters;
- }
}
}