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

hashing function added for natIps #3007

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
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-beta/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-beta/resource_compute_router_nat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,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 @@ -409,6 +425,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