From 65bc2689d50f3238a052389e30d81730ad2c601d Mon Sep 17 00:00:00 2001 From: "J. Eduardo" Date: Mon, 19 Jun 2017 23:35:07 +0200 Subject: Added support for using raw volumes and an example using the functionality. --- examples/format/libvirt.tf | 72 +++++++++++++++++++++++++++++++++ libvirt/resource_libvirt_volume.go | 23 ++++++++++- libvirt/resource_libvirt_volume_test.go | 59 +++++++++++++++++++++++++++ 3 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 examples/format/libvirt.tf diff --git a/examples/format/libvirt.tf b/examples/format/libvirt.tf new file mode 100644 index 00000000..1a0cc0e8 --- /dev/null +++ b/examples/format/libvirt.tf @@ -0,0 +1,72 @@ +provider "libvirt" { + uri = "qemu:///system" +} + +resource "libvirt_network" "tf" { + name = "tf" + domain = "tf.local" + mode = "nat" + addresses = ["10.0.100.0/24"] +} + +# raw image from file +resource "libvirt_volume" "debian8-raw" { + name = "debian8-raw" + format = "raw" + source = "http://localhost:8000/debian8.img" +} + +# qcow2 image from file +resource "libvirt_volume" "debian8-qcow2" { + name = "debian8-qcow2" + source = "http://localhost:8000/debian8.qcow2" +} + +# volume with raw backing storage +resource "libvirt_volume" "vol-debian8-raw" { + name = "vol-debian8-raw" + base_volume_id = "${libvirt_volume.debian8-raw.id}" + base_volume_format = "raw" +} + +# volume with qcow2 backing storage +resource "libvirt_volume" "vol-debian8-qcow2" { + name = "vol-debian8-qcow2" + base_volume_id = "${libvirt_volume.debian8-qcow2.id}" +} + +# domain using raw-backed volume +resource "libvirt_domain" "domain-debian8-raw" { + name = "domain-debian8-raw" + memory = "256" + vcpu = 1 + network_interface { + network_name = "tf" + } + disk { + volume_id = "${libvirt_volume.vol-debian8-raw.id}" + } + graphics { + type = "spice" + listen_type = "address" + autoport = true + } +} + +# domain using qcow2-backed volume +resource "libvirt_domain" "domain-debian8-qcow2" { + name = "domain-debian8-qcow2" + memory = "256" + vcpu = 1 + network_interface { + network_name = "tf" + } + disk { + volume_id = "${libvirt_volume.vol-debian8-qcow2.id}" + } + graphics { + type = "spice" + listen_type = "address" + autoport = true + } +} diff --git a/libvirt/resource_libvirt_volume.go b/libvirt/resource_libvirt_volume.go index 5e20c079..a25d157d 100644 --- a/libvirt/resource_libvirt_volume.go +++ b/libvirt/resource_libvirt_volume.go @@ -35,6 +35,11 @@ func volumeCommonSchema() map[string]*schema.Schema { Computed: true, ForceNew: true, }, + "format": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, "base_volume_id": &schema.Schema{ Type: schema.TypeString, Optional: true, @@ -50,6 +55,11 @@ func volumeCommonSchema() map[string]*schema.Schema { Optional: true, ForceNew: true, }, + "base_volume_format": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, } } @@ -106,6 +116,12 @@ func resourceLibvirtVolumeCreate(d *schema.ResourceData, meta interface{}) error volumeDef.Name = name.(string) } + volumeFormat := "qcow2" + if _, ok := d.GetOk("format"); ok { + volumeFormat = d.Get("format").(string) + } + volumeDef.Target.Format.Type = volumeFormat + var ( img image volume *libvirt.StorageVol = nil @@ -171,7 +187,12 @@ func resourceLibvirtVolumeCreate(d *schema.ResourceData, meta interface{}) error volume = nil volumeDef.BackingStore = new(defBackingStore) - volumeDef.BackingStore.Format.Type = "qcow2" + + baseVolumeFormat := "qcow2" + if _, ok := d.GetOk("base_volume_format"); ok { + baseVolumeFormat = d.Get("base_volume_format").(string) + } + volumeDef.BackingStore.Format.Type = baseVolumeFormat baseVolume, err := virConn.LookupStorageVolByKey(baseVolumeId.(string)) if err != nil { return fmt.Errorf("Can't retrieve volume %s", baseVolumeId.(string)) diff --git a/libvirt/resource_libvirt_volume_test.go b/libvirt/resource_libvirt_volume_test.go index 99fc9608..b76fd848 100644 --- a/libvirt/resource_libvirt_volume_test.go +++ b/libvirt/resource_libvirt_volume_test.go @@ -147,3 +147,62 @@ func TestAccLibvirtVolume_DownloadFromSource(t *testing.T) { }, }) } + +func TestAccLibvirtVolume_Format(t *testing.T) { + var volume libvirt.StorageVol + + const testAccCheckLibvirtVolumeConfig_format = ` + resource "libvirt_volume" "terraform-acceptance-test-3" { + name = "terraform-test" + format = "raw" + size = 1073741824 + }` + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckLibvirtVolumeDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCheckLibvirtVolumeConfig_format, + Check: resource.ComposeTestCheckFunc( + testAccCheckLibvirtVolumeExists("libvirt_volume.terraform-acceptance-test-3", &volume), + resource.TestCheckResourceAttr( + "libvirt_volume.terraform-acceptance-test-3", "name", "terraform-test"), + resource.TestCheckResourceAttr( + "libvirt_volume.terraform-acceptance-test-3", "size", "1073741824"), + resource.TestCheckResourceAttr( + "libvirt_volume.terraform-acceptance-test-3", "format", "raw"), + ), + }, + }, + }) +} + +func TestAccLibvirtVolume_BaseVolumeFormat(t *testing.T) { + var volume libvirt.StorageVol + + const testAccCheckLibvirtVolumeConfig_format = ` + resource "libvirt_volume" "terraform-acceptance-test-4" { + name = "terraform-test" + base_volume_format = "raw" + }` + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckLibvirtVolumeDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccCheckLibvirtVolumeConfig_format, + Check: resource.ComposeTestCheckFunc( + testAccCheckLibvirtVolumeExists("libvirt_volume.terraform-acceptance-test-4", &volume), + resource.TestCheckResourceAttr( + "libvirt_volume.terraform-acceptance-test-4", "name", "terraform-test"), + resource.TestCheckResourceAttr( + "libvirt_volume.terraform-acceptance-test-4", "base_volume_format", "raw"), + ), + }, + }, + }) +} -- cgit v1.2.3