Skip to content

Commit 1afc57f

Browse files
author
Maddie Clayton
committed
update based on comments
1 parent d50ddb2 commit 1afc57f

File tree

1 file changed

+55
-138
lines changed

1 file changed

+55
-138
lines changed

src/ResourceManager/Version2016_09_01/AzureRMCmdlet.cs

Lines changed: 55 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -391,180 +391,97 @@ protected override void BeginProcessing()
391391
base.BeginProcessing();
392392
}
393393

394-
public string GetResourceGroupFromObject<T>(T resource)
394+
public List<T> TopLevelWildcardFilter<T>(string resourceGroupName, string name, IEnumerable<T> resources)
395395
{
396-
System.Reflection.PropertyInfo pi = resource.GetType().GetProperty("Id");
397-
if (pi != null)
396+
IEnumerable<T> output = resources;
397+
if (HasProperty<T>("ResourceId") || HasProperty<T>("Id"))
398398
{
399-
ResourceIdentifier parsedId = new ResourceIdentifier((String)(pi.GetValue(resource, null)));
400-
if (parsedId.ResourceGroupName != null)
399+
string idProperty = HasProperty<T>("ResourceId") ? "ResourceId" : "Id";
400+
if (resourceGroupName != null)
401401
{
402-
return parsedId.ResourceGroupName;
402+
WildcardPattern pattern = new WildcardPattern(resourceGroupName, WildcardOptions.IgnoreCase);
403+
output = output.Select(t => new { Id = new ResourceIdentifier((string) GetPropertyValue(t, idProperty)), Resource = t })
404+
.Where(p => IsMatch(p.Id, "ResourceGroupName", pattern))
405+
.Select(r => r.Resource);
403406
}
404-
}
405407

406-
pi = resource.GetType().GetProperty("ResourceId");
407-
if (pi != null)
408-
{
409-
ResourceIdentifier parsedId = new ResourceIdentifier((String)(pi.GetValue(resource, null)));
410-
if (parsedId.ResourceGroupName != null)
408+
if (name != null)
411409
{
412-
return parsedId.ResourceGroupName;
410+
WildcardPattern pattern = new WildcardPattern(name, WildcardOptions.IgnoreCase);
411+
output = output.Select(t => new { Id = new ResourceIdentifier((string) GetPropertyValue(t, idProperty)), Resource = t })
412+
.Where(p => IsMatch(p.Id, "ResourceName", pattern))
413+
.Select(r => r.Resource);
413414
}
414-
}
415415

416-
pi = resource.GetType().GetProperty("ResourceGroupName");
417-
if (pi != null)
416+
}
417+
else
418418
{
419-
var resourceGroupName = (String)(pi.GetValue(resource, null));
420-
if (resourceGroupName != null)
419+
// if ResourceGroupName property, filter resource group
420+
if (HasProperty<T>("ResourceGroupName") && resourceGroupName != null)
421421
{
422-
return resourceGroupName;
422+
WildcardPattern pattern = new WildcardPattern(resourceGroupName, WildcardOptions.IgnoreCase);
423+
output = output.Where(t => IsMatch(t, "ResourceGroupName", pattern));
424+
}
425+
426+
// if Name property, filter name
427+
if (HasProperty<T>("Name") && name != null)
428+
{
429+
WildcardPattern pattern = new WildcardPattern(name, WildcardOptions.IgnoreCase);
430+
output = output.Where(t => IsMatch(t, "Name", pattern));
423431
}
424432
}
425433

426-
return null;
434+
return output.ToList();
427435
}
428436

429-
public string GetResourceNameFromObject<T>(T resource)
437+
public List<T> SubResourceWildcardFilter<T>(string name, IEnumerable<T> resources)
430438
{
431-
System.Reflection.PropertyInfo pi = resource.GetType().GetProperty("Id");
432-
if (pi != null)
439+
IEnumerable<T> output = resources;
440+
if (HasProperty<T>("ResourceId") || HasProperty<T>("Id"))
433441
{
434-
ResourceIdentifier parsedId = new ResourceIdentifier((String)(pi.GetValue(resource, null)));
435-
if (parsedId.ResourceName != null)
442+
string idProperty = HasProperty<T>("ResourceId") ? "ResourceId" : "Id";
443+
if (name != null)
436444
{
437-
return parsedId.ResourceName;
445+
WildcardPattern pattern = new WildcardPattern(name, WildcardOptions.IgnoreCase);
446+
output = output.Select(t => new { Id = new ResourceIdentifier((string)GetPropertyValue(t, idProperty)), Resource = t })
447+
.Where(p => IsMatch(p.Id, "ResourceName", pattern))
448+
.Select(r => r.Resource);
438449
}
439-
}
440450

441-
pi = resource.GetType().GetProperty("ResourceId");
442-
if (pi != null)
443-
{
444-
ResourceIdentifier parsedId = new ResourceIdentifier((String)(pi.GetValue(resource, null)));
445-
if (parsedId.ResourceName != null)
446-
{
447-
return parsedId.ResourceName;
448-
}
449451
}
450-
451-
pi = resource.GetType().GetProperty("Name");
452-
if (pi != null)
452+
else
453453
{
454-
var name = (String)(pi.GetValue(resource, null));
455-
if (name != null)
454+
// if Name property, filter name
455+
if (HasProperty<T>("Name") && name != null)
456456
{
457-
return name;
457+
WildcardPattern pattern = new WildcardPattern(name, WildcardOptions.IgnoreCase);
458+
output = output.Where(t => IsMatch(t, "Name", pattern));
458459
}
459460
}
460461

461-
return null;
462+
return output.ToList();
462463
}
463464

464-
public List<T> TopLevelWildcardFilter<T>(string ResourceGroupName, string Name, IEnumerable<T> resources)
465+
private bool HasProperty<T>(string property)
465466
{
466-
List<T> resourceGroupMatch = new List<T>();
467-
foreach (var resource in resources)
468-
{
469-
string parsedResourceGroup = GetResourceGroupFromObject(resource);
470-
if (parsedResourceGroup != null)
471-
{
472-
if (string.IsNullOrEmpty(ResourceGroupName))
473-
{
474-
resourceGroupMatch.Add(resource);
475-
}
476-
else if (WildcardPattern.ContainsWildcardCharacters(ResourceGroupName))
477-
{
478-
WildcardPattern regex = new WildcardPattern(ResourceGroupName, WildcardOptions.IgnoreCase);
479-
if (regex.IsMatch(parsedResourceGroup))
480-
{
481-
resourceGroupMatch.Add(resource);
482-
}
483-
}
484-
else
485-
{
486-
if (ResourceGroupName.Equals(parsedResourceGroup, StringComparison.CurrentCultureIgnoreCase))
487-
{
488-
resourceGroupMatch.Add(resource);
489-
}
490-
}
491-
}
492-
else
493-
{
494-
resourceGroupMatch.Add(resource);
495-
}
496-
}
467+
return typeof(T).GetProperty(property) != null;
468+
}
497469

498-
List<T> output = new List<T>();
499-
foreach (var resource in resourceGroupMatch)
470+
private object GetPropertyValue<T>(T resource, string property)
471+
{
472+
System.Reflection.PropertyInfo pi = typeof(T).GetProperty(property);
473+
if (pi != null)
500474
{
501-
var parsedResourceName = GetResourceNameFromObject(resource);
502-
if (parsedResourceName != null)
503-
{
504-
if (string.IsNullOrEmpty(Name))
505-
{
506-
output.Add(resource);
507-
}
508-
else if (WildcardPattern.ContainsWildcardCharacters(Name))
509-
{
510-
WildcardPattern regex = new WildcardPattern(Name, WildcardOptions.IgnoreCase);
511-
if (regex.IsMatch(parsedResourceName))
512-
{
513-
output.Add(resource);
514-
}
515-
}
516-
else
517-
{
518-
if (Name.Equals(parsedResourceName, StringComparison.InvariantCultureIgnoreCase))
519-
{
520-
output.Add(resource);
521-
}
522-
}
523-
}
524-
else
525-
{
526-
output.Add(resource);
527-
}
475+
return pi.GetValue(resource, null);
528476
}
529477

530-
return output;
478+
return null;
531479
}
532480

533-
public List<T> SubResourceWildcardFilter<T>(string Name, IEnumerable<T> resources)
481+
private bool IsMatch<T>(T resource, string property, WildcardPattern pattern)
534482
{
535-
List<T> output = new List<T>();
536-
foreach (var resource in resources)
537-
{
538-
var parsedResourceName = GetResourceNameFromObject(resource);
539-
if (parsedResourceName != null)
540-
{
541-
if (string.IsNullOrEmpty(Name))
542-
{
543-
output.Add(resource);
544-
}
545-
else if (WildcardPattern.ContainsWildcardCharacters(Name))
546-
{
547-
WildcardPattern regex = new WildcardPattern(Name, WildcardOptions.IgnoreCase);
548-
if (regex.IsMatch(parsedResourceName))
549-
{
550-
output.Add(resource);
551-
}
552-
}
553-
else
554-
{
555-
if (Name.Equals(parsedResourceName, StringComparison.InvariantCultureIgnoreCase))
556-
{
557-
output.Add(resource);
558-
}
559-
}
560-
}
561-
else
562-
{
563-
output.Add(resource);
564-
}
565-
}
566-
567-
return output;
483+
var value = (string)GetPropertyValue(resource, property);
484+
return !string.IsNullOrEmpty(value) && pattern.IsMatch(value);
568485
}
569486

570487
public bool ShouldListBySubscription(string resourceGroupName, string name)

0 commit comments

Comments
 (0)