Skip to content

Commit

Permalink
Merge branch 'main' into bez/release
Browse files Browse the repository at this point in the history
  • Loading branch information
BethanyZhou authored Nov 1, 2021
2 parents 4493fc2 + 959d050 commit d36e0b5
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/Accounts/Accounts.Test/ArgumentCompleterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,12 @@ public void TestResourceIdCompleter()
{
TestRunner.RunTestScript("Test-ResourceIdCompleter");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestEnvironmentCompleter()
{
TestRunner.RunTestScript("Test-EnvironmentCompleter");
}
}
}
52 changes: 52 additions & 0 deletions src/Accounts/Accounts.Test/ArgumentCompleterTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"Names": {},
"Variables": {
"SubscriptionId": "9e223dbe-3399-4e19-88eb-0975f02ac87f"
}
}
1 change: 1 addition & 0 deletions src/Accounts/Accounts/Account/ConnectAzureRmAccount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/Accounts/Accounts/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
40 changes: 40 additions & 0 deletions src/Accounts/Accounts/Common/EnvironmentCompleterAttribute.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// This attribute will allow the user to autocomplete the values for valid Azure Environment names when applied to Environment related cmdlet parameters.
/// </summary>
public class EnvironmentCompleterAttribute : ArgumentCompleterAttribute
{
/// <summary>
/// Initializes a new instance of <see cref="EnvironmentCompleterAttribute" /> .
/// </summary>
public EnvironmentCompleterAttribute() : base(CreateScriptBlock())
{
}

/// <summary>
/// Returns an array of available Azure Environment names.
/// </summary>
/// <returns></returns>
public static string[] GetEnvironments()
{
var profileClient = new RMProfileClient(AzureRmProfileProvider.Instance.GetProfile<AzureRmProfile>());
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;
}
}
}
2 changes: 2 additions & 0 deletions src/Accounts/Accounts/Environment/GetAzureRMEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions src/Accounts/Accounts/Environment/SetAzureRMEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down

0 comments on commit d36e0b5

Please sign in to comment.