aboutsummaryrefslogtreecommitdiff
path: root/libvirt
diff options
context:
space:
mode:
authorFlavio Castelli <fcastelli@suse.com>2016-06-27 11:35:39 +0200
committerFlavio Castelli <fcastelli@suse.com>2016-06-27 11:35:39 +0200
commitaf5f25ca9f8d903cf7f365e5284bdc4743fa965c (patch)
tree1dcb0412ac26116e3dba9974f29307989b40aa62 /libvirt
parentef32742d5e4f5311edfcc1ced0a032959153d0f9 (diff)
downloadterraform-provider-libvirt-af5f25ca9f8d903cf7f365e5284bdc4743fa965c.tar
terraform-provider-libvirt-af5f25ca9f8d903cf7f365e5284bdc4743fa965c.tar.gz
Refactoring: move code removal into a dedicated function
This function is going to be shared later on with the cloud-init resource. Signed-off-by: Flavio Castelli <fcastelli@suse.com>
Diffstat (limited to 'libvirt')
-rw-r--r--libvirt/resource_libvirt_volume.go33
-rw-r--r--libvirt/utils.go38
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
+
+}