Skip to content

Commit 89b1e35

Browse files
Add DynamicPortAllocation for Cloud NAT (#6022) (#11707)
Signed-off-by: Modular Magician <[email protected]>
1 parent 874d912 commit 89b1e35

6 files changed

+66
-14
lines changed

.changelog/6022.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
compute: Added `enable_dynamic_port_allocation` to `google_compute_router_nat`
3+
```

google/resource_clouddeploy_delivery_pipeline_generated_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ resource "google_clouddeploy_delivery_pipeline" "primary" {
7575
description = "basic description"
7676
7777
labels = {
78-
my_first_label = "example-label-1"
79-
8078
my_second_label = "example-label-2"
79+
80+
my_first_label = "example-label-1"
8181
}
8282
8383
project = "%{project_name}"

google/resource_clouddeploy_target_generated_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,9 @@ resource "google_clouddeploy_target" "primary" {
221221
}
222222
223223
labels = {
224-
my_second_label = "updated-example-label-2"
225-
226224
my_third_label = "example-label-3"
225+
226+
my_second_label = "updated-example-label-2"
227227
}
228228
229229
project = "%{project_name}"
@@ -241,9 +241,9 @@ resource "google_clouddeploy_target" "primary" {
241241
name = "tf-test-target%{random_suffix}"
242242
243243
annotations = {
244-
my_second_annotation = "updated-example-annotation-2"
245-
246244
my_third_annotation = "example-annotation-3"
245+
246+
my_second_annotation = "updated-example-annotation-2"
247247
}
248248
249249
description = "updated description"
@@ -267,9 +267,9 @@ resource "google_clouddeploy_target" "primary" {
267267
}
268268
269269
labels = {
270-
my_second_label = "updated-example-label-2"
271-
272270
my_third_label = "example-label-3"
271+
272+
my_second_label = "updated-example-label-2"
273273
}
274274
275275
project = "%{project_name}"

google/resource_compute_router_nat.go

+33
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,16 @@ valid static external IPs that have been assigned to the NAT.`,
181181
},
182182
// Default schema.HashSchema is used.
183183
},
184+
"enable_dynamic_port_allocation": {
185+
Type: schema.TypeBool,
186+
Optional: true,
187+
Description: `Enable Dynamic Port Allocation.
188+
If minPorts is set, minPortsPerVm must be set to a power of two greater than or equal to 32.
189+
If minPortsPerVm is not set, a minimum of 32 ports will be allocated to a VM from this NAT config.
190+
191+
Mutually exclusive with enableEndpointIndependentMapping.`,
192+
Default: false,
193+
},
184194
"enable_endpoint_independent_mapping": {
185195
Type: schema.TypeBool,
186196
Optional: true,
@@ -366,6 +376,12 @@ func resourceComputeRouterNatCreate(d *schema.ResourceData, meta interface{}) er
366376
} else if v, ok := d.GetOkExists("min_ports_per_vm"); !isEmptyValue(reflect.ValueOf(minPortsPerVmProp)) && (ok || !reflect.DeepEqual(v, minPortsPerVmProp)) {
367377
obj["minPortsPerVm"] = minPortsPerVmProp
368378
}
379+
enableDynamicPortAllocationProp, err := expandNestedComputeRouterNatEnableDynamicPortAllocation(d.Get("enable_dynamic_port_allocation"), d, config)
380+
if err != nil {
381+
return err
382+
} else if v, ok := d.GetOkExists("enable_dynamic_port_allocation"); !isEmptyValue(reflect.ValueOf(enableDynamicPortAllocationProp)) && (ok || !reflect.DeepEqual(v, enableDynamicPortAllocationProp)) {
383+
obj["enableDynamicPortAllocation"] = enableDynamicPortAllocationProp
384+
}
369385
udpIdleTimeoutSecProp, err := expandNestedComputeRouterNatUdpIdleTimeoutSec(d.Get("udp_idle_timeout_sec"), d, config)
370386
if err != nil {
371387
return err
@@ -528,6 +544,9 @@ func resourceComputeRouterNatRead(d *schema.ResourceData, meta interface{}) erro
528544
if err := d.Set("min_ports_per_vm", flattenNestedComputeRouterNatMinPortsPerVm(res["minPortsPerVm"], d, config)); err != nil {
529545
return fmt.Errorf("Error reading RouterNat: %s", err)
530546
}
547+
if err := d.Set("enable_dynamic_port_allocation", flattenNestedComputeRouterNatEnableDynamicPortAllocation(res["enableDynamicPortAllocation"], d, config)); err != nil {
548+
return fmt.Errorf("Error reading RouterNat: %s", err)
549+
}
531550
if err := d.Set("udp_idle_timeout_sec", flattenNestedComputeRouterNatUdpIdleTimeoutSec(res["udpIdleTimeoutSec"], d, config)); err != nil {
532551
return fmt.Errorf("Error reading RouterNat: %s", err)
533552
}
@@ -602,6 +621,12 @@ func resourceComputeRouterNatUpdate(d *schema.ResourceData, meta interface{}) er
602621
} else if v, ok := d.GetOkExists("min_ports_per_vm"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, minPortsPerVmProp)) {
603622
obj["minPortsPerVm"] = minPortsPerVmProp
604623
}
624+
enableDynamicPortAllocationProp, err := expandNestedComputeRouterNatEnableDynamicPortAllocation(d.Get("enable_dynamic_port_allocation"), d, config)
625+
if err != nil {
626+
return err
627+
} else if v, ok := d.GetOkExists("enable_dynamic_port_allocation"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, enableDynamicPortAllocationProp)) {
628+
obj["enableDynamicPortAllocation"] = enableDynamicPortAllocationProp
629+
}
605630
udpIdleTimeoutSecProp, err := expandNestedComputeRouterNatUdpIdleTimeoutSec(d.Get("udp_idle_timeout_sec"), d, config)
606631
if err != nil {
607632
return err
@@ -844,6 +869,10 @@ func flattenNestedComputeRouterNatMinPortsPerVm(v interface{}, d *schema.Resourc
844869
return v // let terraform core handle it otherwise
845870
}
846871

872+
func flattenNestedComputeRouterNatEnableDynamicPortAllocation(v interface{}, d *schema.ResourceData, config *Config) interface{} {
873+
return v
874+
}
875+
847876
func flattenNestedComputeRouterNatUdpIdleTimeoutSec(v interface{}, d *schema.ResourceData, config *Config) interface{} {
848877
if v == nil || isEmptyValue(reflect.ValueOf(v)) {
849878
return 30
@@ -1032,6 +1061,10 @@ func expandNestedComputeRouterNatMinPortsPerVm(v interface{}, d TerraformResourc
10321061
return v, nil
10331062
}
10341063

1064+
func expandNestedComputeRouterNatEnableDynamicPortAllocation(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
1065+
return v, nil
1066+
}
1067+
10351068
func expandNestedComputeRouterNatUdpIdleTimeoutSec(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
10361069
return v, nil
10371070
}

google/resource_compute_router_nat_test.go

+15-6
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ func TestAccComputeRouterNat_withManualIpAndSubnetConfiguration(t *testing.T) {
167167
})
168168
}
169169

170-
func TestAccComputeRouterNat_withDisabledIndependentEndpointMapping(t *testing.T) {
170+
func TestAccComputeRouterNat_withPortAllocationMethods(t *testing.T) {
171171
t.Parallel()
172172

173173
testId := randString(t, 10)
@@ -179,23 +179,31 @@ func TestAccComputeRouterNat_withDisabledIndependentEndpointMapping(t *testing.T
179179
CheckDestroy: testAccCheckComputeRouterNatDestroyProducer(t),
180180
Steps: []resource.TestStep{
181181
{
182-
Config: testAccComputeRouterNatWithDisabledIndependentEndpointMapping(routerName, true),
182+
Config: testAccComputeRouterNatWithAllocationMethod(routerName, true, false),
183183
},
184184
{
185185
ResourceName: "google_compute_router_nat.foobar",
186186
ImportState: true,
187187
ImportStateVerify: true,
188188
},
189189
{
190-
Config: testAccComputeRouterNatWithDisabledIndependentEndpointMapping(routerName, false),
190+
Config: testAccComputeRouterNatWithAllocationMethod(routerName, false, false),
191191
},
192192
{
193193
ResourceName: "google_compute_router_nat.foobar",
194194
ImportState: true,
195195
ImportStateVerify: true,
196196
},
197197
{
198-
Config: testAccComputeRouterNatWithDisabledIndependentEndpointMapping(routerName, true),
198+
Config: testAccComputeRouterNatWithAllocationMethod(routerName, true, false),
199+
},
200+
{
201+
ResourceName: "google_compute_router_nat.foobar",
202+
ImportState: true,
203+
ImportStateVerify: true,
204+
},
205+
{
206+
Config: testAccComputeRouterNatWithAllocationMethod(routerName, false, true),
199207
},
200208
{
201209
ResourceName: "google_compute_router_nat.foobar",
@@ -552,7 +560,7 @@ resource "google_compute_router_nat" "foobar" {
552560
`, routerName, routerName, routerName, routerName, routerName)
553561
}
554562

555-
func testAccComputeRouterNatWithDisabledIndependentEndpointMapping(routerName string, enabled bool) string {
563+
func testAccComputeRouterNatWithAllocationMethod(routerName string, enableEndpointIndependentMapping, enableDynamicPortAllocation bool) string {
556564
return fmt.Sprintf(`
557565
resource "google_compute_network" "foobar" {
558566
name = "%s-net"
@@ -592,8 +600,9 @@ resource "google_compute_router_nat" "foobar" {
592600
source_ip_ranges_to_nat = ["ALL_IP_RANGES"]
593601
}
594602
enable_endpoint_independent_mapping = %t
603+
enable_dynamic_port_allocation = %t
595604
}
596-
`, routerName, routerName, routerName, routerName, routerName, enabled)
605+
`, routerName, routerName, routerName, routerName, routerName, enableEndpointIndependentMapping, enableDynamicPortAllocation)
597606
}
598607

599608
func testAccComputeRouterNatKeepRouter(routerName string) string {

website/docs/r/compute_router_nat.html.markdown

+7
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,13 @@ The following arguments are supported:
171171
(Optional)
172172
Minimum number of ports allocated to a VM from this NAT.
173173

174+
* `enable_dynamic_port_allocation` -
175+
(Optional)
176+
Enable Dynamic Port Allocation.
177+
If minPorts is set, minPortsPerVm must be set to a power of two greater than or equal to 32.
178+
If minPortsPerVm is not set, a minimum of 32 ports will be allocated to a VM from this NAT config.
179+
Mutually exclusive with enableEndpointIndependentMapping.
180+
174181
* `udp_idle_timeout_sec` -
175182
(Optional)
176183
Timeout (in seconds) for UDP connections. Defaults to 30s if not set.

0 commit comments

Comments
 (0)