Skip to content

Commit c8cbc41

Browse files
authored
Merge pull request Azure#10421 from sandipsh/Policy-Api-Version-2019-06-01
Added Policy API-version 2019-06-01. Policy assignment cmdlet updated to accept EnforcementMode property
2 parents d4225bf + df1af07 commit c8cbc41

File tree

39 files changed

+31555
-7684
lines changed

39 files changed

+31555
-7684
lines changed

src/Resources/ResourceManager/Components/Constants.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,17 @@ public static class Constants
7777
/// <summary>
7878
/// The default policy definition API version.
7979
/// </summary>
80-
public static readonly string PolicyDefinitionApiVersion = "2019-01-01";
80+
public static readonly string PolicyDefinitionApiVersion = "2019-06-01";
8181

8282
/// <summary>
8383
/// The default policy set definition API version.
8484
/// </summary>
85-
public static readonly string PolicySetDefintionApiVersion = "2019-01-01";
85+
public static readonly string PolicySetDefintionApiVersion = "2019-06-01";
8686

8787
/// <summary>
8888
/// The default policy assignment API version.
8989
/// </summary>
90-
public static readonly string PolicyAssignmentApiVersion = "2019-01-01";
90+
public static readonly string PolicyAssignmentApiVersion = "2019-06-01";
9191

9292
/// <summary>
9393
/// The default providers API version.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Policy
16+
{
17+
/// <summary>
18+
/// The policy assignment enforcement mode.
19+
/// </summary>
20+
public enum PolicyAssignmentEnforcementMode
21+
{
22+
/// <summary>
23+
/// The policy effect is enforced during resource creation or update.
24+
/// </summary>
25+
Default,
26+
27+
/// <summary>
28+
/// The policy effect is not enforced during resource creation or update.
29+
/// </summary>
30+
DoNotEnforce
31+
}
32+
}

src/Resources/ResourceManager/Entities/Policy/PolicyAssignmentProperties.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ public class PolicyAssignmentProperties
5252
[JsonProperty(Required = Required.Default)]
5353
public JObject Metadata { get; set; }
5454

55+
/// <summary>
56+
/// The policy assignment enforcement mode.
57+
/// </summary>
58+
[JsonProperty(Required = Required.Default)]
59+
public PolicyAssignmentEnforcementMode? EnforcementMode { get; set; }
60+
5561
/// <summary>
5662
/// The policy definition id.
5763
/// </summary>

src/Resources/ResourceManager/Implementation/Policy/NewAzurePolicyAssignment.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@
1414

1515
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1616
{
17-
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
18-
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Policy;
19-
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
20-
using Newtonsoft.Json.Linq;
21-
using Policy;
2217
using System;
2318
using System.Collections;
2419
using System.Management.Automation;
25-
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
20+
2621
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
22+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
23+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Policy;
24+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
25+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
26+
using Newtonsoft.Json.Linq;
27+
using Policy;
2728

2829
/// <summary>
2930
/// Creates a policy assignment.
@@ -108,6 +109,13 @@ public class NewAzurePolicyAssignmentCmdlet : PolicyCmdletBase, IDynamicParamete
108109
[ValidateNotNullOrEmpty]
109110
public string Metadata { get; set; }
110111

112+
/// <summary>
113+
/// Gets or sets the policy assignment enforcement mode.
114+
/// </summary>
115+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.NewPolicyAssignmentEnforcementModeHelp)]
116+
[ValidateNotNullOrEmpty]
117+
public PolicyAssignmentEnforcementMode? EnforcementMode { get; set; }
118+
111119
/// <summary>
112120
/// Gets or sets a flag indicating whether a system assigned identity should be added to the policy assignment.
113121
/// </summary>
@@ -193,6 +201,7 @@ private JToken GetResource()
193201
Scope = this.Scope,
194202
NotScopes = this.NotScope ?? null,
195203
Metadata = this.Metadata == null ? null : JObject.Parse(this.GetObjectFromParameter(this.Metadata).ToString()),
204+
EnforcementMode = EnforcementMode ?? PolicyAssignmentEnforcementMode.Default,
196205
Parameters = this.GetParameters(this.PolicyParameter, this.PolicyParameterObject)
197206
}
198207
};

src/Resources/ResourceManager/Implementation/Policy/PolicyHelpStrings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public static class PolicyHelpStrings
3535
public const string NewPolicyAssignmentPolicyParameterObjectHelp = "The policy parameters object for the new policy assignment.";
3636
public const string NewPolicyParameterHelp = "The policy parameters file path or string for the new policy assignment.";
3737
public const string NewPolicyAssignmentMetadataHelp = "The metadata for the new policy assignment. This can either be a path to a file name containing the metadata, or the metadata as a string.";
38+
public const string NewPolicyAssignmentEnforcementModeHelp = "The enforcement mode for the new policy assignment, e.g. Default, DoNotEnforce. It indicates whether a policy effect will be enforced or not during assignment creation and update. Please visit https://aka.ms/azure-policyAssignment-enforcement-mode for more information.";
3839
public const string NewPolicyAssignmentSkuHelp = "A hash table which specifies sku properties. This parameter is deprecated and ignored.";
3940
public const string RemovePolicyAssignmentNameHelp = "The name of the policy assignment to delete.";
4041
public const string RemovePolicyAssignmentScopeHelp = "The scope of the policy assignment to delete, e.g. /providers/managementGroups/{managementGroupName}.";

src/Resources/ResourceManager/Implementation/Policy/SetAzurePolicyAssignment.cs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,17 @@
1414

1515
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation
1616
{
17-
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
18-
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Policy;
19-
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
20-
using Newtonsoft.Json.Linq;
21-
using Policy;
17+
using System;
2218
using System.Collections;
2319
using System.Management.Automation;
20+
21+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Components;
22+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Policy;
2423
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Entities.Resources;
24+
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Extensions;
2525
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
26+
using Newtonsoft.Json.Linq;
27+
using Policy;
2628

2729
/// <summary>
2830
/// Sets the policy assignment.
@@ -114,6 +116,13 @@ public class SetAzurePolicyAssignmentCmdlet : PolicyCmdletBase
114116
[LocationCompleter("Microsoft.ManagedIdentity/userAssignedIdentities")]
115117
public string Location { get; set; }
116118

119+
/// <summary>
120+
/// Gets or sets the policy assignment enforcement mode.
121+
/// </summary>
122+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = PolicyHelpStrings.NewPolicyAssignmentEnforcementModeHelp)]
123+
[ValidateNotNullOrEmpty]
124+
public PolicyAssignmentEnforcementMode? EnforcementMode { get; set; }
125+
117126
/// <summary>
118127
/// Executes the cmdlet.
119128
/// </summary>
@@ -154,6 +163,12 @@ private JToken GetResource(string resourceId, string apiVersion)
154163

155164
var metaDataJson = string.IsNullOrEmpty(this.Metadata) ? resource.Properties["metadata"]?.ToString() : GetObjectFromParameter(this.Metadata).ToString();
156165

166+
PolicyAssignmentEnforcementMode? existingMode = null;
167+
if (Enum.TryParse(resource.Properties["enforcementMode"]?.ToString(), true, out PolicyAssignmentEnforcementMode tempMode))
168+
{
169+
existingMode = tempMode;
170+
}
171+
157172
var policyAssignmentObject = new PolicyAssignment
158173
{
159174
Name = this.Name ?? resource.Name,
@@ -167,6 +182,7 @@ private JToken GetResource(string resourceId, string apiVersion)
167182
NotScopes = this.NotScope ?? resource.Properties["NotScopes"]?.ToString().Split(','),
168183
PolicyDefinitionId = resource.Properties["policyDefinitionId"].ToString(),
169184
Metadata = string.IsNullOrEmpty(this.Metadata) ? null : JObject.Parse(metaDataJson),
185+
EnforcementMode = this.EnforcementMode ?? existingMode,
170186
Parameters = this.GetParameters(this.PolicyParameter, this.PolicyParameterObject) ?? (JObject)resource.Properties["parameters"]
171187
}
172188
};

src/Resources/Resources.Test/ScenarioTests/PolicyTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ public void TestPolicyAssignmentIdentity()
6666
TestRunner.RunTestScript("Test-PolicyAssignmentIdentity");
6767
}
6868

69+
[Fact]
70+
[Trait(Category.AcceptanceType, Category.CheckIn)]
71+
public void TestPolicyAssignmentEnforcementMode()
72+
{
73+
TestRunner.RunTestScript("Test-PolicyAssignmentEnforcementMode");
74+
}
75+
6976
[Fact]
7077
[Trait(Category.AcceptanceType, Category.CheckIn)]
7178
public void TestPolicyDefinitionWithParameters()

src/Resources/Resources.Test/ScenarioTests/PolicyTests.ps1

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ $updatedDescription = "Updated $description"
1818
$metadataName = 'testName'
1919
$metadataValue = 'testValue'
2020
$metadata = "{'$metadataName':'$metadataValue'}"
21+
$enforcementModeDefault = 'Default'
22+
$enforcementModeDoNotEnforce = 'DoNotEnforce'
2123

2224
$updatedMetadataName = 'newTestName'
2325
$updatedMetadataValue = 'newTestValue'
@@ -414,6 +416,78 @@ function Test-PolicyAssignmentIdentity
414416
Assert-AreEqual True $remove
415417
}
416418

419+
<#
420+
.SYNOPSIS
421+
Tests Policy assignment CRUD operations with an enforcement mode property
422+
#>
423+
function Test-PolicyAssignmentEnforcementMode
424+
{
425+
# setup
426+
$rgname = Get-ResourceGroupName
427+
$policyName = Get-ResourceName
428+
$location = "westus"
429+
430+
# make a new resource group and policy definition
431+
$rg = New-AzResourceGroup -Name $rgname -Location $location
432+
$policy = New-AzPolicyDefinition -Name $policyName -Policy "$TestOutputRoot\SamplePolicyDefinition.json" -Description $description
433+
434+
# assign the policy definition to the resource group, get the assignment back and validate
435+
$actual = New-AzPolicyAssignment -Name testPA -PolicyDefinition $policy -Scope $rg.ResourceId -Description $description -Location $location -EnforcementMode DoNotEnforce
436+
$expected = Get-AzPolicyAssignment -Name testPA -Scope $rg.ResourceId
437+
Assert-AreEqual $expected.Name $actual.Name
438+
Assert-AreEqual Microsoft.Authorization/policyAssignments $actual.ResourceType
439+
Assert-AreEqual $expected.PolicyAssignmentId $actual.PolicyAssignmentId
440+
Assert-AreEqual $expected.Properties.PolicyDefinitionId $policy.PolicyDefinitionId
441+
Assert-AreEqual $expected.Properties.Scope $rg.ResourceId
442+
Assert-AreEqual $expected.Properties.EnforcementMode $actual.Properties.EnforcementMode
443+
Assert-AreEqual $expected.Properties.EnforcementMode $enforcementModeDoNotEnforce
444+
Assert-AreEqual $location $actual.Location
445+
Assert-AreEqual $expected.Location $actual.Location
446+
447+
# get it back by id and validate
448+
$actualById = Get-AzPolicyAssignment -Id $actual.ResourceId
449+
Assert-AreEqual $actual.Properties.EnforcementMode $actualById.Properties.EnforcementMode
450+
451+
# update the policy assignment, validate enforcement mode is updated correctly with Default enum value.
452+
$setResult = Set-AzPolicyAssignment -Id $actualById.ResourceId -DisplayName "testDisplay" -EnforcementMode Default
453+
Assert-AreEqual "testDisplay" $setResult.Properties.DisplayName
454+
Assert-AreEqual $enforcementModeDefault $setResult.Properties.EnforcementMode
455+
456+
# update the policy assignment, validate enforcement mode is updated correctly with 'Default' enum as string value.
457+
$setResult = Set-AzPolicyAssignment -Id $actualById.ResourceId -DisplayName "testDisplay" -EnforcementMode $enforcementModeDefault
458+
Assert-AreEqual "testDisplay" $setResult.Properties.DisplayName
459+
Assert-AreEqual $enforcementModeDefault $setResult.Properties.EnforcementMode
460+
461+
# make another policy assignment without an enforcementMode, validate default mode is set
462+
$withoutEnforcementMode = New-AzPolicyAssignment -Name test2 -Scope $rg.ResourceId -PolicyDefinition $policy -Description $description
463+
Assert-AreEqual $enforcementModeDefault $withoutEnforcementMode.Properties.EnforcementMode
464+
465+
# set an enforcement mode to the new assignment using the SET cmdlet
466+
$setResult = Set-AzPolicyAssignment -Id $withoutEnforcementMode.ResourceId -Location $location -EnforcementMode $enforcementModeDoNotEnforce
467+
Assert-AreEqual $enforcementModeDoNotEnforce $setResult.Properties.EnforcementMode
468+
469+
# set an enforcement mode to the new assignment using the SET cmdlet enum value and validate
470+
$setResult = Set-AzPolicyAssignment -Id $withoutEnforcementMode.ResourceId -Location $location -EnforcementMode DoNotEnforce
471+
Assert-AreEqual $enforcementModeDoNotEnforce $setResult.Properties.EnforcementMode
472+
473+
# verify enforcement mode is returned in collection GET
474+
$list = Get-AzPolicyAssignment -Scope $rg.ResourceId | ?{ $_.Name -in @('testPA', 'test2') }
475+
Assert-AreEqual 2 @($list.Properties.EnforcementMode | Select -Unique).Count
476+
477+
# clean up
478+
$remove = Remove-AzPolicyAssignment -Name testPA -Scope $rg.ResourceId
479+
Assert-AreEqual True $remove
480+
481+
$remove = Remove-AzPolicyAssignment -Name test2 -Scope $rg.ResourceId
482+
Assert-AreEqual True $remove
483+
484+
$remove = Remove-AzPolicyDefinition -Name $policyName -Force
485+
Assert-AreEqual True $remove
486+
487+
$remove = Remove-AzResourceGroup -Name $rgname -Force
488+
Assert-AreEqual True $remove
489+
}
490+
417491
<#
418492
.SYNOPSIS
419493
Tests Policy set definition CRUD operations

src/Resources/Resources.Test/SessionRecords/Microsoft.Azure.Commands.Resources.Test.ScenarioTests.PolicyTests/TestGetBuiltinsByName.json

Lines changed: 26749 additions & 4749 deletions
Large diffs are not rendered by default.

src/Resources/Resources.Test/SessionRecords/Microsoft.Azure.Commands.Resources.Test.ScenarioTests.PolicyTests/TestGetCmdletFilterParameter.json

Lines changed: 61 additions & 61 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)