Azure / azure-powershell

Microsoft Azure PowerShell
Other
4.21k stars 3.81k forks source link

[Feature]: Get-AzSubscription with SubscriptionId parameter should internally only get a single subscription instead of listing all subs then searching #19115

Closed kwill-MSFT closed 1 year ago

kwill-MSFT commented 2 years ago

Description of the new feature

When calling Get-AzSubscription using the SubscriptionId parameter: Get-AzSubscription -SubscriptionId "123582cb-b98a-4200-a7a5-edee9045a123"

Internally, this executes (GetAzureRMSubscription.cs): this._client.TryGetSubscriptionById(TenantId, this.SubscriptionId, out result)

TryGetSubscriptionById then executes (RMProfileClient.cs) var subscriptionList = ListSubscriptions(tenantId).Where(s => s.GetId() == subscriptionIdGuid);

This has the effect of listing all subscriptions that the caller has access to, then finding the matching SubscriptionId. Given that a SubscriptionId is unique, there is no need to list them all then find the first matching one.

For a scenario where the caller has access to thousands of subscriptions, this consumes a lot of unnecessary time and resources since the code internally has to iterate over several nextLink results and call the /subscriptions API multiple times.

Proposed implementation details (optional)

TryGetSubscriptionById should instead utilize TryGetTenantSubscription, which does a single GET on a specific subscription.

This will significantly reduce the resource load on both the client and on ARM since there will be much less data returned and fewer API calls being made.

dingmeng-xue commented 2 years ago

Thanks for reporting. Target to October. Let me set milestone after internal discussion.

msJinLei commented 1 year ago

Resolved in Az.Accounts 2.10.3