From 97fe735c24b69f3e1491d36829bbee9137c14c8f Mon Sep 17 00:00:00 2001 From: bryan <31219516+bryan-bar@users.noreply.github.com> Date: Wed, 10 May 2023 09:49:50 -0700 Subject: [PATCH] Fix: tags regression from PR-56 (#58) * tags from object to map changed the behavior of default and broke old workflows which did not define 'created_by' * validations removed as defaults cannot be set for map until after the variable is set. * locals used to create defaults * Update templates to grab tags from the specification module's outputs and not directly from the specification module's variables. --- edbterraform/data/templates/aws/network.tf.j2 | 8 +++--- .../data/templates/aws/region_peering.tf.j2 | 4 +-- .../data/templates/azure/network.tf.j2 | 4 +-- .../aws/modules/specification/outputs.tf | 12 ++++++--- .../aws/modules/specification/variables.tf | 12 ++++----- .../azure/modules/specification/outputs.tf | 12 ++++++--- .../azure/modules/specification/variables.tf | 11 +++----- .../gcloud/modules/specification/outputs.tf | 26 +++++++++++-------- .../gcloud/modules/specification/variables.tf | 5 ++++ 9 files changed, 53 insertions(+), 41 deletions(-) diff --git a/edbterraform/data/templates/aws/network.tf.j2 b/edbterraform/data/templates/aws/network.tf.j2 index 77c4c188..2824f2d0 100644 --- a/edbterraform/data/templates/aws/network.tf.j2 +++ b/edbterraform/data/templates/aws/network.tf.j2 @@ -4,7 +4,7 @@ module "vpc_{{ region_ }}" { vpc_cidr_block = try(module.spec.base.regions["{{ region }}"].cidr_block, null) vpc_tag = var.vpc_tag name_id = module.spec.hex_id - tags = module.spec.tags + tags = module.spec.base.tags providers = { aws = aws.{{ region_ }} @@ -22,7 +22,7 @@ module "network_{{ region_ }}" { public_subnet_tag = var.public_subnet_tag cidr_block = each.value.cidr availability_zone = each.value.zone - tags = module.spec.tags + tags = module.spec.base.tags depends_on = [module.vpc_{{ region_ }}] @@ -39,7 +39,7 @@ module "routes_{{ region_ }}" { project_tag = var.project_tag public_cidrblock = var.public_cidrblock cluster_name = module.spec.base.tags.cluster_name - tags = module.spec.tags + tags = module.spec.base.tags depends_on = [module.network_{{ region_ }}] @@ -57,7 +57,7 @@ module "security_{{ region_ }}" { ports = try(module.spec.region_ports["{{ region }}"], []) ingress_cidrs = module.spec.region_cidrblocks egress_cidrs = module.spec.region_cidrblocks - tags = module.spec.tags + tags = module.spec.base.tags depends_on = [module.routes_{{ region_ }}] diff --git a/edbterraform/data/templates/aws/region_peering.tf.j2 b/edbterraform/data/templates/aws/region_peering.tf.j2 index b4ecd587..2345c535 100644 --- a/edbterraform/data/templates/aws/region_peering.tf.j2 +++ b/edbterraform/data/templates/aws/region_peering.tf.j2 @@ -7,7 +7,7 @@ module "vpc_peering_{{ requester_ }}_{{ accepter_ }}" { vpc_id = module.vpc_{{ requester_ }}.vpc_id peer_vpc_id = module.vpc_{{ accepter_ }}.vpc_id peer_region = "{{ accepter }}" - tags = module.spec.tags + tags = module.spec.base.tags depends_on = [module.vpc_{{ requester_ }}, module.vpc_{{ accepter_ }}] @@ -20,7 +20,7 @@ module "vpc_peering_accepter_{{ requester_}}_{{ accepter_ }}" { source = "./modules/vpc_peering_accepter" connection_id = module.vpc_peering_{{ requester_ }}_{{ accepter_ }}.id - tags = module.spec.tags + tags = module.spec.base.tags depends_on = [module.vpc_peering_{{ requester_ }}_{{ accepter_ }}] diff --git a/edbterraform/data/templates/azure/network.tf.j2 b/edbterraform/data/templates/azure/network.tf.j2 index 80b9434f..0dd6fe5e 100644 --- a/edbterraform/data/templates/azure/network.tf.j2 +++ b/edbterraform/data/templates/azure/network.tf.j2 @@ -4,7 +4,7 @@ module "vpc_{{ region_ }}"{ name = "${var.vpc_tag}-{{ region }}-${module.spec.hex_id}" cidr_blocks = [ lookup(lookup(module.spec.base.regions, "{{ region }}"), "cidr_block") ] region = "{{ region }}" - tags = module.spec.tags + tags = module.spec.base.tags providers = { azurerm = azurerm.{{ region_ }} @@ -45,7 +45,7 @@ module "security_{{ region_ }}" { ports = try(module.spec.region_ports["{{ region }}"], []) ingress_cidrs = module.spec.region_cidrblocks egress_cidrs = module.spec.region_cidrblocks - tags = module.spec.tags + tags = module.spec.base.tags depends_on = [module.network_{{ region_ }}] diff --git a/edbterraform/data/terraform/aws/modules/specification/outputs.tf b/edbterraform/data/terraform/aws/modules/specification/outputs.tf index f87acc27..ac5af63c 100644 --- a/edbterraform/data/terraform/aws/modules/specification/outputs.tf +++ b/edbterraform/data/terraform/aws/modules/specification/outputs.tf @@ -1,13 +1,17 @@ -output "base" { - value = var.spec -} - locals { tags = merge(var.spec.tags, { # add ids for tracking terraform_hex = random_id.apply.hex terraform_id = random_id.apply.id terraform_time = time_static.first_created.id + created_by = local.created_by + cluster_name = local.cluster_name + }) +} + +output "base" { + value = merge(var.spec, { + "tags" = local.tags }) } diff --git a/edbterraform/data/terraform/aws/modules/specification/variables.tf b/edbterraform/data/terraform/aws/modules/specification/variables.tf index 76b8f062..f535e3c3 100644 --- a/edbterraform/data/terraform/aws/modules/specification/variables.tf +++ b/edbterraform/data/terraform/aws/modules/specification/variables.tf @@ -129,11 +129,9 @@ variable "spec" { })), {}) }) - validation { - condition = can(var.spec.tags.cluster_name) && can(var.spec.tags.created_by) - error_message = <<-EOT - cluster_name and created_by need to be defined under tags - Tags: ${jsonencode(var.spec.tags)} - EOT - } +} + +locals { + cluster_name = can(var.spec.tags.cluster_name) ? var.spec.tags.cluster_name : "AWS-Cluster-default" + created_by = can(var.spec.tags.created_by) ? var.spec.tags.created_by : "EDB-Terraform-AWS" } diff --git a/edbterraform/data/terraform/azure/modules/specification/outputs.tf b/edbterraform/data/terraform/azure/modules/specification/outputs.tf index 775695f1..20a4a650 100644 --- a/edbterraform/data/terraform/azure/modules/specification/outputs.tf +++ b/edbterraform/data/terraform/azure/modules/specification/outputs.tf @@ -1,13 +1,17 @@ -output "base" { - value = var.spec -} - locals { tags = merge(var.spec.tags, { # add ids for tracking terraform_hex = random_id.apply.hex terraform_id = random_id.apply.id terraform_time = time_static.first_created.id + created_by = local.created_by + cluster_name = local.cluster_name + }) +} + +output "base" { + value = merge(var.spec, { + "tags" = local.tags }) } diff --git a/edbterraform/data/terraform/azure/modules/specification/variables.tf b/edbterraform/data/terraform/azure/modules/specification/variables.tf index 664c1151..0c8e461f 100644 --- a/edbterraform/data/terraform/azure/modules/specification/variables.tf +++ b/edbterraform/data/terraform/azure/modules/specification/variables.tf @@ -131,12 +131,9 @@ EOT ) } - validation { - condition = can(var.spec.tags.cluster_name) && can(var.spec.tags.created_by) - error_message = <<-EOT - cluster_name and created_by need to be defined under tags - Tags: ${jsonencode(var.spec.tags)} - EOT - } +} +locals { + cluster_name = can(var.spec.tags.cluster_name) ? var.spec.tags.cluster_name : "Azure-Cluster-default" + created_by = can(var.spec.tags.created_by) ? var.spec.tags.created_by : "EDB-Terraform-Azure" } diff --git a/edbterraform/data/terraform/gcloud/modules/specification/outputs.tf b/edbterraform/data/terraform/gcloud/modules/specification/outputs.tf index fb9ce6e2..6b38eff3 100644 --- a/edbterraform/data/terraform/gcloud/modules/specification/outputs.tf +++ b/edbterraform/data/terraform/gcloud/modules/specification/outputs.tf @@ -1,19 +1,23 @@ -output "base" { - value = var.spec -} - locals { - # add ids for tracking - # gcloud label restrictions: - # - lowercase letters, numeric characters, underscores and dashes - # - 63 characters max - # to match other providers as close as possible, - # we will do any needed handling and continue to treat - # key-values as tags even though they are labels under gcloud tags = merge(var.spec.tags, { + # add ids for tracking + # gcloud label restrictions: + # - lowercase letters, numeric characters, underscores and dashes + # - 63 characters max + # to match other providers as close as possible, + # we will do any needed handling and continue to treat + # key-values as tags even though they are labels under gcloud terraform_hex = lower(random_id.apply.hex) terraform_id = lower(random_id.apply.id) terraform_time = lower(replace(time_static.first_created.id,":","_")) + created_by = lower(local.created_by) + cluster_name = lower(local.cluster_name) + }) +} + +output "base" { + value = merge(var.spec, { + "tags" = local.tags }) } diff --git a/edbterraform/data/terraform/gcloud/modules/specification/variables.tf b/edbterraform/data/terraform/gcloud/modules/specification/variables.tf index 2e2f335d..8f2903ad 100644 --- a/edbterraform/data/terraform/gcloud/modules/specification/variables.tf +++ b/edbterraform/data/terraform/gcloud/modules/specification/variables.tf @@ -142,3 +142,8 @@ EOT ) } } + +locals { + cluster_name = can(var.spec.tags.cluster_name) ? var.spec.tags.cluster_name : "GCloud-Cluster-default" + created_by = can(var.spec.tags.created_by) ? var.spec.tags.created_by : "EDB-Terraform-GCloud" +}