[Kimchi-devel] [PATCH 2/2] Prevent volume create and delete for certain pool types

lvroyce at linux.vnet.ibm.com lvroyce at linux.vnet.ibm.com
Mon Feb 24 10:24:03 UTC 2014


From: Royce Lv <lvroyce at linux.vnet.ibm.com>

As iscsi, scsi, mpath pools do not support create/delete
storage volumes, when creating and deleting volumes,
these pool types should be eleminated.
REF:
    http://libvirt.org/storage.html

Signed-off-by: Royce Lv <lvroyce at linux.vnet.ibm.com>
---
 src/kimchi/control/base.py         |  2 ++
 src/kimchi/i18n.py                 |  1 +
 src/kimchi/model/storagevolumes.py | 18 +++++++++++++++---
 3 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/src/kimchi/control/base.py b/src/kimchi/control/base.py
index e8f03ae..300981c 100644
--- a/src/kimchi/control/base.py
+++ b/src/kimchi/control/base.py
@@ -130,6 +130,8 @@ class Resource(object):
                 return self.delete()
             except NotFoundError, e:
                 raise cherrypy.HTTPError(404, e.message)
+            except InvalidParameter, e:
+                raise cherrypy.HTTPError(400, e.message)
         elif method == 'PUT':
             try:
                 return self.update()
diff --git a/src/kimchi/i18n.py b/src/kimchi/i18n.py
index 7d08254..03ca3cb 100644
--- a/src/kimchi/i18n.py
+++ b/src/kimchi/i18n.py
@@ -145,6 +145,7 @@ messages = {
     "KCHVOL0009E": _("Unable to wipe storage volumes %(name)s. Details: %(err)s"),
     "KCHVOL0010E": _("Unable to delete storage volume %(name)s. Details: %(err)s"),
     "KCHVOL0011E": _("Unable to resize storage volume %(name)s. Details: %(err)s"),
+    "KCHVOL0012E": _("Storage type %(type)s does not support volume create and delete"),
 
     "KCHIFACE0001E": _("Interface %(name)s does not exist"),
 
diff --git a/src/kimchi/model/storagevolumes.py b/src/kimchi/model/storagevolumes.py
index 20c65b9..d202eaa 100644
--- a/src/kimchi/model/storagevolumes.py
+++ b/src/kimchi/model/storagevolumes.py
@@ -25,9 +25,10 @@ import os
 import libvirt
 
 from kimchi import xmlutils
-from kimchi.exception import InvalidOperation, IsoFormatError
+from kimchi.exception import InvalidOperation, IsoFormatError, InvalidParameter
 from kimchi.exception import MissingParameter, NotFoundError, OperationFailed
 from kimchi.isoinfo import IsoImage
+from kimchi.model.constant import READONLY_TYPE
 from kimchi.model.storagepools import StoragePoolModel
 
 
@@ -40,8 +41,9 @@ VOLUME_TYPE_MAP = {0: 'file',
 class StorageVolumesModel(object):
     def __init__(self, **kargs):
         self.conn = kargs['conn']
+        self.objstore = kargs['objstore']
 
-    def create(self, pool, params):
+    def create(self, pool_name, params):
         vol_xml = """
         <volume>
           <name>%(name)s</name>
@@ -59,12 +61,16 @@ class StorageVolumesModel(object):
 
         name = params['name']
         try:
-            pool = StoragePoolModel.get_storagepool(pool, self.conn)
+            pool = StoragePoolModel.get_storagepool(pool_name, self.conn)
             xml = vol_xml % params
         except KeyError, item:
             raise MissingParameter("KCHVOL0004E", {'item': item,
                                                    'volume': name})
 
+        pool_info = StoragePoolModel(conn=self.conn,
+                                     objstore=self.objstore).lookup(pool_name)
+        if pool_info['type'] in READONLY_TYPE:
+            raise InvalidParameter("KCHVOL0012E", {'type': pool_info['type']})
         try:
             pool.createXML(xml, 0)
         except libvirt.libvirtError as e:
@@ -89,6 +95,7 @@ class StorageVolumesModel(object):
 class StorageVolumeModel(object):
     def __init__(self, **kargs):
         self.conn = kargs['conn']
+        self.objstore = kargs['objstore']
 
     def _get_storagevolume(self, pool, name):
         pool = StoragePoolModel.get_storagepool(pool, self.conn)
@@ -139,6 +146,11 @@ class StorageVolumeModel(object):
                                   {'name': name, 'err': e.get_error_message()})
 
     def delete(self, pool, name):
+        pool_info = StoragePoolModel(conn=self.conn,
+                                     objstore=self.objstore).lookup(pool)
+        if pool_info['type'] in READONLY_TYPE:
+            raise InvalidParameter("KCHVOL0012E", {'type': pool_info['type']})
+
         volume = self._get_storagevolume(pool, name)
         try:
             volume.delete(0)
-- 
1.8.1.2




More information about the Kimchi-devel mailing list