-
Notifications
You must be signed in to change notification settings - Fork 4k
Handle invalid locations for Get-AzureRmResourceProvider. #2380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,9 +14,11 @@ | |
|
||
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation | ||
{ | ||
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels; | ||
using System.Linq; | ||
using System.Management.Automation; | ||
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions; | ||
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkExtensions; | ||
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels; | ||
|
||
/// <summary> | ||
/// Get an existing resource. | ||
|
@@ -59,7 +61,7 @@ public class GetAzureProviderCmdlet : ResourceManagerCmdletBase | |
/// </summary> | ||
public override void ExecuteCmdlet() | ||
{ | ||
var providers = this.ResourceManagerSdkClient.ListPSResourceProviders(providerName: this.ProviderNamespace, listAvailable: this.ListAvailable, location: this.Location); | ||
var providers = this.ListPSResourceProviders(); | ||
|
||
if (!string.IsNullOrEmpty(this.ProviderNamespace)) | ||
{ | ||
|
@@ -89,5 +91,38 @@ public override void ExecuteCmdlet() | |
this.WriteObject(providers, enumerateCollection: true); | ||
} | ||
} | ||
|
||
private PSResourceProvider[] ListPSResourceProviders() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assumed PS stands for PowerShell, its good to call it ListPowerShellResourceProviders There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add summary There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The PowerShell practice is in preference to PS abbreviation. For example, the base class PSObject, PSCustomObject. Here I think we can follow the practice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The PowerShell practice is in preference to PS abbreviation. For example, the base class PSObject, PSCustomObject. Here I think we can follow the practice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
var providers = this.ResourceManagerSdkClient.ListResourceProviders( | ||
providerName: this.ProviderNamespace, | ||
listAvailable: this.ListAvailable); | ||
|
||
if (string.IsNullOrEmpty(this.Location)) | ||
{ | ||
return providers | ||
.Select(provider => provider.ToPSResourceProvider()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure this method is introduced in this CR, but good to spell it out here too There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as the other one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as the other one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
.ToArray(); | ||
} | ||
|
||
var validLocations = this.SubscriptionSdkClient.ListLocations(DefaultContext.Subscription.Id.ToString()); | ||
|
||
if (!validLocations.Any(loc => loc.Name.EqualsAsLocation(this.Location))) | ||
{ | ||
return new PSResourceProvider[] { }; | ||
} | ||
|
||
foreach (var provider in providers) | ||
{ | ||
provider.ResourceTypes = provider.ResourceTypes | ||
.Where(type => !type.Locations.Any() || type.Locations.Any(loc => loc.EqualsAsLocation(this.Location))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check type.Locations not null before type.Locations.Any() There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will fix this. |
||
.ToList(); | ||
} | ||
|
||
return providers | ||
.Where(provider => provider.ResourceTypes.Any()) | ||
.Select(provider => provider.ToPSResourceProvider()) | ||
.ToArray(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,12 +52,6 @@ public class MoveAzureResourceCommand : ResourceManagerCmdletBase | |
[Alias("Id", "SubscriptionId")] | ||
public Guid? DestinationSubscriptionId { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets a value that indicates if the user should be prompted for confirmation. | ||
/// </summary> | ||
[Parameter(Mandatory = false, HelpMessage = "Do not ask for confirmation.")] | ||
public SwitchParameter Force { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the ids of the resources to move. | ||
/// </summary> | ||
|
@@ -66,6 +60,12 @@ public class MoveAzureResourceCommand : ResourceManagerCmdletBase | |
[ValidateNotNullOrEmpty] | ||
public string[] ResourceId { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets a value that indicates if the user should be prompted for confirmation. | ||
/// </summary> | ||
[Parameter(Mandatory = false, HelpMessage = "Do not ask for confirmation.")] | ||
public SwitchParameter Force { get; set; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nothings changed, why changing the location in the file? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The order of the parameters were changed. This was to fix an auto-populating issue on parameters. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SG |
||
|
||
/// <summary> | ||
/// Collects subscription ids from the pipeline. | ||
/// </summary> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,31 +49,40 @@ public class GetAzureProviderCmdletTests : RMTestBase | |
private MockCommandRuntime mockRuntime; | ||
|
||
/// <summary> | ||
/// A mock of the client | ||
/// A mock of the IProvidersOperations | ||
/// </summary> | ||
private readonly Mock<IProvidersOperations> providerOperationsMock; | ||
|
||
/// <summary> | ||
/// A mock of the ISubscriptionsOperations | ||
/// </summary> | ||
private readonly Mock<ISubscriptionsOperations> subscriptionsOperationsMock; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="GetAzureProviderCmdletTests"/> class. | ||
/// </summary> | ||
public GetAzureProviderCmdletTests(ITestOutputHelper output) | ||
{ | ||
this.providerOperationsMock = new Mock<IProvidersOperations>(); | ||
this.subscriptionsOperationsMock = new Mock<ISubscriptionsOperations>(); | ||
XunitTracingInterceptor.AddToContext(new XunitTracingInterceptor(output)); | ||
var resourceManagementClient = new Mock<Microsoft.Azure.Management.ResourceManager.IResourceManagementClient>(); | ||
var subscriptionClient = new Mock<Microsoft.Azure.Management.ResourceManager.ISubscriptionClient>(); | ||
|
||
resourceManagementClient | ||
.SetupGet(client => client.Providers) | ||
.Returns(() => this.providerOperationsMock.Object); | ||
|
||
subscriptionClient | ||
.SetupGet(client => client.Subscriptions) | ||
.Returns(() => this.subscriptionsOperationsMock.Object); | ||
|
||
this.commandRuntimeMock = new Mock<ICommandRuntime>(); | ||
this.cmdlet = new GetAzureProviderCmdletTest | ||
{ | ||
//CommandRuntime = commandRuntimeMock.Object, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove comment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will fix it. |
||
ResourceManagerSdkClient = new ResourceManagerSdkClient | ||
{ | ||
ResourceManagementClient = resourceManagementClient.Object, | ||
} | ||
ResourceManagerSdkClient = new ResourceManagerSdkClient(resourceManagementClient.Object), | ||
SubscriptionSdkClient = new SubscriptionSdkClient(subscriptionClient.Object) | ||
}; | ||
PSCmdletExtensions.SetCommandRuntimeMock(cmdlet, commandRuntimeMock.Object); | ||
mockRuntime = new MockCommandRuntime(); | ||
|
@@ -134,6 +143,25 @@ public void GetsResourceProviderTests() | |
.Setup(f => f.ListWithHttpMessagesAsync(null, null, It.IsAny<CancellationToken>())) | ||
.Returns(() => Task.FromResult(result)); | ||
|
||
var locationList = new List<Location> | ||
{ | ||
new Location | ||
{ | ||
Name = "southus", | ||
DisplayName = "South US", | ||
} | ||
}; | ||
var pagableLocations = new Page<Location>(); | ||
pagableLocations.SetItemValue<Location>(locationList); | ||
var locationsResult = new AzureOperationResponse<IPage<Location>>() | ||
{ | ||
Body = pagableLocations | ||
}; | ||
this.subscriptionsOperationsMock | ||
.Setup(f => f.ListLocationsWithHttpMessagesAsync(It.IsAny<string>(), null, It.IsAny<CancellationToken>())) | ||
.Returns(() => Task.FromResult(locationsResult)); | ||
|
||
|
||
// 1. List only registered providers | ||
this.commandRuntimeMock | ||
.Setup(m => m.WriteObject(It.IsAny<object>())) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to provide an explanation, as we discussed offline
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will add more details in description.