Skip to content

Commit 1fc2278

Browse files
SWP Policy Rule - Mitigate multiple rules issue (#12704)
[upstream:e8431c4467c7d067460a8e3a8ea50439345c8ec6] Signed-off-by: Modular Magician <[email protected]>
1 parent 568c236 commit 1fc2278

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

.changelog/12704.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
networksecurity: added wait time on `google_network_security_gateway_security_policy_rule` resource when creating and deleting to prevent race conditions
3+
```

google-beta/services/networksecurity/resource_network_security_gateway_security_policy_rule.go

+21
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ func resourceNetworkSecurityGatewaySecurityPolicyRuleCreate(d *schema.ResourceDa
194194
obj["basicProfile"] = basicProfileProp
195195
}
196196

197+
lockName, err := tpgresource.ReplaceVars(d, config, "gatewaySecurityPolicies/{{gateway_security_policy}}/rules")
198+
if err != nil {
199+
return err
200+
}
201+
transport_tpg.MutexStore.Lock(lockName)
202+
defer transport_tpg.MutexStore.Unlock(lockName)
203+
197204
url, err := tpgresource.ReplaceVars(d, config, "{{NetworkSecurityBasePath}}projects/{{project}}/locations/{{location}}/gatewaySecurityPolicies/{{gateway_security_policy}}/rules?gatewaySecurityPolicyRuleId={{name}}")
198205
if err != nil {
199206
return err
@@ -385,6 +392,13 @@ func resourceNetworkSecurityGatewaySecurityPolicyRuleUpdate(d *schema.ResourceDa
385392
obj["basicProfile"] = basicProfileProp
386393
}
387394

395+
lockName, err := tpgresource.ReplaceVars(d, config, "gatewaySecurityPolicies/{{gateway_security_policy}}/rules")
396+
if err != nil {
397+
return err
398+
}
399+
transport_tpg.MutexStore.Lock(lockName)
400+
defer transport_tpg.MutexStore.Unlock(lockName)
401+
388402
url, err := tpgresource.ReplaceVars(d, config, "{{NetworkSecurityBasePath}}projects/{{project}}/locations/{{location}}/gatewaySecurityPolicies/{{gateway_security_policy}}/rules/{{name}}")
389403
if err != nil {
390404
return err
@@ -479,6 +493,13 @@ func resourceNetworkSecurityGatewaySecurityPolicyRuleDelete(d *schema.ResourceDa
479493
}
480494
billingProject = project
481495

496+
lockName, err := tpgresource.ReplaceVars(d, config, "gatewaySecurityPolicies/{{gateway_security_policy}}/rules")
497+
if err != nil {
498+
return err
499+
}
500+
transport_tpg.MutexStore.Lock(lockName)
501+
defer transport_tpg.MutexStore.Unlock(lockName)
502+
482503
url, err := tpgresource.ReplaceVars(d, config, "{{NetworkSecurityBasePath}}projects/{{project}}/locations/{{location}}/gatewaySecurityPolicies/{{gateway_security_policy}}/rules/{{name}}")
483504
if err != nil {
484505
return err

google-beta/services/networksecurity/resource_network_security_gateway_security_policy_rule_test.go

+117
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,50 @@ func TestAccNetworkSecurityGatewaySecurityPolicyRule_update(t *testing.T) {
5050
})
5151
}
5252

53+
func TestAccNetworkSecurityGatewaySecurityPolicyRule_multiple(t *testing.T) {
54+
t.Parallel()
55+
56+
context := map[string]interface{}{
57+
"random_suffix": acctest.RandString(t, 10),
58+
}
59+
60+
acctest.VcrTest(t, resource.TestCase{
61+
PreCheck: func() { acctest.AccTestPreCheck(t) },
62+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
63+
CheckDestroy: testAccCheckNetworkSecurityGatewaySecurityPolicyRuleDestroyProducer(t),
64+
Steps: []resource.TestStep{
65+
{
66+
Config: testAccNetworkSecurityGatewaySecurityPolicyRule_multiple(context),
67+
},
68+
{
69+
ResourceName: "google_network_security_gateway_security_policy_rule.rule1",
70+
ImportState: true,
71+
ImportStateVerify: true,
72+
},
73+
{
74+
ResourceName: "google_network_security_gateway_security_policy_rule.rule2",
75+
ImportState: true,
76+
ImportStateVerify: true,
77+
},
78+
{
79+
ResourceName: "google_network_security_gateway_security_policy_rule.rule3",
80+
ImportState: true,
81+
ImportStateVerify: true,
82+
},
83+
{
84+
ResourceName: "google_network_security_gateway_security_policy_rule.rule4",
85+
ImportState: true,
86+
ImportStateVerify: true,
87+
},
88+
{
89+
ResourceName: "google_network_security_gateway_security_policy_rule.rule5",
90+
ImportState: true,
91+
ImportStateVerify: true,
92+
},
93+
},
94+
})
95+
}
96+
5397
func testAccNetworkSecurityGatewaySecurityPolicyRule_basic(gatewaySecurityPolicyName, gatewaySecurityPolicyRuleName string) string {
5498
return fmt.Sprintf(`
5599
resource "google_network_security_gateway_security_policy" "default" {
@@ -94,3 +138,76 @@ resource "google_network_security_gateway_security_policy_rule" "foobar" {
94138
}
95139
`, gatewaySecurityPolicyName, gatewaySecurityPolicyRuleName)
96140
}
141+
142+
func testAccNetworkSecurityGatewaySecurityPolicyRule_multiple(context map[string]interface{}) string {
143+
return acctest.Nprintf(`
144+
resource "google_network_security_gateway_security_policy" "default" {
145+
name = "tf-test-gateway-sp-%{random_suffix}"
146+
location = "us-central1"
147+
description = "gateway security policy created to be used as reference by the rule."
148+
}
149+
150+
resource "google_network_security_gateway_security_policy_rule" "rule1" {
151+
name = "tf-test-gateway-sp-rule1-%{random_suffix}"
152+
location = "us-central1"
153+
gateway_security_policy = google_network_security_gateway_security_policy.default.name
154+
enabled = true
155+
description = "Highest priority rule"
156+
priority = 0
157+
session_matcher = "host() == 'example.com'"
158+
application_matcher = "request.method == 'POST'"
159+
basic_profile = "ALLOW"
160+
}
161+
162+
resource "google_network_security_gateway_security_policy_rule" "rule2" {
163+
name = "tf-test-gateway-sp-rule2-%{random_suffix}"
164+
location = "us-central1"
165+
gateway_security_policy = google_network_security_gateway_security_policy.default.name
166+
enabled = true
167+
description = "Rule priority 762"
168+
priority = 762
169+
session_matcher = "host() == 'example.com'"
170+
application_matcher = "request.method == 'GET'"
171+
tls_inspection_enabled = false
172+
basic_profile = "DENY"
173+
}
174+
175+
resource "google_network_security_gateway_security_policy_rule" "rule3" {
176+
name = "tf-test-gateway-sp-rule3-%{random_suffix}"
177+
location = "us-central1"
178+
gateway_security_policy = google_network_security_gateway_security_policy.default.name
179+
enabled = true
180+
description = "Rule priority 37961"
181+
priority = 37961
182+
session_matcher = "host() == 'update.com'"
183+
application_matcher = "request.method == 'POST'"
184+
basic_profile = "ALLOW"
185+
}
186+
187+
resource "google_network_security_gateway_security_policy_rule" "rule4" {
188+
name = "tf-test-gateway-sp-rule4-%{random_suffix}"
189+
location = "us-central1"
190+
gateway_security_policy = google_network_security_gateway_security_policy.default.name
191+
enabled = true
192+
description = "Rule priority 9572843"
193+
priority = 9572843
194+
session_matcher = "host() == 'update.com'"
195+
application_matcher = "request.method == 'GET'"
196+
tls_inspection_enabled = false
197+
basic_profile = "DENY"
198+
}
199+
200+
resource "google_network_security_gateway_security_policy_rule" "rule5" {
201+
name = "tf-test-gateway-sp-rule5-%{random_suffix}"
202+
location = "us-central1"
203+
gateway_security_policy = google_network_security_gateway_security_policy.default.name
204+
enabled = true
205+
description = "Lowest priority rule"
206+
priority = 2147483647
207+
session_matcher = "host() == 'update.com'"
208+
application_matcher = "request.method == 'GET'"
209+
tls_inspection_enabled = false
210+
basic_profile = "DENY"
211+
}
212+
`, context)
213+
}

0 commit comments

Comments
 (0)