From 2a0a1fa157d20c39196cf396efc92b4dce600373 Mon Sep 17 00:00:00 2001 From: venkykuberan Date: Fri, 26 Feb 2021 14:31:37 -0800 Subject: [PATCH] hashing function added for natIps (#4537) * hashing function added for natIps * test cases added to refer NAT Ips by id and name --- mmv1/products/compute/terraform.yaml | 1 + .../terraform/constants/router_nat.go.erb | 9 ++ .../resource_compute_router_nat_test.go.erb | 118 ++++++++++++++++++ 3 files changed, 128 insertions(+) diff --git a/mmv1/products/compute/terraform.yaml b/mmv1/products/compute/terraform.yaml index 5ae74b8dddf7..baca77677f3b 100644 --- a/mmv1/products/compute/terraform.yaml +++ b/mmv1/products/compute/terraform.yaml @@ -2104,6 +2104,7 @@ overrides: !ruby/object:Overrides::ResourceOverrides function: 'validateRFC1035Name(2, 63)' natIps: !ruby/object:Overrides::Terraform::PropertyOverride is_set: true + set_hash_func: computeRouterNatIPsHash drainNatIps: !ruby/object:Overrides::Terraform::PropertyOverride is_set: true subnetwork: !ruby/object:Overrides::Terraform::PropertyOverride diff --git a/mmv1/templates/terraform/constants/router_nat.go.erb b/mmv1/templates/terraform/constants/router_nat.go.erb index 88f79c9e5fb3..c27bea9708ba 100644 --- a/mmv1/templates/terraform/constants/router_nat.go.erb +++ b/mmv1/templates/terraform/constants/router_nat.go.erb @@ -88,3 +88,12 @@ 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)) +} \ No newline at end of file diff --git a/mmv1/third_party/terraform/tests/resource_compute_router_nat_test.go.erb b/mmv1/third_party/terraform/tests/resource_compute_router_nat_test.go.erb index a5bc0f677313..07a2a8c474c2 100644 --- a/mmv1/third_party/terraform/tests/resource_compute_router_nat_test.go.erb +++ b/mmv1/third_party/terraform/tests/resource_compute_router_nat_test.go.erb @@ -87,6 +87,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), }, @@ -413,6 +429,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" {