Skip to content

Commit

Permalink
Add support for enableIndependentEndpointMapping to router nat (#4324) (
Browse files Browse the repository at this point in the history
#8049)

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Dec 17, 2020
1 parent efc7f9e commit 9e6d959
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/4324.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
compute: added support for `enable_independent_endpoint_mapping` to `google_compute_router_nat` resource
```
30 changes: 30 additions & 0 deletions google/resource_compute_router_nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ valid static external IPs that have been assigned to the NAT.`,
},
// Default schema.HashSchema is used.
},
"enable_endpoint_independent_mapping": {
Type: schema.TypeBool,
Optional: true,
Description: `Specifies if endpoint independent mapping is enabled. This is enabled by default. For more information
see the [official documentation](https://cloud.google.com/nat/docs/overview#specs-rfcs).`,
Default: true,
},
"icmp_idle_timeout_sec": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -380,6 +387,12 @@ func resourceComputeRouterNatCreate(d *schema.ResourceData, meta interface{}) er
} else if v, ok := d.GetOkExists("log_config"); !isEmptyValue(reflect.ValueOf(logConfigProp)) && (ok || !reflect.DeepEqual(v, logConfigProp)) {
obj["logConfig"] = logConfigProp
}
enableEndpointIndependentMappingProp, err := expandNestedComputeRouterNatEnableEndpointIndependentMapping(d.Get("enable_endpoint_independent_mapping"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("enable_endpoint_independent_mapping"); ok || !reflect.DeepEqual(v, enableEndpointIndependentMappingProp) {
obj["enableEndpointIndependentMapping"] = enableEndpointIndependentMappingProp
}

lockName, err := replaceVars(d, config, "router/{{region}}/{{router}}")
if err != nil {
Expand Down Expand Up @@ -521,6 +534,9 @@ func resourceComputeRouterNatRead(d *schema.ResourceData, meta interface{}) erro
if err := d.Set("log_config", flattenNestedComputeRouterNatLogConfig(res["logConfig"], d, config)); err != nil {
return fmt.Errorf("Error reading RouterNat: %s", err)
}
if err := d.Set("enable_endpoint_independent_mapping", flattenNestedComputeRouterNatEnableEndpointIndependentMapping(res["enableEndpointIndependentMapping"], d, config)); err != nil {
return fmt.Errorf("Error reading RouterNat: %s", err)
}

return nil
}
Expand Down Expand Up @@ -607,6 +623,12 @@ func resourceComputeRouterNatUpdate(d *schema.ResourceData, meta interface{}) er
} else if v, ok := d.GetOkExists("log_config"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, logConfigProp)) {
obj["logConfig"] = logConfigProp
}
enableEndpointIndependentMappingProp, err := expandNestedComputeRouterNatEnableEndpointIndependentMapping(d.Get("enable_endpoint_independent_mapping"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("enable_endpoint_independent_mapping"); ok || !reflect.DeepEqual(v, enableEndpointIndependentMappingProp) {
obj["enableEndpointIndependentMapping"] = enableEndpointIndependentMappingProp
}

lockName, err := replaceVars(d, config, "router/{{region}}/{{router}}")
if err != nil {
Expand Down Expand Up @@ -892,6 +914,10 @@ func flattenNestedComputeRouterNatLogConfigFilter(v interface{}, d *schema.Resou
return v
}

func flattenNestedComputeRouterNatEnableEndpointIndependentMapping(v interface{}, d *schema.ResourceData, config *Config) interface{} {
return v
}

func expandNestedComputeRouterNatName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down Expand Up @@ -1047,6 +1073,10 @@ func expandNestedComputeRouterNatLogConfigFilter(v interface{}, d TerraformResou
return v, nil
}

func expandNestedComputeRouterNatEnableEndpointIndependentMapping(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func flattenNestedComputeRouterNat(d *schema.ResourceData, meta interface{}, res map[string]interface{}) (map[string]interface{}, error) {
var v interface{}
var ok bool
Expand Down
83 changes: 83 additions & 0 deletions google/resource_compute_router_nat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,45 @@ func TestAccComputeRouterNat_withManualIpAndSubnetConfiguration(t *testing.T) {
})
}

func TestAccComputeRouterNat_withDisabledIndependentEndpointMapping(t *testing.T) {
t.Parallel()

testId := randString(t, 10)
routerName := fmt.Sprintf("tf-test-router-nat-%s", testId)

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeRouterNatDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeRouterNatWithDisabledIndependentEndpointMapping(routerName, true),
},
{
ResourceName: "google_compute_router_nat.foobar",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccComputeRouterNatWithDisabledIndependentEndpointMapping(routerName, false),
},
{
ResourceName: "google_compute_router_nat.foobar",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccComputeRouterNatWithDisabledIndependentEndpointMapping(routerName, true),
},
{
ResourceName: "google_compute_router_nat.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckComputeRouterNatDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
config := googleProviderConfig(t)
Expand Down Expand Up @@ -364,6 +403,50 @@ resource "google_compute_router_nat" "foobar" {
`, routerName, routerName, routerName, routerName, routerName)
}

func testAccComputeRouterNatWithDisabledIndependentEndpointMapping(routerName string, enabled bool) string {
return fmt.Sprintf(`
resource "google_compute_network" "foobar" {
name = "%s-net"
auto_create_subnetworks = "false"
}
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 = "router-nat-%s-addr"
region = google_compute_subnetwork.foobar.region
}
resource "google_compute_router" "foobar" {
name = "%s"
region = google_compute_subnetwork.foobar.region
network = google_compute_network.foobar.self_link
bgp {
asn = 64514
}
}
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.self_link]
source_subnetwork_ip_ranges_to_nat = "LIST_OF_SUBNETWORKS"
subnetwork {
name = google_compute_subnetwork.foobar.name
source_ip_ranges_to_nat = ["ALL_IP_RANGES"]
}
enable_endpoint_independent_mapping = %t
}
`, routerName, routerName, routerName, routerName, routerName, enabled)
}

func testAccComputeRouterNatKeepRouter(routerName string) string {
return fmt.Sprintf(`
resource "google_compute_network" "foobar" {
Expand Down
5 changes: 5 additions & 0 deletions website/docs/r/compute_router_nat.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ The following arguments are supported:
Configuration for logging on NAT
Structure is documented below.

* `enable_endpoint_independent_mapping` -
(Optional)
Specifies if endpoint independent mapping is enabled. This is enabled by default. For more information
see the [official documentation](https://cloud.google.com/nat/docs/overview#specs-rfcs).

* `region` -
(Optional)
Region where the router and NAT reside.
Expand Down

0 comments on commit 9e6d959

Please sign in to comment.