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

libvirt_domain still created on error #1003

Open alexandre-janniaux opened 1 year ago

alexandre-janniaux commented 1 year ago

System Information

Linux distribution

Archlinux

Terraform version

$ terraform -v                                                                                                                               
Terraform v1.3.9
on linux_amd64
+ provider registry.terraform.io/dmacvicar/libvirt v0.7.1
+ provider registry.terraform.io/hashicorp/template v2.2.0

Provider and libvirt versions

.terraform/providers/registry.terraform.io/dmacvicar/libvirt/0.7.1/linux_amd64/terraform-provider-libvirt_v0.7.1 -version
.terraform/providers/registry.terraform.io/dmacvicar/libvirt/0.7.1/linux_amd64/terraform-provider-libvirt_v0.7.1 0.7.1

Checklist

Description of Issue/Question

Setup

(Please provide the full main.tf file for reproducing the issue (Be sure to remove sensitive information)

The file in nvram must be missing to reproduce:

terraform {
  required_providers {
    libvirt = {
      source = "dmacvicar/libvirt"
    }
  }

  required_version = ">= 0.13"
}

provider "libvirt" {
  uri = "qemu:///system"
}

resource "libvirt_volume" "packer_win10_22H2" {
  name = "packer_win11_22H2"
  pool = "default"
  source = "qemu-drives/packer-win10_22H2"
  format = "qcow2"
}

resource "libvirt_network" "some_network" {
  name = "some_network"
  mode = "nat"
  domain = "kvm"
  dns {
    enabled = true
    local_only = false
  }
}

resource "libvirt_volume" "win10_terraform_01" {
  provider = libvirt.rtiiix
  name = "win10_terraform_01_storage"
  pool = "default"
  base_volume_id = libvirt_volume.packer_win10_22H2.id
  size = "30000000000"
}

resource "libvirt_domain" "win10_terraform_01" {
  provider = libvirt.rtiiix
  # domain name in libvirt, not hostname
  name = "win10_terraform_01"
  memory = "2048"
  vcpu = 2
  firmware = "/usr/share/OVMF/x64/OVMF_CODE.fd"
  nvram {
    file = "win10_terraform_01.fd"
    template = "/usr/share/ovmf/x64/OVMF_VARS.fd"
  }

  autostart = true

  network_interface {
    network_id     = libvirt_network.shady_network.id
    mac            = "52:54:00:6C:3C:02"
    addresses      = ["192.168.100.2"]
    hostname       = "win10_terraform_01"
    wait_for_lease = true
  }

  disk {
    volume_id = libvirt_volume.win10_terraform_01.id
  }

  console {
    type        = "pty"
    target_port = "0"
    target_type = "serial"
  }
}

Steps to Reproduce Issue

(Include debug logs if possible and relevant).

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

libvirt_domain.win10_terraform_01: Creating...
╷
│ Error: error creating libvirt domain: Failed to open file '/usr/share/ovmf/x64/OVMF_VARS.fd': No such file or directory
│
│   with libvirt_domain.win10_terraform_01,
│   on main.tf line 90, in resource "libvirt_domain" "win10_terraform_01":
│   90: resource "libvirt_domain" "win10_terraform_01" {
│
╵

The second time you apply, it will result in

o you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

libvirt_domain.win10_terraform_01: Creating...
╷
│ Error: error defining libvirt domain: operation failed: domain 'win10_terraform_01' already exists with uuid 3142bd26-c41f-40b1-9959-b38ecfd406dd
│
│   with libvirt_domain.win10_terraform_01,
│   on main.tf line 90, in resource "libvirt_domain" "win10_terraform_01":
│   90: resource "libvirt_domain" "win10_terraform_01" {
│
╵

Because the domain would have been created despite the error in the previous case.

Note: line are not matching because I removed what was not necessary after, sorry for that. Since the line is quoted, it's easy to fetch the correct location.

Expected result

The domain should not have been created in the first step, or be destroyed before terraform exits.