<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body smarttemplateinserted="true" text="#000000" bgcolor="#FFFFFF">
    <div id="smartTemplate4-quoteHeader">As Pradeep mentioned:<br>
      we will add more storage pools, how about split these xml to a new
      file?<br>
      &nbsp;</div>
    <div class="moz-cite-prefix">On 12/16/2013 04:01 PM, Zhou Zheng
      Sheng wrote:<br>
    </div>
    <blockquote
      cite="mid:1387180891-22791-2-git-send-email-zhshzhou@linux.vnet.ibm.com"
      type="cite">
      <pre wrap="">In src/kimchi/model.py, _get_pool_xml() is to generate libvirt storage
pool XML definition from arguments provided by the POST data (a JSON
dict). It has to support various types of pool such as dir, netfs and
logical. Now it uses "if ... elif ... elif" to check the requested type
of pool and go in to the respective branch and generates the correct
XML.

This is OK for now but in future we'll have more types of pool, so in
this patch we turn "if ... elif" into a dispatching dict, and put the
respective XML generating code to separated functions.

Signed-off-by: Zhou Zheng Sheng <a class="moz-txt-link-rfc2396E" href="mailto:zhshzhou@linux.vnet.ibm.com">&lt;zhshzhou@linux.vnet.ibm.com&gt;</a>
---
 src/kimchi/model.py | 121 +++++++++++++++++++++++++++++++---------------------
 1 file changed, 73 insertions(+), 48 deletions(-)

diff --git a/src/kimchi/model.py b/src/kimchi/model.py
index 90a9a1b..d22e02d 100644
--- a/src/kimchi/model.py
+++ b/src/kimchi/model.py
@@ -1403,60 +1403,85 @@ class LibvirtVMScreenshot(VMScreenshot):
         finally:
             os.close(fd)

-def _get_pool_xml(**kwargs):
+
+def _get_dir_storagepool_xml(poolArgs):
     # Required parameters
     # name:
     # type:
     # path:
+    xml = """
+    &lt;pool type='%(type)s'&gt;
+      &lt;name&gt;%(name)s&lt;/name&gt;
+      &lt;target&gt;
+        &lt;path&gt;%(path)s&lt;/path&gt;
+      &lt;/target&gt;
+    &lt;/pool&gt;
+    """ % poolArgs
+    return xml
+
+
+def _get_netfs_storagepool_xml(poolArgs):
+    # Required parameters
+    # name:
+    # type:
+    # nfsserver:
     # nfspath:
-    if kwargs['type'] == 'dir':
-        xml = """
-        &lt;pool type='%(type)s'&gt;
-          &lt;name&gt;%(name)s&lt;/name&gt;
-          &lt;target&gt;
-            &lt;path&gt;%(path)s&lt;/path&gt;
-          &lt;/target&gt;
-        &lt;/pool&gt;
-       """
-    elif kwargs['type'] == 'netfs':
-        path = '/var/lib/kimchi/nfs_mount/' + kwargs['name'];
-        if not os.path.exists(path):
-           os.makedirs(path)
-        kwargs.update({'path': path})
-        xml = """
-        &lt;pool type='%(type)s'&gt;
-          &lt;name&gt;%(name)s&lt;/name&gt;
-          &lt;source&gt;
-            &lt;host name='%(nfsserver)s'/&gt;
-            &lt;dir path='%(nfspath)s'/&gt;
-          &lt;/source&gt;
-          &lt;target&gt;
-            &lt;path&gt;%(path)s&lt;/path&gt;
-          &lt;/target&gt;
-        &lt;/pool&gt;
-        """
-    elif kwargs['type'] == 'logical':
-        path = '/var/lib/kimchi/logical_mount/' + kwargs['name'];
-        if not os.path.exists(path):
-           os.makedirs(path)
-        xml = """
-        &lt;pool type='%(type)s'&gt;
-        &lt;name&gt;%(name)s&lt;/name&gt;
-            &lt;source&gt;
-            %(devices)s
-            &lt;/source&gt;
-        &lt;target&gt;
-            &lt;path&gt;%(path)s&lt;/path&gt;
-        &lt;/target&gt;
-        &lt;/pool&gt;
-        """
-        devices = ''
-        for device_path in kwargs['devices']:
-            devices += "&lt;device path=\""+device_path+"\" /&gt;"
+    path = '/var/lib/kimchi/nfs_mount/' + poolArgs['name']
+    if not os.path.exists(path):
+        os.makedirs(path)
+    poolArgs['path'] = path
+    xml = """
+    &lt;pool type='%(type)s'&gt;
+      &lt;name&gt;%(name)s&lt;/name&gt;
+      &lt;source&gt;
+        &lt;host name='%(nfsserver)s'/&gt;
+        &lt;dir path='%(nfspath)s'/&gt;
+      &lt;/source&gt;
+      &lt;target&gt;
+        &lt;path&gt;%(path)s&lt;/path&gt;
+      &lt;/target&gt;
+    &lt;/pool&gt;
+    """ % poolArgs
+    return xml
+
+
+def _get_logical_storagepool_xml(poolArgs):
+    # Required parameters
+    # name:
+    # type:
+    # devices:
+    path = '/var/lib/kimchi/logical_mount/' + poolArgs['name']
+    if not os.path.exists(path):
+        os.makedirs(path)
+
+    devices = []
+    for device_path in poolArgs['devices']:
+        devices.append('&lt;device path="%s" /&gt;' % device_path)
+
+    poolArgs.update({'devices': ''.join(devices),
+                     'path': path})
+    xml = """
+    &lt;pool type='%(type)s'&gt;
+    &lt;name&gt;%(name)s&lt;/name&gt;
+        &lt;source&gt;
+        %(devices)s
+        &lt;/source&gt;
+    &lt;target&gt;
+        &lt;path&gt;%(path)s&lt;/path&gt;
+    &lt;/target&gt;
+    &lt;/pool&gt;
+    """ % poolArgs
+    return xml
+
+
+def _get_pool_xml(**kwargs):
+    getPoolXml = {
+        'dir': _get_dir_storagepool_xml,
+        'netfs': _get_netfs_storagepool_xml,
+        'logical': _get_logical_storagepool_xml,
+        }[kwargs['type']]
+    return getPoolXml(kwargs)

-        kwargs.update({'devices': devices})
-        kwargs.update({'path': path})
-    return xml % kwargs

 def _get_volume_xml(**kwargs):
     # Required parameters
</pre>
    </blockquote>
    <br>
    <div class="moz-signature">
      <pre>Sheldon Feng(&#20911;&#23569;&#21512;)<shaohef@linux.vnet.ibm.com>
IBM Linux Technology Center</shaohef@linux.vnet.ibm.com></pre>
    </div>
  </body>
</html>

<p></p>

-- <br />
project-kimchi mailing list &lt;project-kimchi@googlegroups.com&gt;<br />
<a href="https://groups.google.com/forum/#!forum/project-kimchi">https://groups.google.com/forum/#!forum/project-kimchi</a><br />
--- <br />
You received this message because you are subscribed to the Google Groups &quot;project-kimchi&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an email to project-kimchi+unsubscribe@googlegroups.com.<br />
For more options, visit <a href="https://groups.google.com/groups/opt_out">https://groups.google.com/groups/opt_out</a>.<br />