dmacvicar / terraform-provider-libvirt

Terraform provider to provision infrastructure with Linux's KVM using libvirt
Apache License 2.0
1.54k stars 457 forks source link

Implement Terraform Data blocks for existing resources #884

Open wsidl opened 2 years ago

wsidl commented 2 years ago

Terraform version

v1.0.6

Provider and libvirt versions

v1.43.1


Checklist

Description of Issue/Question

As an administrator setting up multiple environments, I don't want to centralize all of my Terraform configurations in a single set of configuration files. Instead, I'd like to split them up into applications or stages. This means I can re-deploy or destroy one application without affecting others. Doing this with this provider is very annoying and difficult. I now have to do manual tasks which is against the design of IaC.

Please implement Terraform Data Source blocks so I can reference a pre-generated resource. This allows for:


For example, if I want to deploy a set of domains on an existing system, I should be able to reference existing resources to quickly create it:

terraform {
  required_version = ">= 1.0"
  required_providers {
    libvirt = {
      source = "dmacvicar/libvirt"
    }
  }
}
provider "libvirt" {
  uri = "qemu:///system"
}

# System Resources
data "libvirt_pool" "os_iso" {
  name = "os_iso"
}
data "libvirt_pool" "os_disks" {
  name = "os_disks"
}
data "libvirt_volume" "ubuntu" {
  name = "ubuntu-20.04-server-cloudimg-amd64-disk-kvm.img"
  pool = data.libvirt_pool.os_iso.id
}

# Application Resources
resource "libvirt_volume" "root" {
  name           = "app_root.qcow2"
  pool           = data.libvirt_pool.os_disks.name
  size           = 107374182400
  base_volume_id = data.libvirt_volume.ubuntu_cloudinit.id
}
memetb commented 4 months ago

Fyi, I have implemented the equivalent of virsh nodeinfo as a data object. See here

Implementation

(I haven't yet submitted this as a PR because it's incorrectly based of another active and open PR so I don't want to trojan that first one in.)