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

Custom hash function for router_nat.subnetwork #1194

Merged
merged 1 commit into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 28 additions & 2 deletions google-beta/resource_compute_router_nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,32 @@ 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
}

func resourceComputeRouterNat() *schema.Resource {
return &schema.Resource{
Create: resourceComputeRouterNatCreate,
Expand Down Expand Up @@ -176,7 +202,7 @@ func resourceComputeRouterNat() *schema.Resource {
Type: schema.TypeSet,
Optional: true,
Elem: computeRouterNatSubnetworkSchema(),
// Default schema.HashSchema is used.
Set: computeRouterNatSubnetworkHash,
},
"tcp_established_idle_timeout_sec": {
Type: schema.TypeInt,
Expand Down Expand Up @@ -657,7 +683,7 @@ func flattenComputeRouterNatSubnetwork(v interface{}, d *schema.ResourceData) in
return v
}
l := v.([]interface{})
transformed := schema.NewSet(schema.HashResource(computeRouterNatSubnetworkSchema()), []interface{}{})
transformed := schema.NewSet(computeRouterNatSubnetworkHash, []interface{}{})
for _, raw := range l {
original := raw.(map[string]interface{})
if len(original) < 1 {
Expand Down
2 changes: 1 addition & 1 deletion google-beta/resource_compute_router_nat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,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