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

SWP Policy Rule - Mitigate multiple rules issue #12704

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions mmv1/products/networksecurity/GatewaySecurityPolicyRule.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ async:
result:
resource_inside_response: false
custom_code:
pre_create: 'templates/terraform/pre_create/network_security_gateway_security_policy_rule.go.tmpl'
post_delete: 'templates/terraform/post_delete/sleep.go.tmpl'
examples:
- name: 'network_security_gateway_security_policy_rules_basic'
primary_resource_id: 'default'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// When creating more than one rule sometimes the creation conflicts
// Use random sleep duration to mitigate this issue
time.Sleep(time.Duration(rand.Intn(10)) * time.Second)
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,45 @@ func TestAccNetworkSecurityGatewaySecurityPolicyRule_update(t *testing.T) {
})
}

func TestAccNetworkSecurityGatewaySecurityPolicyRule_multiple(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
}

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckNetworkSecurityGatewaySecurityPolicyRuleDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccNetworkSecurityGatewaySecurityPolicyRule_multiple(context),
},
{
ResourceName: "google_network_security_gateway_security_policy_rule.rule1",
ImportState: true,
ImportStateVerify: true,
},
{
ResourceName: "google_network_security_gateway_security_policy_rule.rule2",
ImportState: true,
ImportStateVerify: true,
},
{
ResourceName: "google_network_security_gateway_security_policy_rule.rule3",
ImportState: true,
ImportStateVerify: true,
},
{
ResourceName: "google_network_security_gateway_security_policy_rule.rule4",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccNetworkSecurityGatewaySecurityPolicyRule_basic(gatewaySecurityPolicyName, gatewaySecurityPolicyRuleName string) string {
return fmt.Sprintf(`
resource "google_network_security_gateway_security_policy" "default" {
Expand Down Expand Up @@ -92,3 +131,63 @@ resource "google_network_security_gateway_security_policy_rule" "foobar" {
}
`, gatewaySecurityPolicyName, gatewaySecurityPolicyRuleName)
}

func testAccNetworkSecurityGatewaySecurityPolicyRule_multiple(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_network_security_gateway_security_policy" "default" {
name = "tf-test-gateway-sp-%{random_suffix}"
location = "us-central1"
description = "gateway security policy created to be used as reference by the rule."
}

resource "google_network_security_gateway_security_policy_rule" "rule1" {
name = "tf-test-gateway-sp-rule1-%{random_suffix}"
location = "us-central1"
gateway_security_policy = google_network_security_gateway_security_policy.default.name
enabled = true
description = "First rule"
priority = 1
session_matcher = "host() == 'example.com'"
application_matcher = "request.method == 'POST'"
basic_profile = "ALLOW"
}

resource "google_network_security_gateway_security_policy_rule" "rule2" {
name = "tf-test-gateway-sp-rule2-%{random_suffix}"
location = "us-central1"
gateway_security_policy = google_network_security_gateway_security_policy.default.name
enabled = true
description = "Second rule"
priority = 2
session_matcher = "host() == 'example.com'"
application_matcher = "request.method == 'GET'"
tls_inspection_enabled = false
basic_profile = "DENY"
}

resource "google_network_security_gateway_security_policy_rule" "rule3" {
name = "tf-test-gateway-sp-rule3-%{random_suffix}"
location = "us-central1"
gateway_security_policy = google_network_security_gateway_security_policy.default.name
enabled = true
description = "Third rule"
priority = 3
session_matcher = "host() == 'update.com'"
application_matcher = "request.method == 'POST'"
basic_profile = "ALLOW"
}

resource "google_network_security_gateway_security_policy_rule" "rule4" {
name = "tf-test-gateway-sp-rule4-%{random_suffix}"
location = "us-central1"
gateway_security_policy = google_network_security_gateway_security_policy.default.name
enabled = true
description = "Fourth rule"
priority = 4
session_matcher = "host() == 'update.com'"
application_matcher = "request.method == 'GET'"
tls_inspection_enabled = false
basic_profile = "DENY"
}
`, context)
}
Loading