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

azurerm_policy_assignment - added support for enforcement_mode #7331

Merged
merged 3 commits into from Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion azurerm/internal/services/policy/policy_assignment_resource.go
Expand Up @@ -113,6 +113,16 @@ func resourceArmPolicyAssignment() *schema.Resource {
DiffSuppressFunc: structure.SuppressJsonDiff,
},

"enforcement_mode": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ccampo marked this conversation as resolved.
Show resolved Hide resolved
ccampo marked this conversation as resolved.
Show resolved Hide resolved
ValidateFunc: validation.StringInSlice([]string{
string(policy.Default),
string(policy.DoNotEnforce),
}, false),
},

"not_scopes": {
Type: schema.TypeList,
Optional: true,
Expand All @@ -129,7 +139,7 @@ func resourceArmPolicyAssignmentCreateUpdate(d *schema.ResourceData, meta interf

name := d.Get("name").(string)
scope := d.Get("scope").(string)

enforcementMode := policy.EnforcementMode(d.Get("enforcement_mode").(string))
policyDefinitionId := d.Get("policy_definition_id").(string)
displayName := d.Get("display_name").(string)

Expand All @@ -151,6 +161,7 @@ func resourceArmPolicyAssignmentCreateUpdate(d *schema.ResourceData, meta interf
PolicyDefinitionID: utils.String(policyDefinitionId),
DisplayName: utils.String(displayName),
Scope: utils.String(scope),
EnforcementMode: enforcementMode,
},
}

Expand Down
Expand Up @@ -138,6 +138,24 @@ func TestAccAzureRMPolicyAssignment_not_scopes(t *testing.T) {
})
}

func TestAccAzureRMPolicyAssignment_enforcement_mode(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_policy_assignment", "test")
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMPolicyAssignmentDestroy,
Steps: []resource.TestStep{
{
Config: testAzureRMPolicyAssignment_enforcement_mode(data),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMPolicyAssignmentExists(data.ResourceName),
),
},
data.ImportStep(),
},
})
}

func testCheckAzureRMPolicyAssignmentExists(resourceName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := acceptance.AzureProvider.Meta().(*clients.Client).Policy.AssignmentsClient
Expand Down Expand Up @@ -516,3 +534,73 @@ PARAMETERS
}
`, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.Locations.Primary)
}

func testAzureRMPolicyAssignment_enforcement_mode(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

data "azurerm_subscription" "current" {
}

resource "azurerm_policy_definition" "test" {
name = "acctestpol-%d"
policy_type = "Custom"
mode = "All"
display_name = "acctestpol-%d"

policy_rule = <<POLICY_RULE
{
"if": {
"not": {
"field": "location",
"in": "[parameters('allowedLocations')]"
}
},
"then": {
"effect": "audit"
}
}
POLICY_RULE


parameters = <<PARAMETERS
{
"allowedLocations": {
"type": "Array",
"metadata": {
"description": "The list of allowed locations for resources.",
"displayName": "Allowed locations",
"strongType": "location"
}
}
}
PARAMETERS

}

resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_policy_assignment" "test" {
name = "acctestpa-%d"
scope = data.azurerm_subscription.current.id
policy_definition_id = azurerm_policy_definition.test.id
description = "Policy Assignment created via an Acceptance Test"
enforcement_mode = "DoNotEnforce"
display_name = "Acceptance Test Run %d"

parameters = <<PARAMETERS
{
"allowedLocations": {
"value": [ "%s" ]
}
}
PARAMETERS

}
`, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.Locations.Primary)
}
1 change: 1 addition & 0 deletions website/docs/r/policy_assignment.html.markdown
Expand Up @@ -96,6 +96,7 @@ The following arguments are supported:

* `not_scopes` - (Optional) A list of the Policy Assignment's excluded scopes. The list must contain Resource IDs (such as Subscriptions e.g. `/subscriptions/00000000-0000-0000-000000000000` or Resource Groups e.g.`/subscriptions/00000000-0000-0000-000000000000/resourceGroups/myResourceGroup`).

* `enforcement_mode`- (Optional) Can be set to 'Default' or 'DoNotEnforce' to control whether the assignment is enforced. Default is 'Default'.
---

An `identity` block supports the following:
Expand Down