Dynamically creating multiple vmfs datastores from hostname and diskID

I’m trying to loop through vSphere hosts and dynamically populate their storage disk IDs and create datastores - that is the end goal. My input is hostname and that is all, rest is discovered dynamically.

The following…
output "disk_uuid" { value = formatlist( "%v = %v", #(vsphere_host.add_hosts.*.hostname), (data.vsphere_vmfs_disks.getstorage.*.host_system_id), (data.vsphere_vmfs_disks.getstorage.*.disks) ) }

Provides me with the data I am looking for…


disk_uuid = tolist([
  "host-8543 = [\"naa.6000c2932db4d4a9f589172ef7c44acb\",\"naa.6000c29bfd2c5db4223c677aca1666b4\",\"naa.6000c29ff8fce1358e54c3b7715ee445\"]",
  "host-8545 = [\"naa.6000c295d66bdf27e915a5b11da5d3c3\",\"naa.6000c29a26622da6312694708203fde1\",\"naa.6000c29a84d1bca834d3b5f9f446b04f\"]",
  "host-8544 = [\"naa.6000c29aabac31dccb04e425aa789a61\",\"naa.6000c29c7c61274187fd0fe5d9b3faf4\",\"naa.6000c29fe166e7f7547002a724b0213d\"]",
])

What I am struggling with is the resource block to create the datastores:

resource "vsphere_vmfs_datastore" "createstorage" {
  count          = length(data.vsphere_vmfs_disks.getstorage.*.host_system_id)
  name           = format("tf-nested-%03d", count.index + 1)
  host_system_id = data.vsphere_vmfs_disks.getstorage.*.host_system_id
  disks          = [data.vsphere_vmfs_disks.getstorage.*.disks[count.index]]
}

This is wrong - the result back is that the resource being created is a tuple list - which is correct. What I cannot figure out is how to parse the hostID with each individual diskID.

I was able to get this worked out incase anyone else comes across a similar scenario using flatten…

  host_list = flatten([
    for host in data.vsphere_vmfs_disks.getstorage : [
      for disk in host.disks : {
        host_id = host.host_system_id
        disk_id = disk
      }
    ]
  ])
} 

The code to create the VMFS datastores:

resource "vsphere_vmfs_datastore" "createstorage" {
  count          = length(local.host_list)
  name           = format("tf-nested-%03d", count.index + 1)
  host_system_id = element(local.host_list, count.index).host_id
  disks          = [element(local.host_list, count.index).disk_id]
}