Skip to content

Commit d36e0b5

Browse files
authored
Merge branch 'main' into bez/release
2 parents 4493fc2 + 959d050 commit d36e0b5

File tree

10 files changed

+112
-1
lines changed

10 files changed

+112
-1
lines changed

src/Accounts/Accounts.Test/ArgumentCompleterTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,12 @@ public void TestResourceIdCompleter()
4545
{
4646
TestRunner.RunTestScript("Test-ResourceIdCompleter");
4747
}
48+
49+
[Fact]
50+
[Trait(Category.AcceptanceType, Category.CheckIn)]
51+
public void TestEnvironmentCompleter()
52+
{
53+
TestRunner.RunTestScript("Test-EnvironmentCompleter");
54+
}
4855
}
4956
}

src/Accounts/Accounts.Test/ArgumentCompleterTests.ps1

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,55 @@ function Test-ResourceIdCompleter
5757
$resourceIds = [Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters.ResourceIdCompleterAttribute]::GetResourceIds($resourceType)
5858
Assert-AreEqualArray $resourceIds $expectResourceIds
5959
}
60+
61+
<#
62+
.SYNOPSIS
63+
Tests environment completer
64+
#>
65+
function Test-EnvironmentCompleter
66+
{
67+
$expectedEnvironments = (Get-AzEnvironment).Name
68+
69+
# Test EnvironmentCompleterAttribute static method
70+
$environments = [Microsoft.Azure.Commands.Profile.Common.EnvironmentCompleterAttribute]::GetEnvironments()
71+
Assert-AreEqualArray $environments $expectedEnvironments
72+
73+
# Test completion results for Connect-AzAccount
74+
$connectAzAccount = Get-EnvironmentCompleterResult -CmdletName 'Connect-AzAccount' -ParameterName 'Environment'
75+
Assert-AreEqualArray $connectAzAccount $expectedEnvironments
76+
77+
# Test completion results for Set-AzEnvironment
78+
$setAzEnvironment = Get-EnvironmentCompleterResult -CmdletName 'Set-AzEnvironment' -ParameterName 'Name'
79+
Assert-AreEqualArray $setAzEnvironment $expectedEnvironments
80+
81+
# Test completion results for Get-AzEnvironment
82+
$getAzEnvironment = Get-EnvironmentCompleterResult -CmdletName 'Get-AzEnvironment' -ParameterName 'Name'
83+
Assert-AreEqualArray $getAzEnvironment $expectedEnvironments
84+
85+
# Test completion results for Remove-AzEnvironment
86+
$removeAzEnvironment = Get-EnvironmentCompleterResult -CmdletName 'Remove-AzEnvironment' -ParameterName 'Name'
87+
Assert-AreEqualArray $removeAzEnvironment $expectedEnvironments
88+
}
89+
90+
<#
91+
.SYNOPSIS
92+
Helper function to get parameter completer results for specified cmdlet.
93+
#>
94+
function Get-EnvironmentCompleterResult
95+
{
96+
param
97+
(
98+
[Parameter(Mandatory = $true)]
99+
[string]
100+
$CmdletName,
101+
102+
[Parameter(Mandatory = $true)]
103+
[string]
104+
$ParameterName
105+
)
106+
107+
$command = Get-Command -Name $CmdletName
108+
$environmentCompleterAttribute = $command.Parameters.$ParameterName.Attributes | Where-Object { $_.GetType() -eq [Microsoft.Azure.Commands.Profile.Common.EnvironmentCompleterAttribute]}
109+
110+
return $environmentCompleterAttribute.ScriptBlock.Invoke().CompletionText
111+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"Names": {},
3+
"Variables": {
4+
"SubscriptionId": "9e223dbe-3399-4e19-88eb-0975f02ac87f"
5+
}
6+
}

src/Accounts/Accounts/Account/ConnectAzureRmAccount.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public class ConnectAzureRmAccountCommand : AzureContextModificationCmdlet, IMod
7171
[Parameter(Mandatory = false, HelpMessage = "Name of the environment containing the account to log into")]
7272
[Alias("EnvironmentName")]
7373
[ValidateNotNullOrEmpty]
74+
[EnvironmentCompleter()]
7475
public string Environment { get; set; }
7576

7677
[Parameter(ParameterSetName = ServicePrincipalParameterSet,

src/Accounts/Accounts/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
-->
2020

2121
## Upcoming Release
22+
* Added Environment auto completer to the following cmdlets: Connect-AzAccount, Get-AzEnvironment, Set-AzEnvironment, and Remove-AzEnvironment [#15991]
2223

2324
## Version 2.6.1
2425
* Added new version of AAD service client using Microsoft Graph API
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.Linq;
5+
using System.Management.Automation;
6+
7+
namespace Microsoft.Azure.Commands.Profile.Common
8+
{
9+
/// <summary>
10+
/// This attribute will allow the user to autocomplete the values for valid Azure Environment names when applied to Environment related cmdlet parameters.
11+
/// </summary>
12+
public class EnvironmentCompleterAttribute : ArgumentCompleterAttribute
13+
{
14+
/// <summary>
15+
/// Initializes a new instance of <see cref="EnvironmentCompleterAttribute" /> .
16+
/// </summary>
17+
public EnvironmentCompleterAttribute() : base(CreateScriptBlock())
18+
{
19+
}
20+
21+
/// <summary>
22+
/// Returns an array of available Azure Environment names.
23+
/// </summary>
24+
/// <returns></returns>
25+
public static string[] GetEnvironments()
26+
{
27+
var profileClient = new RMProfileClient(AzureRmProfileProvider.Instance.GetProfile<AzureRmProfile>());
28+
return profileClient.ListEnvironments(null).Select(x => x.Name).ToArray();
29+
}
30+
31+
private static ScriptBlock CreateScriptBlock()
32+
{
33+
string script = "param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)\n" +
34+
"$environments = [Microsoft.Azure.Commands.Profile.Common.EnvironmentCompleterAttribute]::GetEnvironments()\n" +
35+
"$environments | Where-Object { $_ -Like \"$wordToComplete*\" } | ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) }";
36+
ScriptBlock scriptBlock = ScriptBlock.Create(script);
37+
return scriptBlock;
38+
}
39+
}
40+
}

src/Accounts/Accounts/Environment/GetAzureRMEnvironment.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
1616
using Microsoft.Azure.Commands.Common.Authentication.Models;
17+
using Microsoft.Azure.Commands.Profile.Common;
1718
using Microsoft.Azure.Commands.Profile.Models;
1819
using Microsoft.Azure.Commands.ResourceManager.Common;
1920
using Microsoft.WindowsAzure.Commands.Common;
@@ -30,6 +31,7 @@ namespace Microsoft.Azure.Commands.Profile
3031
public class GetAzureRMEnvironmentCommand : AzureRMCmdlet
3132
{
3233
[Parameter(Position = 0, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The environment name")]
34+
[EnvironmentCompleter()]
3335
public string Name { get; set; }
3436

3537
protected override void BeginProcessing()

src/Accounts/Accounts/Environment/RemoveAzureRMEnvironment.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class RemoveAzureRMEnvironmentCommand : AzureContextModificationCmdlet
3434
{
3535
[Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true,
3636
HelpMessage = "The environment name")]
37+
[EnvironmentCompleter()]
3738
public string Name { get; set; }
3839

3940
protected override void BeginProcessing()

src/Accounts/Accounts/Environment/SetAzureRMEnvironment.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public EnvironmentHelper EnvHelper
4646
}
4747

4848
[Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true)]
49+
[EnvironmentCompleter()]
4950
public string Name { get; set; }
5051

5152
[Parameter(ParameterSetName = EnvironmentPropertiesParameterSet, Position = 1, Mandatory = false, ValueFromPipelineByPropertyName = true)]

src/Storage/Storage.Management/help/Update-AzDataLakeGen2AclRecursive.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ do
100100
$totalFailure += $result.TotalFailureCount
101101
$FailedEntries += $result.FailedEntries
102102
$token = $result.ContinuationToken
103-
}while (($token -ne $null) -and (($ContinueOnFailure) -or ($result.TotalFailureCount -eq 0)))
103+
}while (($null -ne $token) -and (($ContinueOnFailure) -or ($result.TotalFailureCount -eq 0)))
104104
echo ""
105105
echo "[Result Summary]"
106106
echo "TotalDirectoriesSuccessfulCount: `t$($TotalDirectoriesSuccess)"

0 commit comments

Comments
 (0)