subnets with the same name in different regions (#67)
* support for subnets with the same name in different regions * fix net-vpc tests
This commit is contained in:
committed by
GitHub
parent
711f113cf0
commit
14ec791556
@@ -13,8 +13,8 @@ module "vpc" {
|
||||
source = "../modules/net-vpc"
|
||||
project_id = "my-project"
|
||||
name = "my-network"
|
||||
subnets = {
|
||||
subnet-1 = {
|
||||
subnets = [
|
||||
{
|
||||
ip_cidr_range = "10.0.0.0/24"
|
||||
name = "production"
|
||||
region = "europe-west1"
|
||||
@@ -22,14 +22,14 @@ module "vpc" {
|
||||
pods = "172.16.0.0/20"
|
||||
services = "192.168.0.0/24"
|
||||
}
|
||||
}
|
||||
subnet-2 = {
|
||||
},
|
||||
{
|
||||
ip_cidr_range = "10.0.16.0/24"
|
||||
name = "production"
|
||||
region = "europe-west2"
|
||||
secondary_ip_range = {}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
@@ -42,17 +42,17 @@ module "vpc-spoke-1" {
|
||||
source = "../modules/net-vpc"
|
||||
project_id = "my-project"
|
||||
name = "my-network"
|
||||
subnets = {
|
||||
subnet-1 = {
|
||||
subnets = [
|
||||
{
|
||||
ip_cidr_range = "10.0.0.0/24"
|
||||
name = null
|
||||
name = "subnet-1"
|
||||
region = "europe-west1"
|
||||
secondary_ip_range = {
|
||||
pods = "172.16.0.0/20"
|
||||
services = "192.168.0.0/24"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
peering_config = {
|
||||
peer_vpc_self_link = module.vpc-hub.self_link
|
||||
export_routes = false
|
||||
@@ -68,30 +68,30 @@ module "vpc-host" {
|
||||
source = "../modules/net-vpc"
|
||||
project_id = "my-project"
|
||||
name = "my-host-network"
|
||||
subnets = {
|
||||
subnet-1 = {
|
||||
subnets = [
|
||||
{
|
||||
ip_cidr_range = "10.0.0.0/24"
|
||||
name = null
|
||||
name = "subnet-1"
|
||||
region = "europe-west1"
|
||||
secondary_ip_range = {
|
||||
pods = "172.16.0.0/20"
|
||||
services = "192.168.0.0/24"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
shared_vpc_host = true
|
||||
shared_vpc_service_projects = [
|
||||
local.service_project_1.project_id,
|
||||
local.service_project_2.project_id
|
||||
]
|
||||
iam_roles = {
|
||||
subnet-1 = [
|
||||
"europe-west1/subnet-1" = [
|
||||
"roles/compute.networkUser",
|
||||
"roles/compute.securityAdmin"
|
||||
]
|
||||
}
|
||||
iam_members = {
|
||||
subnet-1 = {
|
||||
"europe-west1/subnet-1" = {
|
||||
"roles/compute.networkUser" = [
|
||||
local.service_project_1.cloudsvc_sa,
|
||||
local.service_project_1.gke_sa
|
||||
@@ -113,19 +113,19 @@ module "vpc-host" {
|
||||
| project_id | The ID of the project where this VPC will be created | <code title="">string</code> | ✓ | |
|
||||
| *auto_create_subnetworks* | Set to true to create an auto mode subnet, defaults to custom mode. | <code title="">bool</code> | | <code title="">false</code> |
|
||||
| *description* | An optional description of this resource (triggers recreation on change). | <code title="">string</code> | | <code title="">Terraform-managed.</code> |
|
||||
| *iam_members* | List of IAM members keyed by subnet and role. | <code title="map(map(list(string)))">map(map(list(string)))</code> | | <code title="">null</code> |
|
||||
| *iam_roles* | List of IAM roles keyed by subnet. | <code title="map(list(string))">map(list(string))</code> | | <code title="">null</code> |
|
||||
| *iam_members* | List of IAM members keyed by subnet 'region/name' and role. | <code title="map(map(list(string)))">map(map(list(string)))</code> | | <code title="">null</code> |
|
||||
| *iam_roles* | List of IAM roles keyed by subnet 'region/name'. | <code title="map(list(string))">map(list(string))</code> | | <code title="">null</code> |
|
||||
| *log_config_defaults* | Default configuration for flow logs when enabled. | <code title="object({ aggregation_interval = string flow_sampling = number metadata = string })">object({...})</code> | | <code title="{ aggregation_interval = "INTERVAL_5_SEC" flow_sampling = 0.5 metadata = "INCLUDE_ALL_METADATA" }">...</code> |
|
||||
| *log_configs* | Map of per-subnet optional configurations for flow logs when enabled. | <code title="map(map(string))">map(map(string))</code> | | <code title="">null</code> |
|
||||
| *log_configs* | Map keyed by subnet 'region/name' of optional configurations for flow logs when enabled. | <code title="map(map(string))">map(map(string))</code> | | <code title="">null</code> |
|
||||
| *peering_config* | VPC peering configuration. | <code title="object({ peer_vpc_self_link = string export_routes = bool import_routes = bool })">object({...})</code> | | <code title="">null</code> |
|
||||
| *routes* | Network routes, keyed by name. | <code title="map(object({ dest_range = string priority = number tags = list(string) next_hop_type = string # gateway, instance, ip, vpn_tunnel, ilb next_hop = string }))">map(object({...}))</code> | | <code title="">null</code> |
|
||||
| *routing_mode* | The network routing mode (default 'GLOBAL') | <code title="">string</code> | | <code title="">GLOBAL</code> |
|
||||
| *shared_vpc_host* | Enable shared VPC for this project. | <code title="">bool</code> | | <code title="">false</code> |
|
||||
| *shared_vpc_service_projects* | Shared VPC service projects to register with this host | <code title="list(string)">list(string)</code> | | <code title="">[]</code> |
|
||||
| *subnet_descriptions* | Optional map of subnet descriptions, keyed by subnet name. | <code title="map(string)">map(string)</code> | | <code title="">{}</code> |
|
||||
| *subnet_flow_logs* | Optional map of boolean to control flow logs (default is disabled), keyed by subnet name. | <code title="map(bool)">map(bool)</code> | | <code title="">{}</code> |
|
||||
| *subnet_private_access* | Optional map of boolean to control private Google access (default is enabled), keyed by subnet name. | <code title="map(bool)">map(bool)</code> | | <code title="">{}</code> |
|
||||
| *subnets* | Subnets being created. If name is set to null, a default will be used combining network name and this map key. | <code title="map(object({ ip_cidr_range = string name = string region = string secondary_ip_range = map(string) }))">map(object({...}))</code> | | <code title="">null</code> |
|
||||
| *subnet_descriptions* | Optional map of subnet descriptions, keyed by subnet 'region/name'. | <code title="map(string)">map(string)</code> | | <code title="">{}</code> |
|
||||
| *subnet_flow_logs* | Optional map of boolean to control flow logs (default is disabled), keyed by subnet 'region/name'. | <code title="map(bool)">map(bool)</code> | | <code title="">{}</code> |
|
||||
| *subnet_private_access* | Optional map of boolean to control private Google access (default is enabled), keyed by subnet 'region/name'. | <code title="map(bool)">map(bool)</code> | | <code title="">{}</code> |
|
||||
| *subnets* | The list of subnets being created | <code title="map(object({ ip_cidr_range = string region = string secondary_ip_range = map(string) }))">map(object({...}))</code> | | <code title="">null</code> |
|
||||
|
||||
## Outputs
|
||||
|
||||
@@ -136,10 +136,10 @@ module "vpc-host" {
|
||||
| network | Network resource. | |
|
||||
| project_id | Shared VPC host project id. | |
|
||||
| self_link | The URI of the VPC being created. | |
|
||||
| subnet_ips | Map of subnet address ranges keyed by name. | |
|
||||
| subnet_regions | Map of subnet regions keyed by name. | |
|
||||
| subnet_secondary_ranges | Map of subnet secondary ranges keyed by name. | |
|
||||
| subnet_self_links | Map of subnet self links keyed by name. | |
|
||||
| subnet_ips | Map of subnet address ranges keyed by 'region/name'. | |
|
||||
| subnet_regions | Map of subnet regions keyed by 'region/name'. | |
|
||||
| subnet_secondary_ranges | Map of subnet secondary ranges keyed by 'region/name'. | |
|
||||
| subnet_self_links | Map of subnet self links keyed by 'region/name'. | |
|
||||
| subnets | Subnet resources. | |
|
||||
<!-- END TFDOC -->
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ locals {
|
||||
name => data if data.next_hop_type == "vpn_tunnel"
|
||||
}
|
||||
subnet_log_configs = {
|
||||
for name, attrs in local.subnets : name => (
|
||||
for name, attrs in { for s in local.subnets : format("%s/%s", s.region, s.name) => s } : name => (
|
||||
lookup(var.subnet_flow_logs, name, false)
|
||||
? [{
|
||||
for key, value in var.log_config_defaults : key => lookup(
|
||||
@@ -62,15 +62,19 @@ locals {
|
||||
: []
|
||||
)
|
||||
}
|
||||
subnets = var.subnets == null ? {} : var.subnets
|
||||
subnets = {
|
||||
for subnet in var.subnets :
|
||||
"${subnet.region}/${subnet.name}" => subnet
|
||||
}
|
||||
}
|
||||
|
||||
resource "google_compute_network" "network" {
|
||||
project = var.project_id
|
||||
name = var.name
|
||||
description = var.description
|
||||
auto_create_subnetworks = var.auto_create_subnetworks
|
||||
routing_mode = var.routing_mode
|
||||
project = var.project_id
|
||||
name = var.name
|
||||
description = var.description
|
||||
auto_create_subnetworks = var.auto_create_subnetworks
|
||||
delete_default_routes_on_create = var.delete_default_routes_on_create
|
||||
routing_mode = var.routing_mode
|
||||
}
|
||||
|
||||
resource "google_compute_network_peering" "local" {
|
||||
@@ -116,16 +120,16 @@ resource "google_compute_subnetwork" "subnetwork" {
|
||||
project = var.project_id
|
||||
network = google_compute_network.network.name
|
||||
region = each.value.region
|
||||
name = each.value.name != null ? each.value.name : "${var.name}-${each.key}"
|
||||
name = each.value.name
|
||||
ip_cidr_range = each.value.ip_cidr_range
|
||||
secondary_ip_range = each.value.secondary_ip_range == null ? [] : [
|
||||
for name, range in each.value.secondary_ip_range :
|
||||
{ range_name = name, ip_cidr_range = range }
|
||||
]
|
||||
description = lookup(var.subnet_descriptions, each.key, "Terraform-managed.")
|
||||
private_ip_google_access = lookup(var.subnet_private_access, each.key, true)
|
||||
description = lookup(var.subnet_descriptions, "${each.value.region}/${each.value.name}", "Terraform-managed.")
|
||||
private_ip_google_access = lookup(var.subnet_private_access, "${each.value.region}/${each.value.name}", true)
|
||||
dynamic "log_config" {
|
||||
for_each = local.subnet_log_configs[each.key]
|
||||
for_each = local.subnet_log_configs["${each.value.region}/${each.value.name}"]
|
||||
iterator = config
|
||||
content {
|
||||
aggregation_interval = config.value.aggregation_interval
|
||||
|
||||
@@ -20,6 +20,12 @@ variable "auto_create_subnetworks" {
|
||||
default = false
|
||||
}
|
||||
|
||||
variable "delete_default_routes_on_create" {
|
||||
description = "Set to true to delete the default routes at creation time."
|
||||
type = bool
|
||||
default = false
|
||||
}
|
||||
|
||||
variable "description" {
|
||||
description = "An optional description of this resource (triggers recreation on change)."
|
||||
type = string
|
||||
@@ -27,19 +33,19 @@ variable "description" {
|
||||
}
|
||||
|
||||
variable "iam_roles" {
|
||||
description = "List of IAM roles keyed by subnet."
|
||||
description = "List of IAM roles keyed by subnet 'region/name'."
|
||||
type = map(list(string))
|
||||
default = null
|
||||
}
|
||||
|
||||
variable "iam_members" {
|
||||
description = "List of IAM members keyed by subnet and role."
|
||||
description = "List of IAM members keyed by subnet 'region/name' and role."
|
||||
type = map(map(list(string)))
|
||||
default = null
|
||||
}
|
||||
|
||||
variable "log_configs" {
|
||||
description = "Map of per-subnet optional configurations for flow logs when enabled."
|
||||
description = "Map keyed by subnet 'region/name' of optional configurations for flow logs when enabled."
|
||||
type = map(map(string))
|
||||
default = null
|
||||
}
|
||||
@@ -109,30 +115,31 @@ variable "shared_vpc_service_projects" {
|
||||
}
|
||||
|
||||
variable "subnets" {
|
||||
description = "Subnets being created. If name is set to null, a default will be used combining network name and this map key."
|
||||
type = map(object({
|
||||
description = "The list of subnets being created"
|
||||
type = list(object({
|
||||
name = string
|
||||
ip_cidr_range = string
|
||||
name = string
|
||||
region = string
|
||||
secondary_ip_range = map(string)
|
||||
}))
|
||||
default = null
|
||||
default = []
|
||||
}
|
||||
|
||||
variable "subnet_descriptions" {
|
||||
description = "Optional map of subnet descriptions, keyed by subnet name."
|
||||
description = "Optional map of subnet descriptions, keyed by subnet 'region/name'."
|
||||
type = map(string)
|
||||
default = {}
|
||||
}
|
||||
|
||||
variable "subnet_flow_logs" {
|
||||
description = "Optional map of boolean to control flow logs (default is disabled), keyed by subnet name."
|
||||
description = "Optional map of boolean to control flow logs (default is disabled), keyed by subnet 'region/name'."
|
||||
type = map(bool)
|
||||
default = {}
|
||||
}
|
||||
|
||||
variable "subnet_private_access" {
|
||||
description = "Optional map of boolean to control private Google access (default is enabled), keyed by subnet name."
|
||||
description = "Optional map of boolean to control private Google access (default is enabled), keyed by subnet 'region/name'."
|
||||
type = map(bool)
|
||||
default = {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user