Skip to content

Commit

Permalink
Custom hash function for router_nat.subnetwork (#2379)
Browse files Browse the repository at this point in the history
Merged PR #2379.
  • Loading branch information
slevenick authored and modular-magician committed Sep 25, 2019
1 parent 07bcde2 commit dcc8b59
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion build/terraform
2 changes: 1 addition & 1 deletion build/terraform-beta
1 change: 1 addition & 0 deletions products/compute/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,7 @@ overrides: !ruby/object:Overrides::ResourceOverrides
is_set: true
subnetwork: !ruby/object:Overrides::Terraform::PropertyOverride
is_set: true
set_hash_func: computeRouterNatSubnetworkHash
subnetwork.sourceIpRangesToNat: !ruby/object:Overrides::Terraform::PropertyOverride
is_set: true
subnetwork.secondaryIpRangeNames: !ruby/object:Overrides::Terraform::PropertyOverride
Expand Down
26 changes: 26 additions & 0 deletions templates/terraform/constants/router_nat.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,29 @@ func resourceComputeRouterNatDrainNatIpsCustomDiff(diff *schema.ResourceDiff, me
}
return nil
}

func computeRouterNatSubnetworkHash(v interface{}) int {
obj := v.(map[string]interface{})
name := obj["name"]
sourceIpRanges := obj["source_ip_ranges_to_nat"]
sourceIpRangesHash := 0
if sourceIpRanges != nil {
sourceIpSet := sourceIpRanges.(*schema.Set)

for _, ipRange := range sourceIpSet.List() {
sourceIpRangesHash += schema.HashString(ipRange.(string))
}
}

secondaryIpRangeNames := obj["secondary_ip_range_names"]
secondaryIpRangeHash := 0
if secondaryIpRangeNames != nil {
secondaryIpRangeSet := secondaryIpRangeNames.(*schema.Set)

for _, secondaryIp := range secondaryIpRangeSet.List() {
secondaryIpRangeHash += schema.HashString(secondaryIp.(string))
}
}

return schema.HashString(NameFromSelfLinkStateFunc(name)) + sourceIpRangesHash + secondaryIpRangeHash
}
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ resource "google_compute_router_nat" "foobar" {
nat_ips = ["${google_compute_address.foobar.self_link}"]
source_subnetwork_ip_ranges_to_nat = "LIST_OF_SUBNETWORKS"
subnetwork {
name = "${google_compute_subnetwork.foobar.self_link}"
name = "${google_compute_subnetwork.foobar.name}"
source_ip_ranges_to_nat = ["ALL_IP_RANGES"]
}
}`, testId, testId, testId, testId, testId)
Expand Down

0 comments on commit dcc8b59

Please sign in to comment.