Skip to content

Commit ec3088c

Browse files
committed
Merge pull request Azure#976 from chidmdxx/RMProviderLocation
Add location filter to Get-AzureRMResourceProvider and add location output
2 parents cb4e63b + 5747444 commit ec3088c

File tree

4 files changed

+125
-20
lines changed

4 files changed

+125
-20
lines changed

src/ResourceManager/Resources/Commands.Resources.Test/Providers/GetAzureProviderCmdletTests.cs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void GetsResourceProviderTests()
9191
{
9292
new ProviderResourceType
9393
{
94-
Locations = new[] {"West US", "East US"},
94+
Locations = new[] {"West US", "East US", "South US"},
9595
Name = "TestResource2"
9696
}
9797
}
@@ -197,6 +197,52 @@ public void GetsResourceProviderTests()
197197
this.cmdlet.ExecuteCmdlet();
198198

199199
this.VerifyGetCallPatternAndReset();
200+
201+
// 4. List only registered providers with location
202+
this.cmdlet.Location = "South US";
203+
this.cmdlet.ListAvailable = false;
204+
this.cmdlet.ProviderNamespace = null;
205+
206+
this.commandRuntimeMock
207+
.Setup(m => m.WriteObject(It.IsAny<object>()))
208+
.Callback((object obj) =>
209+
{
210+
Assert.IsType<PSResourceProvider[]>(obj);
211+
212+
var providers = (PSResourceProvider[])obj;
213+
Assert.Equal(0, providers.Length);
214+
});
215+
216+
this.cmdlet.ParameterSetOverride = GetAzureProviderCmdlet.ListAvailableParameterSet;
217+
218+
this.cmdlet.ExecuteCmdlet();
219+
220+
this.VerifyListCallPatternAndReset();
221+
222+
// 5. List all providers
223+
this.cmdlet.ListAvailable = true;
224+
this.cmdlet.Location = "South US";
225+
this.cmdlet.ProviderNamespace = null;
226+
227+
this.commandRuntimeMock
228+
.Setup(m => m.WriteObject(It.IsAny<object>()))
229+
.Callback((object obj) =>
230+
{
231+
var providers = (PSResourceProvider[])obj;
232+
Assert.Equal(0, providers.Length);
233+
234+
var provider = providers.Single();
235+
Assert.Equal(UnregisteredProviderNamespace, provider.ProviderNamespace);
236+
237+
Assert.Equal(1, provider.ResourceTypes.Length);
238+
239+
var resourceType = provider.ResourceTypes.Single();
240+
Assert.Equal(ResourceTypeName, resourceType.ResourceTypeName);
241+
});
242+
243+
this.cmdlet.ExecuteCmdlet();
244+
245+
this.VerifyListCallPatternAndReset();
200246
}
201247

202248
/// <summary>

src/ResourceManager/Resources/Commands.Resources/Models.ResourceGroups/PSResourceProvider.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
namespace Microsoft.Azure.Commands.Resources.Models
1616
{
17+
using System;
18+
using System.Linq;
19+
1720
/// <summary>
1821
/// Definition of a resource provider and its registration state
1922
/// </summary>
@@ -33,5 +36,19 @@ public class PSResourceProvider
3336
/// Gets or sets the resource types belonging to this provider.
3437
/// </summary>
3538
public PSResourceProviderResourceType[] ResourceTypes { get; set; }
39+
40+
/// <summary>
41+
/// Gets the locations for the provider.
42+
/// </summary>
43+
public string[] Locations
44+
{
45+
get
46+
{
47+
return this.ResourceTypes
48+
.SelectMany(type => type.Locations)
49+
.Distinct(StringComparer.InvariantCultureIgnoreCase)
50+
.ToArray();
51+
}
52+
}
3653
}
3754
}

src/ResourceManager/Resources/Commands.Resources/Models.ResourceGroups/ResourceClient.cs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -394,16 +394,26 @@ internal List<PSPermission> GetResourcePermissions(ResourceIdentifier identity)
394394
return null;
395395
}
396396

397-
public virtual PSResourceProvider[] ListPSResourceProviders(string providerName = null)
397+
public virtual PSResourceProvider[] ListPSResourceProviders(string providerName = null, bool listAvailable = false, string location = null)
398398
{
399-
return this.ListResourceProviders(providerName: providerName, listAvailable: false)
400-
.Select(provider => provider.ToPSResourceProvider())
401-
.ToArray();
402-
}
399+
var providers = this.ListResourceProviders(providerName: providerName, listAvailable: listAvailable);
403400

404-
public virtual PSResourceProvider[] ListPSResourceProviders(bool listAvailable)
405-
{
406-
return this.ListResourceProviders(providerName: null, listAvailable: listAvailable)
401+
if (string.IsNullOrEmpty(location))
402+
{
403+
return providers
404+
.Select(provider => provider.ToPSResourceProvider())
405+
.ToArray();
406+
}
407+
408+
foreach (var provider in providers)
409+
{
410+
provider.ResourceTypes = provider.ResourceTypes
411+
.Where(type => !type.Locations.Any() || this.ContainsNormalizedLocation(type.Locations.ToArray(), location))
412+
.ToList();
413+
}
414+
415+
return providers
416+
.Where(provider => provider.ResourceTypes.Any())
407417
.Select(provider => provider.ToPSResourceProvider())
408418
.ToArray();
409419
}
@@ -439,6 +449,16 @@ public virtual List<Provider> ListResourceProviders(string providerName = null,
439449
}
440450
}
441451

452+
private bool ContainsNormalizedLocation(string[] locations, string location)
453+
{
454+
return locations.Any(existingLocation => this.NormalizeLetterOrDigitToUpperInvariant(existingLocation).Equals(this.NormalizeLetterOrDigitToUpperInvariant(location)));
455+
}
456+
457+
private string NormalizeLetterOrDigitToUpperInvariant(string value)
458+
{
459+
return value != null ? new string(value.Where(c => char.IsLetterOrDigit(c)).ToArray()).ToUpperInvariant() : null;
460+
}
461+
442462
private bool IsProviderRegistered(Provider provider)
443463
{
444464
return string.Equals(

src/ResourceManager/Resources/Commands.Resources/Providers/GetAzureProviderCmdlet.cs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
namespace Microsoft.Azure.Commands.Providers
1616
{
1717
using System;
18+
using System.Linq;
1819
using System.Management.Automation;
1920
using Microsoft.Azure.Commands.Resources.Models;
2021

@@ -41,6 +42,13 @@ public class GetAzureProviderCmdlet : ResourcesBaseCmdlet
4142
[ValidateNotNullOrEmpty]
4243
public string ProviderNamespace { get; set; }
4344

45+
/// <summary>
46+
/// Gets or sets the provider namespace
47+
/// </summary>
48+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = false, HelpMessage = "The location to look for provider namespace.")]
49+
[ValidateNotNullOrEmpty]
50+
public string Location { get; set; }
51+
4452
/// <summary>
4553
/// Gets or sets a value indicating if unregistered providers should be included in the listing
4654
/// </summary>
@@ -52,20 +60,34 @@ public class GetAzureProviderCmdlet : ResourcesBaseCmdlet
5260
/// </summary>
5361
protected override void ProcessRecord()
5462
{
55-
var parameterSetName = this.DetermineParameterSetName();
63+
var providers = this.ResourcesClient.ListPSResourceProviders(providerName: this.ProviderNamespace, listAvailable: this.ListAvailable, location: this.Location);
5664

57-
switch (parameterSetName)
65+
if (!string.IsNullOrEmpty(this.ProviderNamespace))
5866
{
59-
case GetAzureProviderCmdlet.IndividualProviderParameterSet:
60-
this.WriteObject(this.ResourcesClient.ListPSResourceProviders(providerName: this.ProviderNamespace), enumerateCollection: true);
61-
break;
62-
63-
case GetAzureProviderCmdlet.ListAvailableParameterSet:
64-
this.WriteObject(this.ResourcesClient.ListPSResourceProviders(listAvailable: this.ListAvailable), enumerateCollection: true);
65-
break;
67+
var expandedProviders = providers
68+
.SelectMany(provider =>
69+
provider.ResourceTypes
70+
.Select(type =>
71+
new PSResourceProvider
72+
{
73+
ProviderNamespace = provider.ProviderNamespace,
74+
RegistrationState = provider.RegistrationState,
75+
ResourceTypes = new[]
76+
{
77+
new PSResourceProviderResourceType
78+
{
79+
ResourceTypeName = type.ResourceTypeName,
80+
Locations = type.Locations,
81+
ApiVersions = type.ApiVersions,
82+
}
83+
}
84+
}));
6685

67-
default:
68-
throw new ApplicationException(string.Format("Unknown parameter set encountered: '{0}'", this.ParameterSetName));
86+
this.WriteObject(expandedProviders, enumerateCollection: true);
87+
}
88+
else
89+
{
90+
this.WriteObject(providers, enumerateCollection: true);
6991
}
7092
}
7193
}

0 commit comments

Comments
 (0)