Skip to content

Fix the bug of Get-AzResource with api-version #10712

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 1 commit into from
Dec 18, 2019
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
20 changes: 18 additions & 2 deletions src/Resources/ResourceManager/Components/ApiVersionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
// TODO: Remove IfDef
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Utilities;
// TODO: Remove IfDef
#if NETSTANDARD
using Microsoft.Extensions.Caching.Memory;
#else
Expand Down Expand Up @@ -127,13 +128,28 @@ private static string[] GetApiVersionsForResourceType(IAzureContext context, str
cancellationToken: cancellationToken),
cancellationToken: cancellationToken);

return providers
string[] apiVersions = providers
.CoalesceEnumerable()
.Where(provider => providerNamespace.EqualsInsensitively(provider.Namespace))
.SelectMany(provider => provider.ResourceTypes)
.Where(type => resourceType.EqualsInsensitively(type.ResourceType))
.Select(type => type.ApiVersions)
.FirstOrDefault();
if (apiVersions == null)
{
string topLevelResourceType = ResourceTypeUtility.GetTopLevelResourceType(resourceType);
return providers
.CoalesceEnumerable()
.Where(provider => providerNamespace.EqualsInsensitively(provider.Namespace))
.SelectMany(provider => provider.ResourceTypes)
.Where(type => topLevelResourceType.EqualsInsensitively(type.ResourceType))
.Select(type => type.ApiVersions)
.FirstOrDefault();
}
else
{
return apiVersions;
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
using Microsoft.Azure.Commands.ResourceManager.Common.Paging;
using System.Management.Automation;
using Microsoft.Rest;
using System.Runtime.InteropServices.ComTypes;

namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkClient
{
Expand Down Expand Up @@ -1168,6 +1169,13 @@ public virtual PSResource GetById(string resourceId, string apiVersion)
var resourceType = provider.ResourceTypes
.Where(t => string.Equals(string.Format("{0}/{1}", provider.NamespaceProperty, t.ResourceType), resourceIdentifier.ResourceType, StringComparison.OrdinalIgnoreCase))
.FirstOrDefault();
if (resourceType == null)
{
string topLevelResourceType = ResourceTypeUtility.GetTopLevelResourceTypeWithProvider(resourceIdentifier.ResourceType);
resourceType = provider.ResourceTypes
.Where(t => string.Equals(t.ResourceType, topLevelResourceType, StringComparison.OrdinalIgnoreCase))
.FirstOrDefault();
}
if (resourceType != null)
{
apiVersion = resourceType.ApiVersions.Contains(apiVersion) ? apiVersion : resourceType.ApiVersions.FirstOrDefault();
Expand Down
38 changes: 38 additions & 0 deletions src/Resources/ResourceManager/Utilities/ResourceTypeUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Utilities
{
public static class ResourceTypeUtility
{
public static string GetTopLevelResourceType(string resourceType)
{
if (string.IsNullOrEmpty(resourceType))
{
return null;
}
string[] resourceTypeFieldList = resourceType.Split('/');
// resourceType is like {topLevelResource}/[{subLevelResource}]
return resourceTypeFieldList[0];
}

public static string GetTopLevelResourceTypeWithProvider(string resourceType)
{
if (string.IsNullOrEmpty(resourceType))
{
return null;
}
string[] resourceTypeFieldList = resourceType.Split('/');
// resourceType is like {provider}/{topLevelResource}/[{subLevelResource}]
if (resourceTypeFieldList.Length >= 1)
{
return resourceTypeFieldList[1];
}
else
{
return resourceType;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Text;
using Xunit;
using Xunit.Abstractions;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Utilities;
using Microsoft.WindowsAzure.Commands.ScenarioTest;

namespace Microsoft.Azure.Commands.Resources.Test.UnitTests.Utilities
{
public class TestResourceTypeUtility
{
[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestInputIsNull()
{
string resourceType = ResourceTypeUtility.GetTopLevelResourceType(null);

Assert.Null(resourceType);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestInputTopLevelResourceTypeSingle()
{
string resourceType = ResourceTypeUtility.GetTopLevelResourceType("virtualMachines");

Assert.Equal("virtualMachines", resourceType);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestInputTopLevelResourceTypeAndSubResourceType()
{
string resourceType = ResourceTypeUtility.GetTopLevelResourceType("virtualMachines/networking");

Assert.Equal("virtualMachines", resourceType);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestInputProviderNull()
{
string resourceType = ResourceTypeUtility.GetTopLevelResourceTypeWithProvider(null);

Assert.Null(resourceType);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestInputProviderAndTopLevelResourceType()
{
string resourceType = ResourceTypeUtility.GetTopLevelResourceTypeWithProvider("Microsoft.Compute/virtualMachines");

Assert.Equal("virtualMachines", resourceType);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestInputProviderAndTopLevelResourceTypeAndSubLevelResourceType()
{
string resourceType = ResourceTypeUtility.GetTopLevelResourceTypeWithProvider("Microsoft.Compute/virtualMachines/networking");

Assert.Equal("virtualMachines", resourceType);
}
}
}
1 change: 1 addition & 0 deletions src/Resources/Resources/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* Update references in .psd1 to use relative path
* Fix an issue where template deployment fails to read a template parameter if its name conflicts with some built-in parameter name.
* Updated policy cmdlets to use new api version 2019-09-01 that introduces grouping support within policy set definitions.
* Fix the bug that the output of some sub-resource is empty when using `Get-AzResource`.

## Version 1.8.0
- Updated policy cmdlets to use new api version 2019-06-01 that has new EnforcementMode property in policy assignment.
Expand Down