Skip to content

Commit

Permalink
Add DynamicPortAllocation for Cloud NAT (#6022) (#11707)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored May 16, 2022
1 parent 874d912 commit 89b1e35
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .changelog/6022.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
compute: Added `enable_dynamic_port_allocation` to `google_compute_router_nat`
```
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ resource "google_clouddeploy_delivery_pipeline" "primary" {
description = "basic description"
labels = {
my_first_label = "example-label-1"
my_second_label = "example-label-2"
my_first_label = "example-label-1"
}
project = "%{project_name}"
Expand Down
12 changes: 6 additions & 6 deletions google/resource_clouddeploy_target_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ resource "google_clouddeploy_target" "primary" {
}
labels = {
my_second_label = "updated-example-label-2"
my_third_label = "example-label-3"
my_second_label = "updated-example-label-2"
}
project = "%{project_name}"
Expand All @@ -241,9 +241,9 @@ resource "google_clouddeploy_target" "primary" {
name = "tf-test-target%{random_suffix}"
annotations = {
my_second_annotation = "updated-example-annotation-2"
my_third_annotation = "example-annotation-3"
my_second_annotation = "updated-example-annotation-2"
}
description = "updated description"
Expand All @@ -267,9 +267,9 @@ resource "google_clouddeploy_target" "primary" {
}
labels = {
my_second_label = "updated-example-label-2"
my_third_label = "example-label-3"
my_second_label = "updated-example-label-2"
}
project = "%{project_name}"
Expand Down
33 changes: 33 additions & 0 deletions google/resource_compute_router_nat.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ valid static external IPs that have been assigned to the NAT.`,
},
// Default schema.HashSchema is used.
},
"enable_dynamic_port_allocation": {
Type: schema.TypeBool,
Optional: true,
Description: `Enable Dynamic Port Allocation.
If minPorts is set, minPortsPerVm must be set to a power of two greater than or equal to 32.
If minPortsPerVm is not set, a minimum of 32 ports will be allocated to a VM from this NAT config.
Mutually exclusive with enableEndpointIndependentMapping.`,
Default: false,
},
"enable_endpoint_independent_mapping": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -366,6 +376,12 @@ func resourceComputeRouterNatCreate(d *schema.ResourceData, meta interface{}) er
} else if v, ok := d.GetOkExists("min_ports_per_vm"); !isEmptyValue(reflect.ValueOf(minPortsPerVmProp)) && (ok || !reflect.DeepEqual(v, minPortsPerVmProp)) {
obj["minPortsPerVm"] = minPortsPerVmProp
}
enableDynamicPortAllocationProp, err := expandNestedComputeRouterNatEnableDynamicPortAllocation(d.Get("enable_dynamic_port_allocation"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("enable_dynamic_port_allocation"); !isEmptyValue(reflect.ValueOf(enableDynamicPortAllocationProp)) && (ok || !reflect.DeepEqual(v, enableDynamicPortAllocationProp)) {
obj["enableDynamicPortAllocation"] = enableDynamicPortAllocationProp
}
udpIdleTimeoutSecProp, err := expandNestedComputeRouterNatUdpIdleTimeoutSec(d.Get("udp_idle_timeout_sec"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -528,6 +544,9 @@ func resourceComputeRouterNatRead(d *schema.ResourceData, meta interface{}) erro
if err := d.Set("min_ports_per_vm", flattenNestedComputeRouterNatMinPortsPerVm(res["minPortsPerVm"], d, config)); err != nil {
return fmt.Errorf("Error reading RouterNat: %s", err)
}
if err := d.Set("enable_dynamic_port_allocation", flattenNestedComputeRouterNatEnableDynamicPortAllocation(res["enableDynamicPortAllocation"], d, config)); err != nil {
return fmt.Errorf("Error reading RouterNat: %s", err)
}
if err := d.Set("udp_idle_timeout_sec", flattenNestedComputeRouterNatUdpIdleTimeoutSec(res["udpIdleTimeoutSec"], d, config)); err != nil {
return fmt.Errorf("Error reading RouterNat: %s", err)
}
Expand Down Expand Up @@ -602,6 +621,12 @@ func resourceComputeRouterNatUpdate(d *schema.ResourceData, meta interface{}) er
} else if v, ok := d.GetOkExists("min_ports_per_vm"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, minPortsPerVmProp)) {
obj["minPortsPerVm"] = minPortsPerVmProp
}
enableDynamicPortAllocationProp, err := expandNestedComputeRouterNatEnableDynamicPortAllocation(d.Get("enable_dynamic_port_allocation"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("enable_dynamic_port_allocation"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, enableDynamicPortAllocationProp)) {
obj["enableDynamicPortAllocation"] = enableDynamicPortAllocationProp
}
udpIdleTimeoutSecProp, err := expandNestedComputeRouterNatUdpIdleTimeoutSec(d.Get("udp_idle_timeout_sec"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -844,6 +869,10 @@ func flattenNestedComputeRouterNatMinPortsPerVm(v interface{}, d *schema.Resourc
return v // let terraform core handle it otherwise
}

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

func flattenNestedComputeRouterNatUdpIdleTimeoutSec(v interface{}, d *schema.ResourceData, config *Config) interface{} {
if v == nil || isEmptyValue(reflect.ValueOf(v)) {
return 30
Expand Down Expand Up @@ -1032,6 +1061,10 @@ func expandNestedComputeRouterNatMinPortsPerVm(v interface{}, d TerraformResourc
return v, nil
}

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

func expandNestedComputeRouterNatUdpIdleTimeoutSec(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
Expand Down
21 changes: 15 additions & 6 deletions google/resource_compute_router_nat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func TestAccComputeRouterNat_withManualIpAndSubnetConfiguration(t *testing.T) {
})
}

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

testId := randString(t, 10)
Expand All @@ -179,23 +179,31 @@ func TestAccComputeRouterNat_withDisabledIndependentEndpointMapping(t *testing.T
CheckDestroy: testAccCheckComputeRouterNatDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeRouterNatWithDisabledIndependentEndpointMapping(routerName, true),
Config: testAccComputeRouterNatWithAllocationMethod(routerName, true, false),
},
{
ResourceName: "google_compute_router_nat.foobar",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccComputeRouterNatWithDisabledIndependentEndpointMapping(routerName, false),
Config: testAccComputeRouterNatWithAllocationMethod(routerName, false, false),
},
{
ResourceName: "google_compute_router_nat.foobar",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccComputeRouterNatWithDisabledIndependentEndpointMapping(routerName, true),
Config: testAccComputeRouterNatWithAllocationMethod(routerName, true, false),
},
{
ResourceName: "google_compute_router_nat.foobar",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccComputeRouterNatWithAllocationMethod(routerName, false, true),
},
{
ResourceName: "google_compute_router_nat.foobar",
Expand Down Expand Up @@ -552,7 +560,7 @@ resource "google_compute_router_nat" "foobar" {
`, routerName, routerName, routerName, routerName, routerName)
}

func testAccComputeRouterNatWithDisabledIndependentEndpointMapping(routerName string, enabled bool) string {
func testAccComputeRouterNatWithAllocationMethod(routerName string, enableEndpointIndependentMapping, enableDynamicPortAllocation bool) string {
return fmt.Sprintf(`
resource "google_compute_network" "foobar" {
name = "%s-net"
Expand Down Expand Up @@ -592,8 +600,9 @@ resource "google_compute_router_nat" "foobar" {
source_ip_ranges_to_nat = ["ALL_IP_RANGES"]
}
enable_endpoint_independent_mapping = %t
enable_dynamic_port_allocation = %t
}
`, routerName, routerName, routerName, routerName, routerName, enabled)
`, routerName, routerName, routerName, routerName, routerName, enableEndpointIndependentMapping, enableDynamicPortAllocation)
}

func testAccComputeRouterNatKeepRouter(routerName string) string {
Expand Down
7 changes: 7 additions & 0 deletions website/docs/r/compute_router_nat.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ The following arguments are supported:
(Optional)
Minimum number of ports allocated to a VM from this NAT.

* `enable_dynamic_port_allocation` -
(Optional)
Enable Dynamic Port Allocation.
If minPorts is set, minPortsPerVm must be set to a power of two greater than or equal to 32.
If minPortsPerVm is not set, a minimum of 32 ports will be allocated to a VM from this NAT config.
Mutually exclusive with enableEndpointIndependentMapping.

* `udp_idle_timeout_sec` -
(Optional)
Timeout (in seconds) for UDP connections. Defaults to 30s if not set.
Expand Down

0 comments on commit 89b1e35

Please sign in to comment.