Allow specifying subnet name in net-vpc module (#64)

* add name attribute to net-vpc subnet variable

* update vpc variables in infra examples

* fix old project output used in shared vpc infra example
This commit is contained in:
Ludovico Magnocavallo
2020-04-30 19:29:39 +02:00
committed by GitHub
parent bf137148b3
commit 98f6b30197
12 changed files with 28 additions and 10 deletions

View File

@@ -34,6 +34,7 @@ module "vpc-hub" {
subnets = { subnets = {
default = { default = {
ip_cidr_range = var.ip_ranges.hub ip_cidr_range = var.ip_ranges.hub
name = null
region = var.region region = var.region
secondary_ip_range = {} secondary_ip_range = {}
} }
@@ -59,6 +60,7 @@ module "vpc-spoke-1" {
subnets = { subnets = {
default = { default = {
ip_cidr_range = var.ip_ranges.spoke-1 ip_cidr_range = var.ip_ranges.spoke-1
name = null
region = var.region region = var.region
secondary_ip_range = {} secondary_ip_range = {}
} }
@@ -101,6 +103,7 @@ module "vpc-spoke-2" {
subnets = { subnets = {
default = { default = {
ip_cidr_range = var.ip_ranges.spoke-2 ip_cidr_range = var.ip_ranges.spoke-2
name = null
region = var.region region = var.region
secondary_ip_range = { secondary_ip_range = {
pods = var.ip_secondary_ranges.spoke-2-pods pods = var.ip_secondary_ranges.spoke-2-pods

View File

@@ -33,11 +33,13 @@ module "vpc-hub" {
subnets = { subnets = {
a = { a = {
ip_cidr_range = var.ip_ranges.hub-a ip_cidr_range = var.ip_ranges.hub-a
name = null
region = var.regions.a region = var.regions.a
secondary_ip_range = {} secondary_ip_range = {}
} }
b = { b = {
ip_cidr_range = var.ip_ranges.hub-b ip_cidr_range = var.ip_ranges.hub-b
name = null
region = var.regions.b region = var.regions.b
secondary_ip_range = {} secondary_ip_range = {}
} }
@@ -121,11 +123,13 @@ module "vpc-spoke-1" {
subnets = { subnets = {
a = { a = {
ip_cidr_range = var.ip_ranges.spoke-1-a ip_cidr_range = var.ip_ranges.spoke-1-a
name = null
region = var.regions.a region = var.regions.a
secondary_ip_range = {} secondary_ip_range = {}
} }
b = { b = {
ip_cidr_range = var.ip_ranges.spoke-1-b ip_cidr_range = var.ip_ranges.spoke-1-b
name = null
region = var.regions.a region = var.regions.a
secondary_ip_range = {} secondary_ip_range = {}
} }
@@ -182,11 +186,13 @@ module "vpc-spoke-2" {
subnets = { subnets = {
a = { a = {
ip_cidr_range = var.ip_ranges.spoke-2-a ip_cidr_range = var.ip_ranges.spoke-2-a
name = null
region = var.regions.b region = var.regions.b
secondary_ip_range = {} secondary_ip_range = {}
} }
b = { b = {
ip_cidr_range = var.ip_ranges.spoke-2-b ip_cidr_range = var.ip_ranges.spoke-2-b
name = null
region = var.regions.b region = var.regions.b
secondary_ip_range = {} secondary_ip_range = {}
} }

View File

@@ -55,6 +55,7 @@ module "vpc" {
subnets = { subnets = {
default = { default = {
ip_cidr_range = var.ip_ranges.gcp ip_cidr_range = var.ip_ranges.gcp
name = null
region = var.region region = var.region
secondary_ip_range = {} secondary_ip_range = {}
} }

View File

@@ -91,7 +91,7 @@ module "project-svc-gke" {
module "vpc-shared" { module "vpc-shared" {
source = "../../modules/net-vpc" source = "../../modules/net-vpc"
project_id = module.project-host.iam_project_id project_id = module.project-host.project_id
name = "shared-vpc" name = "shared-vpc"
shared_vpc_host = true shared_vpc_host = true
shared_vpc_service_projects = [ shared_vpc_service_projects = [
@@ -101,11 +101,13 @@ module "vpc-shared" {
subnets = { subnets = {
gce = { gce = {
ip_cidr_range = var.ip_ranges.gce ip_cidr_range = var.ip_ranges.gce
name = null
region = var.region region = var.region
secondary_ip_range = {} secondary_ip_range = {}
} }
gke = { gke = {
ip_cidr_range = var.ip_ranges.gke ip_cidr_range = var.ip_ranges.gke
name = null
region = var.region region = var.region
secondary_ip_range = { secondary_ip_range = {
pods = var.ip_secondary_ranges.gke-pods pods = var.ip_secondary_ranges.gke-pods

View File

@@ -16,6 +16,7 @@ module "vpc" {
subnets = { subnets = {
subnet-1 = { subnet-1 = {
ip_cidr_range = "10.0.0.0/24" ip_cidr_range = "10.0.0.0/24"
name = "production"
region = "europe-west1" region = "europe-west1"
secondary_ip_range = { secondary_ip_range = {
pods = "172.16.0.0/20" pods = "172.16.0.0/20"
@@ -24,7 +25,8 @@ module "vpc" {
} }
subnet-2 = { subnet-2 = {
ip_cidr_range = "10.0.16.0/24" ip_cidr_range = "10.0.16.0/24"
region = "europe-west1" name = "production"
region = "europe-west2"
secondary_ip_range = {} secondary_ip_range = {}
} }
} }
@@ -43,6 +45,7 @@ module "vpc-spoke-1" {
subnets = { subnets = {
subnet-1 = { subnet-1 = {
ip_cidr_range = "10.0.0.0/24" ip_cidr_range = "10.0.0.0/24"
name = null
region = "europe-west1" region = "europe-west1"
secondary_ip_range = { secondary_ip_range = {
pods = "172.16.0.0/20" pods = "172.16.0.0/20"
@@ -68,6 +71,7 @@ module "vpc-host" {
subnets = { subnets = {
subnet-1 = { subnet-1 = {
ip_cidr_range = "10.0.0.0/24" ip_cidr_range = "10.0.0.0/24"
name = null
region = "europe-west1" region = "europe-west1"
secondary_ip_range = { secondary_ip_range = {
pods = "172.16.0.0/20" pods = "172.16.0.0/20"
@@ -121,7 +125,7 @@ module "vpc-host" {
| *subnet_descriptions* | Optional map of subnet descriptions, keyed by subnet name. | <code title="map&#40;string&#41;">map(string)</code> | | <code title="">{}</code> | | *subnet_descriptions* | Optional map of subnet descriptions, keyed by subnet name. | <code title="map&#40;string&#41;">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&#40;bool&#41;">map(bool)</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&#40;bool&#41;">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&#40;bool&#41;">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&#40;bool&#41;">map(bool)</code> | | <code title="">{}</code> |
| *subnets* | The list of subnets being created | <code title="map&#40;object&#40;&#123;&#10;ip_cidr_range &#61; string&#10;region &#61; string&#10;secondary_ip_range &#61; map&#40;string&#41;&#10;&#125;&#41;&#41;">map(object({...}))</code> | | <code title="">null</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&#40;object&#40;&#123;&#10;ip_cidr_range &#61; string&#10;name &#61; string&#10;region &#61; string&#10;secondary_ip_range &#61; map&#40;string&#41;&#10;&#125;&#41;&#41;">map(object({...}))</code> | | <code title="">null</code> |
## Outputs ## Outputs

View File

@@ -116,7 +116,7 @@ resource "google_compute_subnetwork" "subnetwork" {
project = var.project_id project = var.project_id
network = google_compute_network.network.name network = google_compute_network.network.name
region = each.value.region region = each.value.region
name = "${var.name}-${each.key}" name = each.value.name != null ? each.value.name : "${var.name}-${each.key}"
ip_cidr_range = each.value.ip_cidr_range ip_cidr_range = each.value.ip_cidr_range
secondary_ip_range = each.value.secondary_ip_range == null ? [] : [ secondary_ip_range = each.value.secondary_ip_range == null ? [] : [
for name, range in each.value.secondary_ip_range : for name, range in each.value.secondary_ip_range :

View File

@@ -109,9 +109,10 @@ variable "shared_vpc_service_projects" {
} }
variable "subnets" { variable "subnets" {
description = "The list of subnets being created" 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({ type = map(object({
ip_cidr_range = string ip_cidr_range = string
name = string
region = string region = string
secondary_ip_range = map(string) secondary_ip_range = map(string)
})) }))

View File

@@ -99,6 +99,7 @@ variable "subnets" {
description = "The list of subnets being created" description = "The list of subnets being created"
type = map(object({ type = map(object({
ip_cidr_range = string ip_cidr_range = string
name = string
region = string region = string
secondary_ip_range = map(string) secondary_ip_range = map(string)
})) }))

View File

@@ -21,11 +21,11 @@ FIXTURES_DIR = os.path.join(os.path.dirname(__file__), 'fixture')
_VAR_SUBNETS = ( _VAR_SUBNETS = (
'{ ' '{ '
'a={region = "europe-west1", ip_cidr_range = "10.0.0.0/24",' 'a={region = "europe-west1", ip_cidr_range = "10.0.0.0/24",'
' secondary_ip_range=null},' ' name=null, secondary_ip_range=null},'
'b={region = "europe-west1", ip_cidr_range = "10.0.1.0/24",' 'b={region = "europe-west1", ip_cidr_range = "10.0.1.0/24",'
' secondary_ip_range=null},' ' name=null, secondary_ip_range=null},'
'c={region = "europe-west1", ip_cidr_range = "10.0.2.0/24",' 'c={region = "europe-west1", ip_cidr_range = "10.0.2.0/24",'
' secondary_ip_range={a="192.168.0.0/24", b="192.168.1.0/24"}},' ' name="c", secondary_ip_range={a="192.168.0.0/24", b="192.168.1.0/24"}},'
'}' '}'
) )
_VAR_LOG_CONFIG = '{a = { flow_sampling = 0.1 }}' _VAR_LOG_CONFIG = '{a = { flow_sampling = 0.1 }}'
@@ -45,7 +45,7 @@ def test_subnets_simple(plan_runner):
subnets = [r['values'] subnets = [r['values']
for r in resources if r['type'] == 'google_compute_subnetwork'] for r in resources if r['type'] == 'google_compute_subnetwork']
assert set(s['name'] for s in subnets) == set( assert set(s['name'] for s in subnets) == set(
['my-vpc-a', 'my-vpc-b', 'my-vpc-c']) ['my-vpc-a', 'my-vpc-b', 'c'])
assert set(len(s['secondary_ip_range']) for s in subnets) == set([0, 0, 2]) assert set(len(s['secondary_ip_range']) for s in subnets) == set([0, 0, 2])
@@ -75,5 +75,5 @@ def test_subnet_log_configs(plan_runner):
'metadata': 'INCLUDE_ALL_METADATA' 'metadata': 'INCLUDE_ALL_METADATA'
}], }],
# don't enable # don't enable
'my-vpc-c': [] 'c': []
} }