{% if "remote_host" in vars %}
provider "libvirt" {
  uri   = "qemu+ssh://{{ vars['user']|default('root') }}@{{ vars['remote_host'] }}/system"
}
{% else %}
provider "libvirt" {
  uri = "qemu:///system"
}
{% endif %}

resource "libvirt_volume" "image" {
  name = "image.qcow2"
  pool = "images"
  source = "{{ vars['image_source']|default("https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2") }}"
  format = "qcow2"
}

# Define KVM domain to create
resource "libvirt_domain" "server1" {
  name   = "server1"
  memory = "16384"
  vcpu   = 2

  network_interface {
    network_name = "default"
  }

  disk {
    volume_id = "${libvirt_volume.image.id}"
  }

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

  graphics {
    type = "spice"
    listen_type = "address"
    autoport = true
  }
}
