@@ -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.
189189If 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
191193Mutually 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+
871911func 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+
10631107func expandNestedComputeRouterNatEnableDynamicPortAllocation (v interface {}, d TerraformResourceData , config * Config ) (interface {}, error ) {
10641108 return v , nil
10651109}
0 commit comments