Skip to content
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

Upgrade Get-AzLocation cmdlet #18161

Merged
merged 8 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
[Cmdlet(VerbsCommon.Get, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "Location"), OutputType(typeof(PSResourceProviderLocation))]
public class GetAzureLocationCmdlet : ResourceManagerCmdletBaseWithApiVersion
{
public const string ExtendedLocationsParameterSet = "ExtendedLocation";

[Parameter(Mandatory = false, HelpMessage = "Whether to include extended locations.", ParameterSetName = GetAzureLocationCmdlet.ExtendedLocationsParameterSet)]
public bool? ExtendedLocation { get; set; }
/// <summary>
/// Executes the cmdlet
/// </summary>
Expand All @@ -39,14 +43,15 @@ protected override void OnProcessRecord()
{
throw new PSInvalidOperationException(Resources.NoSubscriptionsUnderCurrentDirectory);
}
var allLocations = this.SubscriptionSdkClient.ListLocations(DefaultContext.Subscription.Id.ToString());
var allLocations = this.SubscriptionSdkClient.ListLocations(DefaultContext.Subscription.Id.ToString(), ExtendedLocation);
var providers = this.ResourceManagerSdkClient.ListResourceProviders(providerName: null, listAvailable: true);
var providerLocations = ConstructResourceProviderLocations(allLocations, providers);

this.WriteObject(providerLocations, enumerateCollection: true);
}

private List<PSResourceProviderLocation> ConstructResourceProviderLocations(List<Internal.Subscriptions.Models.Location> locations, List<Provider> providers)


private List<PSResourceProviderLocation> ConstructResourceProviderLocations(List<Management.ResourceManager.Models.Location> locations, List<Provider> providers)
{
var locationProviderMap = GetLocationProviderMap(providers);

Expand All @@ -61,7 +66,15 @@ private List<PSResourceProviderLocation> ConstructResourceProviderLocations(List
{
Location = location.Name,
DisplayName = location.DisplayName,
Providers = mapEntry.Value
Longitude = location.Metadata.Longitude,
Latitude = location.Metadata.Latitude,
PhysicalLocation = location.Metadata.PhysicalLocation,
Providers = mapEntry.Value,
PairedRegion = location.Metadata.PairedRegion,
RegionType = location.Metadata.RegionType,
RegionCategory = location.Metadata.RegionCategory,
GeographyGroup = location.Metadata.GeographyGroup,
Type = location.Type
},
StringComparer.InvariantCultureIgnoreCase);

Expand Down
41 changes: 41 additions & 0 deletions src/Resources/ResourceManager/ResourceManager.format.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,47 @@
<Label>DisplayName</Label>
<PropertyName>DisplayName</PropertyName>
</ListItem>
<ListItem>
<Label>Type</Label>
<PropertyName>Type</PropertyName>
</ListItem>
<ListItem>
<Label>Longitude</Label>
<PropertyName>Longitude</PropertyName>
</ListItem>
<ListItem>
<Label>Latitude</Label>
<PropertyName>Latitude</PropertyName>
</ListItem>
<ListItem>
<Label>PhysicalLocation</Label>
<PropertyName>PhysicalLocation</PropertyName>
</ListItem>
<ListItem>
<Label>RegionType</Label>
<PropertyName>RegionType</PropertyName>
</ListItem>
<ListItem>
<Label>RegionCategory</Label>
<PropertyName>RegionCategory</PropertyName>
</ListItem>
<ListItem>
<Label>GeographyGroup</Label>
<PropertyName>GeographyGroup</PropertyName>
</ListItem>
<ListItem>
<Label>PairedRegion</Label>
<ScriptBlock>
if ($_.PairedRegion -ne $null)
{
ConvertTo-Json $_.PairedRegion -Depth 10
}
else
{
$_.PairedRegion;
}
</ScriptBlock>
</ListItem>
<ListItem>
<Label>Providers</Label>
<PropertyName>Providers</PropertyName>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@

using Microsoft.Azure.Commands.Common.Authentication;
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
using Microsoft.Azure.Commands.Common.Authentication.Models;
using Microsoft.Azure.Internal.Subscriptions;
using Microsoft.Azure.Internal.Subscriptions.Models;
using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Azure.Management.ResourceManager.Models;
using System;
using System.Collections.Generic;

Expand Down Expand Up @@ -52,11 +51,11 @@ public SubscriptionSdkClient(ISubscriptionClient subscriptionClient)
this.SubscriptionClient = subscriptionClient;
}

public List<Location> ListLocations(string subscriptionId)
public List<Location> ListLocations(string subscriptionId, bool? includeExtendedLocations = null)
{
var locationList = new List<Location>();

var tempResult = this.SubscriptionClient.Subscriptions.ListLocations(subscriptionId);
var tempResult = this.SubscriptionClient.Subscriptions.ListLocations(subscriptionId, includeExtendedLocations);
locationList.AddRange(tempResult);

return locationList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ----------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -13,6 +13,7 @@
// ----------------------------------------------------------------------------------

using System.Collections.Generic;
using Microsoft.Azure.Management.ResourceManager.Models;

namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels
{
Expand All @@ -31,6 +32,23 @@ public class PSResourceProviderLocation
/// </summary>
public string DisplayName { get; set; }

public LocationType? Type { get;set; }


public string Longitude { get;set; }

public string Latitude { get;set; }

public string PhysicalLocation { get;set; }

public string RegionType { get;set; }

public string RegionCategory { get;set; }

public string GeographyGroup { get;set; }

public IList<PairedRegion> PairedRegion { get; set; }

/// <summary>
/// Gets or sets the providers that are supported in this location
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,18 @@ public class GetAzureProviderCmdletTests : RMTestBase
/// <summary>
/// A mock of the ISubscriptionsOperations
/// </summary>
private readonly Mock<Internal.Subscriptions.ISubscriptionsOperations> subscriptionsOperationsMock;
private readonly Mock<Management.ResourceManager.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<Internal.Subscriptions.ISubscriptionsOperations>();
this.subscriptionsOperationsMock = new Mock<Management.ResourceManager.ISubscriptionsOperations>();
XunitTracingInterceptor.AddToContext(new XunitTracingInterceptor(output));
var resourceManagementClient = new Mock<Microsoft.Azure.Management.ResourceManager.IResourceManagementClient>();
var subscriptionClient = new Mock<Internal.Subscriptions.ISubscriptionClient>();
var subscriptionClient = new Mock<Management.ResourceManager.ISubscriptionClient>();

resourceManagementClient
.SetupGet(client => client.Providers)
Expand Down Expand Up @@ -139,18 +139,18 @@ public void GetsResourceProviderTests()
.Setup(f => f.ListWithHttpMessagesAsync(null, null, null, It.IsAny<CancellationToken>()))
.Returns(() => Task.FromResult(result));

var locationList = new List<Internal.Subscriptions.Models.Location>
var locationList = new List<Management.ResourceManager.Models.Location>
{
new Internal.Subscriptions.Models.Location(name: "southus", displayName: "South US")
new Management.ResourceManager.Models.Location(name: "southus", displayName: "South US")
};
var pagableLocations = new Page<Internal.Subscriptions.Models.Location>();
pagableLocations.SetItemValue<Internal.Subscriptions.Models.Location>(locationList);
var locationsResult = new AzureOperationResponse<IEnumerable<Internal.Subscriptions.Models.Location>>()
var pagableLocations = new Page<Management.ResourceManager.Models.Location>();
pagableLocations.SetItemValue<Management.ResourceManager.Models.Location>(locationList);
var locationsResult = new AzureOperationResponse<IEnumerable<Management.ResourceManager.Models.Location>>()
{
Body = pagableLocations
};
this.subscriptionsOperationsMock
.Setup(f => f.ListLocationsWithHttpMessagesAsync(It.IsAny<string>(), null, It.IsAny<CancellationToken>()))
.Setup(f => f.ListLocationsWithHttpMessagesAsync(It.IsAny<string>(), null, null, It.IsAny<CancellationToken>()))
.Returns(() => Task.FromResult(locationsResult));


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#>
function Test-AzureLocation
{
$providerLocations = Get-AzLocation
$providerLocations = Get-AzLocation -ExtendedLocation $true

Assert-True { $providerLocations.Count -gt 0 }
foreach ($location in $providerLocations)
Expand Down

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/Resources/Resources/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
-->

## Upcoming Release
* Upgraded and rivised `Get-AzLocation` cmdlet:
- Upgraded `subscriptionClient` for `Get-AzLocation`. Changed its apiVersion from 2016-01-01 to 2021-01-01.[#18002]
- Added all attributes of location info for `Get-AzLocation`, including `pairedRegion` and so on. [#18045][#17536]
- Support ExtendedLocations by `Get-AzLocation` [#18046]
* Added the following cmdlets to remain in parity with 2021-04-01 API version:
- `New-AzHierarchySetting`
- `Get-AzHierarchySetting`
Expand Down