Skip to content

Commit

Permalink
Merge pull request #19 from SamyCoenen/feature/disk-datastore
Browse files Browse the repository at this point in the history
Feature/disk datastore - Tested, Working as expected
  • Loading branch information
Arman-Keyoumarsi authored May 13, 2020
2 parents 17a22ad + 20216ec commit b9010c1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/linux/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
2 changes: 2 additions & 0 deletions examples/windows/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
16 changes: 16 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -95,6 +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 != "" ? data.vsphere_datastore.disk_datastore[0].id : null
}
}

Expand All @@ -108,6 +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 = length(var.data_disk_datastore) > 0 ? data.vsphere_datastore.data_disk_datastore[var.data_disk_datastore[terraform_disks.key]].id : null
}
}

Expand Down Expand Up @@ -185,6 +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 != "" ? data.vsphere_datastore.disk_datastore[0].id : null
}
}

Expand All @@ -198,6 +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 = length(var.data_disk_datastore) > 0 ? data.vsphere_datastore.data_disk_datastore[var.data_disk_datastore[terraform_disks.key]].id : null
}
}

Expand Down
14 changes: 14 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,20 @@ variable "data_disk_size_gb" {
default = []
}

variable "disk_datastore"{
type = string
default = ""
}

variable "data_disk_datastore" {
type = list
default = []
# validation {
# condition = length(var.disk_datastore) == 0 || length(var.disk_datastore) == length(var.data_disk_size_gb)
# error_message = "The list of disk datastore must be equal in length to disk_size_gb"
# }
}

variable "thin_provisioned" {
description = "If true, this disk is thin provisioned, with space for the file being allocated on an as-needed basis."
type = list
Expand Down

0 comments on commit b9010c1

Please sign in to comment.