Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling of var.name #111

Open
officel opened this issue Jan 30, 2024 · 0 comments
Open

Handling of var.name #111

officel opened this issue Jan 30, 2024 · 0 comments

Comments

@officel
Copy link

officel commented Jan 30, 2024

Hi masters!

I saw #106 merged into master.
If I modify our code it will be fine, but I think I could also write the following.
Would you please think once before creating a new release. 🙇

variable "name" {
  default = ""
}

locals {
  tags = {
    name = "locals.tags.name"
    test = "locals.tags.test"
  }
  tags2 = {
    // no name
    test = "locals.tags2.test"
  }
}

// Case A: name is attached to the tag
output "a1_tags" {
  description = "I have already added name to the tag."
  value       = merge(local.tags)
}
output "a2_plan_diff_no_good" {
  description = "This fix (code must be modified or it will be a diff. var.name is not required, is it?)"
  value       = merge(local.tags, { "name" = var.name })
}
output "a3_we_need" {
  description = "This way, the existing tags.name is not disturbed (but there is still the problem of separation from the resource name)."
  value       = merge({ "name" = var.name }, local.tags)
}

// Case B: name was not attached to the tag
output "b1_tags" {
  value = merge(local.tags2)
}
output "b2_plan_diff_maybe_ok" {
  value = merge(local.tags2, { "name" = var.name })
}
output "b3_like_fix_a3_ok" {
  value = merge({ "name" = var.name }, local.tags2)
}


output "case_a_maybe_good4name" {
  value = "name = ${try(local.tags.name, var.name, "default name")}"
}
output "case_a_maybe_good4tags" {
  value = merge({ "name" = var.name }, local.tags)
}

output "case_b_maybe_good4name" {
  value = "name = ${try(local.tags2.name, var.name, "default name")}"
}
output "case_b_maybe_good4tags" {
  value = merge({ "name" = var.name }, local.tags2)
}


check "name" {
  assert {
    condition     = var.name != ""
    error_message = "var.name is empty. try to terraform plan -var name=\"var.name\"."
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant