On 02/11/2014 09:58 PM, shaohef(a)linux.vnet.ibm.com wrote:
From: ShaoHe Feng <shaohef(a)linux.vnet.ibm.com>
When clone template, if no name in parameters, kimchi will set a
default name.
Then copy the info from existing template and create a new one.
Signed-off-by: ShaoHe Feng <shaohef(a)linux.vnet.ibm.com>
---
src/kimchi/mockmodel.py | 19 +++++++++++++++++++
src/kimchi/model/templates.py | 20 ++++++++++++++++++++
2 files changed, 39 insertions(+)
diff --git a/src/kimchi/mockmodel.py b/src/kimchi/mockmodel.py
index 4e276eb..f66de3a 100644
--- a/src/kimchi/mockmodel.py
+++ b/src/kimchi/mockmodel.py
@@ -44,6 +44,7 @@ except ImportError:
from kimchi import config
from kimchi.asynctask import AsyncTask
from kimchi.config import config as kconfig
+from kimchi.control.utils import parse_request
from kimchi.distroloader import DistroLoader
from kimchi.exception import InvalidOperation, InvalidParameter
from kimchi.exception import MissingParameter, NotFoundError, OperationFailed
@@ -202,6 +203,24 @@ class MockModel(object):
self._mock_templates[name] = t
return name
+ def template_clone(self, name):
+ params = parse_request()
+ # set default name
+ if not params.get("name"):
+ subfixs = [v[len(name):] for v in self.templates_get_list()
+ if v.startswith(name)]
+ indexs = [int(v.lstrip("-clone")) for v in subfixs
+ if v.startswith("-clone") and
+ v.lstrip("-clone").isdigit()]
+ indexs.sort()
+ index = "1" if not indexs else str(indexs[-1] + 1)
+ params["name"] = name + "-clone" + index
+
+ temp = self.template_lookup(name)
+ temp.update(params)
+ ident = self.templates_create(temp)
+ raise cherrypy.HTTPRedirect("/templates/%s" % ident, 303)
+
def template_update(self, name, params):
old_t = self.template_lookup(name)
new_t = copy.copy(old_t)
diff --git a/src/kimchi/model/templates.py b/src/kimchi/model/templates.py
index 03632a6..e8eb396 100644
--- a/src/kimchi/model/templates.py
+++ b/src/kimchi/model/templates.py
@@ -22,9 +22,11 @@
import copy
+import cherrypy
import libvirt
from kimchi import xmlutils
+from kimchi.control.utils import parse_request
from kimchi.exception import InvalidOperation, InvalidParameter, NotFoundError
from kimchi.utils import pool_name_from_uri
from kimchi.vmtemplate import VMTemplate
@@ -85,6 +87,24 @@ class TemplateModel(object):
t = self.get_template(name, self.objstore, self.conn)
return t.info
+ def clone(self, name):
+ params = parse_request()
the parse_request() should not be here.
if no one oppose,
I will modify the generate_action_handler as follow.
and let generate_action_handler to pass the params.
But I need do some test to check if this will effect other resource action.
--- a/src/kimchi/control/base.py
+++ b/src/kimchi/control/base.py
@@ -66,6 +66,8 @@ class Resource(object):
if action_args is not None:
model_args.extend(parse_request()[key]
for key in action_args)
+ else:
+ model_args.extend(parse_request())
fn = getattr(self.model, model_fn(self, action_name))
fn(*model_args)
uri_params = tuple(self.model_args)
+ # set default name
+ if not params.get("name"):
+ subfixs = [v[len(name):] for v in self.templates.get_list()
+ if v.startswith(name)]
+ indexs = [int(v.lstrip("-clone")) for v in subfixs
+ if v.startswith("-clone") and
+ v.lstrip("-clone").isdigit()]
+ indexs.sort()
+ index = "1" if not indexs else str(indexs[-1] + 1)
+ params["name"] = name + "-clone" + index
+
+ temp = self.lookup(name)
+ temp.update(params)
+ ident = self.templates.create(temp)
+ raise cherrypy.HTTPRedirect("/templates/%s" % ident, 303)
+
def delete(self, name):
with self.objstore as session:
session.delete('template', name)
--
Thanks and best regards!
Sheldon Feng(冯少合)<shaohef(a)linux.vnet.ibm.com>
IBM Linux Technology Center