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

fix an issue in InternalIpDiffSuppress #9062

Merged
merged 3 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,31 @@ func TestAccComputeForwardingRule_forwardingRuleRegionalSteeringExampleUpdate(t
})
}

func TestAccComputeForwardingRule_forwardingRuleIpAddressIpv6(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: testAccCheckComputeForwardingRuleDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeForwardingRule_forwardingRuleIpAddressIpv6(context),
},
{
ResourceName: "google_compute_forwarding_rule.external",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"backend_service", "network", "subnetwork", "region"},
},
},
})
}

func testAccComputeForwardingRule_basic(poolName, ruleName string) string {
return fmt.Sprintf(`
resource "google_compute_target_pool" "foo-tp" {
Expand Down Expand Up @@ -697,3 +722,45 @@ resource "google_compute_forwarding_rule" "external" {
}
`, context)
}

func testAccComputeForwardingRule_forwardingRuleIpAddressIpv6(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_compute_address" "basic" {
name = "tf-test-address%{random_suffix}"
region = "us-central1"

address_type = "EXTERNAL"
ipv6_endpoint_type = "NETLB"
ip_version = "IPV6"
subnetwork = google_compute_subnetwork.subnetwork-ipv6.id
}

resource "google_compute_region_backend_service" "external" {
name = "tf-test-backend%{random_suffix}"
region = "us-central1"
load_balancing_scheme = "EXTERNAL"
}

resource "google_compute_forwarding_rule" "external" {
name = "tf-test-forwarding-rule%{random_suffix}"
region = "us-central1"
ip_address = google_compute_address.basic.self_link
backend_service = google_compute_region_backend_service.external.self_link
load_balancing_scheme = "EXTERNAL"
}

resource "google_compute_subnetwork" "subnetwork-ipv6" {
name = "tf-test-subnetwork%{random_suffix}"
ip_cidr_range = "10.0.0.0/22"
region = "us-central1"
stack_type = "IPV4_IPV6"
ipv6_access_type = "EXTERNAL"
network = google_compute_network.custom-test.id
}

resource "google_compute_network" "custom-test" {
name = "tf-test-network%{random_suffix}"
auto_create_subnetworks = false
}
`, context)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strconv"
"strings"
"time"
"bytes"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -178,9 +179,32 @@ func TimestampDiffSuppress(format string) schema.SchemaDiffSuppressFunc {
}
}

// suppress diff when saved is Ipv4 format while new is required a reference
// suppress diff when saved is Ipv4 and Ipv6 format while new is required a reference
// this happens for an internal ip for Private Services Connect
func InternalIpDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
olds := strings.Split(old, "/")
news := strings.Split(new, "/")

if len(olds) == 2 {
if len(news) == 2 {
if bytes.Equal(net.ParseIP(olds[0]), net.ParseIP(news[0])) && olds[1] == news[1] {
edwardmedia marked this conversation as resolved.
Show resolved Hide resolved
return true
} else {
return false
}
} else {
return (net.ParseIP(olds[0]) != nil) && (net.ParseIP(new) == nil)
}
}

if (net.ParseIP(old) != nil) && (net.ParseIP(new) != nil) {
if bytes.Equal(net.ParseIP(old), net.ParseIP(new)) {
edwardmedia marked this conversation as resolved.
Show resolved Hide resolved
return true
} else {
return false
}
}

return (net.ParseIP(old) != nil) && (net.ParseIP(new) == nil)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,70 @@ func TestDurationDiffSuppress(t *testing.T) {
}
}

func TestInternalIpDiffSuppress(t *testing.T) {
cases := map[string]struct {
Old, New string
ExpectDiffSuppress bool
}{
"same1 ipv6s": {
Old: "2600:1900:4020:31cd:8000:0:0:0",
New: "2600:1900:4020:31cd:8000::",
ExpectDiffSuppress: true,
},
"same2 ipv6s": {
Old: "2600:1900:4020:31cd:8000:0:0:0/96",
New: "2600:1900:4020:31cd:8000::/96",
ExpectDiffSuppress: true,
},
"same3 ipv6s": {
Old: "2600:1900:4020:31cd:8000:0:0:0/96",
New: "https://www.googleapis.com/compute/v1/projects/myproject/regions/us-central1/addresses/myaddress",
ExpectDiffSuppress: true,
},
"different1 ipv6s": {
Old: "2600:1900:4020:31cd:8000:0:0:0",
New: "2600:1900:4020:31cd:8001::",
ExpectDiffSuppress: false,
},
"different2 ipv6s": {
Old: "2600:1900:4020:31cd:8000:0:0:0",
New: "2600:1900:4020:31cd:8000:0:0:8000",
ExpectDiffSuppress: false,
},
"different3 ipv6s": {
Old: "2600:1900:4020:31cd:8000:0:0:0/96",
New: "2600:1900:4020:31cd:8001::/96",
ExpectDiffSuppress: false,
},
"different ipv4s": {
Old: "1.2.3.4",
New: "1.2.3.5",
ExpectDiffSuppress: false,
},
"same ipv4s": {
Old: "1.2.3.4",
New: "1.2.3.4",
ExpectDiffSuppress: true,
},
"ipv4 vs id": {
Old: "1.2.3.4",
New: "google_compute_address.my_ipv4_addr.address",
ExpectDiffSuppress: true,
},
"ipv6 vs id": {
Old: "2600:1900:4020:31cd:8000:0:0:0",
New: "google_compute_address.my_ipv6_addr.address",
ExpectDiffSuppress: true,
},
}

for tn, tc := range cases {
if InternalIpDiffSuppress("ipv4/v6_compare", tc.Old, tc.New, nil) != tc.ExpectDiffSuppress {
t.Fatalf("bad: %s, '%s' => '%s' expect %t", tn, tc.Old, tc.New, tc.ExpectDiffSuppress)
}
}
}

func TestLastSlashDiffSuppress(t *testing.T) {
cases := map[string]struct {
Old, New string
Expand Down