Skip to content

Commit

Permalink
azurerm_policy_definition - parameters is ForceNew if there are less …
Browse files Browse the repository at this point in the history
…parameters betweenn runs (#26083)
  • Loading branch information
mbfrahry authored May 24, 2024
1 parent 1d4e4d7 commit 6454ea9
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
29 changes: 29 additions & 0 deletions internal/services/policy/policy_definition_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,35 @@ func resourceArmPolicyDefinition() *pluginsdk.Resource {
},

Schema: resourceArmPolicyDefinitionSchema(),

CustomizeDiff: pluginsdk.CustomizeDiffShim(func(ctx context.Context, d *pluginsdk.ResourceDiff, v interface{}) error {
// `parameters` cannot have values removed so we'll ForceNew if there are less parameters between Terraform runs
if d.HasChange("parameters") {
oldParametersRaw, newParametersRaw := d.GetChange("parameters")
if oldParametersString := oldParametersRaw.(string); oldParametersString != "" {
newParametersString := newParametersRaw.(string)
if newParametersString == "" {
return d.ForceNew("parameters")
}

oldParameters, err := expandParameterDefinitionsValueFromString(oldParametersString)
if err != nil {
return fmt.Errorf("expanding JSON for `parameters`: %+v", err)
}

newParameters, err := expandParameterDefinitionsValueFromString(newParametersString)
if err != nil {
return fmt.Errorf("expanding JSON for `parameters`: %+v", err)
}

if len(newParameters) < len(oldParameters) {
return d.ForceNew("parameters")
}
}
}

return nil
}),
}
}

Expand Down
101 changes: 101 additions & 0 deletions internal/services/policy/policy_definition_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,35 @@ func TestAccAzureRMPolicyDefinition_modeUpdate(t *testing.T) {
})
}

func TestAccAzureRMPolicyDefinition_removeParameter(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_policy_definition", "test")
r := PolicyDefinitionResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.additionalParameter(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.basic(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (r PolicyDefinitionResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
definitionsClient := client.Policy.DefinitionsClient
id, err := parse.PolicyDefinitionID(state.ID)
Expand Down Expand Up @@ -442,3 +471,75 @@ PARAMETERS
}
`, data.RandomInteger, mode, data.RandomInteger)
}

func (r PolicyDefinitionResource) additionalParameter(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
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": "AuditIfNotExists",
"details": {
"type": "Microsoft.Insights/diagnosticSettings",
"existenceCondition": {
"allOf": [
{
"field": "Microsoft.Insights/diagnosticSettings/logs[*].retentionPolicy.enabled",
"equals": "true"
},
{
"field": "Microsoft.Insights/diagnosticSettings/logs[*].retentionPolicy.days",
"equals": "[parameters('requiredRetentionDays')]"
}
]
}
}
}
}
POLICY_RULE
parameters = <<PARAMETERS
{
"allowedLocations": {
"type": "Array",
"metadata": {
"description": "The list of allowed locations for resources.",
"displayName": "Allowed locations",
"strongType": "location"
}
},
"requiredRetentionDays": {
"type": "Integer",
"defaultValue": 365,
"allowedValues": [
0,
30,
90,
180,
365
],
"metadata": {
"displayName": "Required retention (days)",
"description": "The required diagnostic logs retention in days"
}
}
}
PARAMETERS
}
`, data.RandomInteger, data.RandomInteger)
}

0 comments on commit 6454ea9

Please sign in to comment.