<html>
  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <div class="moz-cite-prefix"><br>
      <big>Reviewed-by: Aline Manera <a class="moz-txt-link-rfc2396E" href="mailto:alinefm@linux.vnet.ibm.com">&lt;alinefm@linux.vnet.ibm.com&gt;</a></big><br>
      <br>
      On 01/27/2014 05:15 AM, <a class="moz-txt-link-abbreviated" href="mailto:shaohef@linux.vnet.ibm.com">shaohef@linux.vnet.ibm.com</a> wrote:<br>
    </div>
    <blockquote
      cite="mid:1390806953-19733-1-git-send-email-shaohef@linux.vnet.ibm.com"
      type="cite">
      <pre wrap="">From: ShaoHe Feng <a class="moz-txt-link-rfc2396E" href="mailto:shaohef@linux.vnet.ibm.com">&lt;shaohef@linux.vnet.ibm.com&gt;</a>

>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.
<a class="moz-txt-link-freetext" href="https://github.com/kimchi-project/kimchi/wiki/support-unicode">https://github.com/kimchi-project/kimchi/wiki/support-unicode</a>

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 &lt;user&gt; -H 'Accept: application/json' -H 'Content-type:
application/json'  <a class="moz-txt-link-freetext" href="http://localhost:8000/storagepools/">http://localhost:8000/storagepools/</a> -X POST -d '
{
   "name":"kīмсhī-pool",
   "type":"dir",
   "path":"/tmp/test"
} '

2. get this pool
$ curl -u &lt;user&gt; -H 'Accept: application/json' -H 'Content-type:
application/json'  <a class="moz-txt-link-freetext" href="http://localhost:8000/storagepols/kīмсhī-pool">http://localhost:8000/storagepols/kīмсhī-pool</a>

3. delete this pool
$ curl -u &lt;user&gt; -H 'Accept: application/json' -H 'Content-type:
application/json'  <a class="moz-txt-link-freetext" href="http://localhost:8000/storagepols/kīмсhī-pool">http://localhost:8000/storagepols/kīмсhī-pool</a> \
-X DELETE

Signed-off-by: ShaoHe Feng <a class="moz-txt-link-rfc2396E" href="mailto:shaohef@linux.vnet.ibm.com">&lt;shaohef@linux.vnet.ibm.com&gt;</a>
---
 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"""
         &lt;pool type='dir'&gt;
           &lt;name&gt;{name}&lt;/name&gt;
           &lt;target&gt;
@@ -1775,7 +1775,7 @@ class NetfsPoolDef(StoragePoolDef):
         # source[path]:
         poolArgs = copy.deepcopy(self.poolArgs)
         poolArgs['path'] = self.path
-        xml = """
+        xml = u"""
         &lt;pool type='netfs'&gt;
           &lt;name&gt;{name}&lt;/name&gt;
           &lt;source&gt;
@@ -1811,7 +1811,7 @@ class LogicalPoolDef(StoragePoolDef):
         poolArgs['source']['devices'] = ''.join(devices)
         poolArgs['path'] = self.path
 
-        xml = """
+        xml = u"""
         &lt;pool type='logical'&gt;
         &lt;name&gt;{name}&lt;/name&gt;
             &lt;source&gt;
@@ -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'''
             &lt;secret ephemeral='no' private='yes'&gt;
               &lt;description&gt;Secret for iSCSI storage pool {name}&lt;/description&gt;
               &lt;auth type='chap' username='{username}'/&gt;
@@ -1871,7 +1871,7 @@ class IscsiPoolDef(StoragePoolDef):
         except KeyError:
             return ""
 
-        return '''
+        return u'''
         &lt;auth type='chap' username='{username}'&gt;
           &lt;secret type='iscsi' usage='{name}'/&gt;
         &lt;/auth&gt;'''.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"""
         &lt;pool type='iscsi'&gt;
           &lt;name&gt;{name}&lt;/name&gt;
           &lt;source&gt;
</pre>
    </blockquote>
    <br>
  </body>
</html>