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

Linked clones depend on `template_uuid` instead of `snapshot_id`

Open rickhlx opened this issue 5 years ago • 8 comments

Terraform Version

v0.12.3

vSphere Provider Version

v1.12.0

Affected Resource(s)

Please list the resources as a list, for example:

  • vsphere_virtual_machine
  • vsphere_virtual_machine_snapshot

If this issue appears to affect multiple resources, it may be an issue with Terraform's core, so please mention this.

Terraform Configuration Files

resource "random_string" "master_password" {
  length  = 16
  special = true
}

resource "vsphere_virtual_machine" "rdsh_master" {
  name             = var.master_name
  datastore_id     = data.vsphere_datastore.ds.id
  num_cpus         = 4
  memory           = 4096
  guest_id         = data.vsphere_virtual_machine.master_template.guest_id
  resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
  scsi_type        = data.vsphere_virtual_machine.master_template.scsi_type
  annotation       = "Created by Terraform"
  firmware         = data.vsphere_virtual_machine.master_template.firmware

  wait_for_guest_net_timeout = 0

  network_interface {
    network_id   = data.vsphere_network.network.id
    adapter_type = "vmxnet3"
  }

  disk {
    label            = "disk0"
    size             = var.clone_disk_size
    thin_provisioned = true
    eagerly_scrub    = false
  }

  clone {
    template_uuid = data.vsphere_virtual_machine.master_template.id

    customize {

      windows_options {
        computer_name     = "USRDSH-PC-1"
        organization_name = "Org"
        workgroup         = "WORKGROUP"
        time_zone         = 35
        admin_password    = random_string.master_password.result
      }
      network_interface {}

      timeout = 30
    }
  }

}

resource "vsphere_virtual_machine_snapshot" "vm_snapshot" {
  virtual_machine_uuid = vsphere_virtual_machine.rdsh_master.id
  snapshot_name        = "baseline"
  description          = "Managed Snapshot by Terraform for Linked Clones"
  memory               = "false"
  quiesce              = "false"
  remove_children      = "true"
  consolidate          = "true"
}

resource "vsphere_virtual_machine" "rdsh_clone" {
  name             = format("%s-%d", var.clone_prefix, count.index)
  count            = var.clone_count
  datastore_id     = data.vsphere_datastore.ds.id
  num_cpus         = var.clone_num_cpus
  memory           = var.clone_memory
  guest_id         = data.vsphere_virtual_machine.master_template.guest_id
  resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
  scsi_type        = data.vsphere_virtual_machine.master_template.scsi_type
  annotation       = "Created by Terraform"
  firmware         = data.vsphere_virtual_machine.master_template.firmware

  wait_for_guest_net_timeout = 0

  network_interface {
    network_id   = data.vsphere_network.network.id
    adapter_type = "vmxnet3"
  }

  disk {
    label            = "disk0"
    size             = var.clone_disk_size
    thin_provisioned = true
    eagerly_scrub    = false
  }

  clone {
    template_uuid = vsphere_virtual_machine.rdsh_master.id
    linked_clone  = true

    customize {
      windows_options {
        computer_name         = format("%s-%d", var.clone_prefix, count.index)
        join_domain           = "domain"
        organization_name     = "Org"
        domain_admin_user     = var.domain_admin_user
        domain_admin_password = var.domain_admin_password
        time_zone             = 35
        admin_password        = vsphere_virtual_machine_snapshot.vm_snapshot.id
      }

      network_interface {}

      timeout = 30
    }
  }

  depends_on = [vsphere_virtual_machine_snapshot.vm_snapshot]
}

Expected Behavior

When deleting the snapshot a new snapshot should be created, triggering new linked clones to be created (as a result of the password for the machines being the snapshot id).

Actual Behavior

Liked clone resources cannot find the snapshot associated to the template (since it does not exist) and throws an error.

Would expect that snapshot is created, then linked clones.

Steps to Reproduce

Please list the steps required to reproduce the issue, for example:

  1. terraform apply
  2. delete snapshot manually
  3. terraform plan (fails)

Important Factoids

I believe the way to fix this is by optionally asking for a snapshot id when creating linked clones. And forcing a new resource if the id changes.

rickhlx avatar Aug 26 '19 12:08 rickhlx

Thanks @rickhlx! This is an excellent suggestion. Adding a snapshot_id would help with several clone related improvements. We'll need to look more closely at if there would be any unexpected difficulties or issues, but it seems like a good idea.

bill-rich avatar Sep 14 '19 22:09 bill-rich

I would love this feature. Any plans for this to be implemented?

shantur avatar Apr 15 '20 14:04 shantur

Some problem ((

melmandd avatar May 15 '20 14:05 melmandd

..Bump..

shantur avatar Jul 13 '20 22:07 shantur

The work to do this actually isn't too hard. I implemented PR #1158, and it's right near the same spot at internal/vmworkflow/virtual_machine_clone_subresource.go.

Literally just need to add the "snapshot_id" attribute in the VirtualMachineCloneSchema function, and then in the ExpandVirtualMachineCloneSpec function there's a case near the following block of code where you need to snag the id with d.get("clone.0.snapshot_id") and use it to find the snapshot instead of using the current one.

        if d.Get("clone.0.linked_clone").(bool) {
...
                        spec.Snapshot = vprops.Snapshot.CurrentSnapshot
...

arizvisa avatar Aug 07 '20 00:08 arizvisa

This was tagged with "help wanted". However, as I don't work with Terraform and vSphere anymore, if I add the "snapshot_id" attribute to the schema, would someone else be able to test it and perhaps add the field to the documentation? I'd be developing it blindly, but it's literally the addition of a few lines of code...

arizvisa avatar Nov 01 '21 20:11 arizvisa

@arizvisa I'll be happy to flag this one for contribution once others PRs are merged.

Ryan @tenthirtyam

tenthirtyam avatar Nov 01 '21 20:11 tenthirtyam

Yeah. It seems really simple to add and the issue is 3 years old. Plus, there isn't even a PR to start the review process for improving this. Like I noted, I'd be fixing a lot more in this provider because vmware is super-pervasive in the industry I work in, and I hate seeing some of these minor vmware-related issues get stagnant compared to the other platforms.

Unfortunately, I haven't needed to use Terraform since my contributions to this provider from last year, so there's no company interest/sponsorship with regards to any kind of involvement from my end. But at the very least, I can only really try to help with triaging and similar stuff during my off-time.

arizvisa avatar Nov 01 '21 23:11 arizvisa