Skip to content

Commit

Permalink
Add multiple global address support for service networking connection
Browse files Browse the repository at this point in the history
  • Loading branch information
q2w committed Aug 14, 2024
1 parent b3d6203 commit 7990fae
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 74 deletions.
7 changes: 4 additions & 3 deletions examples/service-networking/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
11 changes: 4 additions & 7 deletions modules/service-networking/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | <pre>list(object({<br> name : string,<br> purpose : optional(string, "VPC_PEERING"),<br> type : optional(string, "INTERNAL"),<br> prefix_length : optional(number, 16)<br> }))</pre> | 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 | <pre>object({<br> name = optional(string, null),<br> id = string<br> })</pre> | 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 |

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
25 changes: 13 additions & 12 deletions modules/service-networking/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,28 @@
* 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
}

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
}
Expand All @@ -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
}
46 changes: 23 additions & 23 deletions modules/service-networking/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -168,3 +167,4 @@ spec:
- dns.googleapis.com
- networksecurity.googleapis.com
- iam.googleapis.com
- servicenetworking.googleapis.com
4 changes: 2 additions & 2 deletions modules/service-networking/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
43 changes: 16 additions & 27 deletions modules/service-networking/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 7990fae

Please sign in to comment.