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_network dhcp enable is not correctly detected #998

Open rgl opened 1 year ago

rgl commented 1 year ago

I'm creating a libvirt_network resource with dhcp disabled as:

resource "libvirt_network" "talos" {
  name      = var.prefix
  mode      = "nat"
  domain    = "talos.test"
  addresses = ["10.17.3.0/24"]
  dhcp {
    enabled = false
  }
  dns {
    enabled    = true
    local_only = false
  }
}

But successive terraform plan always shows a dhcp modification from enabled to disabled:

  # libvirt_network.talos will be updated in-place
  ~ resource "libvirt_network" "talos" {
        id        = "01c57a6c-a954-45b6-a232-5f440eecf455"
        name      = "terraform_talos_example"
        # (5 unchanged attributes hidden)

      ~ dhcp {
          ~ enabled = true -> false
        }

        # (1 unchanged block hidden)
    }

I believe its due to the way the dhcp enabled is being detected at:

https://github.com/dmacvicar/terraform-provider-libvirt/blob/9260f4ce9ba2e24e98c4b7970f85790c5436cf7d/libvirt/resource_libvirt_network.go#L576-L584

It should instead look for the existence of range elements.

If you agree to the change, I can submit a PR.

I'm using libvirt 8.0.0-1ubuntu7.4 (from ubuntu 22.04).

For reference, this is a network with dhcp enabled (notice the existence of the /network/ip/dhcp/range element):

# virsh net-dumpxml --network default 
<network>
  <name>default</name>
  <uuid>99f6c784-42f5-4d47-9975-da134c97e9e6</uuid>
  <forward mode='nat'>
    <nat>
      <port start='1024' end='65535'/>
    </nat>
  </forward>
  <bridge name='virbr0' stp='on' delay='0'/>
  <mac address='52:54:00:8e:24:de'/>
  <ip address='192.168.122.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.122.2' end='192.168.122.254'/>
    </dhcp>
  </ip>
</network>

And this is a network with dhcp disabled (notice the lack of the /network/ip/dhcp/range element):

# virsh net-dumpxml --network terraform_talos_example 
<network connections='4'>
  <name>terraform_talos_example</name>
  <uuid>01c57a6c-a954-45b6-a232-5f440eecf455</uuid>
  <forward mode='nat'>
    <nat>
      <port start='1024' end='65535'/>
    </nat>
  </forward>
  <bridge name='virbr5' stp='on' delay='0'/>
  <mac address='52:54:00:65:a6:d1'/>
  <domain name='talos.test'/>
  <dns enable='yes'/>
  <ip family='ipv4' address='10.17.3.1' prefix='24'>
    <dhcp>
      <host mac='52:54:00:16:06:7f' name='terraform_talos_example_w0' ip='10.17.3.20'/>
      <host mac='52:54:00:ba:72:ca' name='terraform_talos_example_c2' ip='10.17.3.12'/>
      <host mac='52:54:00:d6:98:13' name='terraform_talos_example_c1' ip='10.17.3.11'/>
      <host mac='52:54:00:8a:c6:80' name='terraform_talos_example_c0' ip='10.17.3.10'/>
    </dhcp>
  </ip>
</network>