Skip to content

Commit 6767e15

Browse files
Tiano2017Hovsep
authored andcommitted
Fix Get-AzureRmResourcePovider with locations. (Azure#2668)
1 parent 632b427 commit 6767e15

File tree

7 files changed

+367
-2428
lines changed

7 files changed

+367
-2428
lines changed

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

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414

1515
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1616
{
17+
using System;
1718
using System.Linq;
1819
using System.Management.Automation;
1920
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
2021
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkExtensions;
2122
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels;
23+
using ProjectResources = Microsoft.Azure.Commands.ResourceManager.Cmdlets.Properties.Resources;
2224

2325
/// <summary>
2426
/// Get an existing resource.
@@ -94,9 +96,19 @@ public override void ExecuteCmdlet()
9496

9597
private PSResourceProvider[] ListPSResourceProviders()
9698
{
97-
var providers = this.ResourceManagerSdkClient.ListResourceProviders(
98-
providerName: this.ProviderNamespace,
99-
listAvailable: this.ListAvailable);
99+
var allProviders = this.ResourceManagerSdkClient.ListResourceProviders(
100+
providerName: null,
101+
listAvailable: true);
102+
103+
var providers = allProviders;
104+
if (!string.IsNullOrEmpty(this.ProviderNamespace))
105+
{
106+
providers = this.ResourceManagerSdkClient.ListResourceProviders(providerName: this.ProviderNamespace);
107+
}
108+
else if (this.ListAvailable == false)
109+
{
110+
providers = this.ResourceManagerSdkClient.GetRegisteredProviders(providers);
111+
}
100112

101113
if (string.IsNullOrEmpty(this.Location))
102114
{
@@ -105,10 +117,20 @@ private PSResourceProvider[] ListPSResourceProviders()
105117
.ToArray();
106118
}
107119

108-
var validLocations = this.SubscriptionSdkClient.ListLocations(DefaultContext.Subscription.Id.ToString());
120+
var allRPLocations = allProviders
121+
.SelectMany(provider => provider.ResourceTypes.CoalesceEnumerable().SelectMany(type => type.Locations))
122+
.Distinct(StringComparer.InvariantCultureIgnoreCase);
123+
124+
var validLocations = this
125+
.SubscriptionSdkClient
126+
.ListLocations(DefaultContext.Subscription.Id.ToString())
127+
.Select(location => location.DisplayName)
128+
.Concat(allRPLocations)
129+
.Distinct(StringComparer.InvariantCultureIgnoreCase);
109130

110-
if (!validLocations.Any(loc => loc.Name.EqualsAsLocation(this.Location)))
131+
if (!validLocations.Any(loc => loc.EqualsAsLocation(this.Location)))
111132
{
133+
this.WriteErrorWithTimestamp(ProjectResources.InvalidLocation);
112134
return new PSResourceProvider[] { };
113135
}
114136

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/Properties/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,4 +402,7 @@
402402
<data name="CreateDeployment" xml:space="preserve">
403403
<value>Creating Deployment</value>
404404
</data>
405+
<data name="InvalidLocation" xml:space="preserve">
406+
<value>Provided location is not supported</value>
407+
</data>
405408
</root>

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/SdkClient/ResourceManagerSdkClient.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,11 @@ public virtual List<Provider> ListResourceProviders(string providerName = null,
468468
}
469469
}
470470

471+
public List<Provider> GetRegisteredProviders(List<Provider> providers)
472+
{
473+
return providers.CoalesceEnumerable().Where(this.IsProviderRegistered).ToList();
474+
}
475+
471476
private bool IsProviderRegistered(Provider provider)
472477
{
473478
return string.Equals(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ private void ResetCalls()
296296
private void VerifyGetCallPatternAndReset()
297297
{
298298
this.providerOperationsMock.Verify(f => f.GetWithHttpMessagesAsync(It.IsAny<string>(), null, It.IsAny<CancellationToken>()), Times.Once());
299-
this.providerOperationsMock.Verify(f => f.ListWithHttpMessagesAsync(null, null, It.IsAny<CancellationToken>()), Times.Never);
299+
this.providerOperationsMock.Verify(f => f.ListWithHttpMessagesAsync(null, null, It.IsAny<CancellationToken>()), Times.Once());
300300
this.providerOperationsMock.Verify(f => f.ListNextWithHttpMessagesAsync(It.IsAny<string>(), null, It.IsAny<CancellationToken>()), Times.Never);
301301
this.commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny<object>(), It.IsAny<bool>()), Times.Once());
302302
this.ResetCalls();

src/ResourceManager/Resources/Commands.Resources.Test/ScenarioTests/ProviderTests.ps1

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,42 @@
1919
function Test-AzureProvider
2020
{
2121
$defaultProviders = Get-AzureRmResourceProvider
22-
2322
Assert-True { $defaultProviders.Length -gt 0 }
2423

2524
$allProviders = Get-AzureRmResourceProvider -ListAvailable
26-
2725
Assert-True { $allProviders.Length -gt $defaultProviders.Length }
2826

29-
$nonProviders = Get-AzureRmResourceProvider -Location "abc"
27+
$ErrorActionPreference = "SilentlyContinue"
28+
$Error.Clear()
3029

30+
$nonProviders = Get-AzureRmResourceProvider -Location "abc"
31+
Assert-True { $Error[0].Contains("Provided location is not supported") }
3132
Assert-True { $nonProviders.Length -eq 0 }
3233

33-
Register-AzureRmResourceProvider -ProviderName "Microsoft.ApiManagement" -Force
34+
$ErrorActionPreference = "Stop"
35+
36+
$globalProviders = Get-AzureRmResourceProvider -Location "global"
37+
Assert-True { $globalProviders.Length -gt 0 }
38+
39+
Register-AzureRmResourceProvider -ProviderNamespace "Microsoft.ApiManagement" -Force
3440

3541
$endTime = [DateTime]::UtcNow.AddMinutes(5)
3642

37-
while ([DateTime]::UtcNow -lt $endTime -and @(Get-AzureRmResourceProvider -ProviderName "Microsoft.ApiManagement").RegistrationState -ne "Registered")
43+
while ([DateTime]::UtcNow -lt $endTime -and @(Get-AzureRmResourceProvider -ProviderNamespace "Microsoft.ApiManagement")[0].RegistrationState -ne "Registered")
3844
{
3945
[Microsoft.WindowsAzure.Commands.Utilities.Common.TestMockSupport]::Delay(1000)
4046
}
47+
$provider = Get-AzureRmResourceProvider -ProviderNamespace "Microsoft.ApiManagement"
48+
Assert-True { $provider[0].RegistrationState -eq "Registered" }
4149

42-
Assert-True { @(Get-AzureRmResourceProvider -ProviderName "Microsoft.ApiManagement").RegistrationState -eq "Registered" }
50+
Unregister-AzureRmResourceProvider -ProviderNamespace "Microsoft.ApiManagement" -Force
4351

44-
Unregister-AzureRmResourceProvider -ProviderName "Microsoft.ApiManagement" -Force
45-
46-
while ([DateTime]::UtcNow -lt $endTime -and @(Get-AzureRmResourceProvider -ProviderName "Microsoft.ApiManagement").RegistrationState -ne "Unregistered")
52+
while ([DateTime]::UtcNow -lt $endTime -and @(Get-AzureRmResourceProvider -ProviderNamespace "Microsoft.ApiManagement")[0].RegistrationState -ne "Unregistered")
4753
{
4854
[Microsoft.WindowsAzure.Commands.Utilities.Common.TestMockSupport]::Delay(1000)
4955
}
50-
51-
Assert-True { @(Get-AzureRmResourceProvider -ProviderName "Microsoft.ApiManagement").RegistrationState -eq "Unregistered" }
56+
$provider = Get-AzureRmResourceProvider -ProviderNamespace "Microsoft.ApiManagement"
57+
Assert-True { $provider[0].RegistrationState -eq "Unregistered" }
5258
}
5359

5460
<#

src/ResourceManager/Resources/Commands.Resources.Test/SessionRecords/Microsoft.Azure.Commands.Resources.Test.ScenarioTests.ProviderTests/TestAzureProvider.json

Lines changed: 305 additions & 2411 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)