Added conditional project designation feature for relevant modules.

This commit is contained in:
2025-03-31 21:17:33 +02:00
parent fadc7573cb
commit e7222382f1
13 changed files with 89 additions and 20 deletions
+5 -1
View File
@@ -1,3 +1,5 @@
data "google_client_config" "default" {}
data "google_compute_vpn_gateway" "gw" {
name = var.gw_name
}
@@ -6,12 +8,13 @@ locals {
vpc_name = regex("networks/([^/]+)$", data.google_compute_vpn_gateway.gw.network)[0]
}
data google_compute_network network {
data "google_compute_network" "network" {
name = local.vpc_name
}
resource "google_compute_vpn_tunnel" "tunnel" {
name = var.name
project = var.project_id != null ? var.project_id : data.google_client_config.default.project
target_vpn_gateway = data.google_compute_vpn_gateway.gw.id
shared_secret = var.shared_secret
@@ -25,6 +28,7 @@ resource "google_compute_vpn_tunnel" "tunnel" {
resource "google_compute_route" "route_to_remote_network" {
count = length(var.remote_selectors)
name = "${data.google_compute_network.network.name}-to-${replace(var.remote_selectors[count.index], "/[./]/", "-")}"
project = var.project_id != null ? var.project_id : data.google_client_config.default.project
network = data.google_compute_network.network.name
dest_range = var.remote_selectors[count.index]
next_hop_vpn_tunnel = google_compute_vpn_tunnel.tunnel.id
+7
View File
@@ -3,6 +3,13 @@ variable "name" {
type = string
}
variable "project_id" {
description = "The GCP project ID. If not provided, the default project will be used."
type = string
nullable = true
default = null
}
variable "gw_name" {
description = "The name of the VPN gateway"
type = string
+8 -1
View File
@@ -1,14 +1,18 @@
data "google_client_config" "default" {}
# Cloud VPN
resource "google_compute_vpn_gateway" "gw" {
name = var.name
network = var.network_name
region = var.region
project = var.project_id != null ? var.project_id : data.google_client_config.default.project
}
resource "google_compute_address" "vpn_ip" {
count = var.vpn_external_ip != null ? 0 : 1
name = "${var.name}-ip"
project = var.project_id != null ? var.project_id : data.google_client_config.default.project
region = var.region
address_type = "EXTERNAL"
}
@@ -16,11 +20,12 @@ resource "google_compute_address" "vpn_ip" {
data "google_compute_address" "vpn_ip" {
count = var.vpn_external_ip != null ? 1 : 0
name = var.vpn_external_ip
name = var.vpn_external_ip
}
resource "google_compute_forwarding_rule" "gw_fw_esp" {
name = "fwd-esp"
project = var.project_id != null ? var.project_id : data.google_client_config.default.project
ip_protocol = "ESP"
ip_address = var.vpn_external_ip != null ? data.google_compute_address.vpn_ip[0].address : google_compute_address.vpn_ip[0].address
target = google_compute_vpn_gateway.gw.id
@@ -28,6 +33,7 @@ resource "google_compute_forwarding_rule" "gw_fw_esp" {
resource "google_compute_forwarding_rule" "gw_fw_udp_500" {
name = "fwd-udp-500"
project = var.project_id != null ? var.project_id : data.google_client_config.default.project
ip_protocol = "UDP"
ip_address = var.vpn_external_ip != null ? data.google_compute_address.vpn_ip[0].address : google_compute_address.vpn_ip[0].address
port_range = "500"
@@ -36,6 +42,7 @@ resource "google_compute_forwarding_rule" "gw_fw_udp_500" {
resource "google_compute_forwarding_rule" "gw_fw_udp_4500" {
name = "fwd-udp-4500"
project = var.project_id != null ? var.project_id : data.google_client_config.default.project
ip_protocol = "UDP"
ip_address = var.vpn_external_ip != null ? data.google_compute_address.vpn_ip[0].address : google_compute_address.vpn_ip[0].address
port_range = "4500"
+7
View File
@@ -3,6 +3,13 @@ variable "name" {
type = string
}
variable "project_id" {
description = "The GCP project ID. If not provided, the default project will be used."
type = string
nullable = true
default = null
}
variable "network_name" {
description = "The name of the network."
type = string
+4 -2
View File
@@ -1,7 +1,9 @@
data "google_client_config" "default" {}
resource "google_dns_managed_zone" "zone" {
dns_name = var.dns_name
name = var.zone_name != null ? var.zone_name : "${replace(replace(var.dns_name, "/\\.$/", ""), ".", "-")}-zone"
project = var.project_id
project = var.project_id != null ? var.project_id : data.google_client_config.default.project
visibility = var.network_id != null ? "private" : "public"
@@ -38,7 +40,7 @@ resource "google_dns_managed_zone" "zone" {
resource "google_dns_record_set" "records" {
count = length(var.resource_records)
project = var.project_id
project = var.project_id != null ? var.project_id : data.google_client_config.default.project
managed_zone = google_dns_managed_zone.zone.name
name = var.resource_records[count.index].name
+3 -1
View File
@@ -1,6 +1,8 @@
variable "project_id" {
description = "The project ID where the managed zone will be created."
description = "The GCP project ID. If not provided, the default project will be used."
type = string
nullable = true
default = null
}
variable "dns_name" {
+6 -2
View File
@@ -1,7 +1,10 @@
data "google_client_config" "default" {}
resource "google_compute_instance" "vm_hub" {
name = var.name
machine_type = var.machine_type
can_ip_forward = var.can_ip_forward
project = var.project_id != null ? var.project_id : data.google_client_config.default.project
description = var.description
boot_disk {
@@ -11,8 +14,9 @@ resource "google_compute_instance" "vm_hub" {
}
network_interface {
network = var.network_name
subnetwork = var.subnet_name
network = var.network_name
subnetwork = var.subnet_name
subnetwork_project = var.project_id != null ? var.project_id : data.google_client_config.default.project
}
metadata = {
+7
View File
@@ -3,6 +3,13 @@ variable "name" {
type = string
}
variable "project_id" {
description = "The GCP project ID. If not provided, the default project will be used."
type = string
nullable = true
default = null
}
variable "machine_type" {
description = "The machine type of the VM instance."
type = string
+6
View File
@@ -1,13 +1,17 @@
data "google_client_config" "default" {}
# VPC
resource "google_compute_network" "network" {
name = var.name
auto_create_subnetworks = false
project = var.project_id != null ? var.project_id : data.google_client_config.default.project
}
# Subnets
resource "google_compute_subnetwork" "subnet" {
count = length(var.subnets)
name = var.subnets[count.index].name
project = var.project_id != null ? var.project_id : data.google_client_config.default.project
ip_cidr_range = var.subnets[count.index].cidr
region = var.subnets[count.index].region != null ? var.subnets[count.index].region : var.subnets[0].region
network = google_compute_network.network.id
@@ -18,6 +22,7 @@ resource "google_compute_router" "cr" {
count = var.nat ? 1 : 0
name = "${var.name}-router"
network = var.name
project = var.project_id != null ? var.project_id : data.google_client_config.default.project
depends_on = [google_compute_network.network]
}
@@ -25,6 +30,7 @@ resource "google_compute_router" "cr" {
resource "google_compute_router_nat" "nat" {
count = var.nat ? 1 : 0
name = "${var.name}-nat"
project = var.project_id != null ? var.project_id : data.google_client_config.default.project
router = google_compute_router.cr[0].name
nat_ip_allocate_option = "AUTO_ONLY"
source_subnetwork_ip_ranges_to_nat = "ALL_SUBNETWORKS_ALL_IP_RANGES"
+7
View File
@@ -3,6 +3,13 @@ variable "name" {
type = string
}
variable "project_id" {
description = "The GCP project ID. If not provided, the default project will be used."
type = string
nullable = true
default = null
}
# A Cloud NAT will be created in the same region as the first subnet.
variable "subnets" {
description = "A list of subnets with names and CIDRs."