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

Fixes for help, #7522 and #5747 #8331

Merged
merged 5 commits into from
Jan 18, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -15,6 +15,7 @@
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
{
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.ErrorResponses;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
using Microsoft.Azure.Commands.ResourceManager.Common;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -110,15 +111,35 @@ private async Task<ResponseWithContinuation<JObject[]>> GetResources()

if (!string.IsNullOrEmpty(ResourceIdUtility.GetResourceName(resourceId)))
{
var resource = await this
.GetResourcesClient()
.GetResource<JObject>(
resourceId: resourceId,
apiVersion: apiVersion,
cancellationToken: this.CancellationToken.Value)
.ConfigureAwait(continueOnCapturedContext: false);
ResponseWithContinuation<JObject[]> retVal;
return resource.TryConvertTo(out retVal) && retVal.Value != null
JObject resource;
try
{
resource = await this
.GetResourcesClient()
.GetResource<JObject>(
resourceId: resourceId,
apiVersion: apiVersion,
cancellationToken: this.CancellationToken.Value)
.ConfigureAwait(continueOnCapturedContext: false);
}
catch (ErrorResponseMessageException ex)
{
if (!ex.Message.StartsWith("PolicyDefinitionNotFound", StringComparison.OrdinalIgnoreCase))
{
throw;
}

resourceId = this.GetBuiltinResourceId();
resource = await this
.GetResourcesClient()
.GetResource<JObject>(
resourceId: resourceId,
apiVersion: apiVersion,
cancellationToken: this.CancellationToken.Value)
.ConfigureAwait(continueOnCapturedContext: false);
}

return resource.TryConvertTo(out ResponseWithContinuation<JObject[]> retVal) && retVal.Value != null
? retVal
: new ResponseWithContinuation<JObject[]> { Value = resource.AsArray() };
}
Expand All @@ -141,5 +162,13 @@ private string GetResourceId()
{
return this.Id ?? this.MakePolicyDefinitionId(this.ManagementGroupName, this.SubscriptionId, this.Name);
}

/// <summary>
/// Gets the resource Id assuming the name is for a builtin
/// </summary>
private string GetBuiltinResourceId()
{
return $"/{Constants.Providers}/{Constants.MicrosoftAuthorizationPolicyDefinitionType}/{this.Name}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
{
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.ErrorResponses;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
using Microsoft.Azure.Commands.ResourceManager.Common;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -110,15 +111,35 @@ private async Task<ResponseWithContinuation<JObject[]>> GetResources()

if (!string.IsNullOrEmpty(ResourceIdUtility.GetResourceName(resourceId)))
{
var resource = await this
.GetResourcesClient()
.GetResource<JObject>(
resourceId: resourceId,
apiVersion: apiVersion,
cancellationToken: this.CancellationToken.Value)
.ConfigureAwait(continueOnCapturedContext: false);
ResponseWithContinuation<JObject[]> retVal;
return resource.TryConvertTo(out retVal) && retVal.Value != null
JObject resource;
try
{
resource = await this
.GetResourcesClient()
.GetResource<JObject>(
resourceId: resourceId,
apiVersion: apiVersion,
cancellationToken: this.CancellationToken.Value)
.ConfigureAwait(continueOnCapturedContext: false);
}
catch (ErrorResponseMessageException ex)
{
if (!ex.Message.StartsWith("PolicySetDefinitionNotFound", StringComparison.OrdinalIgnoreCase))
{
throw;
}

resourceId = this.GetBuiltinResourceId();
resource = await this
.GetResourcesClient()
.GetResource<JObject>(
resourceId: resourceId,
apiVersion: apiVersion,
cancellationToken: this.CancellationToken.Value)
.ConfigureAwait(continueOnCapturedContext: false);
}

return resource.TryConvertTo(out ResponseWithContinuation<JObject[]> retVal) && retVal.Value != null
? retVal
: new ResponseWithContinuation<JObject[]> { Value = resource.AsArray() };
}
Expand All @@ -141,5 +162,13 @@ private string GetResourceId()
{
return this.Id ?? this.MakePolicySetDefinitionId(this.ManagementGroupName, this.SubscriptionId, this.Name);
}

/// <summary>
/// Gets the resource Id assuming the name is for a builtin
/// </summary>
private string GetBuiltinResourceId()
{
return $"/{Constants.Providers}/{Constants.MicrosoftAuthorizationPolicySetDefinitionType}/{this.Name}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected PSObject[] GetFilteredOutputObjects(string resourceType, ListFilter fi
return true;
}

var policyType = result.GetPSObjectProperty("Properties.policyType");
var policyType = ((PSObject)result.Properties["Properties"].Value).Properties["policyType"].Value;
return policyType == null || string.Equals(policyType.ToString(), filter.ToString(), StringComparison.OrdinalIgnoreCase);
};

Expand Down
14 changes: 14 additions & 0 deletions src/Resources/Resources.Test/ScenarioTests/PolicyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,20 @@ public void TestPolicyDefinitionWithUri()
TestRunner.RunTestScript("Test-PolicyDefinitionWithUri");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestGetCmdletFilterParameter()
{
TestRunner.RunTestScript("Test-GetCmdletFilterParameter");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestGetBuiltinsByName()
{
TestRunner.RunTestScript("Test-GetBuiltinsByName");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestGetPolicyAssignmentParameters()
Expand Down
52 changes: 44 additions & 8 deletions src/Resources/Resources.Test/ScenarioTests/PolicyTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -658,9 +658,6 @@ function Test-PolicyDefinitionCRUDAtManagementGroup
Assert-NotNull($actual.Properties.PolicyRule)
Assert-AreEqual $expected.Properties.Mode $actual.Properties.Mode

# make sure it can't be retrieved at default subscription level
Assert-ThrowsContains { Get-AzureRMPolicyDefinition -Name $policyName } "PolicyDefinitionNotFound : The policy definition '$policyName' could not be found."

# update the same policy definition, get it back and validate the new properties
$actual = Set-AzureRMPolicyDefinition -Name $policyName -ManagementGroupName $managementGroup -DisplayName testDisplay -Description $updatedDescription -Policy ".\SamplePolicyDefinition.json" -Metadata $metadata
$expected = Get-AzureRMPolicyDefinition -Name $policyName -ManagementGroupName $managementGroup
Expand Down Expand Up @@ -741,9 +738,6 @@ function Test-PolicySetDefinitionCRUDAtManagementGroup
Assert-AreEqual $expected.PolicySetDefinitionId $actual.PolicySetDefinitionId
Assert-NotNull($actual.Properties.PolicyDefinitions)

# make sure it can't be retrieved at default subscription level
Assert-ThrowsContains { Get-AzureRMPolicySetDefinition -Name $policySetDefName } "PolicySetDefinitionNotFound : The policy set definition '$policySetDefName' could not be found."

# update the policy set definition, get it back and validate
$expected = Set-AzureRMPolicySetDefinition -Name $policySetDefName -ManagementGroupName $managementGroup -DisplayName testDisplay -Description $updatedDescription
$actual = Get-AzureRMPolicySetDefinition -Name $policySetDefName -ManagementGroupName $managementGroup
Expand Down Expand Up @@ -808,6 +802,48 @@ function Test-PolicySetDefinitionCRUDAtSubscription
Assert-AreEqual True $remove
}

function Test-GetCmdletFilterParameter
{
# policy definitions
$builtins = Get-AzureRmPolicyDefinition -Builtin
$builtins | %{ Assert-AreEqual $_.Properties.PolicyType "Builtin" }

$custom = Get-AzureRmPolicyDefinition -Custom
$custom | %{ Assert-AreEqual $_.Properties.PolicyType "Custom" }

$all = Get-AzureRmPolicyDefinition
Assert-AreEqual ($builtins.Count + $custom.Count) $all.Count

# policy set definitions
$builtins = Get-AzureRmPolicySetDefinition -Builtin
$builtins | %{ Assert-AreEqual $_.Properties.PolicyType "Builtin" }

$custom = Get-AzureRmPolicySetDefinition -Custom
$custom | %{ Assert-AreEqual $_.Properties.PolicyType "Custom" }

$all = Get-AzureRmPolicySetDefinition
Assert-AreEqual ($builtins.Count + $custom.Count) $all.Count
}

function Test-GetBuiltinsByName
{
# policy definitions
$builtins = Get-AzureRmPolicyDefinition -Builtin
foreach ($builtin in $builtins)
{
$definition = Get-AzureRmPolicyDefinition -Name $builtin.Name
Assert-AreEqual $builtin.ResourceId $definition.ResourceId
}

# policy set definitions
$builtins = Get-AzureRmPolicySetDefinition -Builtin
foreach ($builtin in $builtins)
{
$setDefinition = Get-AzureRmPolicySetDefinition -Name $builtin.Name
Assert-AreEqual $builtin.ResourceId $setDefinition.ResourceId
}
}

<#
The following section contains tests for each cmdlet that validate as many combinations of
parameters as possible/reasonable. Tests for all combinations of parameters are present here
Expand Down Expand Up @@ -1015,7 +1051,7 @@ function Test-GetPolicyDefinitionParameters
Assert-ThrowsContains { Get-AzureRmPolicyDefinition -Name $someName -Id $someId -Custom } $parameterSetError

# validate remaining parameter combinations starting with -Id
Assert-ThrowsContains { Get-AzureRmPolicyDefinition -Id $goodId } $policyDefinitionNotFound
$ok = Get-AzureRmPolicyDefinition -Id $goodId
Assert-ThrowsContains { Get-AzureRmPolicyDefinition -Id $goodId -ManagementGroupName $someManagementGroup } $parameterSetError
Assert-ThrowsContains { Get-AzureRmPolicyDefinition -Id $goodId -SubscriptionId $subscriptionId } $parameterSetError
Assert-ThrowsContains { Get-AzureRmPolicyDefinition -Id $goodId -BuiltIn } $parameterSetError
Expand Down Expand Up @@ -1159,7 +1195,7 @@ function Test-GetPolicySetDefinitionParameters
Assert-ThrowsContains { Get-AzureRmPolicySetDefinition -Name $someName -Id $someId -Custom } $parameterSetError

# validate remaining parameter combinations starting with -Id
Assert-ThrowsContains { Get-AzureRmPolicySetDefinition -Id $goodId } $policySetDefinitionNotFound
$ok = Get-AzureRmPolicySetDefinition -Id $goodId
Assert-ThrowsContains { Get-AzureRmPolicySetDefinition -Id $goodId -ManagementGroupName $someManagementGroup } $parameterSetError
Assert-ThrowsContains { Get-AzureRmPolicySetDefinition -Id $goodId -SubscriptionId $subscriptionId } $parameterSetError
Assert-ThrowsContains { Get-AzureRmPolicySetDefinition -Id $goodId -BuiltIn } $parameterSetError
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Loading