From 61d19ac26c08769f5274d3fdc473f9a1edb6023e Mon Sep 17 00:00:00 2001
From: Thibault VINCENT <thibault.vincent@smartjog.com>
Date: Tue, 21 Dec 2010 11:31:28 +0100
Subject: [PATCH] Storage implementation: add_volume del_volume + fixed
 interface docstring for add_volume

---
 ccnode/interface.py      | 26 +++++++--------
 ccnode/libvirtwrapper.py | 70 ++++++++++++++++++++++++++++++++--------
 2 files changed, 69 insertions(+), 27 deletions(-)

diff --git a/ccnode/interface.py b/ccnode/interface.py
index c9681b9..3d0611d 100644
--- a/ccnode/interface.py
+++ b/ccnode/interface.py
@@ -55,13 +55,13 @@ class Host(object):
         Returns storage configuration
         '''
         pass
-    
+
     def get_storage_stats(self):
         '''
         Returns storage statistics
         '''
         pass
-    
+
     def get_info(self):
         '''
         get info about the host
@@ -80,18 +80,18 @@ class Hypervisor(Host):
     Base interface class for all hypervisor types and libraries
     that interacts with them
     '''
-    
+
     _id = 0
 
     def __init__(self):
         super(Hypervisor, self).__init__()
         self.__class__._id += 1
         self.hv_type = 'None'
-    
+
     def list_vms(self):
         '''
         Lists current defined virtual machines in the hypervisor
-        Returns a dict of vms 
+        Returns a dict of vms
         '''
         pass
 
@@ -116,7 +116,7 @@ class VM(Host):
     Base Virtual Machine class
     '''
     _id_count = 0
-    
+
     def __init__(self):
         '''
         Initiate a Virtual Machine
@@ -124,7 +124,7 @@ class VM(Host):
         '''
         self.__class__._id_count += 1
         self._id = self.__class__._id_count
-    
+
     def get_id(self):
         '''
         Gets the id of the virtual machine
@@ -139,13 +139,13 @@ class VM(Host):
         - running
         - blocked in ressource
         - paused by user
-        - being shutdown 
+        - being shutdown
         - is shut off
         - is crached
         '''
         pass
 
-    
+
     def shutdown(self):
         '''
         Shut down given vm
@@ -217,7 +217,7 @@ class HVStorage(object):
         '''
         Adds a volume to the specified pool
 
-        :param pool: name of the pool
+        :param pool: the pool in which to create the volume
         :type pool: :class:`str`
         :param name: name of the new volume
         :type name: :class:`str`
@@ -259,10 +259,10 @@ class HVStorage(object):
         '''
 
 
-        
 
 
-        
 
-   
+
+
+
 
diff --git a/ccnode/libvirtwrapper.py b/ccnode/libvirtwrapper.py
index 3dcb43c..e9c4310 100644
--- a/ccnode/libvirtwrapper.py
+++ b/ccnode/libvirtwrapper.py
@@ -2,7 +2,7 @@
 
 import libvirt
 import sys
-# we use psutils to get host informations we can't get with 
+# we use psutils to get host informations we can't get with
 # libvirt
 import psutil
 from interface import *
@@ -70,7 +70,7 @@ POOL_BACKEND = 'backend'
 
 DEFAULT_VM_START = VM_START_STATES['running']
 
-## 
+##
 # Pretty size outputs
 ##
 
@@ -141,7 +141,7 @@ class LibvirtVm(VM):
 
     def get_vcpu(self):
         return self._domain.info()[3]
-    
+
     def get_cpu_percent(self):
         self._find_pid()
         if self._pid:
@@ -152,7 +152,7 @@ class LibvirtVm(VM):
             return res
         else:
             return 0
-    
+
     def get_status(self):
         return VM_STATUS[self._domain.info()[0]]
 
@@ -167,7 +167,7 @@ class LibvirtVm(VM):
             self._domain.destroy()
         except libvirt.libvirtError:
             raise VMError('%s is not running !' % self.get_name())
-            
+
 
     def start(self):
         try:
@@ -210,12 +210,12 @@ class LibvirtHypervisor(Hypervisor):
         important as it specifies the hypervisor type used when
         connecting to libvirt.
     :type hv_type: :class:`str`
-    
+
     .. note::
         hv_type can be any of kvm, xen, ... , other hypervisors, however only
         one connection at a time is allowed
     '''
-    
+
     def __init__(self, hv_type):
         #FIXME: don't use hard coded hypervisor type in URI
         try:
@@ -224,7 +224,7 @@ class LibvirtHypervisor(Hypervisor):
             else:
                 raise NotImplemented('or unknown hypervisor type')
         except libvirt.libvirtError as error:
-            raise HypervisorError(error, 'libvirt cannot connect to hypervisor')            
+            raise HypervisorError(error, 'libvirt cannot connect to hypervisor')
         #build vm objects
         self._build_vm_list()
         self.hv_info = {}
@@ -308,7 +308,7 @@ class LibvirtHypervisor(Hypervisor):
     def get_status(self):
         raise NotImplementedError()
 
-#TODO: finish storage class, make tests, 
+#TODO: finish storage class, make tests,
 class LibvirtHVStorage(HVStorage):
     '''
     Base storage class using libvirt api for storage managment
@@ -331,7 +331,7 @@ class LibvirtHVStorage(HVStorage):
         pools.extend(self.hv_handle._con_handle.listDefinedStoragePools())
         pools.extend(self.hv_handle._con_handle.listStoragePools())
         self._pools = []
-        self._pools.extend(pool_obj(self.hv_handle._con_handle, pool) for pool in pools) 
+        self._pools.extend(pool_obj(self.hv_handle._con_handle, pool) for pool in pools)
 
     def get_storage_pools(self):
         return [pool.name() for pool in self._pools]
@@ -340,8 +340,8 @@ class LibvirtHVStorage(HVStorage):
         pool_info = {}
         pool_info[POOL_NAME] = pool.name()
         pool_info[POOL_STATUS] = self.get_pool_state(pool)
-        pool_info[POOL_TOTAL_SIZE] = self.get_pool_size(pool) / GIGABYTE_DIV 
-                                        
+        pool_info[POOL_TOTAL_SIZE] = self.get_pool_size(pool) / GIGABYTE_DIV
+
 
         return pool_info
 
@@ -362,8 +362,50 @@ class LibvirtHVStorage(HVStorage):
         :type pool: libvirt.`virStoragePool`
         '''
         return POOL_STATE[pool.info()[0]]
-        
-            
+
+    def add_volume(self, pool, name, space):
+        '''
+        Adds a volume to the specified pool
+
+        :param pool: the pool in which to create the volume
+        :type pool: :class:`str`
+        :param name: name of the new volume
+        :type name: :class:`str`
+        :param space: size of the new volume in gigabytes
+        :type space: :class:`int`
+        '''
+        xml_desc = """
+            <volume>
+                <name>%(vol_name)s</name>
+                <capacity>%(vol_size)u</capacity>
+            </volume>
+        """ % {
+            "vol_name" : name,
+            "vol_size" : space * GIGABYTE_DIV,
+            }
+        try:
+            pool.createXML(xml_desc, 0);
+        except libvirt.libvirtError as e:
+            raise StorageError("Failed to create the volume (%s)" % e)
+
+    def del_volume(self, pool, name):
+        '''
+        Deletes a volume in the specified pool
+
+        :param pool: the pool in which delete the volume
+        :type pool: :class:`str`
+        :param name: the name of the volume
+        :type name: :class:`str`
+        '''
+        try:
+            vol = pool.storageVolLookupByName(name)
+        except libvirt.libvirtError as e:
+            raise StorageError("Volume not found (%s)" % e)
+        try:
+            vol.delete(0)
+        except libvirt.libvirtError as e:
+            raise StorageError("Failed to delete the volume (%s)" % e)
+
 
 #### Helper functions
 
-- 
GitLab