From 5513886108680f22a03b0bac79121e270e6a0065 Mon Sep 17 00:00:00 2001 From: "J. Eduardo" Date: Thu, 29 Jun 2017 23:57:44 +0200 Subject: Added autodetection of base volume format, eliminating the need for explicit configuration. --- examples/format/libvirt.tf | 1 - libvirt/resource_libvirt_volume.go | 32 ++++++++++++++++++++------------ libvirt/resource_libvirt_volume_test.go | 28 ---------------------------- 3 files changed, 20 insertions(+), 41 deletions(-) diff --git a/examples/format/libvirt.tf b/examples/format/libvirt.tf index 1a0cc0e8..e59f883f 100644 --- a/examples/format/libvirt.tf +++ b/examples/format/libvirt.tf @@ -26,7 +26,6 @@ resource "libvirt_volume" "debian8-qcow2" { 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 diff --git a/libvirt/resource_libvirt_volume.go b/libvirt/resource_libvirt_volume.go index a25d157d..c7809e61 100644 --- a/libvirt/resource_libvirt_volume.go +++ b/libvirt/resource_libvirt_volume.go @@ -55,11 +55,6 @@ func volumeCommonSchema() map[string]*schema.Schema { Optional: true, ForceNew: true, }, - "base_volume_format": &schema.Schema{ - Type: schema.TypeString, - Optional: true, - ForceNew: true, - }, } } @@ -187,12 +182,6 @@ func resourceLibvirtVolumeCreate(d *schema.ResourceData, meta interface{}) error volume = nil volumeDef.BackingStore = new(defBackingStore) - - 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)) @@ -202,6 +191,16 @@ func resourceLibvirtVolumeCreate(d *schema.ResourceData, meta interface{}) error return fmt.Errorf("can't get name for base image '%s'", baseVolumeId) } volumeDef.BackingStore.Path = baseVolPath + backingStoreXml, err := baseVolume.GetXMLDesc(0) + if err != nil { + return fmt.Errorf("can't get definitions for base image '%s'", baseVolumeId) + } + backingStore, err := newDefVolumeFromXML(backingStoreXml) + if err != nil { + return fmt.Errorf("can't parse definitions for base image '%s'", baseVolumeId) + } + log.Printf("[DEBUG] base volume format is %s", backingStore.Target.Format.Type) + volumeDef.BackingStore.Format.Type = backingStore.Target.Format.Type } if baseVolumeName, ok := d.GetOk("base_volume_name"); ok { @@ -221,7 +220,6 @@ func resourceLibvirtVolumeCreate(d *schema.ResourceData, meta interface{}) error } volumeDef.BackingStore = new(defBackingStore) - volumeDef.BackingStore.Format.Type = "qcow2" baseVolume, err := baseVolumePool.LookupStorageVolByName(baseVolumeName.(string)) if err != nil { return fmt.Errorf("Can't retrieve volume %s", baseVolumeName.(string)) @@ -231,6 +229,16 @@ func resourceLibvirtVolumeCreate(d *schema.ResourceData, meta interface{}) error return fmt.Errorf("can't get name for base image '%s'", baseVolumeName) } volumeDef.BackingStore.Path = baseVolPath + backingStoreXml, err := baseVolume.GetXMLDesc(0) + if err != nil { + return fmt.Errorf("can't get definitions for base image '%s'", baseVolumeName) + } + backingStore, err := newDefVolumeFromXML(backingStoreXml) + if err != nil { + return fmt.Errorf("can't parse definitions for base image '%s'", baseVolumeName) + } + log.Printf("[DEBUG] base volume format is %s", backingStore.Target.Format.Type) + volumeDef.BackingStore.Format.Type = backingStore.Target.Format.Type } if volume == nil { diff --git a/libvirt/resource_libvirt_volume_test.go b/libvirt/resource_libvirt_volume_test.go index b76fd848..95cabadb 100644 --- a/libvirt/resource_libvirt_volume_test.go +++ b/libvirt/resource_libvirt_volume_test.go @@ -178,31 +178,3 @@ func TestAccLibvirtVolume_Format(t *testing.T) { }, }) } - -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