[Kimchi-devel] [PATCH] Storage pool support unicode correctly

shaohef at linux.vnet.ibm.com shaohef at linux.vnet.ibm.com
Mon Jan 27 07:15:53 UTC 2014


From: ShaoHe Feng <shaohef at linux.vnet.ibm.com>

>From unicode, we have handle the incoming text from
kimchi server and outgoing text to kimchi server correctly.

But we do not handle the incoming text from libvirt and outgoing text to
libvirt correctly.

For some variables of storage pool, such as pool name and pool xml do
not follow our principle.
https://github.com/kimchi-project/kimchi/wiki/support-unicode

This patch will improve the follow:
1. Handle storage pool xml and pool name as unicode in kimchi, and decode
them when pass them to libvirt.
2. Decode the pool name from libvirt.

We should guarantee:
All text strings, everywhere should be of type unicode, not str.
If we're handling text, and our variable is a str, it's a bug!

Test this patch:
1. create a pool
$ curl -u <user> -H 'Accept: application/json' -H 'Content-type:
application/json'  http://localhost:8000/storagepools/ -X POST -d '
{
   "name":"kīмсhī-pool",
   "type":"dir",
   "path":"/tmp/test"
} '

2. get this pool
$ curl -u <user> -H 'Accept: application/json' -H 'Content-type:
application/json'  http://localhost:8000/storagepols/kīмсhī-pool

3. delete this pool
$ curl -u <user> -H 'Accept: application/json' -H 'Content-type:
application/json'  http://localhost:8000/storagepols/kīмсhī-pool \
-X DELETE

Signed-off-by: ShaoHe Feng <shaohef at linux.vnet.ibm.com>
---
 src/kimchi/model.py | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/src/kimchi/model.py b/src/kimchi/model.py
index 81c1507..55de570 100644
--- a/src/kimchi/model.py
+++ b/src/kimchi/model.py
@@ -1170,7 +1170,7 @@ class Model(object):
                 task_id = self._do_deep_scan(params)
             poolDef = StoragePoolDef.create(params)
             poolDef.prepare(conn)
-            xml = poolDef.xml
+            xml = poolDef.xml.encode("utf-8")
         except KeyError, key:
             raise MissingParameter(key)
 
@@ -1297,14 +1297,14 @@ class Model(object):
             conn = self.conn.get()
             names = conn.listStoragePools()
             names += conn.listDefinedStoragePools()
-            return sorted(names)
+            return sorted(map(lambda x: x.decode('utf-8'), names))
         except libvirt.libvirtError as e:
             raise OperationFailed(e.get_error_message())
 
     def _get_storagepool(self, name):
         conn = self.conn.get()
         try:
-            return conn.storagePoolLookupByName(name)
+            return conn.storagePoolLookupByName(name.encode("utf-8"))
         except libvirt.libvirtError as e:
             if e.get_error_code() == libvirt.VIR_ERR_NO_STORAGE_POOL:
                 raise NotFoundError("Storage Pool '%s' not found" % name)
@@ -1594,7 +1594,7 @@ class LibvirtVMTemplate(VMTemplate):
         pool_name = pool_name_from_uri(pool_uri)
         try:
             conn = self.conn.get()
-            pool = conn.storagePoolLookupByName(pool_name)
+            pool = conn.storagePoolLookupByName(pool_name.encode("utf-8"))
         except libvirt.libvirtError:
             raise InvalidParameter('Storage specified by template does not exist')
         if not pool.isActive():
@@ -1719,7 +1719,7 @@ class DirPoolDef(StoragePoolDef):
         # name:
         # type:
         # path:
-        xml = """
+        xml = u"""
         <pool type='dir'>
           <name>{name}</name>
           <target>
@@ -1775,7 +1775,7 @@ class NetfsPoolDef(StoragePoolDef):
         # source[path]:
         poolArgs = copy.deepcopy(self.poolArgs)
         poolArgs['path'] = self.path
-        xml = """
+        xml = u"""
         <pool type='netfs'>
           <name>{name}</name>
           <source>
@@ -1811,7 +1811,7 @@ class LogicalPoolDef(StoragePoolDef):
         poolArgs['source']['devices'] = ''.join(devices)
         poolArgs['path'] = self.path
 
-        xml = """
+        xml = u"""
         <pool type='logical'>
         <name>{name}</name>
             <source>
@@ -1845,7 +1845,7 @@ class IscsiPoolDef(StoragePoolDef):
             virSecret = conn.secretLookupByUsage(
                 libvirt.VIR_SECRET_USAGE_TYPE_ISCSI, self.poolArgs['name'])
         except libvirt.libvirtError:
-            xml = '''
+            xml = u'''
             <secret ephemeral='no' private='yes'>
               <description>Secret for iSCSI storage pool {name}</description>
               <auth type='chap' username='{username}'/>
@@ -1871,7 +1871,7 @@ class IscsiPoolDef(StoragePoolDef):
         except KeyError:
             return ""
 
-        return '''
+        return u'''
         <auth type='chap' username='{username}'>
           <secret type='iscsi' usage='{name}'/>
         </auth>'''.format(name=poolArgs['name'], username=auth['username'])
@@ -1891,7 +1891,7 @@ class IscsiPoolDef(StoragePoolDef):
                                    'auth': self._format_auth(poolArgs)})
         poolArgs['path'] = '/dev/disk/by-id'
 
-        xml = """
+        xml = u"""
         <pool type='iscsi'>
           <name>{name}</name>
           <source>
-- 
1.8.4.2




More information about the Kimchi-devel mailing list