Skip to content

Commit 0619ab7

Browse files
[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 <[email protected]>
1 parent 2cb394f commit 0619ab7

File tree

3 files changed

+55
-20
lines changed

3 files changed

+55
-20
lines changed

src/Accounts/Accounts/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* Upgraded Azure.Core to 1.25.0 and Azure.Identity to 1.6.1
2424
* Upgraded Microsoft.ApplicationInsights to 2.13.1
2525
* Changed target framework of AuthenticationAssemblyLoadContext to netcoreapp3.1.
26+
* Used the ArgumentCompleter attribute to replace the dynamic parameters of `Get-AzContext`. [#18041]
2627
* Removed built-in environment of Azure Germany
2728

2829
## Version 2.10.1
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
2+
using Microsoft.Azure.Commands.Common.Authentication.Models;
3+
using Microsoft.Azure.Commands.ResourceManager.Common;
4+
using System;
5+
using System.Collections;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Management.Automation;
9+
using System.Management.Automation.Language;
10+
using System.Text;
11+
12+
namespace Microsoft.Azure.Commands.Profile.Common
13+
{
14+
/// <summary>
15+
/// This attribute will allow the user to autocomplete the values for valid Azure Context names when applied to context name related cmdlet parameters.
16+
/// </summary>
17+
public class ContextNameCompleterAttribute : ArgumentCompleterAttribute, IArgumentCompleter
18+
{
19+
/// <summary>
20+
/// Initializes a new instance of <see cref="ContextNameCompleterAttribute" /> .
21+
/// </summary>
22+
public ContextNameCompleterAttribute():base(typeof(ContextNameCompleterAttribute))
23+
{
24+
25+
}
26+
27+
/// <summary>
28+
/// Implementations CompleteArgument function of the <see cref="IArgumentCompleter"/>.
29+
/// </summary>
30+
public IEnumerable<CompletionResult> CompleteArgument(string commandName, string parameterName, string wordToComplete, CommandAst commandAst, IDictionary fakeBoundParameters)
31+
{
32+
var profile = AzureRmProfileProvider.Instance.Profile; // Object profile with DefaultContextKey.
33+
AzureRmProfile localProfile = profile as AzureRmProfile;
34+
if (localProfile.Contexts != null && localProfile.Contexts.Count > 0)
35+
{
36+
IEnumerable<string> names = localProfile.Contexts.Keys.ToArray();
37+
foreach (string name in names)
38+
{
39+
yield return new CompletionResult($"'{name}'", $"'{name}'", CompletionResultType.ParameterValue, $"'{name}'");
40+
}
41+
}
42+
else
43+
{
44+
yield return new CompletionResult($"{localProfile.DefaultContextKey}", $"{localProfile.DefaultContextKey}", CompletionResultType.ParameterValue, $"{localProfile.DefaultContextKey}");
45+
}
46+
}
47+
}
48+
}

src/Accounts/Accounts/Context/GetAzureRMContext.cs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using Microsoft.Azure.Commands.Common.Authentication;
2121
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
2222
using Microsoft.Azure.Commands.Common.Authentication.Models;
23+
using Microsoft.Azure.Commands.Profile.Common;
2324
using Microsoft.Azure.Commands.Profile.Models.Core;
2425
using Microsoft.Azure.Commands.ResourceManager.Common;
2526
using Microsoft.WindowsAzure.Commands.Utilities.Common;
@@ -34,6 +35,11 @@ namespace Microsoft.Azure.Commands.Profile
3435
public class GetAzureRMContextCommand : AzureRMCmdlet, IDynamicParameters
3536
{
3637
public const string ListAllParameterSet = "ListAllContexts", GetSingleParameterSet = "GetSingleContext";
38+
39+
[Parameter(Position = 0, Mandatory = false, HelpMessage = "The name of the context", ParameterSetName = GetSingleParameterSet)]
40+
[ContextNameCompleter]
41+
public string Name { get; set; }
42+
3743
/// <summary>
3844
/// Gets the current default context.
3945
/// </summary>
@@ -143,25 +149,5 @@ void WriteContext(IAzureContext azureContext, string name)
143149

144150
WriteObject(context);
145151
}
146-
147-
public new object GetDynamicParameters()
148-
{
149-
var parameters = base.GetDynamicParameters() as RuntimeDefinedParameterDictionary;
150-
AzureRmProfile localProfile = DefaultProfile as AzureRmProfile;
151-
if (localProfile != null && localProfile.Contexts != null && localProfile.Contexts.Count > 0)
152-
{
153-
var nameParameter = new RuntimeDefinedParameter(
154-
"Name", typeof(string),
155-
new Collection<Attribute>()
156-
{
157-
new ParameterAttribute { Position =0, Mandatory=false, HelpMessage="The name of the context", ParameterSetName=GetSingleParameterSet },
158-
new ValidateSetAttribute((DefaultProfile as AzureRmProfile).Contexts.Keys.ToArray())
159-
}
160-
);
161-
parameters.Add(nameParameter.Name, nameParameter);
162-
}
163-
164-
return parameters;
165-
}
166152
}
167153
}

0 commit comments

Comments
 (0)