summaryrefslogtreecommitdiff
path: root/libvirt/resource_libvirt_volume.go
diff options
context:
space:
mode:
authorThomas Hipp <thipp@suse.de>2017-11-29 12:51:21 +0100
committerThomas Hipp <thipp@suse.de>2017-11-29 13:58:07 +0100
commit96f4032f04b2a625b12d188cae4315887bf9fc66 (patch)
tree89244c873f4c42cd5a22e03a8f668c4ec96e042d /libvirt/resource_libvirt_volume.go
parent3fe13bb19661adfd488ff20d978fbac407e34835 (diff)
downloadterraform-provider-libvirt-96f4032f04b2a625b12d188cae4315887bf9fc66.tar
terraform-provider-libvirt-96f4032f04b2a625b12d188cae4315887bf9fc66.tar.gz
check uniqueness of storage volume name
The storage volume name needs to be unique. If this is not the case, the storage volume will be overwritten which is not problematic itself. However, `terraform destroy` will fail since it will try and delete the same storage volume twice. Signed-off-by: Thomas Hipp <thipp@suse.de>
Diffstat (limited to 'libvirt/resource_libvirt_volume.go')
-rw-r--r--libvirt/resource_libvirt_volume.go24
1 files changed, 7 insertions, 17 deletions
diff --git a/libvirt/resource_libvirt_volume.go b/libvirt/resource_libvirt_volume.go
index 7a7f6a40..4f9c3322 100644
--- a/libvirt/resource_libvirt_volume.go
+++ b/libvirt/resource_libvirt_volume.go
@@ -105,12 +105,15 @@ func resourceLibvirtVolumeCreate(d *schema.ResourceData, meta interface{}) error
return pool.Refresh(0)
})
- volumeDef := newDefVolume()
-
- if name, ok := d.GetOk("name"); ok {
- volumeDef.Name = name.(string)
+ // Check whether the storage volume already exists. Its name needs to be
+ // unique.
+ if _, err := pool.LookupStorageVolByName(d.Get("name").(string)); err == nil {
+ return fmt.Errorf("storage volume '%s' already exists", d.Get("name").(string))
}
+ volumeDef := newDefVolume()
+ volumeDef.Name = d.Get("name").(string)
+
volumeFormat := "qcow2"
if _, ok := d.GetOk("format"); ok {
volumeFormat = d.Get("format").(string)
@@ -136,19 +139,6 @@ func resourceLibvirtVolumeCreate(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("'base_volume_name' can't be specified when also 'source' is given")
}
- // Check if we already have this image in the pool
- if len(volumeDef.Name) > 0 {
- if v, err := pool.LookupStorageVolByName(volumeDef.Name); err != nil {
- log.Printf("could not find image %s in pool %s", volumeDef.Name, poolName)
- } else {
- volume = v
- volumeDef, err = newDefVolumeFromLibvirt(volume)
- if err != nil {
- return fmt.Errorf("could not get a volume definition from XML for %s: %s", volumeDef.Name, err)
- }
- }
- }
-
if img, err = newImage(source.(string)); err != nil {
return err
}