Skip to content

Commit

Permalink
Fix: tags regression from PR-56 (#58)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
bryan-bar authored May 10, 2023
1 parent b6cd998 commit 97fe735
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 41 deletions.
8 changes: 4 additions & 4 deletions edbterraform/data/templates/aws/network.tf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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_ }}
Expand All @@ -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_ }}]

Expand All @@ -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_ }}]

Expand All @@ -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_ }}]

Expand Down
4 changes: 2 additions & 2 deletions edbterraform/data/templates/aws/region_peering.tf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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_ }}]

Expand All @@ -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_ }}]

Expand Down
4 changes: 2 additions & 2 deletions edbterraform/data/templates/azure/network.tf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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_ }}
Expand Down Expand Up @@ -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_ }}]

Expand Down
12 changes: 8 additions & 4 deletions edbterraform/data/terraform/aws/modules/specification/outputs.tf
Original file line number Diff line number Diff line change
@@ -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
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Original file line number Diff line number Diff line change
@@ -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
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
26 changes: 15 additions & 11 deletions edbterraform/data/terraform/gcloud/modules/specification/outputs.tf
Original file line number Diff line number Diff line change
@@ -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
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit 97fe735

Please sign in to comment.