hetznercloud / terraform-provider-hcloud

Terraform Hetzner Cloud provider
https://registry.terraform.io/providers/hetznercloud/hcloud/latest
Mozilla Public License 2.0
479 stars 72 forks source link

[Bug]: [hcloud_ssh_key] Error: Provider produced inconsistent result after apply #921

Closed trombonax closed 2 months ago

trombonax commented 2 months ago

What happened?

opentofu-1.6.2

terraform-1.8.2

provider[registry.terraform.io/hetznercloud/hcloud] = 1.46.1

provider[registry.opentofu.org/hetznercloud/hcloud] = 1.46.1

~$ tofu / terraform apply
╷
│ Error: Provider produced inconsistent result after apply
│ 
│ When applying changes to module.ssh_key["key01"].hcloud_ssh_key.this, provider "provider[\"registry.terraform.io/hetznercloud/hcloud\"]" produced an unexpected new value: .labels: was cty.MapValEmpty(cty.String), but now
│ null.
│ 
│ This is a bug in the provider, which should be reported in the provider's own issue tracker.
╵
╷
│ Error: Provider produced inconsistent result after apply
│ 
│ When applying changes to module.ssh_key["key02"].hcloud_ssh_key.this, provider "provider[\"registry.terraform.io/hetznercloud/hcloud\"]" produced an unexpected new value: .labels: was cty.MapValEmpty(cty.String), but now null.
│ 
│ This is a bug in the provider, which should be reported in the provider's own issue tracker.

What did you expect to happen?

simply ssh key add as resource via module

Please provide a minimal working example

module "hcloud-ssh-key":

# -- main

resource "hcloud_ssh_key" "this" {

  lifecycle {
    ignore_changes        = [fingerprint] # TODO: bug
    create_before_destroy = false
  }

  name       = var.key_name
  public_key = var.key
  labels     = try(var.labels, null)
}

# -- vars

variable "key_name" {
  description = "Hetzner Cloud SSH key name"
  type        = string
  default     = ""
}

variable "key" {
  description = "Hetzner Cloud SSH key content"
  type        = string
  default     = ""
  sensitive   = true
}

variable "labels" {
  description = "Hetzner Cloud ssh key user-defined labels"
  type        = map(any)
  default     = {}
}

module call from manifest:

module "ssh_key" {

  for_each = var.ssh_key

  source = "../terraform-modules/hcloud-ssh-key"

  key_name = each.value.name
  key      = each.value.key
}

variable "ssh_key" {
  description = "Hetzner Cloud SSH keys"
  type        = map(any)
  default = {
    "key01" = {
      name = "key01"
      key  = "ssh-ed25519 <key01>"
    },
    "key02" = {
      name = "key02"
      key  = "ssh-ed25519 <key02>"
    }
  }
}
apricote commented 2 months ago

Hey @trombonax,

thank you very much for reporting this issue! This was recently refactored in #855, #881 and #817 and we did not catch this with our test suite.

The issue happens once you assign an empty map to labels: labels = {} which happens in your code in try(var.labels, null), as var.labels is set to {} by default.

We are working on a fix and an extended test suite to make sure this does not get reintroduced. Until that is released you can fix your issue, but using null instead of {} as the default for your labels. I think you can even remove the try().

trombonax commented 1 month ago

@apricote hello with provider 1.47.0 is all fine, I'm doesn't see reported issue anymore thank you !