From 43751d05048ba30d5a2ec6d317d3dd17f0da1603 Mon Sep 17 00:00:00 2001 From: Dmitry Dagunts Date: Fri, 15 Apr 2022 11:51:58 -0400 Subject: [PATCH] include 'Exceed Redirect Options' in security policy rules --- .../resource_compute_security_policy.go.erb | 30 ++++---- ...source_compute_security_policy_test.go.erb | 70 +++++++++++++++++++ 2 files changed, 86 insertions(+), 14 deletions(-) diff --git a/mmv1/third_party/terraform/resources/resource_compute_security_policy.go.erb b/mmv1/third_party/terraform/resources/resource_compute_security_policy.go.erb index dd27619ddd07..7fbd2539f148 100644 --- a/mmv1/third_party/terraform/resources/resource_compute_security_policy.go.erb +++ b/mmv1/third_party/terraform/resources/resource_compute_security_policy.go.erb @@ -824,13 +824,14 @@ func expandSecurityPolicyRuleRateLimitOptions(configured []interface{}) *compute data := configured[0].(map[string]interface{}) return &compute.SecurityPolicyRuleRateLimitOptions{ - BanThreshold: expandThreshold(data["ban_threshold"].([]interface{})), - RateLimitThreshold: expandThreshold(data["rate_limit_threshold"].([]interface{})), - ExceedAction: data["exceed_action"].(string), - ConformAction: data["conform_action"].(string), - EnforceOnKey: data["enforce_on_key"].(string), - EnforceOnKeyName: data["enforce_on_key_name"].(string), - BanDurationSec: int64(data["ban_duration_sec"].(int)), + BanThreshold: expandThreshold(data["ban_threshold"].([]interface{})), + RateLimitThreshold: expandThreshold(data["rate_limit_threshold"].([]interface{})), + ExceedAction: data["exceed_action"].(string), + ConformAction: data["conform_action"].(string), + EnforceOnKey: data["enforce_on_key"].(string), + EnforceOnKeyName: data["enforce_on_key_name"].(string), + BanDurationSec: int64(data["ban_duration_sec"].(int)), + ExceedRedirectOptions: expandSecurityPolicyRuleRedirectOptions(data["exceed_redirect_options"].([]interface{})), } } @@ -852,13 +853,14 @@ func flattenSecurityPolicyRuleRateLimitOptions(conf *compute.SecurityPolicyRuleR } data := map[string]interface{}{ - "ban_threshold": flattenThreshold(conf.BanThreshold), - "rate_limit_threshold": flattenThreshold(conf.RateLimitThreshold), - "exceed_action": conf.ExceedAction, - "conform_action": conf.ConformAction, - "enforce_on_key": conf.EnforceOnKey, - "enforce_on_key_name": conf.EnforceOnKeyName, - "ban_duration_sec": conf.BanDurationSec, + "ban_threshold": flattenThreshold(conf.BanThreshold), + "rate_limit_threshold": flattenThreshold(conf.RateLimitThreshold), + "exceed_action": conf.ExceedAction, + "conform_action": conf.ConformAction, + "enforce_on_key": conf.EnforceOnKey, + "enforce_on_key_name": conf.EnforceOnKeyName, + "ban_duration_sec": conf.BanDurationSec, + "exceed_redirect_options": flattenSecurityPolicyRedirectOptions(conf.ExceedRedirectOptions), } return []map[string]interface{}{data} diff --git a/mmv1/third_party/terraform/tests/resource_compute_security_policy_test.go.erb b/mmv1/third_party/terraform/tests/resource_compute_security_policy_test.go.erb index 7e4944e67863..9c8841c8b316 100644 --- a/mmv1/third_party/terraform/tests/resource_compute_security_policy_test.go.erb +++ b/mmv1/third_party/terraform/tests/resource_compute_security_policy_test.go.erb @@ -169,6 +169,28 @@ func TestAccComputeSecurityPolicy_withRateLimitOptions(t *testing.T) { }, }) } + +func TestAccComputeSecurityPolicy_withRateLimitWithRedirectOptions(t *testing.T) { + t.Parallel() + + spName := fmt.Sprintf("tf-test-%s", randString(t, 10)) + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckComputeSecurityPolicyDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccComputeSecurityPolicy_withRateLimitWithRedirectOptions(spName), + }, + { + ResourceName: "google_compute_security_policy.policy", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} <% end -%> func testAccCheckComputeSecurityPolicyDestroyProducer(t *testing.T) func(s *terraform.State) error { @@ -425,6 +447,54 @@ resource "google_compute_security_policy" "policy" { } `, spName) } + +func testAccComputeSecurityPolicy_withRateLimitWithRedirectOptions(spName string) string { + return fmt.Sprintf(` +resource "google_compute_security_policy" "policy" { + name = "%s" + description = "updated description" + + rule { + action = "allow" + priority = "2147483647" + match { + versioned_expr = "SRC_IPS_V1" + config { + src_ip_ranges = ["*"] + } + } + description = "default rule" + } + + rule { + action = "throttle" + priority = 100 + match { + versioned_expr = "SRC_IPS_V1" + config { + src_ip_ranges = [ + "0.0.0.0/32", + ] + } + } + rate_limit_options { + conform_action = "allow" + exceed_action = "redirect" + enforce_on_key = "IP" + exceed_redirect_options { + type = "EXTERNAL_302" + target = "https://www.example.com" + } + rate_limit_threshold { + count = 100 + interval_sec = 60 + } + } + } +} +`, spName) +} + <% end -%> <% unless version == 'ga' -%>