Skip to content

Commit

Permalink
hashing function added for natIps (#4537) (#8576)
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

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Feb 26, 2021
1 parent eec4d61 commit cd3e229
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .changelog/4537.txt
Original file line number Diff line number Diff line change
@@ -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`

```
12 changes: 11 additions & 1 deletion google/resource_compute_router_nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"log"
"reflect"
"strconv"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
118 changes: 118 additions & 0 deletions google/resource_compute_router_nat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,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 @@ -360,6 +376,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 cd3e229

Please sign in to comment.