@@ -22,6 +22,26 @@ import (
22
22
"google.golang.org/api/compute/v1"
23
23
)
24
24
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
+
25
45
// IsEmptyValue does not consider a empty PreconfiguredWafConfig object as empty so we check it's nested values
26
46
func preconfiguredWafConfigIsEmptyValue (config * compute.SecurityPolicyRulePreconfiguredWafConfig ) bool {
27
47
if tpgresource .IsEmptyValue (reflect .ValueOf (config .Exclusions )) &&
@@ -1155,7 +1175,7 @@ func flattenSecurityPolicyRules(rules []*compute.SecurityPolicyRule, d *schema.R
1155
1175
"priority" : rule .Priority ,
1156
1176
"action" : rule .Action ,
1157
1177
"preview" : rule .Preview ,
1158
- "match" : flattenMatch (rule .Match ),
1178
+ "match" : flattenMatch (rule .Match , d , int ( rule . Priority ) ),
1159
1179
"preconfigured_waf_config" : flattenPreconfiguredWafConfig (rule .PreconfiguredWafConfig , d , int (rule .Priority )),
1160
1180
"rate_limit_options" : flattenSecurityPolicyRuleRateLimitOptions (rule .RateLimitOptions ),
1161
1181
"redirect_options" : flattenSecurityPolicyRedirectOptions (rule .RedirectOptions ),
@@ -1166,7 +1186,7 @@ func flattenSecurityPolicyRules(rules []*compute.SecurityPolicyRule, d *schema.R
1166
1186
return rulesSchema
1167
1187
}
1168
1188
1169
- func flattenMatch (match * compute.SecurityPolicyRuleMatcher ) []map [string ]interface {} {
1189
+ func flattenMatch (match * compute.SecurityPolicyRuleMatcher , d * schema. ResourceData , rulePriority int ) []map [string ]interface {} {
1170
1190
if match == nil {
1171
1191
return nil
1172
1192
}
@@ -1175,7 +1195,7 @@ func flattenMatch(match *compute.SecurityPolicyRuleMatcher) []map[string]interfa
1175
1195
"versioned_expr" : match .VersionedExpr ,
1176
1196
"config" : flattenMatchConfig (match .Config ),
1177
1197
"expr" : flattenMatchExpr (match ),
1178
- "expr_options" : flattenMatchExprOptions (match .ExprOptions ),
1198
+ "expr_options" : flattenMatchExprOptions (match .ExprOptions , d , rulePriority ),
1179
1199
}
1180
1200
1181
1201
return []map [string ]interface {}{data }
@@ -1193,11 +1213,18 @@ func flattenMatchConfig(conf *compute.SecurityPolicyRuleMatcherConfig) []map[str
1193
1213
return []map [string ]interface {}{data }
1194
1214
}
1195
1215
1196
- func flattenMatchExprOptions (exprOptions * compute.SecurityPolicyRuleMatcherExprOptions ) []map [string ]interface {} {
1216
+ func flattenMatchExprOptions (exprOptions * compute.SecurityPolicyRuleMatcherExprOptions , d * schema. ResourceData , rulePriority int ) []map [string ]interface {} {
1197
1217
if exprOptions == nil {
1198
1218
return nil
1199
1219
}
1200
1220
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
+
1201
1228
data := map [string ]interface {}{
1202
1229
"recaptcha_options" : flattenMatchExprOptionsRecaptchaOptions (exprOptions .RecaptchaOptions ),
1203
1230
}
@@ -1239,22 +1266,9 @@ func flattenPreconfiguredWafConfig(config *compute.SecurityPolicyRulePreconfigur
1239
1266
return nil
1240
1267
}
1241
1268
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
1258
1272
}
1259
1273
1260
1274
data := map [string ]interface {}{
0 commit comments