From 9b7844d7a2d2dc87d88fad59ca497432ff2d5ff2 Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Fri, 26 Feb 2021 22:40:10 +0000 Subject: [PATCH] hashing function added for natIps (#4537) * hashing function added for natIps * test cases added to refer NAT Ips by id and name Signed-off-by: Modular Magician --- .changelog/4537.txt | 4 + google-beta/resource_compute_router_nat.go | 12 +- .../resource_compute_router_nat_test.go | 118 ++++++++++++++++++ 3 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 .changelog/4537.txt diff --git a/.changelog/4537.txt b/.changelog/4537.txt new file mode 100644 index 0000000000..d759624985 --- /dev/null +++ b/.changelog/4537.txt @@ -0,0 +1,4 @@ +```release-note:bug +compute : fixed a perma-diff for `nat_ips` that were specified as short forms in `google_compute_router_nat` + +``` diff --git a/google-beta/resource_compute_router_nat.go b/google-beta/resource_compute_router_nat.go index 9fbb5ff322..a0c9e73afb 100644 --- a/google-beta/resource_compute_router_nat.go +++ b/google-beta/resource_compute_router_nat.go @@ -20,6 +20,7 @@ import ( "log" "reflect" "strconv" + "strings" "time" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -104,6 +105,15 @@ func computeRouterNatSubnetworkHash(v interface{}) int { return schema.HashString(NameFromSelfLinkStateFunc(name)) + sourceIpRangesHash + secondaryIpRangeHash } +func computeRouterNatIPsHash(v interface{}) int { + val := (v.(string)) + newParts := strings.Split(val, "/") + if len(newParts) == 1 { + return schema.HashString(newParts[0]) + } + return schema.HashString(GetResourceNameFromSelfLink(val)) +} + func resourceComputeRouterNat() *schema.Resource { return &schema.Resource{ Create: resourceComputeRouterNatCreate, @@ -221,7 +231,7 @@ is set to MANUAL_ONLY.`, Type: schema.TypeString, DiffSuppressFunc: compareSelfLinkOrResourceName, }, - // Default schema.HashSchema is used. + Set: computeRouterNatIPsHash, }, "region": { Type: schema.TypeString, diff --git a/google-beta/resource_compute_router_nat_test.go b/google-beta/resource_compute_router_nat_test.go index de34a37b9e..19db8fefba 100644 --- a/google-beta/resource_compute_router_nat_test.go +++ b/google-beta/resource_compute_router_nat_test.go @@ -86,6 +86,22 @@ func TestAccComputeRouterNat_update(t *testing.T) { ImportState: true, ImportStateVerify: true, }, + { + Config: testAccComputeRouterNatUpdateToNatIPsId(routerName), + }, + { + ResourceName: "google_compute_router_nat.foobar", + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccComputeRouterNatUpdateToNatIPsName(routerName), + }, + { + ResourceName: "google_compute_router_nat.foobar", + ImportState: true, + ImportStateVerify: true, + }, { Config: testAccComputeRouterNatBasicBeforeUpdate(routerName), }, @@ -409,6 +425,108 @@ resource "google_compute_router_nat" "foobar" { `, routerName, routerName, routerName, routerName, routerName) } +func testAccComputeRouterNatUpdateToNatIPsId(routerName string) string { + return fmt.Sprintf(` +resource "google_compute_router" "foobar" { +name = "%s" +region = google_compute_subnetwork.foobar.region +network = google_compute_network.foobar.self_link +} + +resource "google_compute_network" "foobar" { +name = "%s-net" +} +resource "google_compute_subnetwork" "foobar" { +name = "%s-subnet" +network = google_compute_network.foobar.self_link +ip_cidr_range = "10.0.0.0/16" +region = "us-central1" +} + +resource "google_compute_address" "foobar" { +name = "%s-addr" +region = google_compute_subnetwork.foobar.region +} + +resource "google_compute_router_nat" "foobar" { + name = "%s" + router = google_compute_router.foobar.name + region = google_compute_router.foobar.region + + nat_ip_allocate_option = "MANUAL_ONLY" + nat_ips = [google_compute_address.foobar.id] + + source_subnetwork_ip_ranges_to_nat = "LIST_OF_SUBNETWORKS" + + subnetwork { + name = google_compute_subnetwork.foobar.self_link + source_ip_ranges_to_nat = ["ALL_IP_RANGES"] + } + + udp_idle_timeout_sec = 60 + icmp_idle_timeout_sec = 60 + tcp_established_idle_timeout_sec = 1600 + tcp_transitory_idle_timeout_sec = 60 + + log_config { + enable = true + filter = "TRANSLATIONS_ONLY" + } +} +`, routerName, routerName, routerName, routerName, routerName) +} + +func testAccComputeRouterNatUpdateToNatIPsName(routerName string) string { + return fmt.Sprintf(` +resource "google_compute_router" "foobar" { +name = "%s" +region = google_compute_subnetwork.foobar.region +network = google_compute_network.foobar.self_link +} + +resource "google_compute_network" "foobar" { +name = "%s-net" +} +resource "google_compute_subnetwork" "foobar" { +name = "%s-subnet" +network = google_compute_network.foobar.self_link +ip_cidr_range = "10.0.0.0/16" +region = "us-central1" +} + +resource "google_compute_address" "foobar" { +name = "%s-addr" +region = google_compute_subnetwork.foobar.region +} + +resource "google_compute_router_nat" "foobar" { + name = "%s" + router = google_compute_router.foobar.name + region = google_compute_router.foobar.region + + nat_ip_allocate_option = "MANUAL_ONLY" + nat_ips = [google_compute_address.foobar.name] + + source_subnetwork_ip_ranges_to_nat = "LIST_OF_SUBNETWORKS" + + subnetwork { + name = google_compute_subnetwork.foobar.self_link + source_ip_ranges_to_nat = ["ALL_IP_RANGES"] + } + + udp_idle_timeout_sec = 60 + icmp_idle_timeout_sec = 60 + tcp_established_idle_timeout_sec = 1600 + tcp_transitory_idle_timeout_sec = 60 + + log_config { + enable = true + filter = "TRANSLATIONS_ONLY" + } +} +`, routerName, routerName, routerName, routerName, routerName) +} + func testAccComputeRouterNatWithManualIpAndSubnetConfiguration(routerName string) string { return fmt.Sprintf(` resource "google_compute_network" "foobar" {