Skip to content

Commit

Permalink
Allow user to override peerings names (#2459)
Browse files Browse the repository at this point in the history
  • Loading branch information
juliocc authored Jul 31, 2024
1 parent 35d61e4 commit 1d508d2
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 44 deletions.
11 changes: 6 additions & 5 deletions modules/net-vpc-peering/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. | <code>string</code> || |
| [peer_network](variables.tf#L28) | Resource link of the peer network. | <code>string</code> || |
| [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. | <code>bool</code> | | <code>true</code> |
| [prefix](variables.tf#L33) | Optional name prefix for the network peerings. | <code>string</code> | | <code>null</code> |
| [routes_config](variables.tf#L43) | Control import/export for local and remote peer. Remote configuration is only used when creating remote peering. | <code title="object&#40;&#123;&#10; local &#61; optional&#40;object&#40;&#123;&#10; export &#61; optional&#40;bool, true&#41;&#10; import &#61; optional&#40;bool, true&#41;&#10; public_export &#61; optional&#40;bool&#41;&#10; public_import &#61; optional&#40;bool&#41;&#10; &#125;&#41;, &#123;&#125;&#41;&#10; peer &#61; optional&#40;object&#40;&#123;&#10; export &#61; optional&#40;bool, true&#41;&#10; import &#61; optional&#40;bool, true&#41;&#10; public_export &#61; optional&#40;bool&#41;&#10; public_import &#61; optional&#40;bool&#41;&#10; &#125;&#41;, &#123;&#125;&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>&#123;&#125;</code> |
| [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. | <code>string</code> | | <code>null</code> |
| [peer_network](variables.tf#L38) | Resource link of the peer network. | <code>string</code> || |
| [name](variables.tf#L22) | Optional names for the the peering resources. If not set, peering names will be generated based on the network names. | <code title="object&#40;&#123;&#10; local &#61; optional&#40;string&#41;&#10; peer &#61; optional&#40;string&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>&#123;&#125;</code> |
| [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. | <code>bool</code> | | <code>true</code> |
| [prefix](variables.tf#L43) | Optional name prefix for the network peerings. | <code>string</code> | | <code>null</code> |
| [routes_config](variables.tf#L53) | Control import/export for local and remote peer. Remote configuration is only used when creating remote peering. | <code title="object&#40;&#123;&#10; local &#61; optional&#40;object&#40;&#123;&#10; export &#61; optional&#40;bool, true&#41;&#10; import &#61; optional&#40;bool, true&#41;&#10; public_export &#61; optional&#40;bool&#41;&#10; public_import &#61; optional&#40;bool&#41;&#10; &#125;&#41;, &#123;&#125;&#41;&#10; peer &#61; optional&#40;object&#40;&#123;&#10; export &#61; optional&#40;bool, true&#41;&#10; import &#61; optional&#40;bool, true&#41;&#10; public_export &#61; optional&#40;bool&#41;&#10; public_import &#61; optional&#40;bool&#41;&#10; &#125;&#41;, &#123;&#125;&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>&#123;&#125;</code> |
| [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. | <code>string</code> | | <code>null</code> |

## Outputs

Expand Down
76 changes: 39 additions & 37 deletions modules/net-vpc-peering/main.tf
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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."
}
}
}
14 changes: 12 additions & 2 deletions modules/net-vpc-peering/variables.tf
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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'."
}
}
}

0 comments on commit 1d508d2

Please sign in to comment.