diff --git a/modules/net-vpc-peering/README.md b/modules/net-vpc-peering/README.md index 1def1ad19a..869ad15632 100644 --- a/modules/net-vpc-peering/README.md +++ b/modules/net-vpc-peering/README.md @@ -78,11 +78,12 @@ module "peering" { | name | description | type | required | default | |---|---|:---:|:---:|:---:| | [local_network](variables.tf#L17) | Resource link of the network to add a peering to. | string | ✓ | | -| [peer_network](variables.tf#L28) | Resource link of the peer network. | string | ✓ | | -| [peer_create_peering](variables.tf#L22) | Create the peering on the remote side. If false, only the peering from this network to the remote network is created. | bool | | true | -| [prefix](variables.tf#L33) | Optional name prefix for the network peerings. | string | | null | -| [routes_config](variables.tf#L43) | Control import/export for local and remote peer. Remote configuration is only used when creating remote peering. | object({…}) | | {} | -| [stack_type](variables.tf#L63) | IP version(s) of traffic and routes that are allowed to be imported or exported between peer networks. Possible values: IPV4_ONLY, IPV4_IPV6. | string | | null | +| [peer_network](variables.tf#L38) | Resource link of the peer network. | string | ✓ | | +| [name](variables.tf#L22) | Optional names for the the peering resources. If not set, peering names will be generated based on the network names. | object({…}) | | {} | +| [peer_create_peering](variables.tf#L32) | Create the peering on the remote side. If false, only the peering from this network to the remote network is created. | bool | | true | +| [prefix](variables.tf#L43) | Optional name prefix for the network peerings. | string | | null | +| [routes_config](variables.tf#L53) | Control import/export for local and remote peer. Remote configuration is only used when creating remote peering. | object({…}) | | {} | +| [stack_type](variables.tf#L73) | IP version(s) of traffic and routes that are allowed to be imported or exported between peer networks. Possible values: IPV4_ONLY, IPV4_IPV6. | string | | null | ## Outputs diff --git a/modules/net-vpc-peering/main.tf b/modules/net-vpc-peering/main.tf index bb754ae8c2..b907aba0c1 100644 --- a/modules/net-vpc-peering/main.tf +++ b/modules/net-vpc-peering/main.tf @@ -1,5 +1,5 @@ /** - * Copyright 2022 Google LLC + * Copyright 2024 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,46 +16,48 @@ locals { local_network_name = element(reverse(split("/", var.local_network)), 0) - peer_network_name = element(reverse(split("/", var.peer_network)), 0) - prefix = var.prefix == null ? "" : "${var.prefix}-" + auto_local_name = "${local.prefix}${local.local_network_name}-${local.peer_network_name}" + + peer_network_name = element(reverse(split("/", var.peer_network)), 0) + auto_peer_name = "${local.prefix}${local.peer_network_name}-${local.local_network_name}" + + prefix = var.prefix == null ? "" : "${var.prefix}-" } resource "google_compute_network_peering" "local_network_peering" { - name = "${local.prefix}${local.local_network_name}-${local.peer_network_name}" - network = var.local_network - peer_network = var.peer_network - export_custom_routes = try( - var.routes_config.local.export, null - ) - import_custom_routes = try( - var.routes_config.local.import, null - ) - export_subnet_routes_with_public_ip = try( - var.routes_config.local.public_export, null - ) - import_subnet_routes_with_public_ip = try( - var.routes_config.local.public_import, null - ) - stack_type = var.stack_type + name = coalesce(var.name.local, local.auto_local_name) + network = var.local_network + peer_network = var.peer_network + export_custom_routes = var.routes_config.local.export + import_custom_routes = var.routes_config.local.import + export_subnet_routes_with_public_ip = var.routes_config.local.public_export + import_subnet_routes_with_public_ip = var.routes_config.local.public_import + stack_type = var.stack_type + + lifecycle { + precondition { + condition = (length(local.auto_local_name) <= 63 || var.name.local != null) + error_message = "The default peering name is greater than 63 characters. Use var.name.local to override the name." + } + } } resource "google_compute_network_peering" "peer_network_peering" { - count = var.peer_create_peering ? 1 : 0 - name = "${local.prefix}${local.peer_network_name}-${local.local_network_name}" - network = var.peer_network - peer_network = var.local_network - export_custom_routes = try( - var.routes_config.peer.export, null - ) - import_custom_routes = try( - var.routes_config.peer.import, null - ) - export_subnet_routes_with_public_ip = try( - var.routes_config.peer.public_export, null - ) - import_subnet_routes_with_public_ip = try( - var.routes_config.peer.public_import, null - ) - stack_type = var.stack_type - depends_on = [google_compute_network_peering.local_network_peering] + count = var.peer_create_peering ? 1 : 0 + name = coalesce(var.name.peer, local.auto_peer_name) + network = var.peer_network + peer_network = var.local_network + export_custom_routes = var.routes_config.peer.export + import_custom_routes = var.routes_config.peer.import + export_subnet_routes_with_public_ip = var.routes_config.peer.public_export + import_subnet_routes_with_public_ip = var.routes_config.peer.public_import + stack_type = var.stack_type + depends_on = [google_compute_network_peering.local_network_peering] + + lifecycle { + precondition { + condition = (length(local.auto_peer_name) <= 63 || var.name.peer != null) + error_message = "The default peering name is greater than 63 characters. Use var.name.peer to override the name." + } + } } diff --git a/modules/net-vpc-peering/variables.tf b/modules/net-vpc-peering/variables.tf index 9058da1ded..1eca17b377 100644 --- a/modules/net-vpc-peering/variables.tf +++ b/modules/net-vpc-peering/variables.tf @@ -1,5 +1,5 @@ /** - * Copyright 2022 Google LLC + * Copyright 2024 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,16 @@ variable "local_network" { type = string } +variable "name" { + description = "Optional names for the the peering resources. If not set, peering names will be generated based on the network names." + type = object({ + local = optional(string) + peer = optional(string) + }) + default = {} + nullable = false +} + variable "peer_create_peering" { description = "Create the peering on the remote side. If false, only the peering from this network to the remote network is created." type = bool @@ -68,4 +78,4 @@ variable "stack_type" { condition = var.stack_type == "IPV4_ONLY" || var.stack_type == "IPV4_IPV6" || var.stack_type == null error_message = "The stack_type must be either 'IPV4_ONLY' or 'IPV4_IPV6'." } -} \ No newline at end of file +}