terraform-provider-tfe icon indicating copy to clipboard operation
terraform-provider-tfe copied to clipboard

Applying tags with uppercase letters to a workspace causes the workspace to be modified each time a plan is run.

Open Josh-Tracy opened this issue 3 years ago • 3 comments
trafficstars

Terraform Cloud/Enterprise version

Terraform Cloud

Terraform version

Terraform v1.2.3
on darwin_arm64
+ provider registry.terraform.io/hashicorp/tfe v0.32.0

Terraform Configuration Files

I'm calling a module one directory down

module "workspacer" {
  source = "../.."

  organization   = "ise-joshua-tracy"
  workspace_name = "workspacer-test"
  workspace_desc = "Alex's worksapcer test"
  workspace_tags = ["azure", "infra", "test", "Alex"]
}

The modules workspace resource

resource "tfe_workspace" "ws" {
  organization                  = var.organization
  name                          = var.workspace_name
  description                   = var.workspace_desc
  execution_mode                = var.execution_mode
  agent_pool_id                 = var.agent_pool_id
  auto_apply                    = var.auto_apply
  terraform_version             = var.terraform_version
  working_directory             = var.working_directory
  global_remote_state           = var.global_remote_state
  remote_state_consumer_ids     = var.remote_state_consumer_ids
  structured_run_output_enabled = var.structured_run_output_enabled
  ssh_key_id                    = var.ssh_key_id
  allow_destroy_plan            = var.allow_destroy_plan
  tag_names                     = var.workspace_tags

Expected Behavior

  1. Terraform plan and apply creates the workspace with the workspace_tags
  2. Each additional plan should show no modifications if the tags are not modified

Actual Behavior

  1. Terraform plan and apply is creating the workspace with the workspace_tags as expected
  2. Terraform is modifying the uppercase letters in the Alex tag to lowercase when adding it to the workspace
  3. Each additional plan is attempting to replace the lowercase tag on the existing resource with the uppercase one, causing modifications.
  # module.workspacer.tfe_workspace.ws will be updated in-place
  ~ resource "tfe_workspace" "ws" {
        id                            = "ws-i65QPpVEqBRG94LM"
        name                          = "workspacer-test"
      ~ tag_names                     = [
          + "Alex",
          - "alex",
            # (3 unchanged elements hidden)
        ]
        # (14 unchanged attributes hidden)
    }

Additional Context

I have attempted to find documentation on whether or not this is to be expected, or if there is a recommendation to only use lowercase lettering to avoid this, but I could not.

Josh-Tracy avatar Jun 21 '22 16:06 Josh-Tracy

@Josh-Tracy while we evaluate potential solutions, you can try using the terraform lower function when setting your tag names:

resource "tfe_workspace" "example_workspace" {
  organization = "hashicorp"
  name = "example-workspace"
  tag_names = [for tag in ["lowercase", "MiXeDcAsE"]: lower(tag)]
}

brandonc avatar Jun 21 '22 19:06 brandonc

Hi @Josh-Tracy ! Did some more investigation and it turns out the API auto downcases tag names, which is the reason for this conflict (your configuration and state are never in sync). Unfortunately there is no trivial way for you to continue using tags containing uppercases because your configuration will always describe an intent that state cannot match (hence the always performing an update with each run)... you're saying I want workspaceA with a tag Foo but the API will only ever understand workspaceA with tag foo. Now this is all true barring an API change to ease on case sensitivity for workspace tags -- but that's a more complicated discussion and likely a breaking change.

The weird part is the API accepts a tag name containing uppercases which I imagine can lead to even more confusion. There also isn't much documentation referring to the down-casing behavior so I'll make a PR include it in the docs for the provider.

sebasslash avatar Jun 27 '22 22:06 sebasslash

Thank you for that explanation!

Josh-Tracy avatar Jun 28 '22 11:06 Josh-Tracy