Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters
{
using Commands.Common.Authentication;
using Commands.Common.Authentication.Abstractions;
using Management.Internal.Resources;
using Management.Internal.Resources.Models;
using Properties;
using Rest.Azure;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;

/// <summary>
/// This attribute will allow the user to autocomplete the -ResourceGroup parameter of a cmdlet with valid resource groups
/// </summary>
public class ResourceGroupCompleterAttribute : ArgumentCompleterAttribute
{
private static IDictionary<int, IList<String>> _resourceGroupNamesDictionary = new ConcurrentDictionary<int, IList<string>>();
private static readonly object _lock = new object();

protected static IList<String> ResourceGroupNames
{
get
{
lock (_lock)
{
IAzureContext context = AzureRmProfileProvider.Instance.Profile.DefaultContext;
var contextHash = HashContext(context);
if (!_resourceGroupNamesDictionary.ContainsKey(contextHash))
{
var tempResourceGroupList = new List<string>();
try
{
var instance = AzureSession.Instance;
var client = instance.ClientFactory.CreateCustomArmClient<ResourceManagementClient>(
context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager),
instance.AuthenticationFactory.GetServiceClientCredentials(context, AzureEnvironment.Endpoint.ResourceManager),
instance.ClientFactory.GetCustomHandlers());
client.SubscriptionId = context.Subscription.Id;
// Retrieve only the first page of ResourceGroups to display to the user
var resourceGroups = client.ResourceGroups.ListAsync();
if (resourceGroups.Wait(TimeSpan.FromSeconds(5)))
{
tempResourceGroupList = CreateResourceGroupList(resourceGroups.Result);
if (resourceGroups.Result != null)
{
_resourceGroupNamesDictionary[contextHash] = tempResourceGroupList;
}
}
#if DEBUG
else
{
throw new Exception("client.ResourceGroups call timed out");
}
#endif
}

catch (Exception ex)
{
#if DEBUG
throw ex;
#endif
}

return tempResourceGroupList;
}

else
{
return _resourceGroupNamesDictionary[contextHash];
}
}
}
}

/// <summary>
/// This class will provide a list of resource groups that are available to the user (with default resource group first if it exists). This will then be available to the user to tab through.
/// Example: [Parameter(ParameterSetName = ListByNameInTenantParameterSet, ValueFromPipelineByPropertyName = true, Mandatory = false), ResourceGroupCompleter()]
/// </summary>
/// <param name="resourceTypes"></param>
public ResourceGroupCompleterAttribute() : base(CreateScriptBlock())
{
}

public static string[] GetResourceGroups()
{
IAzureContext context = AzureRmProfileProvider.Instance.Profile.DefaultContext;
var resourceGroupNamesCopy = ResourceGroupNames;
if (context.IsPropertySet(Resources.DefaultResourceGroupKey))
{
return GetResourceGroups(resourceGroupNamesCopy, context.ExtendedProperties[Resources.DefaultResourceGroupKey]);
}
return GetResourceGroups(resourceGroupNamesCopy, null);
}

public static string[] GetResourceGroups(IList<string> resourceGroupNames, string defaultResourceGroup)
{
if (defaultResourceGroup != null)
{
if (resourceGroupNames.Contains(defaultResourceGroup))
{
resourceGroupNames.Remove(defaultResourceGroup);
}
resourceGroupNames.Insert(0, defaultResourceGroup);
}
return resourceGroupNames.ToArray();
}

private static ScriptBlock CreateScriptBlock()
{
string script = "param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)\n" +
"$locations = [Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters.ResourceGroupCompleterAttribute]::GetResourceGroups()\n" +
"$locations | Where-Object { $_ -Like \"$wordToComplete*\" } | ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) }";
ScriptBlock scriptBlock = ScriptBlock.Create(script);
return scriptBlock;
}

private static int HashContext(IAzureContext context)
{
return (context.Account.Id + context.Environment.Name + context.Subscription.Id + context.Tenant.Id).GetHashCode();
}

public static List<string> CreateResourceGroupList(IPage<ResourceGroup> result)
{
var tempResourceGroupList = new List<string>();
if (result != null)
{
foreach (ResourceGroup resourceGroup in result)
{
tempResourceGroupList.Add(resourceGroup.Name);
}
}
#if DEBUG
else
{
throw new Exception("Result from client.ResourceGroups is null");
}
#endif
return tempResourceGroupList;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="ArgumentCompleters\ResourceGroupCompleter.cs" />
<Compile Include="ResponseWithContinuation.cs" />
<Compile Include="RPRegistrationDelegatingHandler.cs" />
<Compile Include="ServiceClientTracingInterceptor.cs" />
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,7 @@ The data is collected by Microsoft.
Use the Disable-AzureDataCollection cmdlet to turn the feature Off. The cmdlet can be found in the Azure module. To disable data collection: PS &gt; Disable-AzureDataCollection.
Use the Enable-AzureDataCollection cmdlet to turn the feature On. The cmdlet can be found in the Azure module. To enable data collection: PS &gt; Enable-AzureDataCollection.</value>
</data>
<data name="DefaultResourceGroupKey" xml:space="preserve">
<value>Default Resource Group</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@
<Compile Include="ProfileController.cs" />
<Compile Include="ProtectedFileProviderTests.cs" />
<Compile Include="PSSerializationTests.cs" />
<Compile Include="ResourceGroupCompleterUnitTests.cs" />
<Compile Include="RPRegistrationDelegatingHandlerTests.cs" />
<Compile Include="SessionInitializationTests.cs" />
<Compile Include="SubscriptionCmdletTests.cs" />
Expand Down Expand Up @@ -286,4 +287,4 @@
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />
</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using System;
using System.Collections.Generic;
using Xunit;

namespace Microsoft.Azure.Commands.Profile.Test
{
public class ResourceGroupCompleterUnitTests
{
[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void ReturnsEmptyListWhenNoResourceGroupsExist()
{
IList<string> resourceGroupsReturned = new List<string>();
Assert.Collection(ResourceGroupCompleterAttribute.GetResourceGroups(resourceGroupsReturned, null));
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void OneResourceGroupNoDefault()
{
IList<string> resourceGroupsReturned = new List<string>();
resourceGroupsReturned.Add("test1");
Assert.Collection(ResourceGroupCompleterAttribute.GetResourceGroups(resourceGroupsReturned, null), e1 => Assert.Equal("test1", e1));
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void OneResourceGroupWithDefault()
{
IList<string> resourceGroupsReturned = new List<string>();
resourceGroupsReturned.Add("test1");
Assert.Collection(ResourceGroupCompleterAttribute.GetResourceGroups(resourceGroupsReturned, "test1"), e1 => Assert.Equal("test1", e1));
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void OneResourceGroupWithInvalidDefault()
{
IList<string> resourceGroupsReturned = new List<string>();
resourceGroupsReturned.Add("test1");
Assert.Collection(ResourceGroupCompleterAttribute.GetResourceGroups(resourceGroupsReturned, "defaultOutOfPage"), e1 => Assert.Equal("defaultOutOfPage", e1),
e2 => Assert.Equal("test1", e2));
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void MultipleResourceGroupsNoDefault()
{
IList<string> resourceGroupsReturned = new List<string>();
resourceGroupsReturned.Add("test1");
resourceGroupsReturned.Add("test2");
resourceGroupsReturned.Add("test3");
resourceGroupsReturned.Add("test4");
Assert.Collection(ResourceGroupCompleterAttribute.GetResourceGroups(resourceGroupsReturned, null), e1 => Assert.Equal("test1", e1),
e2 => Assert.Equal("test2", e2), e3 => Assert.Equal("test3", e3), e4 => Assert.Equal("test4", e4));
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void MultipleResourceGroupsWithDefault()
{
IList<string> resourceGroupsReturned = new List<string>();
resourceGroupsReturned.Add("test1");
resourceGroupsReturned.Add("test2");
resourceGroupsReturned.Add("test3");
resourceGroupsReturned.Add("test4");
Assert.Collection(ResourceGroupCompleterAttribute.GetResourceGroups(resourceGroupsReturned, "test3"), e1 => Assert.Equal("test3", e1),
e2 => Assert.Equal("test1", e2), e3 => Assert.Equal("test2", e3), e4 => Assert.Equal("test4", e4));
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void ThrowsErrorWhenResultNull()
{
var ex = Assert.Throws<Exception>(() => ResourceGroupCompleterAttribute.CreateResourceGroupList(null));
Assert.Equal(ex.Message, "Result from client.ResourceGroups is null");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Microsoft.Azure.Commands.Profile.Models;
using Microsoft.Azure.Commands.Profile.Properties;
using Microsoft.Azure.Commands.ResourceManager.Common;
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
using Microsoft.Azure.Management.Internal.Resources;
using Microsoft.Azure.Management.Internal.Resources.Models;
using System.Management.Automation;
Expand All @@ -36,6 +37,7 @@ public class SetAzureRMDefaultCommand : AzureContextModificationCmdlet
private const string ResourceGroupNameParameterSet = "ResourceGroupName";

[Parameter(ParameterSetName = ResourceGroupNameParameterSet, Mandatory = false, HelpMessage = "Name of the resource group being set as default", ValueFromPipelineByPropertyName = true)]
[ResourceGroupCompleter()]
[ValidateNotNullOrEmpty]
public string ResourceGroupName { get; set; }

Expand Down