12
12
// limitations under the License.
13
13
// ----------------------------------------------------------------------------------
14
14
15
- namespace Microsoft . Azure . Commands . ResourceManager . Common . TabCompletion
15
+ namespace Microsoft . Azure . Commands . ResourceManager . Common . ArgumentCompleters
16
16
{
17
17
using Commands . Common . Authentication ;
18
18
using Commands . Common . Authentication . Abstractions ;
19
19
using Management . Internal . Resources ;
20
20
using Management . Internal . Resources . Models ;
21
21
using Properties ;
22
22
using System ;
23
+ using System . Collections . Concurrent ;
23
24
using System . Collections . Generic ;
24
25
using System . Linq ;
25
26
using System . Management . Automation ;
@@ -29,7 +30,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Common.TabCompletion
29
30
/// </summary>
30
31
public class ResourceGroupCompleterAttribute : ArgumentCompleterAttribute
31
32
{
32
- private static IList < String > _resourceGroupNames = new List < string > ( ) ;
33
+ private static IDictionary < int , IList < String > > _resourceGroupNamesDictionary = new ConcurrentDictionary < int , IList < string > > ( ) ;
33
34
private static readonly object _lock = new object ( ) ;
34
35
35
36
protected static IList < String > ResourceGroupNames
@@ -38,52 +39,56 @@ protected static IList<String> ResourceGroupNames
38
39
{
39
40
lock ( _lock )
40
41
{
41
- _resourceGroupNames = new List < string > ( ) ;
42
42
IAzureContext context = AzureRmProfileProvider . Instance . Profile . DefaultContext ;
43
- try
43
+ var contextHash = HashContext ( context ) ;
44
+ if ( ! _resourceGroupNamesDictionary . ContainsKey ( contextHash ) )
44
45
{
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
54
48
{
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 ) ) )
56
58
{
57
- var resourceGroupList = resourceGroups . Result ;
58
- foreach ( ResourceGroup resourceGroup in resourceGroupList )
59
+ if ( resourceGroups . Result != null )
59
60
{
60
- _resourceGroupNames . Add ( resourceGroup . Name ) ;
61
+ var resourceGroupList = resourceGroups . Result ;
62
+ foreach ( ResourceGroup resourceGroup in resourceGroupList )
63
+ {
64
+ _resourceGroupNamesDictionary [ contextHash ] . Add ( resourceGroup . Name ) ;
65
+ }
61
66
}
67
+ #if DEBUG
68
+ else
69
+ {
70
+ throw new Exception ( "Result from client.ResourceGroups is null" ) ;
71
+ }
72
+ #endif
62
73
}
63
74
#if DEBUG
64
75
else
65
76
{
66
- throw new Exception ( "Result from client.ResourceGroups is null " ) ;
77
+ throw new Exception ( "client.ResourceGroups call timed out " ) ;
67
78
}
68
79
#endif
69
80
}
70
- #if DEBUG
71
- else
72
- {
73
- throw new Exception ( "client.ResourceGroups call timed out" ) ;
74
- }
75
- #endif
76
- }
77
81
78
- catch ( Exception ex )
79
- {
82
+ catch ( Exception ex )
83
+ {
80
84
#if DEBUG
81
- throw ex ;
85
+ throw ex ;
82
86
#endif
87
+ }
83
88
}
84
- }
85
89
86
- return _resourceGroupNames ;
90
+ return _resourceGroupNamesDictionary [ contextHash ] ;
91
+ }
87
92
}
88
93
}
89
94
@@ -100,7 +105,7 @@ public static string[] GetResourceGroups()
100
105
{
101
106
IAzureContext context = AzureRmProfileProvider . Instance . Profile . DefaultContext ;
102
107
var resourceGroupNamesCopy = ResourceGroupNames ;
103
- if ( context . ExtendedProperties . ContainsKey ( Resources . DefaultResourceGroupKey ) )
108
+ if ( context . IsPropertySet ( Resources . DefaultResourceGroupKey ) )
104
109
{
105
110
return GetResourceGroups ( resourceGroupNamesCopy , context . ExtendedProperties [ Resources . DefaultResourceGroupKey ] ) ;
106
111
}
@@ -123,10 +128,15 @@ public static string[] GetResourceGroups(IList<string> resourceGroupNames, strin
123
128
private static ScriptBlock CreateScriptBlock ( )
124
129
{
125
130
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 " +
127
132
"$locations | Where-Object { $_ -Like \" $wordToComplete*\" } | ForEach-Object { [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) }" ;
128
133
ScriptBlock scriptBlock = ScriptBlock . Create ( script ) ;
129
134
return scriptBlock ;
130
135
}
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
+ }
131
141
}
132
142
}
0 commit comments