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

Add support for datastore_id in data.vsphere_virtual_machine.whatever.disks in provider "vsphere"

Open KrzesloSzatan opened this issue 2 years ago • 1 comments

Community Guidelines

  • [X] I have read and agree to the HashiCorp Community Guidelines .
  • [X] Vote on this issue by adding a 👍 reaction to the original issue initial description to help the maintainers prioritize.
  • [X] Do not leave "+1" or other comments that do not add relevant information or questions.
  • [X] If you are interested in working on this issue or have submitted a pull request, please leave a comment.

Description

Hello, i have approached an issue while using existing VM as a template. Probably the same would happen while using a proper template since its the same resource. Anyway, i am creating disks dynamically with a dynamic {} block and it looks like this:

provider "vsphere" {
  user                 = var.vsphere_user
  password             = var.vsphere_password
  vsphere_server       = var.vsphere_server
  allow_unverified_ssl = true
}

data "vsphere_virtual_machine" "bitbucket_instance_template" {
  count         = var.template_name != "" ? 1 : 0
  name          = var.template_name
  datacenter_id = data.vsphere_datacenter.datacenter.id
}

resource "vsphere_virtual_machine" "bitbucket_instance" {
  name                  = var.instance_name
  resource_pool_id      = data.vsphere_resource_pool.resource_pool.id
  datastore_id          = data.vsphere_datastore.vm_datastore.id
  num_cpus              = try(data.vsphere_virtual_machine.bitbucket_instance_template[0].num_cpus, var.vm_num_cpus)
  num_cores_per_socket  = try(data.vsphere_virtual_machine.bitbucket_instance_template[0].num_cores_per_socket, var.vm_num_cores_per_socket)
  memory                = try(data.vsphere_virtual_machine.bitbucket_instance_template[0].memory, var.vm_memory)
  guest_id              = try(data.vsphere_virtual_machine.bitbucket_instance_template[0].guest_id, var.vm_guest_id)
  scsi_type             = try(data.vsphere_virtual_machine.bitbucket_instance_template[0].scsi_type, var.vm_scsi_type)
  firmware              = try(data.vsphere_virtual_machine.bitbucket_instance_template[0].firmware, var.vm_firmware)
  enable_logging        = try(data.vsphere_virtual_machine.bitbucket_instance_template[0].enable_logging, var.enable_logging)
  folder                = "/${var.folder_location}"
  cpu_share_level       = var.vm_cpu_share_level
  cpu_reservation       = var.vm_cpu_reservation
  memory_reservation    = var.vm_memory_reservation
  sync_time_with_host   = var.vm_sync_time_with_host
  
  dynamic "network_interface" {
    for_each = try(data.vsphere_virtual_machine.bitbucket_instance_template[0].network_interfaces, local.network_interfaces)
    content {
      network_id   = network_interface.value.network_id
      adapter_type = network_interface.value.adapter_type
    }
  }
  
  cdrom {
    client_device = true
  }
  
  wait_for_guest_net_timeout = 5
  wait_for_guest_ip_timeout  = 0
  
  dynamic "disk" {
    for_each = [for disk in try(data.vsphere_virtual_machine.bitbucket_instance_template[0].disks, local.disks) : disk]
    content {
      datastore_id     = disk.value.datastore_id
      label            = "disk${disk.key}"
      unit_number      = disk.key
      size             = disk.value.size
      thin_provisioned = disk.value.thin_provisioned
      keep_on_remove   = true
    }
  }
  
  #####
  # Clone block below is for templated deployment only.
  #####
  
  dynamic "clone" {
    for_each = var.template_name != "" ? [1] : []
    content {
      template_uuid = data.vsphere_virtual_machine.bitbucket_instance_template[0].id
      customize {
        linux_options {
          host_name = var.instance_name
          domain    = "zeiss.org"
        }
        
        dynamic "network_interface" {
          for_each = data.vsphere_virtual_machine.bitbucket_instance_template[0].network_interfaces
          iterator = item
          content {
            ipv4_address = var.vm_static_ip[item.key].ipv4
            ipv4_netmask = var.vm_static_ip[item.key].ipv4_netmask
            ipv6_address = var.vm_static_ip[item.key].ipv6
            ipv6_netmask = var.vm_static_ip[item.key].ipv6_netmask
          }
        }
        
        ipv4_gateway = var.vm_gateway
        
      }
    }
  }
}

Its made like that so it can be either created from a template or from no resource just by typing appropriate variables.

Anyway, the issue here is that while using a template to configure disks i get an error:

│ Error: Unsupported attribute
│
│   on ..\modules\bitbucket_instance\main.tf line 90, in resource "vsphere_virtual_machine" "bitbucket_instance":
│   90:       datastore_id     = disk.value.datastore_id
│     ├────────────────
│     │ disk.value is object with 5 attributes
│
│ This object does not have an attribute named "datastore_id".

And considering that the VM itself needs a datastore, and every disk can be stored on a different datastore why is there no datastore attribute in data.vsphere_virtual_machine.whatever.disks ? This means that in order to use a template, which actually has info about disks i have to still use a, for example, local parameter to provide proper datastore_id to every disk that the template vm has.

Use Case(s)

Create a dynamic disk block from a template that has multiple disks attached on different datastores. Like below:

  dynamic "disk" {
    for_each = [for disk in try(data.vsphere_virtual_machine.bitbucket_instance_template[0].disks, local.disks) : disk]
    content {
      datastore_id     = disk.value.datastore_id
      label            = "disk${disk.key}"
      unit_number      = disk.key
      size             = disk.value.size
      thin_provisioned = disk.value.thin_provisioned
      keep_on_remove   = true
    }
  }

Potential Terraform Provider Configuration

No response

References

No response

KrzesloSzatan avatar Sep 21 '22 11:09 KrzesloSzatan

Hello, KrzesloSzatan! 🖐

Thank you for submitting an issue for this provider. The issue will now enter into the issue lifecycle.

If you want to contribute to this project, please review the contributing guidelines and information on submitting pull requests.

github-actions[bot] avatar Sep 21 '22 11:09 github-actions[bot]

Marking this issue as stale due to inactivity in the past 180 days. This helps us focus on the active issues. If this issue is reproducible with the latest version of the provider, please comment. If this issue receives no comments in the next 30 days it will automatically be closed. If this issue was automatically closed and you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thank you!

github-actions[bot] avatar Apr 10 '23 00:04 github-actions[bot]

Marking this issue as stale due to inactivity in the past 180 days. This helps us focus on the active issues. If this issue is reproducible with the latest version of the provider, please comment. If this issue receives no comments in the next 30 days it will automatically be closed. If this issue was automatically closed and you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thank you!

github-actions[bot] avatar Nov 08 '23 00:11 github-actions[bot]

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

github-actions[bot] avatar Jan 07 '24 02:01 github-actions[bot]