Skip to content

Commit

Permalink
[Account] Use the ArgumentCompleter attribute to replace the dynamic …
Browse files Browse the repository at this point in the history
…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]>
  • Loading branch information
LucasYao93 and wyunchi-ms authored Sep 28, 2022
1 parent 2cb394f commit 0619ab7
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/Accounts/Accounts/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions src/Accounts/Accounts/Common/ContextNameCompleterAttribute.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// This attribute will allow the user to autocomplete the values for valid Azure Context names when applied to context name related cmdlet parameters.
/// </summary>
public class ContextNameCompleterAttribute : ArgumentCompleterAttribute, IArgumentCompleter
{
/// <summary>
/// Initializes a new instance of <see cref="ContextNameCompleterAttribute" /> .
/// </summary>
public ContextNameCompleterAttribute():base(typeof(ContextNameCompleterAttribute))
{

}

/// <summary>
/// Implementations CompleteArgument function of the <see cref="IArgumentCompleter"/>.
/// </summary>
public IEnumerable<CompletionResult> 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<string> 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}");
}
}
}
}
26 changes: 6 additions & 20 deletions src/Accounts/Accounts/Context/GetAzureRMContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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; }

/// <summary>
/// Gets the current default context.
/// </summary>
Expand Down Expand Up @@ -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<Attribute>()
{
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;
}
}
}

0 comments on commit 0619ab7

Please sign in to comment.