From fd74562a4156886879e1ec39451aec9bb2ace33e Mon Sep 17 00:00:00 2001 From: Julio Diez Date: Wed, 1 Nov 2023 19:16:21 +0100 Subject: [PATCH] Refactor into project_configs variable --- .../serverless/cloud-run-microservices/alb.tf | 2 +- .../cloud-run-microservices/cloudrun.tf | 6 +-- .../serverless/cloud-run-microservices/dns.tf | 2 +- .../cloud-run-microservices/main.tf | 20 +++++----- .../cloud-run-microservices/outputs.tf | 10 ++++- .../cloud-run-microservices/variables.tf | 37 +++++++++---------- 6 files changed, 40 insertions(+), 37 deletions(-) diff --git a/blueprints/serverless/cloud-run-microservices/alb.tf b/blueprints/serverless/cloud-run-microservices/alb.tf index 820083546..497cae96a 100644 --- a/blueprints/serverless/cloud-run-microservices/alb.tf +++ b/blueprints/serverless/cloud-run-microservices/alb.tf @@ -19,7 +19,7 @@ # Internal Application Load Balancer in main (host) project module "int-alb" { source = "../../../modules/net-lb-app-int" - count = var.service_project.project_id != null ? 1 : 0 + count = try(var.project_configs.service.project_id, null) != null ? 1 : 0 project_id = module.main-project.project_id name = "int-alb-cr" region = var.region diff --git a/blueprints/serverless/cloud-run-microservices/cloudrun.tf b/blueprints/serverless/cloud-run-microservices/cloudrun.tf index 8973bf927..d7938e33d 100644 --- a/blueprints/serverless/cloud-run-microservices/cloudrun.tf +++ b/blueprints/serverless/cloud-run-microservices/cloudrun.tf @@ -28,13 +28,13 @@ resource "google_cloud_run_v2_service" "svc_a" { image = var.svc_a_image } dynamic "vpc_access" { - for_each = var.service_project.project_id == null ? [""] : [] + for_each = try(var.project_configs.service.project_id, null) == null ? [""] : [] content { # Use Serverless VPC Access connector connector = google_vpc_access_connector.connector[0].id } } dynamic "vpc_access" { - for_each = var.service_project.project_id != null ? [""] : [] + for_each = try(var.project_configs.service.project_id, null) != null ? [""] : [] content { # Use Direct VPC Egress network_interfaces { subnetwork = module.vpc-main.subnets["${var.region}/subnet-vpc-direct"].name @@ -80,7 +80,7 @@ module "cloud-run-svc-b" { # a VPC access connector to connect from service A to service B. # The use case with Shared VPC and internal ALB uses Direct VPC Egress. resource "google_vpc_access_connector" "connector" { - count = var.service_project.project_id == null ? 1 : 0 + count = try(var.project_configs.service.project_id, null) == null ? 1 : 0 name = "connector" project = module.main-project.project_id region = var.region diff --git a/blueprints/serverless/cloud-run-microservices/dns.tf b/blueprints/serverless/cloud-run-microservices/dns.tf index 1b3e3939c..dfa9ff611 100644 --- a/blueprints/serverless/cloud-run-microservices/dns.tf +++ b/blueprints/serverless/cloud-run-microservices/dns.tf @@ -35,7 +35,7 @@ module "private-dns-main" { # DNS configuration for the Cloud Run custom domain (when using internal ALB) module "private-dns-main-custom" { source = "../../../modules/dns" - count = var.service_project.project_id != null ? 1 : 0 + count = try(var.project_configs.service.project_id, null) != null ? 1 : 0 project_id = module.main-project.project_id name = "cloud-run-custom" zone_config = { diff --git a/blueprints/serverless/cloud-run-microservices/main.tf b/blueprints/serverless/cloud-run-microservices/main.tf index d178a7726..5a2f52afb 100644 --- a/blueprints/serverless/cloud-run-microservices/main.tf +++ b/blueprints/serverless/cloud-run-microservices/main.tf @@ -22,14 +22,13 @@ locals { svc_b_name = "svc-b" } -# Main (or host) project module "main-project" { source = "../../../modules/project" - name = var.main_project.project_id + name = var.project_configs.main.project_id prefix = var.prefix - project_create = var.main_project.billing_account_id != null - billing_account = try(var.main_project.billing_account_id, null) - parent = try(var.main_project.parent, null) + project_create = var.project_configs.main.billing_account_id != null + billing_account = try(var.project_configs.main.billing_account_id, null) + parent = try(var.project_configs.main.parent, null) # Enable Shared VPC by default, some use cases will use this project as host shared_vpc_host_config = { enabled = true @@ -43,15 +42,14 @@ module "main-project" { skip_delete = true } -# Service project 1 module "service-project" { source = "../../../modules/project" - count = var.service_project.project_id != null ? 1 : 0 - name = var.service_project.project_id + count = try(var.project_configs.service.project_id, null) != null ? 1 : 0 + name = var.project_configs.service.project_id prefix = var.prefix - project_create = var.service_project.billing_account_id != null - billing_account = try(var.service_project.billing_account_id, null) - parent = try(var.service_project.parent, null) + project_create = var.project_configs.service.billing_account_id != null + billing_account = try(var.project_configs.service.billing_account_id, null) + parent = try(var.project_configs.service.parent, null) shared_vpc_service_config = { host_project = module.main-project.project_id } diff --git a/blueprints/serverless/cloud-run-microservices/outputs.tf b/blueprints/serverless/cloud-run-microservices/outputs.tf index 250722776..5646985d4 100644 --- a/blueprints/serverless/cloud-run-microservices/outputs.tf +++ b/blueprints/serverless/cloud-run-microservices/outputs.tf @@ -16,7 +16,10 @@ output "custom_domain" { description = "Custom domain for the Application Load Balancer." - value = var.service_project.project_id != null ? "http://${var.custom_domain}" : "none" + value = ( + try(var.project_configs.service.project_id, null) != null + ? "http://${var.custom_domain}" : "none" + ) } output "default_URL_svc_a" { @@ -31,5 +34,8 @@ output "default_URL_svc_b" { output "load_balancer_ip" { description = "Load Balancer IP address." - value = var.service_project.project_id != null ? module.int-alb[0].address : "none" + value = ( + try(var.project_configs.service.project_id, null) != null + ? module.int-alb[0].address : "none" + ) } diff --git a/blueprints/serverless/cloud-run-microservices/variables.tf b/blueprints/serverless/cloud-run-microservices/variables.tf index a17fe9e3c..5f07ac9f0 100644 --- a/blueprints/serverless/cloud-run-microservices/variables.tf +++ b/blueprints/serverless/cloud-run-microservices/variables.tf @@ -32,15 +32,6 @@ variable "ip_ranges" { } } -variable "main_project" { - description = "Main (or host) project." - type = object({ - billing_account_id = optional(string) - parent = optional(string) - project_id = string - }) -} - variable "prefix" { description = "Prefix used for project names." type = string @@ -50,22 +41,30 @@ variable "prefix" { } } +variable "project_configs" { + description = "Projects to use, one project or host and service projects." + type = map(object({ + billing_account_id = optional(string) + parent = optional(string) + project_id = optional(string) + })) + default = { + main = {} # Or host project + service = {} + } + nullable = false + validation { + condition = var.project_configs.main.project_id != null + error_message = "At least the main project ID is needed." + } +} + variable "region" { description = "Cloud region where resources will be deployed." type = string default = "europe-west1" } -variable "service_project" { - description = "Service project." - type = object({ - billing_account_id = optional(string) - parent = optional(string) - project_id = optional(string) - }) - default = {} -} - variable "svc_a_image" { description = "Container image to deploy in service A." type = string