Skip to content

Commit 3c62b93

Browse files
include 'Exceed Redirect Options' in security policy rules (#5946) (#4238)
Signed-off-by: Modular Magician <[email protected]>
1 parent 72f0f99 commit 3c62b93

File tree

3 files changed

+89
-14
lines changed

3 files changed

+89
-14
lines changed

.changelog/5946.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
```release-note:enhancement
2+
compute: added passing `exceed_redirect_options` field for `google_compute_security_policy` rules
3+
4+
```

google-beta/resource_compute_security_policy.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -778,13 +778,14 @@ func expandSecurityPolicyRuleRateLimitOptions(configured []interface{}) *compute
778778

779779
data := configured[0].(map[string]interface{})
780780
return &compute.SecurityPolicyRuleRateLimitOptions{
781-
BanThreshold: expandThreshold(data["ban_threshold"].([]interface{})),
782-
RateLimitThreshold: expandThreshold(data["rate_limit_threshold"].([]interface{})),
783-
ExceedAction: data["exceed_action"].(string),
784-
ConformAction: data["conform_action"].(string),
785-
EnforceOnKey: data["enforce_on_key"].(string),
786-
EnforceOnKeyName: data["enforce_on_key_name"].(string),
787-
BanDurationSec: int64(data["ban_duration_sec"].(int)),
781+
BanThreshold: expandThreshold(data["ban_threshold"].([]interface{})),
782+
RateLimitThreshold: expandThreshold(data["rate_limit_threshold"].([]interface{})),
783+
ExceedAction: data["exceed_action"].(string),
784+
ConformAction: data["conform_action"].(string),
785+
EnforceOnKey: data["enforce_on_key"].(string),
786+
EnforceOnKeyName: data["enforce_on_key_name"].(string),
787+
BanDurationSec: int64(data["ban_duration_sec"].(int)),
788+
ExceedRedirectOptions: expandSecurityPolicyRuleRedirectOptions(data["exceed_redirect_options"].([]interface{})),
788789
}
789790
}
790791

@@ -806,13 +807,14 @@ func flattenSecurityPolicyRuleRateLimitOptions(conf *compute.SecurityPolicyRuleR
806807
}
807808

808809
data := map[string]interface{}{
809-
"ban_threshold": flattenThreshold(conf.BanThreshold),
810-
"rate_limit_threshold": flattenThreshold(conf.RateLimitThreshold),
811-
"exceed_action": conf.ExceedAction,
812-
"conform_action": conf.ConformAction,
813-
"enforce_on_key": conf.EnforceOnKey,
814-
"enforce_on_key_name": conf.EnforceOnKeyName,
815-
"ban_duration_sec": conf.BanDurationSec,
810+
"ban_threshold": flattenThreshold(conf.BanThreshold),
811+
"rate_limit_threshold": flattenThreshold(conf.RateLimitThreshold),
812+
"exceed_action": conf.ExceedAction,
813+
"conform_action": conf.ConformAction,
814+
"enforce_on_key": conf.EnforceOnKey,
815+
"enforce_on_key_name": conf.EnforceOnKeyName,
816+
"ban_duration_sec": conf.BanDurationSec,
817+
"exceed_redirect_options": flattenSecurityPolicyRedirectOptions(conf.ExceedRedirectOptions),
816818
}
817819

818820
return []map[string]interface{}{data}

google-beta/resource_compute_security_policy_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,28 @@ func TestAccComputeSecurityPolicy_withRateLimitOptions(t *testing.T) {
164164
})
165165
}
166166

167+
func TestAccComputeSecurityPolicy_withRateLimitWithRedirectOptions(t *testing.T) {
168+
t.Parallel()
169+
170+
spName := fmt.Sprintf("tf-test-%s", randString(t, 10))
171+
172+
vcrTest(t, resource.TestCase{
173+
PreCheck: func() { testAccPreCheck(t) },
174+
Providers: testAccProviders,
175+
CheckDestroy: testAccCheckComputeSecurityPolicyDestroyProducer(t),
176+
Steps: []resource.TestStep{
177+
{
178+
Config: testAccComputeSecurityPolicy_withRateLimitWithRedirectOptions(spName),
179+
},
180+
{
181+
ResourceName: "google_compute_security_policy.policy",
182+
ImportState: true,
183+
ImportStateVerify: true,
184+
},
185+
},
186+
})
187+
}
188+
167189
func testAccCheckComputeSecurityPolicyDestroyProducer(t *testing.T) func(s *terraform.State) error {
168190
return func(s *terraform.State) error {
169191
config := googleProviderConfig(t)
@@ -414,6 +436,53 @@ resource "google_compute_security_policy" "policy" {
414436
`, spName)
415437
}
416438

439+
func testAccComputeSecurityPolicy_withRateLimitWithRedirectOptions(spName string) string {
440+
return fmt.Sprintf(`
441+
resource "google_compute_security_policy" "policy" {
442+
name = "%s"
443+
description = "updated description"
444+
445+
rule {
446+
action = "allow"
447+
priority = "2147483647"
448+
match {
449+
versioned_expr = "SRC_IPS_V1"
450+
config {
451+
src_ip_ranges = ["*"]
452+
}
453+
}
454+
description = "default rule"
455+
}
456+
457+
rule {
458+
action = "throttle"
459+
priority = 100
460+
match {
461+
versioned_expr = "SRC_IPS_V1"
462+
config {
463+
src_ip_ranges = [
464+
"0.0.0.0/32",
465+
]
466+
}
467+
}
468+
rate_limit_options {
469+
conform_action = "allow"
470+
exceed_action = "redirect"
471+
enforce_on_key = "IP"
472+
exceed_redirect_options {
473+
type = "EXTERNAL_302"
474+
target = "https://www.example.com"
475+
}
476+
rate_limit_threshold {
477+
count = 100
478+
interval_sec = 60
479+
}
480+
}
481+
}
482+
}
483+
`, spName)
484+
}
485+
417486
func TestAccComputeSecurityPolicy_withRedirectOptionsRecaptcha(t *testing.T) {
418487
t.Parallel()
419488

0 commit comments

Comments
 (0)