provider/google: fix single port diff cycle

When specifying a single port in port_range, the API would accept it as
input, but would return it as {PORT}-{PORT}. Terraform would then see
this as different, even though (semantically) it's the same.

This commit adds a test that exposes the diff cycle created by this, and
an inline DiffSuppressFunc to resolve it.

Fixes #9051.
This commit is contained in:
Paddy 2017-03-13 16:56:25 -07:00
parent 4d6242dfe0
commit 023ede0c26
2 changed files with 43 additions and 0 deletions

View File

@ -76,6 +76,12 @@ func resourceComputeForwardingRule() *schema.Resource {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if old == new+"-"+new {
return true
}
return false
},
},
"ports": &schema.Schema{

View File

@ -29,6 +29,26 @@ func TestAccComputeForwardingRule_basic(t *testing.T) {
})
}
func TestAccComputeForwardingRule_singlePort(t *testing.T) {
poolName := fmt.Sprintf("tf-%s", acctest.RandString(10))
ruleName := fmt.Sprintf("tf-%s", acctest.RandString(10))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeForwardingRuleDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccComputeForwardingRule_singlePort(poolName, ruleName),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeForwardingRuleExists(
"google_compute_forwarding_rule.foobar"),
),
},
},
})
}
func TestAccComputeForwardingRule_ip(t *testing.T) {
addrName := fmt.Sprintf("tf-%s", acctest.RandString(10))
poolName := fmt.Sprintf("tf-%s", acctest.RandString(10))
@ -133,6 +153,23 @@ resource "google_compute_forwarding_rule" "foobar" {
`, poolName, ruleName)
}
func testAccComputeForwardingRule_singlePort(poolName, ruleName string) string {
return fmt.Sprintf(`
resource "google_compute_target_pool" "foobar-tp" {
description = "Resource created for Terraform acceptance testing"
instances = ["us-central1-a/foo", "us-central1-b/bar"]
name = "%s"
}
resource "google_compute_forwarding_rule" "foobar" {
description = "Resource created for Terraform acceptance testing"
ip_protocol = "UDP"
name = "%s"
port_range = "80"
target = "${google_compute_target_pool.foobar-tp.self_link}"
}
`, poolName, ruleName)
}
func testAccComputeForwardingRule_ip(addrName, poolName, ruleName string) string {
return fmt.Sprintf(`
resource "google_compute_address" "foo" {