digitalocean / terraform-provider-digitalocean

Terraform DigitalOcean provider
https://registry.terraform.io/providers/digitalocean/digitalocean/latest/docs
Mozilla Public License 2.0
503 stars 270 forks source link

After importing `digitalocean_volume` to TF state, `initial_filesystem_*` attributes triggers recreation #984

Open phandox opened 1 year ago

phandox commented 1 year ago

Bug Report


Describe the bug

I had empty state and imported created digitalocean_volume via terraform import digitalocean_volume.volume <ID> .

My definition of volume is this:

resource "digitalocean_volume" "volume" {
  name                     = var.name
  region                   = var.region
  size                     = var.size
  initial_filesystem_type  = "ext4"
  initial_filesystem_label = var.fs_label
  tags                     = concat(var.tags, [var.env])
}

After running Terraform plan, it tries to recreate it, which is something I don't want to do:

digitalocean_volume.volume: Refreshing state... [id=fbb475a4-ca1d-11ed-9d7b-0a58ac14d0b2]

Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
-/+ destroy and then create replacement

Terraform will perform the following actions:

  # digitalocean_volume.volume must be replaced
-/+ resource "digitalocean_volume" "volume" {
      ~ droplet_ids              = [] -> (known after apply)
      ~ filesystem_label         = "pgsql_prod_data" -> (known after apply)
      ~ filesystem_type          = "ext4" -> (known after apply)
      ~ id                       = "<ID of volume>" -> (known after apply)
      + initial_filesystem_label = "pgsql_prod_data" # forces replacement
      + initial_filesystem_type  = "ext4" # forces replacement
        name                     = "pgsql-prod-data"
        tags                     = [
            "prod",
        ]
      ~ urn                      = "do:volume:<ID of volume>" -> (known after apply)
        # (2 unchanged attributes hidden)
    }

Plan: 1 to add, 0 to change, 1 to destroy.

var.fs_label still contains the same value as before (pgsql_prod_data). When I comment the initial_filesystem_* attributes, the terraform plan reports no changes.

Affected Resource(s)

Expected Behavior

Terraform plan should report no changes after import to state on digitalocean_volume

Actual Behavior

Terraform plan tries to recreated the volume, which is undesirable and causes lost of data in volume

Steps to Reproduce

> 1. Create a volume in Digital Ocean via CLI / UI 2. Import it to Terraform state with `initial_filesystem_*` attributes filled in, reflecting the resource made via console 3. Terraform plan wants to recreate volume. **Terraform Configuration Files**

main.tf

resource "digitalocean_volume" "volume" {
  name                     = var.name
  region                   = var.region
  size                     = var.size
  initial_filesystem_type  = "ext4"
  initial_filesystem_label = var.fs_label
  tags                     = concat(var.tags, [var.env])
}

output "volume_id" {
  value = digitalocean_volume.volume.id
}

variables.tf

variable "name" {
  type        = string
  description = "Name of network volume"
}
variable "region" {
  type        = string
  default     = "fra1"
  description = "Region of network volume. Must match droplet"
}
variable "size" {
  default     = 1
  type        = number
  description = "Volume size in GiB"
}

variable "env" {
  type        = string
  description = "Environment for volume"
}

variable "fs_label" {
  default     = "example"
  type        = string
  description = "Filesystem label for volume, useful for mounting"

  validation {
    condition     = length(var.fs_label) <= 16
    error_message = "Max 16 characters for FS label for ext4 filesystem"
  }
}

variable "tags" {
  default     = []
  type        = list(string)
  description = "Additional tags to be assigned on volume"
}

Terraform version

Terraform - v1.3.9 digitalocean provider - v2.26.0

Debug Output

Panic Output

Additional context

Important Factoids

References

mohsenSy commented 1 year ago

@phandox thanks for reporting this, after investigating the code I found that initial_filesystem_type and initial_filesystem_label are not set for new resources when reading them here. I will send a PR to fix this tomorrow after considering acceptance test for this.