Standardise reuse variable from project module and implement for net-vpc (#3205)

* exposing the network_id from the net-vpc module for use with tag bindings

* convert vpc_create to vpc_reuse

* Changed the reuse vars to standardised attributes

* fixed readme tests and schemas

* modified apigee blueprint to use vpc_reuse with network_id passthrough

---------

Co-authored-by: Ludovico Magnocavallo <ludomagno@google.com>
This commit is contained in:
Liam Nesteroff
2025-07-03 09:12:05 +10:00
committed by GitHub
parent 7e20abc19d
commit f07e4f64e9
43 changed files with 190 additions and 106 deletions

File diff suppressed because one or more lines are too long

View File

@@ -63,7 +63,19 @@ module "shared_vpc" {
source = "../../../modules/net-vpc"
project_id = var.project_config.shared_vpc_service_config.host_project
name = var.network_config.shared_vpc.name
vpc_create = false
vpc_reuse = (
var.network_config.shared_vpc.network_id == null
? {
use_data_source = true
attributes = null
}
: {
use_data_source = false
attributes = {
network_id = var.network_config.shared_vpc.network_id
}
}
)
}
module "apigee_vpc" {
@@ -71,7 +83,7 @@ module "apigee_vpc" {
source = "../../../modules/net-vpc"
project_id = module.project.project_id
name = coalesce(var.network_config.apigee_vpc.name, "apigee-vpc")
vpc_create = var.network_config.apigee_vpc.auto_create
vpc_reuse = var.network_config.apigee_vpc.vpc_reuse
psa_configs = [{
ranges = merge(flatten([for k, v in var.apigee_config.instances : merge(
v.runtime_ip_cidr_range == null ? {} : { "apigee-22-${k}" = v.runtime_ip_cidr_range },

View File

@@ -29,15 +29,15 @@ locals {
}
network = try(module.shared_vpc[0].id, module.apigee_vpc[0].id)
neg_subnets = (var.network_config.shared_vpc == null ?
(try(var.network_config.apigee_vpc.auto_create, false) ?
(try(var.network_config.apigee_vpc.vpc_reuse, null) == null ?
{ for k, v in module.apigee_vpc[0].subnets_psc : v.region => v.id } :
{ for k, v in var.network_config.apigee_vpc.subnets_psc : v => v.id }) :
{ for k, v in var.network_config.apigee_vpc.subnets_psc : k => v.id }) :
var.network_config.shared_vpc.subnets_psc
)
ilb_subnets = (var.network_config.shared_vpc == null ?
(try(var.network_config.apigee_vpc.auto_create, false) ?
(try(var.network_config.apigee_vpc.vpc_reuse, null) == null ?
{ for k, v in module.apigee_vpc[0].subnets : v.region => v.id } :
{ for k, v in var.network_config.apigee_vpc.subnets : v => v.id }) :
{ for k, v in var.network_config.apigee_vpc.subnets : k => v.id }) :
var.network_config.shared_vpc.subnets
)
ext_instances = var.ext_lb_config == null ? {} : { for k, v in local.neg_subnets : k => module.apigee.instances[k] }

View File

@@ -292,12 +292,17 @@ variable "network_config" {
type = object({
shared_vpc = optional(object({
name = string
network_id = optional(number)
subnets = map(string)
subnets_psc = map(string)
}))
apigee_vpc = optional(object({
name = optional(string)
auto_create = optional(bool, true)
name = optional(string)
vpc_reuse = optional(object({
use_data_source = optional(bool, true)
attributes = optional(object({
network_id = number
})) }))
subnets = optional(map(object({
id = optional(string)
name = optional(string)

View File

@@ -66,7 +66,7 @@ Do the following to verify that everything works as expected.
| [organization](variables.tf#L59) | Apigee organization. | <code title="object&#40;&#123;&#10; display_name &#61; optional&#40;string, &#34;Apigee organization created by tf module&#34;&#41;&#10; description &#61; optional&#40;string, &#34;Apigee organization created by tf module&#34;&#41;&#10; authorized_network &#61; optional&#40;string, &#34;vpc&#34;&#41;&#10; runtime_type &#61; optional&#40;string, &#34;CLOUD&#34;&#41;&#10; billing_type &#61; optional&#40;string&#41;&#10; database_encryption_key &#61; optional&#40;string&#41;&#10; analytics_region &#61; optional&#40;string, &#34;europe-west1&#34;&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code title="&#123;&#10;&#125;">&#123;&#8230;&#125;</code> |
| [path](variables.tf#L75) | Bucket path. | <code>string</code> | | <code>&#34;&#47;analytics&#34;</code> |
| [project_create](variables.tf#L82) | Parameters for the creation of the new project. | <code title="object&#40;&#123;&#10; billing_account_id &#61; string&#10; parent &#61; string&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
| [vpc_create](variables.tf#L103) | Boolean flag indicating whether the VPC should be created or not. | <code>bool</code> | | <code>true</code> |
| [vpc_reuse](variables.tf#L103) | Reuse existing VPC if not null. If the network_id number is not passed in, a data source is used. | <code title="object&#40;&#123;&#10; use_data_source &#61; optional&#40;bool, true&#41;&#10; attributes &#61; optional&#40;object&#40;&#123;&#10; network_id &#61; number&#10; &#125;&#41;&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
## Outputs

View File

@@ -59,7 +59,7 @@ module "vpc" {
source = "../../../modules/net-vpc"
project_id = module.project.project_id
name = var.organization.authorized_network
vpc_create = var.vpc_create
vpc_reuse = var.vpc_reuse
subnets_psc = [for k, v in var.psc_config : {
ip_cidr_range = v
name = "subnet-psc-${k}"

View File

@@ -100,8 +100,20 @@ variable "psc_config" {
nullable = false
}
variable "vpc_create" {
description = "Boolean flag indicating whether the VPC should be created or not."
type = bool
default = true
variable "vpc_reuse" {
description = "Reuse existing VPC if not null. If the network_id number is not passed in, a data source is used."
type = object({
use_data_source = optional(bool, true)
attributes = optional(object({
network_id = number
}))
})
default = null
validation {
condition = (
try(var.vpc_reuse.use_data_source, null) != false ||
try(var.vpc_reuse.attributes, null) != null
)
error_message = "Reuse datasource can be disabled only if attributes are set."
}
}

View File

@@ -72,7 +72,12 @@ module "vpc" {
region = var.region
}
] : []
vpc_create = var.project_create != null ? true : false
vpc_reuse = (
var.project_create == null
? {
use_data_source = true
} : null
)
}
module "firewall" {

View File

@@ -69,8 +69,8 @@ Alternatively you can also check all the above using the dashboards available in
| [mgmt_subnet_cidr_block](variables.tf#L60) | Management subnet IP CIDR range. | <code>string</code> | | <code>&#34;10.0.2.0&#47;24&#34;</code> |
| [project_create](variables.tf#L66) | Parameters for the creation of the new project. | <code title="object&#40;&#123;&#10; billing_account_id &#61; string&#10; parent &#61; string&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
| [region](variables.tf#L80) | Region. | <code>string</code> | | <code>&#34;europe-west1&#34;</code> |
| [vpc_create](variables.tf#L86) | Flag indicating whether the VPC should be created or not. | <code>bool</code> | | <code>true</code> |
| [vpc_name](variables.tf#L92) | VPC name. | <code>string</code> | | <code>&#34;vpc&#34;</code> |
| [vpc_name](variables.tf#L86) | VPC name. | <code>string</code> | | <code>&#34;vpc&#34;</code> |
| [vpc_reuse](variables.tf#L93) | Reuse existing VPC if not null. If the network_id number is not passed in, a data source is used. | <code title="object&#40;&#123;&#10; use_data_source &#61; optional&#40;bool, true&#41;&#10; attributes &#61; optional&#40;object&#40;&#123;&#10; network_id &#61; number&#10; &#125;&#41;&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
## Outputs

View File

@@ -83,15 +83,27 @@ variable "region" {
default = "europe-west1"
}
variable "vpc_create" {
description = "Flag indicating whether the VPC should be created or not."
type = bool
default = true
}
variable "vpc_name" {
description = "VPC name."
type = string
nullable = false
default = "vpc"
}
variable "vpc_reuse" {
description = "Reuse existing VPC if not null. If the network_id number is not passed in, a data source is used."
type = object({
use_data_source = optional(bool, true)
attributes = optional(object({
network_id = number
}))
})
default = null
validation {
condition = (
try(var.vpc_reuse.use_data_source, null) != false ||
try(var.vpc_reuse.attributes, null) != null
)
error_message = "Reuse datasource can be disabled only if attributes are set."
}
}

View File

@@ -18,7 +18,7 @@ module "vpc" {
source = "../../../modules/net-vpc"
project_id = module.project.project_id
name = var.vpc_name
vpc_create = var.vpc_create
vpc_reuse = var.vpc_reuse
subnets = [
{
ip_cidr_range = var.mgmt_subnet_cidr_block

View File

@@ -56,7 +56,12 @@ module "project" {
parent = try(var.project_create.parent, null)
billing_account = try(var.project_create.billing_account, null)
name = var.project_id
project_create = var.project_create != null
project_reuse = (
var.project_create == null
? {
use_data_source = true
} : null
)
services = compact([
"anthos.googleapis.com",
var.registry_create ? "artifactregistry.googleapis.com" : null,