diff --git a/src/Accounts/Accounts.Test/ArgumentCompleterTests.cs b/src/Accounts/Accounts.Test/ArgumentCompleterTests.cs index 8795868044c7..f838ae49adbf 100644 --- a/src/Accounts/Accounts.Test/ArgumentCompleterTests.cs +++ b/src/Accounts/Accounts.Test/ArgumentCompleterTests.cs @@ -45,5 +45,12 @@ public void TestResourceIdCompleter() { TestRunner.RunTestScript("Test-ResourceIdCompleter"); } + + [Fact] + [Trait(Category.AcceptanceType, Category.CheckIn)] + public void TestEnvironmentCompleter() + { + TestRunner.RunTestScript("Test-EnvironmentCompleter"); + } } } \ No newline at end of file diff --git a/src/Accounts/Accounts.Test/ArgumentCompleterTests.ps1 b/src/Accounts/Accounts.Test/ArgumentCompleterTests.ps1 index 0b77caee038f..23a8dae086de 100644 --- a/src/Accounts/Accounts.Test/ArgumentCompleterTests.ps1 +++ b/src/Accounts/Accounts.Test/ArgumentCompleterTests.ps1 @@ -57,3 +57,55 @@ function Test-ResourceIdCompleter $resourceIds = [Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters.ResourceIdCompleterAttribute]::GetResourceIds($resourceType) Assert-AreEqualArray $resourceIds $expectResourceIds } + +<# +.SYNOPSIS +Tests environment completer +#> +function Test-EnvironmentCompleter +{ + $expectedEnvironments = (Get-AzEnvironment).Name + + # Test EnvironmentCompleterAttribute static method + $environments = [Microsoft.Azure.Commands.Profile.Common.EnvironmentCompleterAttribute]::GetEnvironments() + Assert-AreEqualArray $environments $expectedEnvironments + + # Test completion results for Connect-AzAccount + $connectAzAccount = Get-EnvironmentCompleterResult -CmdletName 'Connect-AzAccount' -ParameterName 'Environment' + Assert-AreEqualArray $connectAzAccount $expectedEnvironments + + # Test completion results for Set-AzEnvironment + $setAzEnvironment = Get-EnvironmentCompleterResult -CmdletName 'Set-AzEnvironment' -ParameterName 'Name' + Assert-AreEqualArray $setAzEnvironment $expectedEnvironments + + # Test completion results for Get-AzEnvironment + $getAzEnvironment = Get-EnvironmentCompleterResult -CmdletName 'Get-AzEnvironment' -ParameterName 'Name' + Assert-AreEqualArray $getAzEnvironment $expectedEnvironments + + # Test completion results for Remove-AzEnvironment + $removeAzEnvironment = Get-EnvironmentCompleterResult -CmdletName 'Remove-AzEnvironment' -ParameterName 'Name' + Assert-AreEqualArray $removeAzEnvironment $expectedEnvironments +} + +<# +.SYNOPSIS +Helper function to get parameter completer results for specified cmdlet. +#> +function Get-EnvironmentCompleterResult +{ + param + ( + [Parameter(Mandatory = $true)] + [string] + $CmdletName, + + [Parameter(Mandatory = $true)] + [string] + $ParameterName + ) + + $command = Get-Command -Name $CmdletName + $environmentCompleterAttribute = $command.Parameters.$ParameterName.Attributes | Where-Object { $_.GetType() -eq [Microsoft.Azure.Commands.Profile.Common.EnvironmentCompleterAttribute]} + + return $environmentCompleterAttribute.ScriptBlock.Invoke().CompletionText +} diff --git a/src/Accounts/Accounts.Test/SessionRecords/Microsoft.Azure.Commands.Profile.Test.ArgumentCompleterTests/TestEnvironmentCompleter.json b/src/Accounts/Accounts.Test/SessionRecords/Microsoft.Azure.Commands.Profile.Test.ArgumentCompleterTests/TestEnvironmentCompleter.json new file mode 100644 index 000000000000..ef27ed2769ac --- /dev/null +++ b/src/Accounts/Accounts.Test/SessionRecords/Microsoft.Azure.Commands.Profile.Test.ArgumentCompleterTests/TestEnvironmentCompleter.json @@ -0,0 +1,6 @@ +{ + "Names": {}, + "Variables": { + "SubscriptionId": "9e223dbe-3399-4e19-88eb-0975f02ac87f" + } +} diff --git a/src/Accounts/Accounts/Account/ConnectAzureRmAccount.cs b/src/Accounts/Accounts/Account/ConnectAzureRmAccount.cs index ffd629971bb5..6ba8e430d632 100644 --- a/src/Accounts/Accounts/Account/ConnectAzureRmAccount.cs +++ b/src/Accounts/Accounts/Account/ConnectAzureRmAccount.cs @@ -71,6 +71,7 @@ public class ConnectAzureRmAccountCommand : AzureContextModificationCmdlet, IMod [Parameter(Mandatory = false, HelpMessage = "Name of the environment containing the account to log into")] [Alias("EnvironmentName")] [ValidateNotNullOrEmpty] + [EnvironmentCompleter()] public string Environment { get; set; } [Parameter(ParameterSetName = ServicePrincipalParameterSet, diff --git a/src/Accounts/Accounts/ChangeLog.md b/src/Accounts/Accounts/ChangeLog.md index 026889b69d30..757508c0bbac 100644 --- a/src/Accounts/Accounts/ChangeLog.md +++ b/src/Accounts/Accounts/ChangeLog.md @@ -19,6 +19,7 @@ --> ## Upcoming Release +* Added Environment auto completer to the following cmdlets: Connect-AzAccount, Get-AzEnvironment, Set-AzEnvironment, and Remove-AzEnvironment [#15991] ## Version 2.6.1 * Added new version of AAD service client using Microsoft Graph API diff --git a/src/Accounts/Accounts/Common/EnvironmentCompleterAttribute.cs b/src/Accounts/Accounts/Common/EnvironmentCompleterAttribute.cs new file mode 100644 index 000000000000..9e960d06bf3e --- /dev/null +++ b/src/Accounts/Accounts/Common/EnvironmentCompleterAttribute.cs @@ -0,0 +1,40 @@ +using Microsoft.Azure.Commands.Common.Authentication.Abstractions; +using Microsoft.Azure.Commands.Common.Authentication.Models; +using Microsoft.Azure.Commands.ResourceManager.Common; +using System.Linq; +using System.Management.Automation; + +namespace Microsoft.Azure.Commands.Profile.Common +{ + /// + /// This attribute will allow the user to autocomplete the values for valid Azure Environment names when applied to Environment related cmdlet parameters. + /// + public class EnvironmentCompleterAttribute : ArgumentCompleterAttribute + { + /// + /// Initializes a new instance of . + /// + public EnvironmentCompleterAttribute() : base(CreateScriptBlock()) + { + } + + /// + /// Returns an array of available Azure Environment names. + /// + /// + public static string[] GetEnvironments() + { + var profileClient = new RMProfileClient(AzureRmProfileProvider.Instance.GetProfile()); + return profileClient.ListEnvironments(null).Select(x => x.Name).ToArray(); + } + + private static ScriptBlock CreateScriptBlock() + { + string script = "param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)\n" + + "$environments = [Microsoft.Azure.Commands.Profile.Common.EnvironmentCompleterAttribute]::GetEnvironments()\n" + + "$environments | Where-Object { $_ -Like \"$wordToComplete*\" } | ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) }"; + ScriptBlock scriptBlock = ScriptBlock.Create(script); + return scriptBlock; + } + } +} \ No newline at end of file diff --git a/src/Accounts/Accounts/Environment/GetAzureRMEnvironment.cs b/src/Accounts/Accounts/Environment/GetAzureRMEnvironment.cs index 1a3b50854b67..afd0d1066b87 100644 --- a/src/Accounts/Accounts/Environment/GetAzureRMEnvironment.cs +++ b/src/Accounts/Accounts/Environment/GetAzureRMEnvironment.cs @@ -14,6 +14,7 @@ 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; using Microsoft.Azure.Commands.ResourceManager.Common; using Microsoft.WindowsAzure.Commands.Common; @@ -30,6 +31,7 @@ namespace Microsoft.Azure.Commands.Profile public class GetAzureRMEnvironmentCommand : AzureRMCmdlet { [Parameter(Position = 0, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The environment name")] + [EnvironmentCompleter()] public string Name { get; set; } protected override void BeginProcessing() diff --git a/src/Accounts/Accounts/Environment/RemoveAzureRMEnvironment.cs b/src/Accounts/Accounts/Environment/RemoveAzureRMEnvironment.cs index ff432b61154e..60b813365799 100644 --- a/src/Accounts/Accounts/Environment/RemoveAzureRMEnvironment.cs +++ b/src/Accounts/Accounts/Environment/RemoveAzureRMEnvironment.cs @@ -34,6 +34,7 @@ public class RemoveAzureRMEnvironmentCommand : AzureContextModificationCmdlet { [Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "The environment name")] + [EnvironmentCompleter()] public string Name { get; set; } protected override void BeginProcessing() diff --git a/src/Accounts/Accounts/Environment/SetAzureRMEnvironment.cs b/src/Accounts/Accounts/Environment/SetAzureRMEnvironment.cs index d27a74fe2edf..91f0d1f71fbb 100644 --- a/src/Accounts/Accounts/Environment/SetAzureRMEnvironment.cs +++ b/src/Accounts/Accounts/Environment/SetAzureRMEnvironment.cs @@ -46,6 +46,7 @@ public EnvironmentHelper EnvHelper } [Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true)] + [EnvironmentCompleter()] public string Name { get; set; } [Parameter(ParameterSetName = EnvironmentPropertiesParameterSet, Position = 1, Mandatory = false, ValueFromPipelineByPropertyName = true)] diff --git a/src/Storage/Storage.Management/help/Update-AzDataLakeGen2AclRecursive.md b/src/Storage/Storage.Management/help/Update-AzDataLakeGen2AclRecursive.md index c55caf38c6f8..9dc5cfe917ee 100644 --- a/src/Storage/Storage.Management/help/Update-AzDataLakeGen2AclRecursive.md +++ b/src/Storage/Storage.Management/help/Update-AzDataLakeGen2AclRecursive.md @@ -100,7 +100,7 @@ do $totalFailure += $result.TotalFailureCount $FailedEntries += $result.FailedEntries $token = $result.ContinuationToken -}while (($token -ne $null) -and (($ContinueOnFailure) -or ($result.TotalFailureCount -eq 0))) +}while (($null -ne $token) -and (($ContinueOnFailure) -or ($result.TotalFailureCount -eq 0))) echo "" echo "[Result Summary]" echo "TotalDirectoriesSuccessfulCount: `t$($TotalDirectoriesSuccess)"