From c2d1901c48cbb6a7a48df6fefa05d12ad93068bd Mon Sep 17 00:00:00 2001
From: Thibault VINCENT <thibault.vincent@smartjog.com>
Date: Tue, 21 Dec 2010 17:53:40 +0100
Subject: [PATCH] fix storage interface, add exception handling, implement
 volume methods

---
 ccnode/interface.py      | 26 +++++++++++--
 ccnode/libvirtwrapper.py | 82 +++++++++++++++++++++++++++++++++-------
 2 files changed, 91 insertions(+), 17 deletions(-)

diff --git a/ccnode/interface.py b/ccnode/interface.py
index 8aea05f..503a9ba 100644
--- a/ccnode/interface.py
+++ b/ccnode/interface.py
@@ -178,9 +178,9 @@ class HVStorage(object):
         '''
         pass
 
-    def get_volumes(self, pool=None):
+    def get_volume_names(self, pool=None):
         '''
-        Returns volumes stored in pool or all pools
+        Returns volume names stored in this pool or all pools
         '''
         pass
 
@@ -214,7 +214,7 @@ class HVStorage(object):
         '''
         pass
 
-    def del_volume(self, pool, name):
+    def del_volume(self, pool, name, wipe=False):
         '''
         Deletes a volume in the specified pool
 
@@ -235,6 +235,15 @@ class HVStorage(object):
             pool is suitable
         '''
 
+    def get_pool_name(self, pool):
+        '''
+        Returns the name of this pool
+
+        :param pool: the storage pool name
+        :type pool: libvirt.`virStoragePool`
+        :return: :class:`str` name of the pool
+        '''
+
     def get_pool_state(self, pool):
         '''
         Returns the running state of the pool
@@ -269,3 +278,14 @@ class HVStorage(object):
         :type pool: :class:`virStoragePool`
         :return: :class:`int` of used space in gigabytes
         '''
+        
+    def get_volume_path(self, pool, vol_name):
+        '''
+        Returns the file path to the volume
+
+        :param pool: the storage pool containing this volume
+        :type pool: libvirt.`virStoragePool`
+        :param volume_name: name of the pool volume
+        :type volume_name: :class:`str`
+        :return: :class:`str` path to the volume file
+        '''
diff --git a/ccnode/libvirtwrapper.py b/ccnode/libvirtwrapper.py
index dd8f29f..26119c9 100644
--- a/ccnode/libvirtwrapper.py
+++ b/ccnode/libvirtwrapper.py
@@ -339,16 +339,19 @@ class LibvirtHVStorage(HVStorage):
         '''
         return self._pools
 
-    def get_volumes(self, pool=None):
+    def get_volume_names(self, pool=None):
         '''
-        Returns volumes stored in pool or all pools
+        Returns volume names stored in this pool or all pools
         '''
         volumes = []
-        if pool is None:
-            for pool in self._pools.iteritems():
-                volumes.extend(pool[1].listVolumes())
-        else:
-            volumes = pool.listVolumes()
+        try:
+            if pool is None:
+                for pool in self._pools.iteritems():
+                    volumes.extend(pool[1].listVolumes())
+            else:
+                volumes = pool.listVolumes()
+        except libvirt.libvirtError as e:
+            raise StorageError("Failed to get volume list (%s)" % e)
         return volumes
 
     def add_volume(self, pool, name, space):
@@ -376,7 +379,7 @@ class LibvirtHVStorage(HVStorage):
         except libvirt.libvirtError as e:
             raise StorageError("Failed to create the volume (%s)" % e)
 
-    def del_volume(self, pool, name):
+    def del_volume(self, pool, name, wipe=False):
         '''
         Deletes a volume in the specified pool
 
@@ -389,10 +392,17 @@ class LibvirtHVStorage(HVStorage):
             vol = pool.storageVolLookupByName(name)
         except libvirt.libvirtError as e:
             raise StorageError("Volume not found (%s)" % e)
+        if wipe:
+            try:
+                vol.wipe(0)
+            except libvirt.libvirtError as e:
+                raise StorageError("Failed to wipe volume, data integrity"
+                    " is unknown (%s)" % e)
         try:
             vol.delete(0)
         except libvirt.libvirtError as e:
-            raise StorageError("Failed to delete the volume (%s)" % e)
+            raise StorageError("Failed to delete the volume, but it may be"
+                " wiped (%s)" % e)
 
     def find_space(self, new_space):
         '''
@@ -408,9 +418,22 @@ class LibvirtHVStorage(HVStorage):
                 if new_space * GIGABYTE_DIV < pool[1].info()[3]:
                     return (pool)
             except libvirt.libvirtError as e:
-                raise StorageError("Can't query pool informations (%s)" % e)
+                raise StorageError("Can't get pool informations (%s)" % e)
         return False
 
+    def get_pool_name(self, pool):
+        '''
+        Returns the name of this pool
+
+        :param pool: the storage pool name
+        :type pool: libvirt.`virStoragePool`
+        :return: :class:`str` name of the pool
+        '''
+        try:
+            return pool.name()
+        except libvirt.libvirtError as e:
+            raise StorageError("Can't get pool name (%s)" % e)
+
     def get_pool_state(self, pool):
         '''
         Returns the running state of the pool
@@ -418,7 +441,10 @@ class LibvirtHVStorage(HVStorage):
         :param pool: the storage pool name
         :type pool: libvirt.`virStoragePool`
         '''
-        return POOL_STATE[pool.info()[0]]
+        try:
+            return POOL_STATE[pool.info()[0]]
+        except libvirt.libvirtError as e:
+            raise StorageError("Can't get pool state (%s)" % e)
     
     def get_pool_total_space(self, pool):
         '''
@@ -428,7 +454,10 @@ class LibvirtHVStorage(HVStorage):
         :type pool: :class:`virStoragePool`
         :return: :class:`int` of capacity in gigabytes
         '''
-        return pool.info()[1];
+        try:
+            return pool.info()[1]
+        except libvirt.libvirtError as e:
+            raise StorageError("Can't get pool informations (%s)" % e)
     
     def get_pool_available_space(self, pool):
         '''
@@ -438,7 +467,10 @@ class LibvirtHVStorage(HVStorage):
         :type pool: :class:`virStoragePool`
         :return: :class:`int` of available free space in gigabytes
         '''
-        return pool.info()[2];
+        try:
+            return pool.info()[2]
+        except libvirt.libvirtError as e:
+            raise StorageError("Can't get pool informations (%s)" % e)
     
     def get_pool_used_space(self, pool):
         '''
@@ -448,7 +480,29 @@ class LibvirtHVStorage(HVStorage):
         :type pool: :class:`virStoragePool`
         :return: :class:`int` of used space in gigabytes
         '''
-        return pool.info()[3];
+        try:
+            return pool.info()[3]
+        except libvirt.libvirtError as e:
+            raise StorageError("Can't get pool informations (%s)" % e)
+    
+    def get_volume_path(self, pool, vol_name):
+        '''
+        Returns the file path to the volume
+
+        :param pool: the storage pool containing this volume
+        :type pool: libvirt.`virStoragePool`
+        :param volume_name: name of the pool volume
+        :type volume_name: :class:`str`
+        :return: :class:`str` path to the volume file
+        '''
+        try:
+            vol = pool.storageVolLookupByName(vol_name)
+        except libvirt.libvirtError as e:
+            raise StorageError("Can't find volume in pool (%s)" % e)
+        try:
+            return vol.path()
+        except libvirt.libvirtError as e:
+            raise StorageError("Volume has no path information (%s)" % e)
 
 
 #### Helper functions
-- 
GitLab