Skip to content

Commit

Permalink
hashing function added for natIps (#4537)
Browse files Browse the repository at this point in the history
* hashing function added for natIps

* test cases added to refer NAT Ips by id and name
  • Loading branch information
venkykuberan authored Feb 26, 2021
1 parent 137dbdf commit 2a0a1fa
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
1 change: 1 addition & 0 deletions mmv1/products/compute/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions mmv1/templates/terraform/constants/router_nat.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
Expand Down Expand Up @@ -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" {
Expand Down

0 comments on commit 2a0a1fa

Please sign in to comment.