From 0fad70363b3e9059150a8c5ac9c994b2c4f8d473 Mon Sep 17 00:00:00 2001 From: Philippe Caseiro Date: Wed, 5 Jul 2023 09:43:06 +0200 Subject: [PATCH] feat(vmware): adding vmware support --- modules/vmware/main.tf | 83 +++++++++++++++++++++++++++++++++++++++ modules/vmware/output.tf | 7 ++++ modules/vmware/version.tf | 8 ++++ 3 files changed, 98 insertions(+) create mode 100644 modules/vmware/main.tf create mode 100644 modules/vmware/output.tf create mode 100644 modules/vmware/version.tf diff --git a/modules/vmware/main.tf b/modules/vmware/main.tf new file mode 100644 index 0000000..b5aaca1 --- /dev/null +++ b/modules/vmware/main.tf @@ -0,0 +1,83 @@ +variable "vms" { + type = map(object( + { + domain = string + cpu = number + ram = number + guest_id = string + disks = list(object({ + name = string + size = string + })) + network_interfaces = map(object( + { + mac = string + } + )) + resource_pool_id = string + datastore_id = string + folder = string + } + )) + description = "Virtual Machines description" +} + +variable "cluster" { + type = map(object( + { + datacenter_name = string + datastore_name = string + resource_pool_name = string + folder = string + } + )) + description = "vSphere cluster definition" +} + +data "vsphere_datacenter" "datacenter" { + name = var.cluster.datacenter_name +} + +data "vsphere_datastore" "datastore" { + name = var.cluster.datastore_name + datacenter_id = data.vsphere_datacenter.datacenter.id +} + +data "vsphere_resource_pool" "pool" { + name = var.cluster.resource_pool_name + datacenter_id = data.vsphere_datacenter.datacenter.id +} + +data "vsphere_network" "network" { + count = length(var.vms.network_interfaces) + + name = var.networks[count.index].key + datacenter_id = data.vsphere_datacenter.datacenter.id +} + +resource "vphere_virtual_virtual_machine" "vm" { + for_each = var.vms + iterator = "machine" + name = "${machine.key}.${machine.value.domain}" + resource_pool_id = "${machine.key.resource_pool_id}" + datastore_id = machine.key.datastore_id + folder = machine.folder + num_cpus = machine.cpu + memory = machine.ram + guest_id = machine.guest_id + dynamic "network_interface" { + for_each = data.vsphere_network.network + iterator = "vnet" + content { + networkid = vnet.value.id + mac = machine.network_interfaces["${vnet.key}"].mac + } + } + dynamic "disk" { + for_each = each.key.disks + content { + label = each.key.name + size = each.key.size + } + } +} diff --git a/modules/vmware/output.tf b/modules/vmware/output.tf new file mode 100644 index 0000000..d5443fb --- /dev/null +++ b/modules/vmware/output.tf @@ -0,0 +1,7 @@ +output "shortnames" { + value = keys(var.vms) +} + +output "virtual_machines" { + value = vsphere.virtual_machine.vm //template_nic[0].computed_mac) +} \ No newline at end of file diff --git a/modules/vmware/version.tf b/modules/vmware/version.tf new file mode 100644 index 0000000..838dedb --- /dev/null +++ b/modules/vmware/version.tf @@ -0,0 +1,8 @@ +terraform { + required_providers { + opennebula = { + source = "hashicorp/vsphere" + version = "~> 2.4.0" + } + } +} \ No newline at end of file