Skip to content

Commit

Permalink
feat(removes nat_ip_allocate_option)!: add dynamic port mapping (#73)
Browse files Browse the repository at this point in the history
Co-authored-by: Nikhil Makhijani <Nikhilmakhijani2893@gmail.com>
Co-authored-by: Bharath KKB <bharathkrishnakb@gmail.com>
Co-authored-by: Andrew Peabody <andrewpeabody@google.com>
  • Loading branch information
4 people authored May 11, 2023
1 parent 40c3253 commit 0cf1d69
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 19 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ Then perform the following commands on the root folder:
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| create\_router | Create router instead of using an existing one, uses 'router' variable for new resource name. | `bool` | `false` | no |
| enable\_dynamic\_port\_allocation | Enable Dynamic Port Allocation. If minPorts is set, minPortsPerVm must be set to a power of two greater than or equal to 32. | `bool` | `false` | no |
| enable\_endpoint\_independent\_mapping | Specifies if endpoint independent mapping is enabled. | `bool` | `null` | no |
| icmp\_idle\_timeout\_sec | Timeout (in seconds) for ICMP connections. Defaults to 30s if not set. Changing this forces a new NAT to be created. | `string` | `"30"` | no |
| log\_config\_enable | Indicates whether or not to export logs | `bool` | `false` | no |
| log\_config\_filter | Specifies the desired filtering of logs on this NAT. Valid values are: "ERRORS\_ONLY", "TRANSLATIONS\_ONLY", "ALL" | `string` | `"ALL"` | no |
| min\_ports\_per\_vm | Minimum number of ports allocated to a VM from this NAT config. Defaults to 64 if not set. Changing this forces a new NAT to be created. | `string` | `"64"` | no |
| name | Defaults to 'cloud-nat-RANDOM\_SUFFIX'. Changing this forces a new NAT to be created. | `string` | `""` | no |
| nat\_ip\_allocate\_option | Value inferred based on nat\_ips. If present set to MANUAL\_ONLY, otherwise AUTO\_ONLY. | `string` | `"false"` | no |
| nat\_ips | List of self\_links of external IPs. Changing this forces a new NAT to be created. | `list(string)` | `[]` | no |
| nat\_ips | List of self\_links of external IPs. Changing this forces a new NAT to be created. Value of `nat_ip_allocate_option` is inferred based on nat\_ips. If present set to MANUAL\_ONLY, otherwise AUTO\_ONLY. | `list(string)` | `[]` | no |
| network | VPN name, only if router is not passed in and is created by the module. | `string` | `""` | no |
| project\_id | The project ID to deploy to | `string` | n/a | yes |
| region | The region to deploy to | `string` | n/a | yes |
Expand Down Expand Up @@ -85,7 +85,7 @@ Before this module can be used on a project, you must ensure that the following
### Terraform plugins

- [Terraform](https://www.terraform.io/downloads.html) >= 0.13.0
- [terraform-provider-google](https://github.com/terraform-providers/terraform-provider-google) plugin v3.52.0
- [terraform-provider-google](https://github.com/terraform-providers/terraform-provider-google) plugin v4.27.0

### Configure a Service Account

Expand All @@ -102,4 +102,4 @@ In order to operate with the Service Account you must activate the following API

## Contributing

Refer to the [contribution guidelines](../CONTRIBUTING.md) for information on contributing to this module.
Refer to the [contribution guidelines](./CONTRIBUTING.md) for information on contributing to this module.
9 changes: 4 additions & 5 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2018 Google LLC
* Copyright 2018-2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -22,11 +22,9 @@ resource "random_string" "name_suffix" {

locals {
# intermediate locals
default_name = "cloud-nat-${random_string.name_suffix.result}"
nat_ips_length = length(var.nat_ips)
default_nat_ip_allocate_option = local.nat_ips_length > 0 ? "MANUAL_ONLY" : "AUTO_ONLY"
default_name = "cloud-nat-${random_string.name_suffix.result}"
# locals for google_compute_router_nat
nat_ip_allocate_option = var.nat_ip_allocate_option ? var.nat_ip_allocate_option : local.default_nat_ip_allocate_option
nat_ip_allocate_option = length(var.nat_ips) > 0 ? "MANUAL_ONLY" : "AUTO_ONLY"
name = var.name != "" ? var.name : local.default_name
router = var.create_router ? google_compute_router.router[0].name : var.router
}
Expand Down Expand Up @@ -58,6 +56,7 @@ resource "google_compute_router_nat" "main" {
tcp_transitory_idle_timeout_sec = var.tcp_transitory_idle_timeout_sec
tcp_time_wait_timeout_sec = var.tcp_time_wait_timeout_sec
enable_endpoint_independent_mapping = var.enable_endpoint_independent_mapping
enable_dynamic_port_allocation = var.enable_dynamic_port_allocation

dynamic "subnetwork" {
for_each = var.subnetworks
Expand Down
2 changes: 1 addition & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2018 Google LLC
* Copyright 2018-2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
1 change: 1 addition & 0 deletions test/integration/advanced/testdata/TestAdvanced.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"enableEndpointIndependentMapping": true,
"enableDynamicPortAllocation": false,
"endpointTypes": [
"ENDPOINT_TYPE_VM"
],
Expand Down
1 change: 1 addition & 0 deletions test/integration/basic/testdata/TestBasic.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"enableEndpointIndependentMapping": true,
"enableDynamicPortAllocation": false,
"endpointTypes": [
"ENDPOINT_TYPE_VM"
],
Expand Down
1 change: 1 addition & 0 deletions test/integration/subnetworks/testdata/TestSubnetworks.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"enableEndpointIndependentMapping": true,
"enableDynamicPortAllocation": false,
"endpointTypes":[
"ENDPOINT_TYPE_VM"
],
Expand Down
16 changes: 8 additions & 8 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2018 Google LLC
* Copyright 2018-2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -42,15 +42,9 @@ variable "name" {
default = ""
}

variable "nat_ip_allocate_option" {
type = string
description = "Value inferred based on nat_ips. If present set to MANUAL_ONLY, otherwise AUTO_ONLY."
default = "false"
}

variable "nat_ips" {
type = list(string)
description = "List of self_links of external IPs. Changing this forces a new NAT to be created."
description = "List of self_links of external IPs. Changing this forces a new NAT to be created. Value of `nat_ip_allocate_option` is inferred based on nat_ips. If present set to MANUAL_ONLY, otherwise AUTO_ONLY."
default = []
}

Expand Down Expand Up @@ -134,6 +128,12 @@ variable "log_config_filter" {
default = "ALL"
}

variable "enable_dynamic_port_allocation" {
type = bool
description = "Enable Dynamic Port Allocation. If minPorts is set, minPortsPerVm must be set to a power of two greater than or equal to 32."
default = false

}
variable "enable_endpoint_independent_mapping" {
type = bool
description = "Specifies if endpoint independent mapping is enabled."
Expand Down
2 changes: 1 addition & 1 deletion versions.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2021 Google LLC
* Copyright 2018-2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down

0 comments on commit 0cf1d69

Please sign in to comment.