Commit c2d1901c authored by Thibault VINCENT's avatar Thibault VINCENT
Browse files

fix storage interface, add exception handling, implement volume methods

parent cf264fe7
Loading
Loading
Loading
Loading
+23 −3
Original line number Diff line number Diff line
@@ -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
        '''
+68 −14
Original line number Diff line number Diff line
@@ -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 = []
        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`
        '''
        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