Skip to content

Commit 922b67a

Browse files
compute: support maxPortsPerVm field related to Cloud NAT's enableDynamicPortAllocation (#6155) (#11933)
Support for the Dynamic Port Allocation feature (tracked in terraform-google-modules/terraform-google-cloud-nat#64 and #11052) was initially implemented in #6022, but it lacked support for the maxPortsPerVm field. This field is crucial to allow the full configuration to work. Signed-off-by: Modular Magician <[email protected]>
1 parent 6e28de4 commit 922b67a

File tree

4 files changed

+111
-2
lines changed

4 files changed

+111
-2
lines changed

.changelog/6155.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
compute: add maxPortsPerVm field to `google_compute_router_nat` resource
3+
```

google/resource_compute_router_nat.go

+45-1
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,10 @@ valid static external IPs that have been assigned to the NAT.`,
185185
Computed: true,
186186
Optional: true,
187187
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.
188+
If minPortsPerVm is set, minPortsPerVm must be set to a power of two greater than or equal to 32.
189189
If minPortsPerVm is not set, a minimum of 32 ports will be allocated to a VM from this NAT config.
190+
If maxPortsPerVm is set, maxPortsPerVm must be set to a power of two greater than minPortsPerVm.
191+
If maxPortsPerVm is not set, a maximum of 65536 ports will be allocated to a VM from this NAT config.
190192
191193
Mutually exclusive with enableEndpointIndependentMapping.`,
192194
},
@@ -224,6 +226,12 @@ see the [official documentation](https://cloud.google.com/nat/docs/overview#spec
224226
},
225227
},
226228
},
229+
"max_ports_per_vm": {
230+
Type: schema.TypeInt,
231+
Optional: true,
232+
Description: `Maximum number of ports allocated to a VM from this NAT.
233+
This field can only be set when enableDynamicPortAllocation is enabled.`,
234+
},
227235
"min_ports_per_vm": {
228236
Type: schema.TypeInt,
229237
Optional: true,
@@ -375,6 +383,12 @@ func resourceComputeRouterNatCreate(d *schema.ResourceData, meta interface{}) er
375383
} else if v, ok := d.GetOkExists("min_ports_per_vm"); !isEmptyValue(reflect.ValueOf(minPortsPerVmProp)) && (ok || !reflect.DeepEqual(v, minPortsPerVmProp)) {
376384
obj["minPortsPerVm"] = minPortsPerVmProp
377385
}
386+
maxPortsPerVmProp, err := expandNestedComputeRouterNatMaxPortsPerVm(d.Get("max_ports_per_vm"), d, config)
387+
if err != nil {
388+
return err
389+
} else if v, ok := d.GetOkExists("max_ports_per_vm"); !isEmptyValue(reflect.ValueOf(maxPortsPerVmProp)) && (ok || !reflect.DeepEqual(v, maxPortsPerVmProp)) {
390+
obj["maxPortsPerVm"] = maxPortsPerVmProp
391+
}
378392
enableDynamicPortAllocationProp, err := expandNestedComputeRouterNatEnableDynamicPortAllocation(d.Get("enable_dynamic_port_allocation"), d, config)
379393
if err != nil {
380394
return err
@@ -543,6 +557,9 @@ func resourceComputeRouterNatRead(d *schema.ResourceData, meta interface{}) erro
543557
if err := d.Set("min_ports_per_vm", flattenNestedComputeRouterNatMinPortsPerVm(res["minPortsPerVm"], d, config)); err != nil {
544558
return fmt.Errorf("Error reading RouterNat: %s", err)
545559
}
560+
if err := d.Set("max_ports_per_vm", flattenNestedComputeRouterNatMaxPortsPerVm(res["maxPortsPerVm"], d, config)); err != nil {
561+
return fmt.Errorf("Error reading RouterNat: %s", err)
562+
}
546563
if err := d.Set("enable_dynamic_port_allocation", flattenNestedComputeRouterNatEnableDynamicPortAllocation(res["enableDynamicPortAllocation"], d, config)); err != nil {
547564
return fmt.Errorf("Error reading RouterNat: %s", err)
548565
}
@@ -620,6 +637,12 @@ func resourceComputeRouterNatUpdate(d *schema.ResourceData, meta interface{}) er
620637
} else if v, ok := d.GetOkExists("min_ports_per_vm"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, minPortsPerVmProp)) {
621638
obj["minPortsPerVm"] = minPortsPerVmProp
622639
}
640+
maxPortsPerVmProp, err := expandNestedComputeRouterNatMaxPortsPerVm(d.Get("max_ports_per_vm"), d, config)
641+
if err != nil {
642+
return err
643+
} else if v, ok := d.GetOkExists("max_ports_per_vm"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, maxPortsPerVmProp)) {
644+
obj["maxPortsPerVm"] = maxPortsPerVmProp
645+
}
623646
enableDynamicPortAllocationProp, err := expandNestedComputeRouterNatEnableDynamicPortAllocation(d.Get("enable_dynamic_port_allocation"), d, config)
624647
if err != nil {
625648
return err
@@ -868,6 +891,23 @@ func flattenNestedComputeRouterNatMinPortsPerVm(v interface{}, d *schema.Resourc
868891
return v // let terraform core handle it otherwise
869892
}
870893

894+
func flattenNestedComputeRouterNatMaxPortsPerVm(v interface{}, d *schema.ResourceData, config *Config) interface{} {
895+
// Handles the string fixed64 format
896+
if strVal, ok := v.(string); ok {
897+
if intVal, err := stringToFixed64(strVal); err == nil {
898+
return intVal
899+
}
900+
}
901+
902+
// number values are represented as float64
903+
if floatVal, ok := v.(float64); ok {
904+
intVal := int(floatVal)
905+
return intVal
906+
}
907+
908+
return v // let terraform core handle it otherwise
909+
}
910+
871911
func flattenNestedComputeRouterNatEnableDynamicPortAllocation(v interface{}, d *schema.ResourceData, config *Config) interface{} {
872912
return v
873913
}
@@ -1060,6 +1100,10 @@ func expandNestedComputeRouterNatMinPortsPerVm(v interface{}, d TerraformResourc
10601100
return v, nil
10611101
}
10621102

1103+
func expandNestedComputeRouterNatMaxPortsPerVm(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
1104+
return v, nil
1105+
}
1106+
10631107
func expandNestedComputeRouterNatEnableDynamicPortAllocation(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
10641108
return v, nil
10651109
}

google/resource_compute_router_nat_test.go

+55
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,14 @@ func TestAccComputeRouterNat_withPortAllocationMethods(t *testing.T) {
210210
ImportState: true,
211211
ImportStateVerify: true,
212212
},
213+
{
214+
Config: testAccComputeRouterNatWithAllocationMethodWithParameters(routerName, false, true, 256, 8192),
215+
},
216+
{
217+
ResourceName: "google_compute_router_nat.foobar",
218+
ImportState: true,
219+
ImportStateVerify: true,
220+
},
213221
},
214222
})
215223
}
@@ -605,6 +613,53 @@ resource "google_compute_router_nat" "foobar" {
605613
`, routerName, routerName, routerName, routerName, routerName, enableEndpointIndependentMapping, enableDynamicPortAllocation)
606614
}
607615

616+
func testAccComputeRouterNatWithAllocationMethodWithParameters(routerName string, enableEndpointIndependentMapping, enableDynamicPortAllocation bool, minPortsPerVm, maxPortsPerVm uint32) string {
617+
return fmt.Sprintf(`
618+
resource "google_compute_network" "foobar" {
619+
name = "%s-net"
620+
auto_create_subnetworks = "false"
621+
}
622+
623+
resource "google_compute_subnetwork" "foobar" {
624+
name = "%s-subnet"
625+
network = google_compute_network.foobar.self_link
626+
ip_cidr_range = "10.0.0.0/16"
627+
region = "us-central1"
628+
}
629+
630+
resource "google_compute_address" "foobar" {
631+
name = "router-nat-%s-addr"
632+
region = google_compute_subnetwork.foobar.region
633+
}
634+
635+
resource "google_compute_router" "foobar" {
636+
name = "%s"
637+
region = google_compute_subnetwork.foobar.region
638+
network = google_compute_network.foobar.self_link
639+
bgp {
640+
asn = 64514
641+
}
642+
}
643+
644+
resource "google_compute_router_nat" "foobar" {
645+
name = "%s"
646+
router = google_compute_router.foobar.name
647+
region = google_compute_router.foobar.region
648+
nat_ip_allocate_option = "MANUAL_ONLY"
649+
nat_ips = [google_compute_address.foobar.self_link]
650+
source_subnetwork_ip_ranges_to_nat = "LIST_OF_SUBNETWORKS"
651+
subnetwork {
652+
name = google_compute_subnetwork.foobar.name
653+
source_ip_ranges_to_nat = ["ALL_IP_RANGES"]
654+
}
655+
enable_endpoint_independent_mapping = %t
656+
enable_dynamic_port_allocation = %t
657+
min_ports_per_vm = %d
658+
max_ports_per_vm = %d
659+
}
660+
`, routerName, routerName, routerName, routerName, routerName, enableEndpointIndependentMapping, enableDynamicPortAllocation, minPortsPerVm, maxPortsPerVm)
661+
}
662+
608663
func testAccComputeRouterNatKeepRouter(routerName string) string {
609664
return fmt.Sprintf(`
610665
resource "google_compute_network" "foobar" {

website/docs/r/compute_router_nat.html.markdown

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

174+
* `max_ports_per_vm` -
175+
(Optional)
176+
Maximum number of ports allocated to a VM from this NAT.
177+
This field can only be set when enableDynamicPortAllocation is enabled.
178+
174179
* `enable_dynamic_port_allocation` -
175180
(Optional)
176181
Enable Dynamic Port Allocation.
177-
If minPorts is set, minPortsPerVm must be set to a power of two greater than or equal to 32.
182+
If minPortsPerVm is set, minPortsPerVm must be set to a power of two greater than or equal to 32.
178183
If minPortsPerVm is not set, a minimum of 32 ports will be allocated to a VM from this NAT config.
184+
If maxPortsPerVm is set, maxPortsPerVm must be set to a power of two greater than minPortsPerVm.
185+
If maxPortsPerVm is not set, a maximum of 65536 ports will be allocated to a VM from this NAT config.
179186
Mutually exclusive with enableEndpointIndependentMapping.
180187

181188
* `udp_idle_timeout_sec` -

0 commit comments

Comments
 (0)