diff --git a/examples/service-networking/main.tf b/examples/service-networking/main.tf index 00907a34..8c38e264 100644 --- a/examples/service-networking/main.tf +++ b/examples/service-networking/main.tf @@ -24,7 +24,8 @@ module "service_networking" { source = "terraform-google-modules/network/google//modules/service-networking" version = "~> 9.0" - project_id = var.project_id - network_id = google_compute_network.peering_network.id - address_name = "global-address" + project_id = var.project_id + network = { id : google_compute_network.peering_network.id } + global_addresses = [{ name : "global-address" }] + service = "servicenetworking.googleapis.com" } diff --git a/modules/service-networking/README.md b/modules/service-networking/README.md index df7d4efa..0544ab29 100644 --- a/modules/service-networking/README.md +++ b/modules/service-networking/README.md @@ -6,26 +6,23 @@ This module creates global network address and a service networking | Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| -| address\_name | Global address name | `string` | n/a | yes | -| address\_prefix\_length | Global address prefix length | `number` | `16` | no | -| address\_purpose | Global address purpose | `string` | `"VPC_PEERING"` | no | -| address\_type | Global address type | `string` | `"INTERNAL"` | no | | create\_peered\_dns\_domain | Create peered dns domain | `bool` | `false` | no | | create\_peering\_routes\_config | Create peering route config | `bool` | `false` | no | | deletion\_policy | Deletion policy for service networking resource | `string` | `null` | no | | dns\_suffix | Dns suffix | `string` | `null` | no | | domain\_name | Domain name | `string` | `null` | no | | export\_custom\_routes | Export custom routes | `bool` | `false` | no | +| global\_addresses | List of global addresses to be created |
list(object({
name : string,
purpose : optional(string, "VPC_PEERING"),
type : optional(string, "INTERNAL"),
prefix_length : optional(number, 16)
}))
| n/a | yes | | import\_custom\_routes | Import custom routes to peering rout config | `bool` | `false` | no | -| network\_id | Network id | `string` | n/a | yes | -| network\_name | Network name | `string` | `null` | no | +| network | Network details including name and id |
object({
name = optional(string, null),
id = string
})
| n/a | yes | | project\_id | Project ID | `string` | n/a | yes | +| service | Service to create service networking connection | `string` | n/a | yes | ## Outputs | Name | Description | |------|-------------| -| address\_id | Global address id | +| address\_ids | Global address id | | peering | Service networking connection peering | diff --git a/modules/service-networking/main.tf b/modules/service-networking/main.tf index f9578f15..ff417f92 100644 --- a/modules/service-networking/main.tf +++ b/modules/service-networking/main.tf @@ -14,19 +14,20 @@ * limitations under the License. */ -resource "google_compute_global_address" "global_address" { +resource "google_compute_global_address" "global_addresses" { + for_each = { for address in var.global_addresses : address.name => address } project = var.project_id - name = var.address_name - purpose = var.address_purpose - address_type = var.address_type - prefix_length = var.address_prefix_length - network = var.network_id + name = each.value.name + purpose = each.value.purpose + address_type = each.value.type + prefix_length = each.value.prefix_length + network = var.network.id } resource "google_service_networking_connection" "default" { - network = var.network_id - service = "servicenetworking.googleapis.com" - reserved_peering_ranges = [google_compute_global_address.global_address.name] + network = var.network.id + service = var.service + reserved_peering_ranges = [for name, _ in google_compute_global_address.global_addresses : name] deletion_policy = var.deletion_policy } @@ -34,7 +35,7 @@ resource "google_compute_network_peering_routes_config" "peering_routes" { count = var.create_peering_routes_config ? 1 : 0 project = var.project_id peering = google_service_networking_connection.default.peering - network = var.network_name + network = var.network.name import_custom_routes = var.import_custom_routes export_custom_routes = var.export_custom_routes } @@ -43,7 +44,7 @@ resource "google_service_networking_peered_dns_domain" "default" { count = var.create_peered_dns_domain ? 1 : 0 project = var.project_id name = var.domain_name - network = var.network_name + network = var.network.name dns_suffix = var.dns_suffix - service = "servicenetworking.googleapis.com" + service = var.service } diff --git a/modules/service-networking/metadata.yaml b/modules/service-networking/metadata.yaml index 196bb5c5..36fac5bc 100644 --- a/modules/service-networking/metadata.yaml +++ b/modules/service-networking/metadata.yaml @@ -90,22 +90,6 @@ spec: location: examples/submodule_vpc_serverless_connector interfaces: variables: - - name: address_name - description: Global address name - varType: string - required: true - - name: address_prefix_length - description: Global address prefix length - varType: number - defaultValue: 16 - - name: address_purpose - description: Global address purpose - varType: string - defaultValue: VPC_PEERING - - name: address_type - description: Global address type - varType: string - defaultValue: INTERNAL - name: create_peered_dns_domain description: Create peered dns domain varType: bool @@ -127,23 +111,38 @@ spec: description: Export custom routes varType: bool defaultValue: false + - name: global_addresses + description: List of global addresses to be created + varType: |- + list(object({ + name : string, + purpose : optional(string, "VPC_PEERING"), + type : optional(string, "INTERNAL"), + prefix_length : optional(number, 16) + })) + required: true - name: import_custom_routes description: Import custom routes to peering rout config varType: bool defaultValue: false - - name: network_id - description: Network id - varType: string + - name: network + description: Network details including name and id + varType: |- + object({ + name = optional(string, null), + id = string + }) required: true - - name: network_name - description: Network name - varType: string - name: project_id description: Project ID varType: string required: true + - name: service + description: Service to create service networking connection + varType: string + required: true outputs: - - name: address_id + - name: address_ids description: Global address id - name: peering description: Service networking connection peering @@ -168,3 +167,4 @@ spec: - dns.googleapis.com - networksecurity.googleapis.com - iam.googleapis.com + - servicenetworking.googleapis.com diff --git a/modules/service-networking/outputs.tf b/modules/service-networking/outputs.tf index a3a9a153..25a3b270 100644 --- a/modules/service-networking/outputs.tf +++ b/modules/service-networking/outputs.tf @@ -14,9 +14,9 @@ * limitations under the License. */ -output "address_id" { +output "address_ids" { description = "Global address id" - value = google_compute_global_address.global_address.id + value = [for id, _ in google_compute_global_address.global_addresses : id] } output "peering" { diff --git a/modules/service-networking/variables.tf b/modules/service-networking/variables.tf index 5b1048e1..8bc3c5ad 100644 --- a/modules/service-networking/variables.tf +++ b/modules/service-networking/variables.tf @@ -19,37 +19,26 @@ variable "project_id" { type = string } -variable "address_name" { - description = "Global address name" - type = string -} - -variable "address_purpose" { - description = "Global address purpose" - type = string - default = "VPC_PEERING" +variable "global_addresses" { + description = "List of global addresses to be created" + type = list(object({ + name : string, + purpose : optional(string, "VPC_PEERING"), + type : optional(string, "INTERNAL"), + prefix_length : optional(number, 16) + })) } -variable "address_type" { - description = "Global address type" - type = string - default = "INTERNAL" -} - -variable "address_prefix_length" { - description = "Global address prefix length" - type = number - default = 16 -} - -variable "network_name" { - description = "Network name" - type = string - default = null +variable "network" { + description = "Network details including name and id" + type = object({ + name = optional(string, null), + id = string + }) } -variable "network_id" { - description = "Network id" +variable "service" { + description = "Service to create service networking connection" type = string }