From 9d1732ec51a5164c6ff7d8376ba42c041c7d597c Mon Sep 17 00:00:00 2001 From: Samy Coenen Date: Wed, 13 May 2020 16:25:25 +0200 Subject: [PATCH 1/2] Fix empty disk_datastore --- main.tf | 8 ++++---- variables.tf | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/main.tf b/main.tf index 54ddeac..4b46176 100644 --- a/main.tf +++ b/main.tf @@ -107,7 +107,7 @@ resource "vsphere_virtual_machine" "Linux" { unit_number = template_disks.key thin_provisioned = data.vsphere_virtual_machine.template.disks[template_disks.key].thin_provisioned eagerly_scrub = data.vsphere_virtual_machine.template.disks[template_disks.key].eagerly_scrub - datastore_id = var.disk_datastore != null ? data.vsphere_datastore.disk_datastore[0].id : null + datastore_id = var.disk_datastore != "" ? data.vsphere_datastore.disk_datastore[0].id : null } } @@ -121,7 +121,7 @@ resource "vsphere_virtual_machine" "Linux" { unit_number = terraform_disks.key + local.template_disk_count thin_provisioned = var.thin_provisioned != null ? var.thin_provisioned[terraform_disks.key] : null eagerly_scrub = var.eagerly_scrub != null ? var.eagerly_scrub[terraform_disks.key] : null - datastore_id = var.data_disk_datastore != null ? data.vsphere_datastore.data_disk_datastore[var.data_disk_datastore[terraform_disks.key]].id : null + datastore_id = length(var.data_disk_datastore) > 0 ? data.vsphere_datastore.data_disk_datastore[var.data_disk_datastore[terraform_disks.key]].id : null } } @@ -199,7 +199,7 @@ resource "vsphere_virtual_machine" "Windows" { unit_number = template_disks.key thin_provisioned = data.vsphere_virtual_machine.template.disks[template_disks.key].thin_provisioned eagerly_scrub = data.vsphere_virtual_machine.template.disks[template_disks.key].eagerly_scrub - datastore_id = var.disk_datastore != null ? data.vsphere_datastore.disk_datastore[0].id : null + datastore_id = var.disk_datastore != "" ? data.vsphere_datastore.disk_datastore[0].id : null } } @@ -213,7 +213,7 @@ resource "vsphere_virtual_machine" "Windows" { unit_number = terraform_disks.key + local.template_disk_count thin_provisioned = var.thin_provisioned != null ? var.thin_provisioned[terraform_disks.key] : null eagerly_scrub = var.eagerly_scrub != null ? var.eagerly_scrub[terraform_disks.key] : null - datastore_id = var.data_disk_datastore != null ? data.vsphere_datastore.data_disk_datastore[var.data_disk_datastore[terraform_disks.key]].id : null + datastore_id = length(var.data_disk_datastore) > 0 ? data.vsphere_datastore.data_disk_datastore[var.data_disk_datastore[terraform_disks.key]].id : null } } diff --git a/variables.tf b/variables.tf index 5673fa5..a3d4fa6 100644 --- a/variables.tf +++ b/variables.tf @@ -156,8 +156,8 @@ variable "data_disk_size_gb" { } variable "disk_datastore"{ - type = string - default = null + type = string + default = "" } variable "data_disk_datastore" { From 20216ec9f9b5d38b2b91677cf386ad9bf09550b0 Mon Sep 17 00:00:00 2001 From: Samy Coenen Date: Wed, 13 May 2020 16:37:48 +0200 Subject: [PATCH 2/2] Add data datastore after merge, add examples --- examples/linux/main.tf | 2 ++ examples/windows/main.tf | 2 ++ main.tf | 12 ++++++++++++ 3 files changed, 16 insertions(+) diff --git a/examples/linux/main.tf b/examples/linux/main.tf index f354419..6bc9428 100644 --- a/examples/linux/main.tf +++ b/examples/linux/main.tf @@ -36,6 +36,8 @@ module "example-server-linuxvm-advanced" { "VM Network" = ["192.168.0.4", ""] // Here the first instance will use Static Ip and Second set to DHCP "test" = ["", "192.168.0.3"] } + disk_datastore = "vsanDatastore" + data_disk_datastore = ["vsanDatastore", "nfsDatastore"] data_disk_size_gb = [10, 5] // Aditional Disks to be used thin_provisioned = ["true", "false"] vmdns = ["192.168.0.2", "192.168.0.1"] diff --git a/examples/windows/main.tf b/examples/windows/main.tf index 69fe001..46e8b9e 100644 --- a/examples/windows/main.tf +++ b/examples/windows/main.tf @@ -56,6 +56,8 @@ module "example-server-windowsvm-advanced" { "VM Network" = ["192.168.0.4", ""] // Here the first instance will use Static Ip and Second DHCP "test" = ["", "192.168.0.3"] } + disk_datastore = "vsanDatastore" + data_disk_datastore = ["vsanDatastore", "nfsDatastore"] data_disk_size_gb = [10, 5] // Aditional Disks to be used thin_provisioned = ["true", "false"] vmdns = ["192.168.0.2", "192.168.0.1"] diff --git a/main.tf b/main.tf index 8d231ab..4b46176 100644 --- a/main.tf +++ b/main.tf @@ -14,6 +14,18 @@ data "vsphere_datastore" "datastore" { datacenter_id = data.vsphere_datacenter.dc.id } +data "vsphere_datastore" "disk_datastore" { + count = var.disk_datastore != "" ? 1 : 0 + name = var.disk_datastore + datacenter_id = data.vsphere_datacenter.dc.id +} + +data "vsphere_datastore" "data_disk_datastore" { + for_each = toset(var.data_disk_datastore) + name = each.key + datacenter_id = data.vsphere_datacenter.dc.id +} + data "vsphere_resource_pool" "pool" { name = var.vmrp datacenter_id = data.vsphere_datacenter.dc.id