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

add customize diff to prevent two rules with the same priority #5834

Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .changelog/3202.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
compute: fixed `google_compute_security_policy` from allowing two rules with the same priority.
```
17 changes: 17 additions & 0 deletions google/resource_compute_security_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func resourceComputeSecurityPolicy() *schema.Resource {
Importer: &schema.ResourceImporter{
State: resourceSecurityPolicyStateImporter,
},
CustomizeDiff: rulesCustomizeDiff,

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(4 * time.Minute),
Expand Down Expand Up @@ -150,6 +151,22 @@ func resourceComputeSecurityPolicy() *schema.Resource {
}
}

func rulesCustomizeDiff(diff *schema.ResourceDiff, _ interface{}) error {
_, n := diff.GetChange("rule")
nSet := n.(*schema.Set)

nPriorities := map[int64]bool{}
for _, rule := range nSet.List() {
priority := int64(rule.(map[string]interface{})["priority"].(int))
if nPriorities[priority] {
return fmt.Errorf("Two rules have the same priority, please update one of the priorities to be different.")
}
nPriorities[priority] = true
}

return nil
}

func resourceComputeSecurityPolicyCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

Expand Down
52 changes: 52 additions & 0 deletions google/resource_compute_security_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package google

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
Expand Down Expand Up @@ -72,6 +73,11 @@ func TestAccComputeSecurityPolicy_update(t *testing.T) {
ImportStateVerify: true,
},

{
Config: testAccComputeSecurityPolicy_updateSamePriority(spName),
ExpectError: regexp.MustCompile("Two rules have the same priority, please update one of the priorities to be different."),
},

{
Config: testAccComputeSecurityPolicy_update(spName),
},
Expand Down Expand Up @@ -153,6 +159,52 @@ resource "google_compute_security_policy" "policy" {
`, spName)
}

func testAccComputeSecurityPolicy_updateSamePriority(spName string) string {
return fmt.Sprintf(`
resource "google_compute_security_policy" "policy" {
name = "%s"
description = "updated description"

// keep this
rule {
action = "allow"
priority = "2147483647"
match {
versioned_expr = "SRC_IPS_V1"
config {
src_ip_ranges = ["*"]
}
}
description = "default rule"
}

// add this
rule {
action = "deny(403)"
priority = "2000"
match {
versioned_expr = "SRC_IPS_V1"
config {
src_ip_ranges = ["10.0.1.0/24"]
}
}
}

rule {
action = "allow"
priority = "2000"
match {
versioned_expr = "SRC_IPS_V1"
config {
src_ip_ranges = ["10.0.0.0/24"]
}
}
preview = true
}
}
`, spName)
}

func testAccComputeSecurityPolicy_update(spName string) string {
return fmt.Sprintf(`
resource "google_compute_security_policy" "policy" {
Expand Down