Skip to content

Commit 1319074

Browse files
author
Maddie Clayton
authored
Merge pull request #73 from maddieclayton/completer
address comments
2 parents 6b18c6a + 38d6f94 commit 1319074

File tree

1 file changed

+93
-85
lines changed

1 file changed

+93
-85
lines changed

src/ResourceManager/ArgumentCompleters/ResourceNameCompleter.cs

Lines changed: 93 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -55,94 +55,11 @@ public static string[] FindResources(string resourceType, string[] parentResourc
5555

5656
public static string[] FindResources(string resourceType, string[] parentResources)
5757
{
58-
IAzureContext context = AzureRmProfileProvider.Instance?.Profile?.DefaultContext;
5958
try
6059
{
61-
IResourceManagementClient client = AzureSession.Instance.ClientFactory.CreateArmClient<ResourceManagementClient>(context, AzureEnvironment.Endpoint.ResourceManager);
62-
Task<IPage<GenericResource>> allProviders = null;
63-
var odataQuery = new ODataQuery<GenericResourceFilter>(r => r.ResourceType == resourceType);
64-
65-
if (string.IsNullOrWhiteSpace(parentResources[0]))
66-
{
67-
allProviders = client.Resources.ListAsync(odataQuery);
68-
}
69-
else
70-
{
71-
allProviders = client.ResourceGroups.ListResourcesAsync(parentResources[0], odataQuery);
72-
}
73-
74-
List<ResourceIdentifier> ids = new List<ResourceIdentifier>();
75-
if (_timeout == -1)
76-
{
77-
allProviders.Wait();
78-
if (allProviders.Result != null)
79-
{
80-
foreach(var resource in allProviders.Result.ToList())
81-
{
82-
ids.Add(new ResourceIdentifier(resource.Id));
83-
}
84-
}
85-
else
86-
{
87-
#if DEBUG
88-
throw new InvalidOperationException("Result from client.Providers is null");
89-
#endif
90-
}
91-
}
92-
else if (allProviders.Wait(TimeSpan.FromSeconds(_timeout)))
93-
{
94-
if (allProviders.Result != null)
95-
{
96-
foreach (var resource in allProviders.Result.ToList())
97-
{
98-
ids.Add(new ResourceIdentifier(resource.Id));
99-
}
100-
}
101-
else
102-
{
103-
#if DEBUG
104-
throw new InvalidOperationException("Result from client.Providers is null");
105-
#endif
106-
}
107-
}
108-
else
109-
{
110-
#if DEBUG
111-
throw new InvalidOperationException(Resources.TimeOutForProviderList);
112-
#endif
113-
}
60+
List<ResourceIdentifier> ids = GetResourceIdsFromClient(resourceType, parentResources[0]);
11461

115-
List<string> output = new List<string>();
116-
foreach (var resource in ids)
117-
{
118-
var include = true;
119-
120-
if (resource.ParentResource != null)
121-
{
122-
var actualParentResource = resource.ParentResource.Split('/');
123-
if (actualParentResource.Count() / 2 == parentResources.Count() - 1)
124-
{
125-
for (int i = 0; i < actualParentResource.Count() / 2; i++)
126-
{
127-
if (!string.IsNullOrEmpty(parentResources[i + 1]) && !string.Equals(actualParentResource[i * 2 + 1], parentResources[i + 1], StringComparison.OrdinalIgnoreCase))
128-
{
129-
include = false;
130-
}
131-
}
132-
}
133-
else
134-
{
135-
#if DEBUG
136-
throw new InvalidOperationException("Improper number of parent resources were given");
137-
#endif
138-
}
139-
}
140-
141-
if (include)
142-
{
143-
output.Add(resource.ResourceName);
144-
}
145-
}
62+
List<string> output = FilterByParentResource(ids, parentResources);
14663

14764
return output.ToArray();
14865
}
@@ -198,5 +115,96 @@ public static string CreateFilter(
198115
? filterStringBuilder.ToString()
199116
: filter.CoalesceString();
200117
}
118+
119+
public static List<ResourceIdentifier> GetResourceIdsFromClient(string resourceType, string resourceGroupName)
120+
{
121+
IAzureContext context = AzureRmProfileProvider.Instance?.Profile?.DefaultContext;
122+
IResourceManagementClient client = AzureSession.Instance.ClientFactory.CreateArmClient<ResourceManagementClient>(context, AzureEnvironment.Endpoint.ResourceManager);
123+
Task<IPage<GenericResource>> allProviders = null;
124+
var odataQuery = new ODataQuery<GenericResourceFilter>(r => r.ResourceType == resourceType);
125+
126+
if (string.IsNullOrWhiteSpace(resourceGroupName))
127+
{
128+
allProviders = client.Resources.ListAsync(odataQuery);
129+
}
130+
else
131+
{
132+
allProviders = client.ResourceGroups.ListResourcesAsync(resourceGroupName, odataQuery);
133+
}
134+
135+
List<ResourceIdentifier> ids = new List<ResourceIdentifier>();
136+
if (_timeout == -1)
137+
{
138+
allProviders.Wait(TimeSpan.FromMinutes(5));
139+
if (allProviders.Result != null)
140+
{
141+
allProviders.Result.ToList().ForEach(resource => ids.Add(new ResourceIdentifier(resource.Id)));
142+
}
143+
else
144+
{
145+
#if DEBUG
146+
throw new InvalidOperationException("Result from client.Providers is null");
147+
#endif
148+
}
149+
}
150+
else if (allProviders.Wait(TimeSpan.FromSeconds(_timeout)))
151+
{
152+
if (allProviders.Result != null)
153+
{
154+
allProviders.Result.ToList().ForEach(resource => ids.Add(new ResourceIdentifier(resource.Id)));
155+
}
156+
else
157+
{
158+
#if DEBUG
159+
throw new InvalidOperationException("Result from client.Providers is null");
160+
#endif
161+
}
162+
}
163+
else
164+
{
165+
#if DEBUG
166+
throw new InvalidOperationException(Resources.TimeOutForProviderList);
167+
#endif
168+
}
169+
170+
return ids;
171+
}
172+
173+
public static List<string> FilterByParentResource(List<ResourceIdentifier> ids, string[] parentResources)
174+
{
175+
List<string> output = new List<string>();
176+
foreach (var resource in ids)
177+
{
178+
var include = true;
179+
180+
if (resource.ParentResource != null)
181+
{
182+
var actualParentResource = resource.ParentResource.Split('/');
183+
if (actualParentResource.Count() / 2 == parentResources.Count() - 1)
184+
{
185+
for (int i = 0; i < actualParentResource.Count() / 2; i++)
186+
{
187+
if (!string.IsNullOrEmpty(parentResources[i + 1]) && !string.Equals(actualParentResource[i * 2 + 1], parentResources[i + 1], StringComparison.OrdinalIgnoreCase))
188+
{
189+
include = false;
190+
}
191+
}
192+
}
193+
else
194+
{
195+
#if DEBUG
196+
throw new InvalidOperationException("Improper number of parent resources were given");
197+
#endif
198+
}
199+
}
200+
201+
if (include)
202+
{
203+
output.Add(resource.ResourceName);
204+
}
205+
}
206+
207+
return output;
208+
}
201209
}
202210
}

0 commit comments

Comments
 (0)