hashicorp / terraform-provider-vsphere

Terraform Provider for VMware vSphere
https://registry.terraform.io/providers/hashicorp/vsphere/
Mozilla Public License 2.0
623 stars 453 forks source link

Allow datastore_cluster for content library clones with `vm_templates` #2268

Open Zheer09 opened 2 months ago

Zheer09 commented 2 months ago

Community Guidelines

Terraform

OpenTofu v1.8.2

Terraform Provider

v2.9.2

VMware vSphere

ESXI 7.0 U2

Description

Referencing from this https://knowledge.broadcom.com/external/article/312205/vm-template-cannot-be-deployed-to-datast.html and PR that seemed to fix this issue

I see this was tested with ovf template i am using VM templates

Affected Resources or Data Sources

resource/vsphere_virtual_machine

Terraform Configuration

resource "vsphere_virtual_machine"

resource "vsphere_virtual_machine" "vm" {
  name                  = length(var.ip-addresses) > 1 ? "${var.name}-${count.index + 1}" : var.name
  resource_pool_id      = data.vsphere_compute_cluster.cluster.resource_pool_id
  datastore_cluster_id  = data.vsphere_datastore_cluster.datastore_cluster.id
  host_system_id        = data.vsphere_host.host.id
  sata_controller_count = 1
  scsi_controller_count = 1
  count                 = length(var.ip-addresses) > 0 ? length(var.ip-addresses) : 1

  # only if you DO NOT want to wait for an IP address
  wait_for_guest_net_routable = false

  num_cpus = var.num_cpus
  memory   = var.memory
  guest_id = "ubuntu64Guest"

  network_interface {
    network_id = data.vsphere_network.network.id
  }

  disk {
    label            = "disk0"
    size             = var.disk_size
    unit_number      = 0
    thin_provisioned = false
  }

  clone {
    template_uuid = data.vsphere_content_library_item.item.id

    customize {
      linux_options {
        host_name = "${count.index}" > 1 ? "${var.name}-${count.index}" : var.name
        domain    = "${count.index}" > 1 ? "${var.name}-${count.index}" : var.name
      }
      network_interface {
        ipv4_address = length(var.ip-addresses) > 0 ? element(var.ip-addresses, count.index) : var.ip-address
        ipv4_netmask = 24
      }
      ipv4_gateway = "10.10.10.1"
    }
  }
}

Data file

data "vsphere_datacenter" "dc" {
  name = var.datacenter_name
}

data "vsphere_datastore_cluster" "datastore_cluster" {
  name          = var.datastore_cluster
  datacenter_id = data.vsphere_datacenter.dc.id
}

data "vsphere_content_library" "library" {
  name = "HQ-UAT"
}

data "vsphere_content_library_item" "item" {
  name       = "ubuntu-20-v1-dev-2024-09-25"
  type       = "vm-template"
  library_id = data.vsphere_content_library.library.id
}

Debug Output

None

Panic Output

No response

Expected Behavior

Creating vm with datastore cluster

Actual Behavior

I got this error

โ•ท
โ”‚ Error: 400 Bad Request: {"type":"com.vmware.vapi.std.errors.invalid_argument","value":{"error_type":"INVALID_ARGUMENT","messages":[{"args":["group-p105:2e683356-6528-4429-b53e-78ab46e2136b"],"default_message":"The provided datastore ID group-p105:2e683356-6528-4429-b53e-78ab46e2136b is invalid.","id":"com.vmware.vdcs.vmtx-main.invalid_datastore_id_format"}]}}
โ”‚ 
โ”‚   with module.virtual_machine.vsphere_virtual_machine.vm[1],
โ”‚   on modules/virtual-machine/vm_machine.tf line 2, in resource "vsphere_virtual_machine" "vm":
โ”‚    2: resource "vsphere_virtual_machine" "vm" {
โ•ต

Steps to Reproduce

Storing vm template to content library from using packer and with this configuration which getting the datastore cluster data has been validated but while creating the vm i am getting this error.

Environment Details

No response

Screenshots

No response

References

https://github.com/hashicorp/terraform-provider-vsphere/pull/2061

github-actions[bot] commented 2 months ago

Hello, Zheer09! ๐Ÿ–

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.