Skip to content

Commit 06f7963

Browse files
committed
fix PR comments
1 parent 86d26f4 commit 06f7963

File tree

5 files changed

+47
-35
lines changed

5 files changed

+47
-35
lines changed

src/ResourceManager/Common/Commands.ResourceManager.Common/TabCompleter/ResourceGroupCompleter.cs renamed to src/ResourceManager/Common/Commands.ResourceManager.Common/ArgumentCompleters/ResourceGroupCompleter.cs

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
namespace Microsoft.Azure.Commands.ResourceManager.Common.TabCompletion
15+
namespace Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters
1616
{
1717
using Commands.Common.Authentication;
1818
using Commands.Common.Authentication.Abstractions;
1919
using Management.Internal.Resources;
2020
using Management.Internal.Resources.Models;
2121
using Properties;
2222
using System;
23+
using System.Collections.Concurrent;
2324
using System.Collections.Generic;
2425
using System.Linq;
2526
using System.Management.Automation;
@@ -29,7 +30,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Common.TabCompletion
2930
/// </summary>
3031
public class ResourceGroupCompleterAttribute : ArgumentCompleterAttribute
3132
{
32-
private static IList<String> _resourceGroupNames = new List<string>();
33+
private static IDictionary<int, IList<String>> _resourceGroupNamesDictionary = new ConcurrentDictionary<int, IList<string>>();
3334
private static readonly object _lock = new object();
3435

3536
protected static IList<String> ResourceGroupNames
@@ -38,52 +39,56 @@ protected static IList<String> ResourceGroupNames
3839
{
3940
lock (_lock)
4041
{
41-
_resourceGroupNames = new List<string>();
4242
IAzureContext context = AzureRmProfileProvider.Instance.Profile.DefaultContext;
43-
try
43+
var contextHash = HashContext(context);
44+
if (!_resourceGroupNamesDictionary.ContainsKey(contextHash))
4445
{
45-
var instance = AzureSession.Instance;
46-
var client = instance.ClientFactory.CreateCustomArmClient<ResourceManagementClient>(
47-
context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager),
48-
instance.AuthenticationFactory.GetServiceClientCredentials(context, AzureEnvironment.Endpoint.ResourceManager),
49-
instance.ClientFactory.GetCustomHandlers());
50-
client.SubscriptionId = context.Subscription.Id;
51-
// Retrieve only the first page of ResourceGroups to display to the user
52-
var resourceGroups = client.ResourceGroups.ListAsync();
53-
if (resourceGroups.Wait(TimeSpan.FromSeconds(5)))
46+
_resourceGroupNamesDictionary[contextHash] = new List<string>();
47+
try
5448
{
55-
if (resourceGroups.Result != null)
49+
var instance = AzureSession.Instance;
50+
var client = instance.ClientFactory.CreateCustomArmClient<ResourceManagementClient>(
51+
context.Environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager),
52+
instance.AuthenticationFactory.GetServiceClientCredentials(context, AzureEnvironment.Endpoint.ResourceManager),
53+
instance.ClientFactory.GetCustomHandlers());
54+
client.SubscriptionId = context.Subscription.Id;
55+
// Retrieve only the first page of ResourceGroups to display to the user
56+
var resourceGroups = client.ResourceGroups.ListAsync();
57+
if (resourceGroups.Wait(TimeSpan.FromSeconds(5)))
5658
{
57-
var resourceGroupList = resourceGroups.Result;
58-
foreach (ResourceGroup resourceGroup in resourceGroupList)
59+
if (resourceGroups.Result != null)
5960
{
60-
_resourceGroupNames.Add(resourceGroup.Name);
61+
var resourceGroupList = resourceGroups.Result;
62+
foreach (ResourceGroup resourceGroup in resourceGroupList)
63+
{
64+
_resourceGroupNamesDictionary[contextHash].Add(resourceGroup.Name);
65+
}
6166
}
67+
#if DEBUG
68+
else
69+
{
70+
throw new Exception("Result from client.ResourceGroups is null");
71+
}
72+
#endif
6273
}
6374
#if DEBUG
6475
else
6576
{
66-
throw new Exception("Result from client.ResourceGroups is null");
77+
throw new Exception("client.ResourceGroups call timed out");
6778
}
6879
#endif
6980
}
70-
#if DEBUG
71-
else
72-
{
73-
throw new Exception("client.ResourceGroups call timed out");
74-
}
75-
#endif
76-
}
7781

78-
catch (Exception ex)
79-
{
82+
catch (Exception ex)
83+
{
8084
#if DEBUG
81-
throw ex;
85+
throw ex;
8286
#endif
87+
}
8388
}
84-
}
8589

86-
return _resourceGroupNames;
90+
return _resourceGroupNamesDictionary[contextHash];
91+
}
8792
}
8893
}
8994

@@ -100,7 +105,7 @@ public static string[] GetResourceGroups()
100105
{
101106
IAzureContext context = AzureRmProfileProvider.Instance.Profile.DefaultContext;
102107
var resourceGroupNamesCopy = ResourceGroupNames;
103-
if (context.ExtendedProperties.ContainsKey(Resources.DefaultResourceGroupKey))
108+
if (context.IsPropertySet(Resources.DefaultResourceGroupKey))
104109
{
105110
return GetResourceGroups(resourceGroupNamesCopy, context.ExtendedProperties[Resources.DefaultResourceGroupKey]);
106111
}
@@ -123,10 +128,15 @@ public static string[] GetResourceGroups(IList<string> resourceGroupNames, strin
123128
private static ScriptBlock CreateScriptBlock()
124129
{
125130
string script = "param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)\n" +
126-
"$locations = [Microsoft.Azure.Commands.ResourceManager.Common.TabCompletion.ResourceGroupCompleterAttribute]::GetResourceGroups()\n" +
131+
"$locations = [Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters.ResourceGroupCompleterAttribute]::GetResourceGroups()\n" +
127132
"$locations | Where-Object { $_ -Like \"$wordToComplete*\" } | ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) }";
128133
ScriptBlock scriptBlock = ScriptBlock.Create(script);
129134
return scriptBlock;
130135
}
136+
137+
private static int HashContext(IAzureContext context)
138+
{
139+
return (context.Account.Id + context.Environment.Name + context.Subscription.Id + context.Tenant.Id).GetHashCode();
140+
}
131141
}
132142
}

src/ResourceManager/Common/Commands.ResourceManager.Common/Commands.ResourceManager.Common.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,15 @@
177177
<Compile Include="Generated\TenantsOperations.cs" />
178178
<Compile Include="Generated\TenantsOperationsExtensions.cs" />
179179
<Compile Include="GlobalSuppressions.cs" />
180-
<Compile Include="ArgumentCompleter\LocationCompleter.cs" />
180+
<Compile Include="ArgumentCompleters\LocationCompleter.cs" />
181181
<Compile Include="PaginatedResponseHelper.cs" />
182182
<Compile Include="Properties\AssemblyInfo.cs" />
183183
<Compile Include="Properties\Resources.Designer.cs">
184184
<AutoGen>True</AutoGen>
185185
<DesignTime>True</DesignTime>
186186
<DependentUpon>Resources.resx</DependentUpon>
187187
</Compile>
188-
<Compile Include="TabCompleter\ResourceGroupCompleter.cs" />
188+
<Compile Include="ArgumentCompleters\ResourceGroupCompleter.cs" />
189189
<Compile Include="ResponseWithContinuation.cs" />
190190
<Compile Include="RPRegistrationDelegatingHandler.cs" />
191191
<Compile Include="ServiceClientTracingInterceptor.cs" />

src/ResourceManager/Profile/Commands.Profile.Test/ResourcGroupCompleterUnitTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// limitations under the License.
1313
// ----------------------------------------------------------------------------------
1414

15-
using Microsoft.Azure.Commands.ResourceManager.Common.TabCompletion;
15+
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
1616
using Microsoft.WindowsAzure.Commands.ScenarioTest;
1717
using System.Collections.Generic;
1818
using Xunit;

src/ResourceManager/Profile/Commands.Profile/Default/SetAzureRMDefault.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using Microsoft.Azure.Commands.Profile.Models;
2020
using Microsoft.Azure.Commands.Profile.Properties;
2121
using Microsoft.Azure.Commands.ResourceManager.Common;
22+
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
2223
using Microsoft.Azure.Management.Internal.Resources;
2324
using Microsoft.Azure.Management.Internal.Resources.Models;
2425
using System.Management.Automation;
@@ -36,6 +37,7 @@ public class SetAzureRMDefaultCommand : AzureContextModificationCmdlet
3637
private const string ResourceGroupNameParameterSet = "ResourceGroupName";
3738

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

0 commit comments

Comments
 (0)