@@ -391,180 +391,97 @@ protected override void BeginProcessing()
391
391
base . BeginProcessing ( ) ;
392
392
}
393
393
394
- public string GetResourceGroupFromObject < T > ( T resource )
394
+ public List < T > TopLevelWildcardFilter < T > ( string resourceGroupName , string name , IEnumerable < T > resources )
395
395
{
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" ) )
398
398
{
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 )
401
401
{
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 ) ;
403
406
}
404
- }
405
407
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 )
411
409
{
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 ) ;
413
414
}
414
- }
415
415
416
- pi = resource . GetType ( ) . GetProperty ( "ResourceGroupName" ) ;
417
- if ( pi != null )
416
+ }
417
+ else
418
418
{
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 )
421
421
{
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 ) ) ;
423
431
}
424
432
}
425
433
426
- return null ;
434
+ return output . ToList ( ) ;
427
435
}
428
436
429
- public string GetResourceNameFromObject < T > ( T resource )
437
+ public List < T > SubResourceWildcardFilter < T > ( string name , IEnumerable < T > resources )
430
438
{
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" ) )
433
441
{
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 )
436
444
{
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 ) ;
438
449
}
439
- }
440
450
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
- }
449
451
}
450
-
451
- pi = resource . GetType ( ) . GetProperty ( "Name" ) ;
452
- if ( pi != null )
452
+ else
453
453
{
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 )
456
456
{
457
- return name ;
457
+ WildcardPattern pattern = new WildcardPattern ( name , WildcardOptions . IgnoreCase ) ;
458
+ output = output . Where ( t => IsMatch ( t , "Name" , pattern ) ) ;
458
459
}
459
460
}
460
461
461
- return null ;
462
+ return output . ToList ( ) ;
462
463
}
463
464
464
- public List < T > TopLevelWildcardFilter < T > ( string ResourceGroupName , string Name , IEnumerable < T > resources )
465
+ private bool HasProperty < T > ( string property )
465
466
{
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
+ }
497
469
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 )
500
474
{
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 ) ;
528
476
}
529
477
530
- return output ;
478
+ return null ;
531
479
}
532
480
533
- public List < T > SubResourceWildcardFilter < T > ( string Name , IEnumerable < T > resources )
481
+ private bool IsMatch < T > ( T resource , string property , WildcardPattern pattern )
534
482
{
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 ) ;
568
485
}
569
486
570
487
public bool ShouldListBySubscription ( string resourceGroupName , string name )
0 commit comments