Skip to content

Commit f8b70a6

Browse files
Fixed recaptcha_options permadiff causing rules being recreated (#12458) (#20617)
[upstream:b0b7f57d696854f3025089d6866a2c8a385c5536] Signed-off-by: Modular Magician <magic-modules@google.com>
1 parent 0ee47a8 commit f8b70a6

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

.changelog/12458.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
compute: fixed permadiff on the `recaptcha_options` field for `google_compute_security_policy` resource
3+
```

google/services/compute/resource_compute_security_policy.go

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,26 @@ import (
2222
"google.golang.org/api/compute/v1"
2323
)
2424

25+
func verifyRulePriorityCompareEmptyValues(d *schema.ResourceData, rulePriority int, schemaKey string) bool {
26+
if schemaRules, ok := d.GetOk("rule"); ok {
27+
for _, itemRaw := range schemaRules.(*schema.Set).List() {
28+
if itemRaw == nil {
29+
continue
30+
}
31+
item := itemRaw.(map[string]interface{})
32+
33+
schemaPriority := item["priority"].(int)
34+
if rulePriority == schemaPriority {
35+
if tpgresource.IsEmptyValue(reflect.ValueOf(item[schemaKey])) {
36+
return true
37+
}
38+
break
39+
}
40+
}
41+
}
42+
return false
43+
}
44+
2545
// IsEmptyValue does not consider a empty PreconfiguredWafConfig object as empty so we check it's nested values
2646
func preconfiguredWafConfigIsEmptyValue(config *compute.SecurityPolicyRulePreconfiguredWafConfig) bool {
2747
if tpgresource.IsEmptyValue(reflect.ValueOf(config.Exclusions)) &&
@@ -1155,7 +1175,7 @@ func flattenSecurityPolicyRules(rules []*compute.SecurityPolicyRule, d *schema.R
11551175
"priority": rule.Priority,
11561176
"action": rule.Action,
11571177
"preview": rule.Preview,
1158-
"match": flattenMatch(rule.Match),
1178+
"match": flattenMatch(rule.Match, d, int(rule.Priority)),
11591179
"preconfigured_waf_config": flattenPreconfiguredWafConfig(rule.PreconfiguredWafConfig, d, int(rule.Priority)),
11601180
"rate_limit_options": flattenSecurityPolicyRuleRateLimitOptions(rule.RateLimitOptions),
11611181
"redirect_options": flattenSecurityPolicyRedirectOptions(rule.RedirectOptions),
@@ -1166,7 +1186,7 @@ func flattenSecurityPolicyRules(rules []*compute.SecurityPolicyRule, d *schema.R
11661186
return rulesSchema
11671187
}
11681188

1169-
func flattenMatch(match *compute.SecurityPolicyRuleMatcher) []map[string]interface{} {
1189+
func flattenMatch(match *compute.SecurityPolicyRuleMatcher, d *schema.ResourceData, rulePriority int) []map[string]interface{} {
11701190
if match == nil {
11711191
return nil
11721192
}
@@ -1175,7 +1195,7 @@ func flattenMatch(match *compute.SecurityPolicyRuleMatcher) []map[string]interfa
11751195
"versioned_expr": match.VersionedExpr,
11761196
"config": flattenMatchConfig(match.Config),
11771197
"expr": flattenMatchExpr(match),
1178-
"expr_options": flattenMatchExprOptions(match.ExprOptions),
1198+
"expr_options": flattenMatchExprOptions(match.ExprOptions, d, rulePriority),
11791199
}
11801200

11811201
return []map[string]interface{}{data}
@@ -1193,11 +1213,18 @@ func flattenMatchConfig(conf *compute.SecurityPolicyRuleMatcherConfig) []map[str
11931213
return []map[string]interface{}{data}
11941214
}
11951215

1196-
func flattenMatchExprOptions(exprOptions *compute.SecurityPolicyRuleMatcherExprOptions) []map[string]interface{} {
1216+
func flattenMatchExprOptions(exprOptions *compute.SecurityPolicyRuleMatcherExprOptions, d *schema.ResourceData, rulePriority int) []map[string]interface{} {
11971217
if exprOptions == nil {
11981218
return nil
11991219
}
12001220

1221+
// We check if the API is returning a empty non-null value then we find the current value for this field in the rule config and check if its empty
1222+
if (tpgresource.IsEmptyValue(reflect.ValueOf(exprOptions.RecaptchaOptions.ActionTokenSiteKeys)) &&
1223+
tpgresource.IsEmptyValue(reflect.ValueOf(exprOptions.RecaptchaOptions.SessionTokenSiteKeys))) &&
1224+
verifyRulePriorityCompareEmptyValues(d, rulePriority, "recaptcha_options") {
1225+
return nil
1226+
}
1227+
12011228
data := map[string]interface{}{
12021229
"recaptcha_options": flattenMatchExprOptionsRecaptchaOptions(exprOptions.RecaptchaOptions),
12031230
}
@@ -1239,22 +1266,9 @@ func flattenPreconfiguredWafConfig(config *compute.SecurityPolicyRulePreconfigur
12391266
return nil
12401267
}
12411268

1242-
// We find the current value for this field in the config and check if its empty, then check if the API is returning a empty non-null value
1243-
if schemaRules, ok := d.GetOk("rule"); ok {
1244-
for _, itemRaw := range schemaRules.(*schema.Set).List() {
1245-
if itemRaw == nil {
1246-
continue
1247-
}
1248-
item := itemRaw.(map[string]interface{})
1249-
1250-
schemaPriority := item["priority"].(int)
1251-
if rulePriority == schemaPriority {
1252-
if preconfiguredWafConfigIsEmptyValue(config) && tpgresource.IsEmptyValue(reflect.ValueOf(item["preconfigured_waf_config"])) {
1253-
return nil
1254-
}
1255-
break
1256-
}
1257-
}
1269+
// We check if the API is returning a empty non-null value then we find the current value for this field in the rule config and check if its empty
1270+
if preconfiguredWafConfigIsEmptyValue(config) && verifyRulePriorityCompareEmptyValues(d, rulePriority, "preconfigured_waf_config") {
1271+
return nil
12581272
}
12591273

12601274
data := map[string]interface{}{

0 commit comments

Comments
 (0)