diff options
-rw-r--r-- | libvirt/resource_libvirt_volume.go | 33 | ||||
-rw-r--r-- | libvirt/utils.go | 38 |
2 files changed, 39 insertions, 32 deletions
diff --git a/libvirt/resource_libvirt_volume.go b/libvirt/resource_libvirt_volume.go index af565e15..e125e75e 100644 --- a/libvirt/resource_libvirt_volume.go +++ b/libvirt/resource_libvirt_volume.go @@ -375,36 +375,5 @@ func resourceLibvirtVolumeDelete(d *schema.ResourceData, meta interface{}) error return fmt.Errorf("The libvirt connection was nil.") } - volume, err := virConn.LookupStorageVolByKey(d.Id()) - if err != nil { - return fmt.Errorf("Can't retrieve volume %s", d.Id()) - } - defer volume.Free() - - // Refresh the pool of the volume so that libvirt knows it is - // not longer in use. - volPool, err := volume.LookupPoolByVolume() - if err != nil { - return fmt.Errorf("Error retrieving pool for volume: %s", err) - } - defer volPool.Free() - - WaitForSuccess("Error refreshing pool for volume", func() error { - return volPool.Refresh(0) - }) - - // Workaround for redhat#1293804 - // https://bugzilla.redhat.com/show_bug.cgi?id=1293804#c12 - // Does not solve the problem but it makes it happen less often. - _, err = volume.GetXMLDesc(0) - if err != nil { - return fmt.Errorf("Can't retrieve volume %s XML desc: %s", d.Id(), err) - } - - err = volume.Delete(0) - if err != nil { - return fmt.Errorf("Can't delete volume %s: %s", d.Id(), err) - } - - return nil + return RemoveVolume(virConn, d.Id()) } diff --git a/libvirt/utils.go b/libvirt/utils.go index ed645938..1ec739ae 100644 --- a/libvirt/utils.go +++ b/libvirt/utils.go @@ -3,6 +3,7 @@ package libvirt import ( "crypto/rand" "fmt" + libvirt "github.com/dmacvicar/libvirt-go" "log" "time" ) @@ -53,3 +54,40 @@ func RandomMACAddress() (string, error) { return fmt.Sprintf("%02x:%02x:%02x:%02x:%02x:%02x", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]), nil } + +// Remove the volume identified by `key` from libvirt +func RemoveVolume(virConn *libvirt.VirConnection, key string) error { + volume, err := virConn.LookupStorageVolByKey(key) + if err != nil { + return fmt.Errorf("Can't retrieve volume %s", key) + } + defer volume.Free() + + // Refresh the pool of the volume so that libvirt knows it is + // not longer in use. + volPool, err := volume.LookupPoolByVolume() + if err != nil { + return fmt.Errorf("Error retrieving pool for volume: %s", err) + } + defer volPool.Free() + + WaitForSuccess("Error refreshing pool for volume", func() error { + return volPool.Refresh(0) + }) + + // Workaround for redhat#1293804 + // https://bugzilla.redhat.com/show_bug.cgi?id=1293804#c12 + // Does not solve the problem but it makes it happen less often. + _, err = volume.GetXMLDesc(0) + if err != nil { + return fmt.Errorf("Can't retrieve volume %s XML desc: %s", key, err) + } + + err = volume.Delete(0) + if err != nil { + return fmt.Errorf("Can't delete volume %s: %s", key, err) + } + + return nil + +} |