Skip to content

Fixed a bug to continue visting NextPageLink when listing key vaults from ARM API #17618

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

Merged
merged 3 commits into from
Mar 29, 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
3 changes: 2 additions & 1 deletion src/KeyVault/KeyVault/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
- Additional information about change #1
-->
## Upcoming Release

* Fixed a bug to continue visting `NextPageLink` when listing key vaults from ARM API
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bug id

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No bug id, this issue is reported by email


## Version 4.3.0
* `New-AzKeyVaultManagedHsm`: supported specifying how long a deleted managed hsm is retained by `SoftDeleteRetentionInDays` and enabling purge protection by `EnablePurgeProtection`
* `Update-AzKeyVaultManagedHsm`: supported enabling purge protection by `EnablePurgeProtection`
Expand Down
12 changes: 10 additions & 2 deletions src/KeyVault/KeyVault/Commands/GetAzureKeyVault.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using Microsoft.Azure.Commands.KeyVault.Helpers;
using Microsoft.Azure.Commands.KeyVault.Models;
Expand Down Expand Up @@ -103,7 +105,13 @@ public override void ExecuteCmdlet()
switch (ParameterSetName)
{
case GetVaultParameterSet:
ResourceGroupName = string.IsNullOrWhiteSpace(ResourceGroupName) ? GetResourceGroupName(VaultName) : ResourceGroupName;
List<PSKeyVaultIdentityItem> vaults = null;

if (string.IsNullOrWhiteSpace(ResourceGroupName))
{
vaults = ListVaults(ResourceGroupName, Tag);
ResourceGroupName = vaults?.FirstOrDefault(r => r.VaultName.Equals(VaultName, StringComparison.OrdinalIgnoreCase))?.ResourceGroupName;
}

if (ShouldGetByName(ResourceGroupName, VaultName))
{
Expand All @@ -115,7 +123,7 @@ public override void ExecuteCmdlet()
}
else
{
WriteObject(TopLevelWildcardFilter(ResourceGroupName, VaultName, ListVaults(ResourceGroupName, Tag)), true);
WriteObject(TopLevelWildcardFilter(ResourceGroupName, VaultName, vaults ?? ListVaults(ResourceGroupName, Tag)), true);
}

break;
Expand Down
53 changes: 31 additions & 22 deletions src/KeyVault/KeyVault/Models/KeyVaultManagementCmdletBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,12 @@ protected List<PSKeyVaultIdentityItem> ListVaults(string resourceGroupName, Hash
return vaults;
}

IEnumerable<PSKeyVaultIdentityItem> listResult;
var resourceType = resourceTypeName.Equals(ResourceTypeName.Hsm) ?
KeyVaultManagementClient.ManagedHsmResourceType : KeyVaultManagementClient.VaultsResourceType;
if (ShouldListByResourceGroup(resourceGroupName, null))
{
listResult = ListByResourceGroup(resourceGroupName,
new Rest.Azure.OData.ODataQuery<GenericResourceFilter>(
r => r.ResourceType == resourceType));
}
else
{
listResult = ListResources(
new Rest.Azure.OData.ODataQuery<GenericResourceFilter>(
r => r.ResourceType == resourceType));
}

IEnumerable<PSKeyVaultIdentityItem> listResult = ListPagable(resourceGroupName,
new Rest.Azure.OData.ODataQuery<GenericResourceFilter>(
r => r.ResourceType == resourceType));

if (listResult != null)
{
Expand All @@ -161,22 +152,40 @@ protected List<PSKeyVaultIdentityItem> ListVaults(string resourceGroupName, Hash
return vaults;
}

public virtual IEnumerable<PSKeyVaultIdentityItem> ListResources(Rest.Azure.OData.ODataQuery<GenericResourceFilter> filter = null, ulong first = ulong.MaxValue, ulong skip = ulong.MinValue)
IEnumerable<PSKeyVaultIdentityItem> ListPagable(string resourceGroupName, Rest.Azure.OData.ODataQuery<GenericResourceFilter> filter = null)
{
IResourceManagementClient armClient = ResourceClient;
string nextPageLink = null;
var results = new List<PSKeyVaultIdentityItem>();
do
{
var response = ShouldListByResourceGroup(resourceGroupName, null) ?
ListByResourceGroup(resourceGroupName, filter, nextPageLink) :
ListResources(filter, nextPageLink);
results.AddRange(response.Select(r => new PSKeyVaultIdentityItem(r)));
nextPageLink = response?.NextPageLink;
} while (!string.IsNullOrEmpty(nextPageLink));
return results;
}

return new GenericPageEnumerable<GenericResource>(() => armClient.Resources.List(filter), armClient.Resources.ListNext, first, skip).Select(r => new PSKeyVaultIdentityItem(r));
public virtual Rest.Azure.IPage<GenericResource> ListResources(
Rest.Azure.OData.ODataQuery<GenericResourceFilter> filter = null,
string NextPageLink = null)
{
IResourceManagementClient armClient = ResourceClient;
return string.IsNullOrEmpty(NextPageLink) ?
armClient.Resources.List(filter) :
armClient.Resources.ListNext(NextPageLink);
}

private IEnumerable<PSKeyVaultIdentityItem> ListByResourceGroup(
private Rest.Azure.IPage<GenericResource> ListByResourceGroup(
string resourceGroupName,
Rest.Azure.OData.ODataQuery<GenericResourceFilter> filter,
ulong first = ulong.MaxValue,
ulong skip = ulong.MinValue)
Rest.Azure.OData.ODataQuery<GenericResourceFilter> filter = null,
string NextPageLink = null)
{
IResourceManagementClient armClient = ResourceClient;

return new GenericPageEnumerable<GenericResource>(() => armClient.ResourceGroups.ListResources(resourceGroupName, filter), armClient.ResourceGroups.ListResourcesNext, first, skip).Select(r => new PSKeyVaultIdentityItem(r));
return string.IsNullOrEmpty(NextPageLink) ?
armClient.ResourceGroups.ListResources(resourceGroupName, filter) :
armClient.ResourceGroups.ListResourcesNext(NextPageLink);
}

protected string GetResourceGroupName(string name, bool isHsm = false)
Expand Down