Skip to content

Problem Deleting Disk in Terraform Nutanix Provider – Incorrect Disk Removal and Index Shifting #804

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

Open
akli-ime opened this issue Mar 25, 2025 · 2 comments

Comments

@akli-ime
Copy link
Contributor

akli-ime commented Mar 25, 2025

Terraform v1.9.6
Nutanix v2.0.0

Issue Summary
When attempting to remove a specific disk from a virtual machine using the nutanix_virtual_machine_v2 resource, Terraform does not delete the correct disk. Instead, it shifts the index of all subsequent disks, causing unintended changes to disk assignments.

Terraform Configuration Example

Given the following disk configuration:

`
disks = [
{ id = "disk-1", size_gb = 140 , index = 1 },
{ id = "disk-2", size_gb = 130 , index = 2 },
//{ id = "disk-3", size_gb = 100 , index = 3 },
{ id = "disk-4", size_gb = 40 , index = 4 },
{ id = "disk-5", size_gb = 30 , index = 5 },
{ id = "disk-6", size_gb = 40 , index = 6 }

]
`
Disks input is a list of object

nutanix_virtual_machine_v2.vm will be updated in-place

`
variable "disks" {

type = list(object({

id      = string

index   = number

size_gb = number

}))
default = []
}
`

Terraform conf

` dynamic "disks" {

for_each = { for disk in var.disks : disk.id => disk if disk.size_gb > 0 }

content {

  disk_address {

    bus_type = "SCSI"


    index    = disks.value.index
  }

  backing_info {

    vm_disk {

      disk_size_bytes = 1024 * 1024 * 1024 * disks.value.size_gb

      storage_container {

        ext_id = data.nutanix_storage_containers_v2.storage-containers.storage_containers[0].ext_id

      }
    }

  }
}

}
`
When I remove disk-3, I expect only disk-3 to be deleted. However, the behavior I see is that Terraform:

Deletes disk-6 instead.

Moves disk-5 → index 6

Moves disk-4 → index 5

Moves disk-3 (which is now missing) → index 4

Terraform Plan Output

`
~ update in-place

Terraform will perform the following actions:

~ resource "nutanix_virtual_machine_v2" "vm" {

    id                           = "06bda877-0734-4ef0-700f-f82a4d2bc55a"

    name                         = "DCDFYRE"

    # (24 unchanged attributes hidden)

  ~ disks {
        # (1 unchanged attribute hidden)

      ~ backing_info {
          ~ vm_disk {
              ~ disk_size_bytes          = 107374182400 -> 42949672960
                # (2 unchanged attributes hidden)

                # (1 unchanged block hidden)
            }
        }

      ~ disk_address {
          ~ index    = 3 -> 4
            # (1 unchanged attribute hidden)
        }
    }
  ~ disks {
        # (1 unchanged attribute hidden)

      ~ backing_info {
          ~ vm_disk {
              ~ disk_size_bytes          = 42949672960 -> 32212254720
                # (2 unchanged attributes hidden)

                # (1 unchanged block hidden)
            }
        }

      ~ disk_address {
          ~ index    = 4 -> 5
            # (1 unchanged attribute hidden)
        }
    }
  ~ disks {
        # (1 unchanged attribute hidden)

      ~ backing_info {
          ~ vm_disk {
              ~ disk_size_bytes          = 32212254720 -> 42949672960
                # (2 unchanged attributes hidden)

                # (1 unchanged block hidden)
            }
        }

      ~ disk_address {
          ~ index    = 5 -> 6
            # (1 unchanged attribute hidden)
        }
    }
  - disks {
      - ext_id = "ccb576a3-cead-4f72-99b9-4a5d3255eb0f" -> null

      - backing_info {
          - vm_disk {
              - disk_ext_id              = "ccb576a3-cead-4f72-99b9-4a5d3255eb0f" -> null
              - disk_size_bytes          = 42949672960 -> null
              - is_migration_in_progress = false -> null

              - storage_container {
                  - ext_id = "1f6f93d7-bfbc-44b7-9fe2-27766db07903" -> null
                }
            }
        }

      - disk_address {
          - bus_type = "SCSI" -> null
          - index    = 6 -> null
        }
    }

    # (12 unchanged blocks hidden)
}

`

This behavior incorrectly shifts disk indices instead of deleting disk-3 directly.

Expected Behavior

Terraform should only remove the specified disk without modifying the indices of other disks.

NB: This test is possible thanks to pull request : #761

Regards

@GullapalliAkhil
Copy link
Collaborator

We were able to reproduce the issue. Here is the detailed RCA

Context: From user's perspective, I attempted to create a Virtual Machine with 4 disks using the current approach — Inline Nested Block (a list-based schema where disks are defined directly inside the virtual_machine_v2 resource block).

Terraform Config — Initial Disk Configuration for creating a VM

resource “virtual_machine_v2” “create_vm” {
disks {

    // Disk at index 0
    // vm_disk-data_source
  }
  disks {
    // disk at index 1
    // vm_disk-storage container
  }
  disks {
     // disk at index 2
     // vm_disk-storage container
  }
  disks {
      //disk at index 3
      // vm_disk-storage container
  }
}

Once Virtual Machine creation is successful, I tried to delete the disk at index 1, now the user Terraform config looks like

resource “virtual_machine_v2” “create_vm” {
disks {
    // Disk at index 0
    // vm_disk-data_source
  }
  disks {
     // disk at index 2
     // vm_disk-storage container
  }
  disks {
      //disk at index 3
      // vm_disk-storage container
  }
}

Observations:
When user did Terraform Apply on above config, What the System Does (Behind the Scenes):

Disk at index 1 is updated with the disk content of index 2.
Disk at index 2 is updated with the disk content of index 3.
The disk at index 3 is finally removed from the list.

This gives the illusion to the user that the disk at index 1 was deleted, but technically:

No disk was deleted at index 1.
The contents of subsequent disks are shifted left.
The last disk (index 3) is deleted to maintain size consistency.

This is the limitation with Terraform, it tracks repeated inline blocks as ordered lists and matches them by position rather than content, so any insertions, deletions, or reordering can cause it to shift items, update the wrong blocks, or destroy and recreate resources unintentionally.(The same behavior will be seen for NICS, CD_ROMS)

Proposed Solution:

I’m thinking of why we can’t have a dedicated resource for disks(something like nutanix_virtual_machine_disk), where each disk is a uniquely identifiable resource, can be created, modified, and deleted without impacting others and avoids index shifting issues entirely. It helps to

  • Greatly enhance user control.
  • Prevent cascading disk updates.
  • Bring Nutanix in line with other major cloud providers.( Major Cloud providers like AWS and Azure follow a similar approach when dealing with disks)

@akli-ime
Copy link
Contributor Author

Thank you @GullapalliAkhil for your reply.

I think it's a great idea to externalize the disks to another resource. This will fix the suppression bug and make disk handling simpler and more efficient.
We are looking forward to the release of this new functionality.

Best regards,

@GullapalliAkhil GullapalliAkhil marked this as a duplicate of #505 May 29, 2025
@GullapalliAkhil GullapalliAkhil marked this as a duplicate of #690 May 29, 2025
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

2 participants