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

Add DynamicPortAllocation for Cloud NAT #4316

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
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 @@ -106,9 +106,9 @@ resource "google_clouddeploy_delivery_pipeline" "primary" {
name = "tf-test-pipeline%{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 Down
8 changes: 4 additions & 4 deletions google-beta/resource_clouddeploy_target_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,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 Down Expand Up @@ -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-beta/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-beta/resource_compute_router_nat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,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 @@ -180,23 +180,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 @@ -601,7 +609,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 @@ -641,8 +649,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 testAccComputeRouterNatBaseResourcesWithNatIps(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